SCED/src/arkhamdb/InstructionGenerator.ttslua
2024-02-16 11:28:44 +01:00

93 lines
2.7 KiB
Plaintext

local searchLib = require("util/SearchLib")
local idList = {}
function onLoad()
-- "generate" button
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.height = 200
buttonParameters.width = 1200
buttonParameters.font_size = 75
buttonParameters.click_function = "generate"
buttonParameters.label = "Generate instructions!"
buttonParameters.position = { 0, 0.06, 1.55 }
self.createButton(buttonParameters)
-- "output" text field
local inputParameters = {}
inputParameters.label = "Click button above"
inputParameters.input_function = "none"
inputParameters.function_owner = self
inputParameters.position = { 0, 0.05, 1.95 }
inputParameters.width = 1200
inputParameters.height = 130
inputParameters.font_size = 107
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(obj.getGMNotes(), obj.getName(), playerColor)
elseif obj.type == "Deck" then
for _, deepObj in ipairs(obj.getData().ContainedObjects) do
processCard(deepObj.GMNotes, deepObj.Nickname, playerColor)
end
end
end
if #idList == 0 then
broadcastToColor("Didn't find any valid cards.", playerColor, "Red")
return
else
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 (new line for each instruction)
local description = "++SCED import instructions++"
for _, entry in ipairs(idList) do
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
-- use the ZoopGuid as fallback if no id present
function getIdFromData(metadata)
if metadata.id then
return metadata.id
elseif metadata.TtsZoopGuid then
return metadata.TtsZoopGuid
end
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
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