From 8e9829931b19c97c5f781f4a9bdcff82b7b6a32c Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Sun, 7 Jul 2024 23:03:29 +0200 Subject: [PATCH] update for cards with helper --- src/playercards/CardsThatRedrawTokens.ttslua | 15 +++----- src/playercards/CardsWithHelper.ttslua | 20 ++++++++++- .../cards/BookofLivingMyths.ttslua | 36 ++++++++++--------- .../cards/EmpiricalHypothesis.ttslua | 13 ++++--- .../cards/FamilyInheritance.ttslua | 29 +++++---------- src/playercards/cards/KohakuNarukami.ttslua | 34 +++++++++--------- src/playercards/cards/WellConnected.ttslua | 36 ++++++++++++------- 7 files changed, 100 insertions(+), 83 deletions(-) diff --git a/src/playercards/CardsThatRedrawTokens.ttslua b/src/playercards/CardsThatRedrawTokens.ttslua index b975f8a2..5f7c8813 100644 --- a/src/playercards/CardsThatRedrawTokens.ttslua +++ b/src/playercards/CardsThatRedrawTokens.ttslua @@ -51,20 +51,21 @@ As a nice reminder the XML button takes on the Frost color and icon with the tex > require... ----------------------------------------------------------]] -local isHelperEnabled = false +-- intentionally global +hasXML = true +isHelperEnabled = false function updateSave() self.script_state = JSON.encode({ isHelperEnabled = isHelperEnabled }) end function onLoad(savedData) - self.addTag("CardWithHelper") if savedData and savedData ~= "" then local loadedData = JSON.decode(savedData) isHelperEnabled = loadedData.isHelperEnabled end createHelperXML() - checkOptionPanel() + syncDisplayWithOptionPanel() end function createHelperXML() @@ -95,14 +96,6 @@ function createHelperXML() self.UI.setXmlTable(xmlTable) end -function shutOff() - self.UI.hide("Helper") -end - -function initialize() - self.UI.show("Helper") -end - function triggerXMLTokenLabelCreation() Global.call("activeRedrawEffect", { VALID_TOKENS = VALID_TOKENS, diff --git a/src/playercards/CardsWithHelper.ttslua b/src/playercards/CardsWithHelper.ttslua index c92ffda9..dc67ee1b 100644 --- a/src/playercards/CardsWithHelper.ttslua +++ b/src/playercards/CardsWithHelper.ttslua @@ -1,7 +1,23 @@ +--[[ Library for cards that have helpers +This file is used to share code between cards with helpers. +It syncs the visibility of the helper with the option panel and +makes sure the card has the respective tag. +Additionally, it will call 'initiliaze()' and 'shutOff()' +in the parent file if they are present. + +Instructions: +1) Define the global variables before requiring this file: +hasXML = true (whether the card has an XML display) +isHelperEnabled = false (default state of the helper, should be 'false') + +2) In 'onLoad()'', call 'syncDisplayWithOptionPanel()' +----------------------------------------------------------]] + local optionPanelApi = require("core/OptionPanelApi") -- if the respective option is enabled in onLoad(), enable the helper -function checkOptionPanel() +function syncDisplayWithOptionPanel() + self.addTag("CardWithHelper") local options = optionPanelApi.getOptions() if options.enableCardHelpers then setHelperState(true) @@ -33,10 +49,12 @@ function actualDisplayUpdate() if isHelperEnabled then self.clearContextMenu() self.addContextMenuItem("Disable Helper", toggleHelper) + if hasXML then self.UI.show("Helper") end if initialize then initialize() end else self.clearContextMenu() self.addContextMenuItem("Enable Helper", toggleHelper) + if hasXML then self.UI.hide("Helper") end if shutOff then shutOff() end end end diff --git a/src/playercards/cards/BookofLivingMyths.ttslua b/src/playercards/cards/BookofLivingMyths.ttslua index 776c9d8b..b4d564d0 100644 --- a/src/playercards/cards/BookofLivingMyths.ttslua +++ b/src/playercards/cards/BookofLivingMyths.ttslua @@ -1,38 +1,40 @@ require("playercards/CardsWithHelper") -local blessCurseManagerApi = require("chaosbag/BlessCurseManagerApi") -local chaosBagApi = require("chaosbag/ChaosBagApi") -local guidReferenceApi = require("core/GUIDReferenceApi") -local playermatApi = require("playermat/PlayermatApi") +local blessCurseManagerApi = require("chaosbag/BlessCurseManagerApi") +local chaosBagApi = require("chaosbag/ChaosBagApi") +local guidReferenceApi = require("core/GUIDReferenceApi") +local playermatApi = require("playermat/PlayermatApi") -local isHelperEnabled = false -local updated +-- intentionally global +hasXML = true +isHelperEnabled = false +local updated, loopId function updateSave() - self.script_state = JSON.encode({ isHelperEnabled = isHelperEnabled }) + self.script_state = JSON.encode({ + isHelperEnabled = isHelperEnabled, + loopId = loopId + }) end function onLoad(savedData) - self.addTag("CardWithHelper") if savedData and savedData ~= "" then local loadedData = JSON.decode(savedData) isHelperEnabled = loadedData.isHelperEnabled + loopId = loadedData.loopId end - checkOptionPanel() + syncDisplayWithOptionPanel() end --- hide buttons and stop monitoring function shutOff() - self.UI.hide("Helper") - Wait.stopAll() - updateSave() + if loopId then + Wait.stop(loopId) + loopId = nil + end end --- show buttons and begin monitoring chaos bag for curse and bless tokens function initialize() - self.UI.show("Helper") maybeUpdateButtonState() - Wait.time(maybeUpdateButtonState, 1, -1) - updateSave() + loopId = Wait.time(maybeUpdateButtonState, 1, -1) end function resolveToken(player, _, tokenType) diff --git a/src/playercards/cards/EmpiricalHypothesis.ttslua b/src/playercards/cards/EmpiricalHypothesis.ttslua index 929b4aec..2a68a3bd 100644 --- a/src/playercards/cards/EmpiricalHypothesis.ttslua +++ b/src/playercards/cards/EmpiricalHypothesis.ttslua @@ -1,5 +1,9 @@ require("playercards/CardsWithHelper") -local playermatApi = require("playermat/PlayermatApi") +local playermatApi = require("playermat/PlayermatApi") + +-- intentionally global +hasXML = false +isHelperEnabled = false -- common button parameters local buttonParameters = {} @@ -28,7 +32,6 @@ local customizableList = { -- index of the currently selected button (0-indexed from the top) local activeButtonIndex = -1 -local isHelperEnabled = false local hypothesisList = {} function updateSave() @@ -39,17 +42,17 @@ function updateSave() end function onLoad(savedData) - self.addTag("CardWithHelper") if savedData and savedData ~= "" then local loadedData = JSON.decode(savedData) isHelperEnabled = loadedData.isHelperEnabled activeButtonIndex = loadedData.activeButtonIndex end - checkOptionPanel() if activeButtonIndex > 0 then selectButton(activeButtonIndex) end + + syncDisplayWithOptionPanel() end function initialize() @@ -74,7 +77,7 @@ function selectButton(index) end end --- Create buttons based on the button parameters +-- create buttons based on the button parameters function createButtons() self.clearButtons() diff --git a/src/playercards/cards/FamilyInheritance.ttslua b/src/playercards/cards/FamilyInheritance.ttslua index 901f5a0c..e30f944b 100644 --- a/src/playercards/cards/FamilyInheritance.ttslua +++ b/src/playercards/cards/FamilyInheritance.ttslua @@ -1,23 +1,24 @@ require("playercards/CardsWithHelper") -local playermatApi = require("playermat/PlayermatApi") -local searchLib = require("util/SearchLib") -local tokenManager = require("core/token/TokenManager") +local playermatApi = require("playermat/PlayermatApi") +local searchLib = require("util/SearchLib") +local tokenManager = require("core/token/TokenManager") -local isHelperEnabled = false +-- intentionally global +hasXML = true +isHelperEnabled = false local clickableResourceCounter = nil -local foundTokens = 0 +local foundTokens = 0 function updateSave() self.script_state = JSON.encode({ isHelperEnabled = isHelperEnabled }) end -function onLoad() - self.addTag("CardWithHelper") +function onLoad(savedData) if savedData and savedData ~= "" then local loadedData = JSON.decode(savedData) isHelperEnabled = loadedData.isHelperEnabled end - checkOptionPanel() + syncDisplayWithOptionPanel() end function searchSelf() @@ -66,15 +67,3 @@ function loseAll(player) end printToColor("Discarded " .. foundTokens .. " resource(s).", player.color) end - --- hide buttons -function shutOff() - self.UI.hide("Helper") - updateSave() -end - --- show buttons -function initialize() - self.UI.show("Helper") - updateSave() -end diff --git a/src/playercards/cards/KohakuNarukami.ttslua b/src/playercards/cards/KohakuNarukami.ttslua index 5d478997..d033135f 100644 --- a/src/playercards/cards/KohakuNarukami.ttslua +++ b/src/playercards/cards/KohakuNarukami.ttslua @@ -5,8 +5,10 @@ local playermatApi = require("playermat/PlayermatApi") local searchLib = require("util/SearchLib") local tokenManager = require("core/token/TokenManager") -local isHelperEnabled = false -local updated +-- intentionally global +hasXML = true +isHelperEnabled = false +local updated, loopId local xmlData = { Action = { color = "#6D202CE6", onClick = "removeAndExtraAction" }, @@ -16,31 +18,31 @@ local xmlData = { } function updateSave() - self.script_state = JSON.encode({ isHelperEnabled = isHelperEnabled }) + self.script_state = JSON.encode({ + isHelperEnabled = isHelperEnabled, + loopId = loopId + }) end function onLoad(savedData) - self.addTag("CardWithHelper") if savedData and savedData ~= "" then local loadedData = JSON.decode(savedData) isHelperEnabled = loadedData.isHelperEnabled + loopId = loadedData.loopId end - checkOptionPanel() + syncDisplayWithOptionPanel() end --- hide buttons and stop monitoring -function shutOff() - self.UI.hide("Helper") - Wait.stopAll() - updateSave() -end - --- show buttons and begin monitoring chaos bag for curse and bless tokens function initialize() - self.UI.show("Helper") maybeUpdateButtonState() - Wait.time(maybeUpdateButtonState, 1, -1) - updateSave() + loopId = Wait.time(maybeUpdateButtonState, 1, -1) +end + +function shutOff() + if loopId then + Wait.stop(loopId) + loopId = nil + end end function addTokenToBag(_, _, tokenType) diff --git a/src/playercards/cards/WellConnected.ttslua b/src/playercards/cards/WellConnected.ttslua index c90ca5e4..8f8653f2 100644 --- a/src/playercards/cards/WellConnected.ttslua +++ b/src/playercards/cards/WellConnected.ttslua @@ -1,5 +1,11 @@ require("playercards/CardsWithHelper") -local playermatApi = require("playermat/PlayermatApi") +local playermatApi = require("playermat/PlayermatApi") + +-- intentionally global +hasXML = false +isHelperEnabled = false + +local modValue, loopId local buttonParameters = { click_function = "shutOff", @@ -10,13 +16,20 @@ local buttonParameters = { height = 175 } -local modValue - function updateSave() - self.script_state = JSON.encode({ isHelperEnabled = isHelperEnabled }) + self.script_state = JSON.encode({ + isHelperEnabled = isHelperEnabled, + loopId = loopId + }) end function onLoad(savedData) + if savedData and savedData ~= "" then + local loadedData = JSON.decode(savedData) + isHelperEnabled = loadedData.isHelperEnabled + loopId = loadedData.loopId + end + -- use metadata to detect level and adjust modValue accordingly if JSON.decode(self.getGMNotes()).level == 0 then modValue = 5 @@ -24,25 +37,22 @@ function onLoad(savedData) modValue = 4 end - self.addTag("CardWithHelper") - if savedData and savedData ~= "" then - local loadedData = JSON.decode(savedData) - isHelperEnabled = loadedData.isHelperEnabled - end - checkOptionPanel() - updateDisplay() + syncDisplayWithOptionPanel() end function initialize() self.clearButtons() self.createButton(buttonParameters) updateButton() - Wait.time(updateButton, 2, -1) + loopId = Wait.time(updateButton, 2, -1) end function shutOff() self.clearButtons() - Wait.stopAll() + if loopId then + Wait.stop(loopId) + loopId = nil + end end function updateButton()