From e8d77d35b04790b574bf1be22d4e98f4f073276a Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Sun, 5 Mar 2023 03:33:11 +0100 Subject: [PATCH] adding more functionality to player card sealing --- src/chaosbag/BlessCurseManager.ttslua | 6 +- src/playercards/CardsThatSealTokens.ttslua | 107 ++++++++++++++---- src/playercards/cards/DarkRitual.ttslua | 2 +- src/playercards/cards/DayofReckoning.ttslua | 2 +- src/playercards/cards/FavoroftheMoon1.ttslua | 2 +- src/playercards/cards/FavoroftheSun1.ttslua | 2 +- .../cards/FluteoftheOuterGods4.ttslua | 2 +- src/playercards/cards/HolySpear5.ttslua | 97 +--------------- src/playercards/cards/Nephthys4.ttslua | 2 +- .../cards/ProtectiveIncantation1.ttslua | 78 +------------ src/playercards/cards/RadiantSmite1.ttslua | 2 +- .../cards/RiteofSanctification.ttslua | 2 +- .../cards/SealoftheSeventhSign5.ttslua | 2 +- src/playercards/cards/SerpentsofYig.ttslua | 2 +- src/playercards/cards/ShardsoftheVoid3.ttslua | 2 +- src/playercards/cards/ShieldofFaith2.ttslua | 2 +- .../cards/TheChthonianStone.ttslua | 8 +- .../cards/TheChthonianStone3.ttslua | 8 +- src/playercards/cards/TheCodexofAges.ttslua | 2 +- src/playercards/cards/Unrelenting1.ttslua | 99 +--------------- 20 files changed, 125 insertions(+), 304 deletions(-) diff --git a/src/chaosbag/BlessCurseManager.ttslua b/src/chaosbag/BlessCurseManager.ttslua index 49257167..4c89694e 100644 --- a/src/chaosbag/BlessCurseManager.ttslua +++ b/src/chaosbag/BlessCurseManager.ttslua @@ -1,4 +1,4 @@ -local tokenArrangerApi = require("accessories/TokenArrangerApi") +local tokenArrangerApi = require("accessories/TokenArrangerApi") -- common button parameters local buttonParamaters = {} @@ -7,11 +7,11 @@ buttonParamaters.color = { 0, 0, 0, 0 } buttonParamaters.width = 700 buttonParamaters.height = 700 -local altState = false +local altState = false local MODE = { [false] = "Add / Remove",[true] = "Take / Return" } local BUTTON_COLOR = { [false] = { 0.4, 0.4, 0.4 },[true] = { 0.9, 0.9, 0.9 } } local FONT_COLOR = { [false] = { 1, 1, 1 },[true] = { 0, 0, 0 } } -local whitespace = " " +local whitespace = " " local updating --------------------------------------------------------- diff --git a/src/playercards/CardsThatSealTokens.ttslua b/src/playercards/CardsThatSealTokens.ttslua index dd994cfd..ab703653 100644 --- a/src/playercards/CardsThatSealTokens.ttslua +++ b/src/playercards/CardsThatSealTokens.ttslua @@ -1,31 +1,93 @@ +--[[ Library for cards that seal tokens +This file is used to add sealing option to cards' context menu. +Valid options (set before requiring this file): +SHOW_READ_BAG -- boolean +SHOW_SINGLE_RELEASE -- boolean +SHOW_MULTI_RELEASE -- number (amount of tokens to release at once) +VALID_TOKENS -- table ([tokenName] = true) +INVALID_TOKENS -- table ([tokenName] = true)]] + local blessCurseManagerApi = require("chaosbag/BlessCurseManagerApi") +local tokenArrangerApi = require("accessories/TokenArrangerApi") function onSave() return JSON.encode(sealedTokens) end function onLoad(savedData) sealedTokens = JSON.decode(savedData) or {} ID_URL_MAP = Global.getTable("ID_URL_MAP") + generateContextMenu() +end - -- add menu items +-- builds the context menu +function generateContextMenu() + self.clearContextMenu() + + -- only show this for cards that need a dynamic list of tokens (for example 'Unrelenting') + if SHOW_READ_BAG then + self.addContextMenuItem("Update list", generateContextMenu) + readBag() + end + + -- conditional single or multi release options if SHOW_SINGLE_RELEASE then self.addContextMenuItem("Release token", releaseOneToken) elseif SHOW_MULTI_RELEASE then self.addContextMenuItem("Release " .. SHOW_MULTI_RELEASE .. " token(s)", function(playerColor) - for i = 1, SHOW_MULTI_RELEASE do - releaseOneToken(playerColor) + if SHOW_MULTI_RELEASE >= #sealedTokens then + for i = 1, SHOW_MULTI_RELEASE do + releaseOneToken(playerColor) + end + else + printToColor("Not enough " .. name .. " tokens sealed.", playerColor) end end) else self.addContextMenuItem("Release token(s)", releaseTokens) end + -- main context menu options to seal tokens for _, map in pairs(ID_URL_MAP) do - if VALID_TOKENS[map.name] ~= nil then - self.addContextMenuItem("Seal " .. map.name, function(playerColor) sealToken(map.name, playerColor) end) + if (VALID_TOKENS[map.name] ~= nil) or (SHOW_READ_BAG and tokensInBag[map.name] and not INVALID_TOKENS[map.name]) then + if not SHOW_MULTI_SEAL then + self.addContextMenuItem("Seal " .. map.name, function(playerColor) + sealToken(map.name, playerColor) + end) + else + self.addContextMenuItem("Seal " .. SHOW_MULTI_SEAL .. " " .. map.name, function(playerColor) + readBag() + local allowed = true + local notFound + + for name, _ in pairs(VALID_TOKENS) do + if (tokensInBag[name] or 0) < SHOW_MULTI_SEAL then + allowed = false + notFound = name + end + end + + if allowed then + for i = 1, SHOW_MULTI_SEAL do + sealToken(map.name, playerColor) + end + else + printToColor("Not enough " .. notFound .. " tokens in the chaos bag.", playerColor) + end + end) + end end end end +-- generates a list of chaos tokens that is in the chaos bag +function readBag() + local chaosbag = Global.call("findChaosBag") + tokensInBag = {} + + for _, token in ipairs(chaosbag.getObjects()) do + tokensInBag[token.name] = (tokensInBag[token.name] or 0) + 1 + end +end + -- seals the named token on this card function sealToken(name, playerColor) local chaosbag = Global.call("findChaosBag") @@ -39,7 +101,7 @@ function sealToken(name, playerColor) callback_function = function(token) local guid = token.getGUID() table.insert(sealedTokens, guid) - + tokenArrangerApi.layout() if name == "Bless" or name == "Curse" then blessCurseManagerApi.sealedToken(name, guid) end @@ -53,18 +115,11 @@ end -- release the last sealed token function releaseOneToken(playerColor) - if sealedTokens == {} then + if sealedTokens == {} or #sealedTokens == 0 then printToColor("No sealed token(s) found", playerColor) else printToColor("Releasing token", playerColor) - local chaosbag = Global.call("findChaosBag") - local guid = table.remove(sealedTokens) - local token = getObjectFromGUID(guid) - local name = token.getName() - chaosbag.putObject(token) - if name == "Bless" or name == "Curse" then - blessCurseManagerApi.releasedToken(name, guid) - end + putTokenAway(table.remove(sealedTokens)) end end @@ -74,15 +129,23 @@ function releaseTokens(playerColor) printToColor("No sealed token(s) found", playerColor) else printToColor("Releasing token(s)", playerColor) - local chaosbag = Global.call("findChaosBag") for _, guid in ipairs(sealedTokens) do - local token = getObjectFromGUID(guid) - local name = token.getName() - chaosbag.putObject(token) - if name == "Bless" or name == "Curse" then - blessCurseManagerApi.releasedToken(name, guid) - end + putTokenAway(guid) end sealedTokens = {} end end + +-- returns the token (referenced by GUID) to the chaos bag +function putTokenAway(guid) + local token = getObjectFromGUID(guid) + if not token then return end + + local name = token.getName() + local chaosbag = Global.call("findChaosBag") + chaosbag.putObject(token) + tokenArrangerApi.layout() + if name == "Bless" or name == "Curse" then + blessCurseManagerApi.releasedToken(name, guid) + end +end diff --git a/src/playercards/cards/DarkRitual.ttslua b/src/playercards/cards/DarkRitual.ttslua index 6cec94f7..540513c1 100644 --- a/src/playercards/cards/DarkRitual.ttslua +++ b/src/playercards/cards/DarkRitual.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Curse"] = true + ["Curse"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/DayofReckoning.ttslua b/src/playercards/cards/DayofReckoning.ttslua index 46cdab93..78e4493c 100644 --- a/src/playercards/cards/DayofReckoning.ttslua +++ b/src/playercards/cards/DayofReckoning.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Elder Sign"] = true + ["Elder Sign"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/FavoroftheMoon1.ttslua b/src/playercards/cards/FavoroftheMoon1.ttslua index 1413142b..a1a7782a 100644 --- a/src/playercards/cards/FavoroftheMoon1.ttslua +++ b/src/playercards/cards/FavoroftheMoon1.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Curse"] = true + ["Curse"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/FavoroftheSun1.ttslua b/src/playercards/cards/FavoroftheSun1.ttslua index 8c91d129..fe5a27fc 100644 --- a/src/playercards/cards/FavoroftheSun1.ttslua +++ b/src/playercards/cards/FavoroftheSun1.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Bless"] = true + ["Bless"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/FluteoftheOuterGods4.ttslua b/src/playercards/cards/FluteoftheOuterGods4.ttslua index 1413142b..a1a7782a 100644 --- a/src/playercards/cards/FluteoftheOuterGods4.ttslua +++ b/src/playercards/cards/FluteoftheOuterGods4.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Curse"] = true + ["Curse"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/HolySpear5.ttslua b/src/playercards/cards/HolySpear5.ttslua index d34e5462..aa948b10 100644 --- a/src/playercards/cards/HolySpear5.ttslua +++ b/src/playercards/cards/HolySpear5.ttslua @@ -1,93 +1,8 @@ -function onload() - mode = "Bless" - chaosbag = getChaosBag() - manager = getObjectFromGUID("5933fb") - sealedTokens = { } - IMAGE_TOKEN_MAP = { } - for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do - IMAGE_TOKEN_MAP[i] = v - end +VALID_TOKENS = { + ["Bless"] = true +} - -- add menu items - self.clearContextMenu() - self.addContextMenuItem("Release Token", releaseTokens, true) - for url,name in pairs(IMAGE_TOKEN_MAP) do - if name == mode then - self.addContextMenuItem("Seal 2 " .. mode, function(playerColor) sealToken(url, playerColor) end, true) - end - end -end +SHOW_SINGLE_RELEASE = true +SHOW_MULTI_SEAL = 2 -function sealToken(url, playerColor) - local pos = self.getPosition() - - local name = IMAGE_TOKEN_MAP[url] - local indexes = {} - - for i,obj in ipairs(chaosbag.getObjects()) do - if obj.name == name then - table.insert(indexes, obj.index) - end - end - if #indexes < 2 then - printToColor("Fewer than 2 " .. name .. " tokens in bag", playerColor) - return - end - table.sort(indexes) - chaosbag.takeObject({ - position={ pos.x, pos.y + 1, pos.z }, - index=indexes[#indexes], - smooth=false, - callback_function=_sealToken - }) - chaosbag.takeObject({ - position={ pos.x, pos.y + 2, pos.z }, - index=indexes[#indexes-1], - smooth=false, - callback_function=_sealToken - }) -end - -function _sealToken(obj) - table.insert(sealedTokens, obj) - local guid = obj.getGUID() - local tokensTaken = manager.getVar("tokensTaken") - table.insert(tokensTaken[mode], guid) - manager.setVar("tokensTaken", tokensTaken) - manager.setVar("mode", mode) - printToAll("Sealing " .. mode .. " token " .. manager.call("getTokenCount")) -end - -function releaseTokens(playerColor) - if #sealedTokens == 0 then return end - local token = sealedTokens[#sealedTokens] - if token ~= nil then - local guid = token.getGUID() - chaosbag.putObject(token) - local tokensTaken = manager.getVar("tokensTaken") - for i,v in ipairs(tokensTaken[mode]) do - if v == guid then - table.remove(tokensTaken[mode], i) - break - end - end - manager.setVar("tokensTaken", tokensTaken) - manager.setVar("mode", mode) - printToAll("Releasing " .. mode .. " token" .. manager.call("getTokenCount")) - end - - table.remove(sealedTokens) -end - -function getChaosBag() - local items = getObjectFromGUID("83ef06").getObjects() - local chaosbag = nil - for i,v in ipairs(items) do - if v.getDescription() == "Chaos Bag" then - chaosbag = getObjectFromGUID(v.getGUID()) - break - end - end - if chaosbag == nil then printToAll("No chaos bag found") end - return chaosbag -end +require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/Nephthys4.ttslua b/src/playercards/cards/Nephthys4.ttslua index b0550977..5d42b3f3 100644 --- a/src/playercards/cards/Nephthys4.ttslua +++ b/src/playercards/cards/Nephthys4.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Bless"] = true + ["Bless"] = true } SHOW_MULTI_RELEASE = 3 diff --git a/src/playercards/cards/ProtectiveIncantation1.ttslua b/src/playercards/cards/ProtectiveIncantation1.ttslua index e30935a6..b86535df 100644 --- a/src/playercards/cards/ProtectiveIncantation1.ttslua +++ b/src/playercards/cards/ProtectiveIncantation1.ttslua @@ -1,75 +1,9 @@ -function onload() - chaosbag = getChaosBag() - sealedTokens = { } - IMAGE_TOKEN_MAP = { } - for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do - IMAGE_TOKEN_MAP[i] = v - end - readBag() -end +VALID_TOKENS = {} -function readBag(playerColor) - if playerColor ~= nil then - printToColor("Reading chaos bag", playerColor) - end +INVALID_TOKENS = { + ["Auto-fail"] = true +} - local tokensInBag = { } - for i,token in ipairs(chaosbag.getObjects()) do - if token.name ~= nil and token.name ~= "" then - tokensInBag[token.name] = true - end - end +SHOW_READ_BAG = true - -- add menu items - self.clearContextMenu() - self.addContextMenuItem("Release Token", releaseTokens) - for url,name in pairs(IMAGE_TOKEN_MAP) do - if tokensInBag[name] and name ~= "Auto-fail" then - self.addContextMenuItem("Seal " .. name, function(playerColor) sealToken(url, playerColor) end) - end - end - self.addContextMenuItem("Read Chaos Bag", readBag) -end - -function sealToken(url, playerColor) - local pos = self.getPosition() - - local name = IMAGE_TOKEN_MAP[url] - for i,obj in ipairs(chaosbag.getObjects()) do - if obj.name == name then - chaosbag.takeObject({ - position={ pos.x, pos.y + 1, pos.z }, - index=i-1, - smooth=false, - callback_function=_sealToken - }) - return - end - end - printToColor(name .. " token not found in bag", playerColor) -end - -function _sealToken(obj) - table.insert(sealedTokens, obj) -end - -function releaseTokens(playerColor) - printToColor("Releasing token", playerColor) - for i,obj in ipairs(sealedTokens) do - chaosbag.putObject(obj) - end - sealedTokens = { } -end - -function getChaosBag() - local items = getObjectFromGUID("83ef06").getObjects() - local chaosbag = nil - for i,v in ipairs(items) do - if v.getDescription() == "Chaos Bag" then - chaosbag = getObjectFromGUID(v.getGUID()) - break - end - end - if chaosbag == nil then printToAll("No chaos bag found") end - return chaosbag -end +require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/RadiantSmite1.ttslua b/src/playercards/cards/RadiantSmite1.ttslua index f4f0ed6d..79219afc 100644 --- a/src/playercards/cards/RadiantSmite1.ttslua +++ b/src/playercards/cards/RadiantSmite1.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Bless"] = true + ["Bless"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/RiteofSanctification.ttslua b/src/playercards/cards/RiteofSanctification.ttslua index 8c91d129..fe5a27fc 100644 --- a/src/playercards/cards/RiteofSanctification.ttslua +++ b/src/playercards/cards/RiteofSanctification.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Bless"] = true + ["Bless"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/SealoftheSeventhSign5.ttslua b/src/playercards/cards/SealoftheSeventhSign5.ttslua index 5cf8e709..736b14a8 100644 --- a/src/playercards/cards/SealoftheSeventhSign5.ttslua +++ b/src/playercards/cards/SealoftheSeventhSign5.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Auto-fail"] = true + ["Auto-fail"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/SerpentsofYig.ttslua b/src/playercards/cards/SerpentsofYig.ttslua index 46cdab93..78e4493c 100644 --- a/src/playercards/cards/SerpentsofYig.ttslua +++ b/src/playercards/cards/SerpentsofYig.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Elder Sign"] = true + ["Elder Sign"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/ShardsoftheVoid3.ttslua b/src/playercards/cards/ShardsoftheVoid3.ttslua index 00808f9f..10645275 100644 --- a/src/playercards/cards/ShardsoftheVoid3.ttslua +++ b/src/playercards/cards/ShardsoftheVoid3.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["0"] = true + ["0"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/ShieldofFaith2.ttslua b/src/playercards/cards/ShieldofFaith2.ttslua index 8c91d129..fe5a27fc 100644 --- a/src/playercards/cards/ShieldofFaith2.ttslua +++ b/src/playercards/cards/ShieldofFaith2.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Bless"] = true + ["Bless"] = true } SHOW_SINGLE_RELEASE = true diff --git a/src/playercards/cards/TheChthonianStone.ttslua b/src/playercards/cards/TheChthonianStone.ttslua index 6898ac09..050c1176 100644 --- a/src/playercards/cards/TheChthonianStone.ttslua +++ b/src/playercards/cards/TheChthonianStone.ttslua @@ -1,8 +1,8 @@ VALID_TOKENS = { - ["Skull"] = true, - ["Cultist"] = true, - ["Tablet"] = true, - ["Elder Thing"] = true, + ["Skull"] = true, + ["Cultist"] = true, + ["Tablet"] = true, + ["Elder Thing"] = true, } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/TheChthonianStone3.ttslua b/src/playercards/cards/TheChthonianStone3.ttslua index 6898ac09..050c1176 100644 --- a/src/playercards/cards/TheChthonianStone3.ttslua +++ b/src/playercards/cards/TheChthonianStone3.ttslua @@ -1,8 +1,8 @@ VALID_TOKENS = { - ["Skull"] = true, - ["Cultist"] = true, - ["Tablet"] = true, - ["Elder Thing"] = true, + ["Skull"] = true, + ["Cultist"] = true, + ["Tablet"] = true, + ["Elder Thing"] = true, } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/TheCodexofAges.ttslua b/src/playercards/cards/TheCodexofAges.ttslua index 46cdab93..78e4493c 100644 --- a/src/playercards/cards/TheCodexofAges.ttslua +++ b/src/playercards/cards/TheCodexofAges.ttslua @@ -1,5 +1,5 @@ VALID_TOKENS = { - ["Elder Sign"] = true + ["Elder Sign"] = true } require("playercards/CardsThatSealTokens") diff --git a/src/playercards/cards/Unrelenting1.ttslua b/src/playercards/cards/Unrelenting1.ttslua index 16e20d25..ccf5ca2b 100644 --- a/src/playercards/cards/Unrelenting1.ttslua +++ b/src/playercards/cards/Unrelenting1.ttslua @@ -1,97 +1,6 @@ -function onload() - chaosbag = getChaosBag() - manager = getObjectFromGUID("5933fb") - sealedTokens = { } - IMAGE_TOKEN_MAP = { } - for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do - IMAGE_TOKEN_MAP[i] = v - end +VALID_TOKENS = {} +INVALID_TOKENS = {} - readBag() -end +SHOW_READ_BAG = true -function sealToken(url, playerColor) - local pos = self.getPosition() - - local name = IMAGE_TOKEN_MAP[url] - for i,obj in ipairs(chaosbag.getObjects()) do - if obj.name == name then - chaosbag.takeObject({ - position={ pos.x, pos.y + 1, pos.z }, - index=i-1, - smooth=false, - callback_function=_sealToken - }) - return - end - end - printToColor(name .. " token not found in bag", playerColor) -end - -function _sealToken(obj) - table.insert(sealedTokens, obj) - local guid = obj.getGUID() - local name = obj.getName() - if name == "Bless" or name == "Curse" then - local tokensTaken = manager.getVar("tokensTaken") - table.insert(tokensTaken[name], guid) - manager.setVar("tokensTaken", tokensTaken) - manager.setVar("mode", name) - printToAll("Sealing " .. name .. " token " .. manager.call("getTokenCount")) - end -end - -function releaseTokens(playerColor) - if #sealedTokens == 0 then return end - for i,token in ipairs(sealedTokens) do - local guid = token.getGUID() - local name = token.getName() - chaosbag.putObject(token) - if name == "Bless" or name == "Curse" then - local tokensTaken = manager.getVar("tokensTaken") - for i,v in ipairs(tokensTaken[name]) do - if v == guid then - table.remove(tokensTaken[name], i) - break - end - end - manager.setVar("tokensTaken", tokensTaken) - manager.setVar("mode", name) - printToAll("Releasing " .. name .. " token" .. manager.call("getTokenCount")) - end - end - - sealedTokens = { } -end - -function getChaosBag() - local items = getObjectFromGUID("83ef06").getObjects() - local chaosbag = nil - for i,v in ipairs(items) do - if v.getDescription() == "Chaos Bag" then - chaosbag = getObjectFromGUID(v.getGUID()) - break - end - end - if chaosbag == nil then printToAll("No chaos bag found") end - return chaosbag -end - -function readBag() - -- add menu items - self.clearContextMenu() - self.addContextMenuItem("Release Tokens", releaseTokens) - - local bagTokens = { } - local tokens = chaosbag.getObjects() - for i,token in ipairs(tokens) do - bagTokens[token.name] = true - end - - for url,token in pairs(IMAGE_TOKEN_MAP) do - if bagTokens[token] then - self.addContextMenuItem("Seal " .. token, function(playerColor) sealToken(url, playerColor) end, true) - end - end - self.addContextMenuItem("Refresh Seal Options", readBag) -end +require("playercards/CardsThatSealTokens")