From 1eb3a44874a70ab8b7d511876594140269e6c804 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Mon, 12 Feb 2024 09:22:08 +0100 Subject: [PATCH] updated import instructions --- src/arkhamdb/ArkhamDb.ttslua | 4 ++ src/arkhamdb/InstructionGenerator.ttslua | 48 +++++++++++++++--------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/arkhamdb/ArkhamDb.ttslua b/src/arkhamdb/ArkhamDb.ttslua index e8cb4093..0afc5199 100644 --- a/src/arkhamdb/ArkhamDb.ttslua +++ b/src/arkhamdb/ArkhamDb.ttslua @@ -307,6 +307,10 @@ do -- remove spaces line = line:gsub("%s", "") + -- remove balanced brackets + line = line:gsub("%b()", "") + line = line:gsub("%b[]", "") + -- get instructor local instructor = "" for word in line:gmatch("%a+:") do diff --git a/src/arkhamdb/InstructionGenerator.ttslua b/src/arkhamdb/InstructionGenerator.ttslua index 1b8d4c9e..3faf1e57 100644 --- a/src/arkhamdb/InstructionGenerator.ttslua +++ b/src/arkhamdb/InstructionGenerator.ttslua @@ -1,5 +1,7 @@ local searchLib = require("util/SearchLib") +local idList = {} + function onLoad() local buttonParameters = {} buttonParameters.function_owner = self @@ -23,26 +25,13 @@ function onLoad() end function generate(_, playerColor) - local idList = {} + idList = {} for _, obj in ipairs(searchLib.onObject(self, "isCardOrDeck")) do if obj.type == "Card" then - local cardMetadata = JSON.decode(obj.getGMNotes()) - - if cardMetadata then - local id = getIdFromData(cardMetadata) - if id then - table.insert(idList, id) - end - end + processCard(JSON.decode(obj.getGMNotes()), obj.getName()) elseif obj.type == "Deck" then for _, deepObj in ipairs(obj.getData().ContainedObjects) do - local cardMetadata = JSON.decode(deepObj.GMNotes) - if cardMetadata then - local id = getIdFromData(cardMetadata) - if id then - table.insert(idList, id) - end - end + processCard(JSON.decode(deepObj.GMNotes), deepObj.Nickname) end end end @@ -54,10 +43,13 @@ function generate(_, playerColor) broadcastToColor("Created deck instruction for " .. #idList .. " card(s). Copy it from the input field.", playerColor, "Green") end + -- sort the idList + table.sort(idList, sortById) + -- construct the string local description = "++SCED import instructions++\n- add: " - for _, id in ipairs(idList) do - description = description .. id .. ", " + for _, entry in ipairs(idList) do + description = description .. entry.id .. " (**" .. entry.name .. "**)" .. ", " end -- remove last delimiter (last two characters) @@ -74,4 +66,24 @@ function getIdFromData(metadata) end end +function processCard(metadata, name) + if metadata then + local id = getIdFromData(metadata) + if id then + table.insert(idList, {id = id, name = name}) + end + end +end + +function sortById(a, b) + local numA = tonumber(a.id) + local numB = tonumber(b.id) + + if numA and numB then + return numA < numB + else + return a.name < b.name + end +end + function none() end