SCED/src/arkhamdb/InstructionGenerator.ttslua

91 lines
2.6 KiB
Plaintext
Raw Normal View History

2023-12-18 10:19:51 -05:00
local searchLib = require("util/SearchLib")
2023-12-18 09:26:08 -05:00
2024-02-12 03:22:08 -05:00
local idList = {}
2023-12-18 09:26:08 -05:00
function onLoad()
-- "generate" button
2023-12-18 09:26:08 -05:00
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
2023-12-18 09:26:08 -05:00
local inputParameters = {}
inputParameters.label = "Click button above"
2023-12-18 10:19:51 -05:00
inputParameters.input_function = "none"
2023-12-18 09:26:08 -05:00
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
2023-12-18 10:19:51 -05:00
function generate(_, playerColor)
2024-02-12 03:22:08 -05:00
idList = {}
2023-12-18 10:19:51 -05:00
for _, obj in ipairs(searchLib.onObject(self, "isCardOrDeck")) do
if obj.type == "Card" then
2024-02-16 05:28:44 -05:00
processCard(obj.getGMNotes(), obj.getName(), playerColor)
2023-12-18 10:19:51 -05:00
elseif obj.type == "Deck" then
for _, deepObj in ipairs(obj.getData().ContainedObjects) do
2024-02-16 05:28:44 -05:00
processCard(deepObj.GMNotes, deepObj.Nickname, playerColor)
2023-12-18 10:19:51 -05:00
end
end
end
2023-12-18 09:26:08 -05:00
2023-12-18 10:19:51 -05:00
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
2024-02-12 03:22:08 -05:00
-- sort the idList
table.sort(idList, sortById)
2024-02-16 04:12:58 -05:00
-- construct the string (new line for each instruction)
local description = "++SCED import instructions++"
2024-02-12 03:22:08 -05:00
for _, entry in ipairs(idList) do
2024-02-16 11:11:55 -05:00
description = description .. "\n- add: " .. entry.id .. " (**" .. entry.name .. "**)"
2023-12-18 10:19:51 -05:00
end
2024-02-16 11:11:55 -05:00
2023-12-18 10:19:51 -05:00
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
2024-02-16 05:28:44 -05:00
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")
2024-02-12 03:22:08 -05:00
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
2023-12-18 10:19:51 -05:00
function none() end