Merge pull request #596 from argonui/import-instructions

Import Instructions: Bugfixes and QoL
This commit is contained in:
BootleggerFinn 2024-02-16 12:24:41 -06:00 committed by GitHub
commit 2186d5f07e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 28 deletions

View File

@ -299,9 +299,9 @@ do
local pos = string.find(description, "++SCED import instructions++")
if not pos then return end
-- remove everything before instructions (including newline)
local tempStr = string.sub(description, pos + 30)
-- remove everything before instructions
local tempStr = string.sub(description, pos)
-- parse each line in instructions
for line in tempStr:gmatch("([^\n]+)") do
-- remove dashes at the start
@ -321,7 +321,10 @@ do
break
end
if instructor == "" or (instructor ~= "add:" and instructor ~= "remove:") then break end
-- go to the next line if no valid instructor found
if instructor ~= "add:" and instructor ~= "remove:" then
goto nextLine
end
-- remove instructor from line
line = line:gsub(instructor, "")
@ -332,20 +335,23 @@ do
slots[str] = (slots[str] or 0) + 1
elseif instructor == "remove:" then
if slots[str] == nil then
internal.maybePrint("Tried to remove " .. str .. ", but didn't find card in deck.", playerColor)
break
end
slots[str] = math.max(slots[str] - 1, 0)
internal.maybePrint("Tried to remove card ID " .. str .. ", but didn't find card in deck.", playerColor)
else
slots[str] = math.max(slots[str] - 1, 0)
-- fully remove cards that have a quantity of 0
if slots[str] == 0 then
slots[str] = nil
end
-- fully remove cards that have a quantity of 0
if slots[str] == 0 then
slots[str] = nil
-- also remove related minicard
slots[str .. "-m"] = nil
-- also remove related minicard
slots[str .. "-m"] = nil
end
end
end
end
-- jump mark at the end of the loop
::nextLine::
end
end

View File

@ -3,6 +3,7 @@ local searchLib = require("util/SearchLib")
local idList = {}
function onLoad()
-- "generate" button
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.height = 200
@ -13,6 +14,7 @@ function onLoad()
buttonParameters.position = { 0, 0.06, 1.55 }
self.createButton(buttonParameters)
-- "output" text field
local inputParameters = {}
inputParameters.label = "Click button above"
inputParameters.input_function = "none"
@ -24,14 +26,15 @@ function onLoad()
self.createInput(inputParameters)
end
-- generates a string for the ArkhamDB deck notes that will instruct the deck import to add the specified cards
function generate(_, playerColor)
idList = {}
for _, obj in ipairs(searchLib.onObject(self, "isCardOrDeck")) do
if obj.type == "Card" then
processCard(JSON.decode(obj.getGMNotes()), obj.getName())
processCard(obj.getGMNotes(), obj.getName(), playerColor)
elseif obj.type == "Deck" then
for _, deepObj in ipairs(obj.getData().ContainedObjects) do
processCard(JSON.decode(deepObj.GMNotes), deepObj.Nickname)
processCard(deepObj.GMNotes, deepObj.Nickname, playerColor)
end
end
end
@ -46,14 +49,12 @@ function generate(_, playerColor)
-- sort the idList
table.sort(idList, sortById)
-- construct the string
local description = "++SCED import instructions++\n- add: "
-- construct the string (new line for each instruction)
local description = "++SCED import instructions++"
for _, entry in ipairs(idList) do
description = description .. entry.id .. " (**" .. entry.name .. "**)" .. ", "
description = description .. "\n- add: " .. entry.id .. " (**" .. entry.name .. "**)"
end
-- remove last delimiter (last two characters)
description = description:sub(1, -3)
self.editInput({index = 0, value = description})
end
@ -66,12 +67,12 @@ 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
function processCard(notes, name, playerColor)
local id = getIdFromData(JSON.decode(notes) or {})
if id then
table.insert(idList, {id = id, name = name})
else
broadcastToColor("Couldn't get ID for " .. name .. ".", playerColor, "Red")
end
end