From 7f58638ef8672b5254f91df2c62674e3781d05bd Mon Sep 17 00:00:00 2001 From: bankey Date: Tue, 29 Aug 2023 15:56:12 -0400 Subject: [PATCH 1/3] adds context menu option to 'Well Connected' player cards to display skill boost value right on the card --- .../WellConnected.66b7d5.json | 2 +- .../WellConnected3.170127.json | 2 +- src/playercards/cards/WellConnected.ttslua | 52 +++++++++++++++++++ src/playercards/cards/WellConnected3.ttslua | 52 +++++++++++++++++++ src/playermat/Playmat.ttslua | 5 ++ src/playermat/PlaymatApi.ttslua | 9 ++++ 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/playercards/cards/WellConnected.ttslua create mode 100644 src/playercards/cards/WellConnected3.ttslua diff --git a/objects/AllPlayerCards.15bb07/WellConnected.66b7d5.json b/objects/AllPlayerCards.15bb07/WellConnected.66b7d5.json index bbfc74cf..c2484b7a 100644 --- a/objects/AllPlayerCards.15bb07/WellConnected.66b7d5.json +++ b/objects/AllPlayerCards.15bb07/WellConnected.66b7d5.json @@ -33,7 +33,7 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, - "LuaScript": "", + "LuaScript": "require(\"playercards/cards/WellConnected\")", "LuaScriptState": "", "MeasureMovement": false, "Name": "Card", diff --git a/objects/AllPlayerCards.15bb07/WellConnected3.170127.json b/objects/AllPlayerCards.15bb07/WellConnected3.170127.json index e9dadd28..7f2975ca 100644 --- a/objects/AllPlayerCards.15bb07/WellConnected3.170127.json +++ b/objects/AllPlayerCards.15bb07/WellConnected3.170127.json @@ -33,7 +33,7 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, - "LuaScript": "", + "LuaScript": "require(\"playercards/cards/WellConnected3\")", "LuaScriptState": "", "MeasureMovement": false, "Name": "Card", diff --git a/src/playercards/cards/WellConnected.ttslua b/src/playercards/cards/WellConnected.ttslua new file mode 100644 index 00000000..9a6a8d45 --- /dev/null +++ b/src/playercards/cards/WellConnected.ttslua @@ -0,0 +1,52 @@ +local playmatApi = require("playermat/PlaymatApi") + +local display = false +local count = 0 +local modValue = 5 +local loopId = nil + +local b_display = { + index = 0, + click_function = 'none', + function_owner = self, + position = {-0.25,1,-0.75}, + font_color = {1,1,1,100}, + font_size = 250, + width = 0, + height = 0 +} + +function onLoad() + self.addContextMenuItem('Toggle Counter', toggleCounter) +end + +function toggleCounter() + display = not display + + if display then + createUpdateDisplay() + loopId = Wait.time(|| createUpdateDisplay(), 2, -1) + else + Wait.stop(loopId) + self.clearButtons() + loopId = nil + end +end + +function createUpdateDisplay() + count = math.max(math.floor(getPlayerResources() / modValue), 0) + + b_display.label = tostring(count) + + if loopId == nil then + self.createButton(b_display) + else + self.editButton(b_display) + end +end + +function getPlayerResources() + local matColor = playmatApi.getMatColorByPosition(self.getPosition()) + + return playmatApi.getResourceCount(matColor) +end \ No newline at end of file diff --git a/src/playercards/cards/WellConnected3.ttslua b/src/playercards/cards/WellConnected3.ttslua new file mode 100644 index 00000000..f412610a --- /dev/null +++ b/src/playercards/cards/WellConnected3.ttslua @@ -0,0 +1,52 @@ +local playmatApi = require("playermat/PlaymatApi") + +local display = false +local count = 0 +local modValue = 4 +local loopId = nil + +local b_display = { + index = 0, + click_function = 'none', + function_owner = self, + position = {-0.25,1,-0.75}, + font_color = {1,1,1,100}, + font_size = 250, + width = 0, + height = 0 +} + +function onLoad() + self.addContextMenuItem('Toggle Counter', toggleCounter) +end + +function toggleCounter() + display = not display + + if display then + createUpdateDisplay() + loopId = Wait.time(|| createUpdateDisplay(), 2, -1) + else + Wait.stop(loopId) + self.clearButtons() + loopId = nil + end +end + +function createUpdateDisplay() + count = math.max(math.floor(getPlayerResources() / modValue), 0) + + b_display.label = tostring(count) + + if loopId == nil then + self.createButton(b_display) + else + self.editButton(b_display) + end +end + +function getPlayerResources() + local matColor = playmatApi.getMatColorByPosition(self.getPosition()) + + return playmatApi.getResourceCount(matColor) +end \ No newline at end of file diff --git a/src/playermat/Playmat.ttslua b/src/playermat/Playmat.ttslua index ca349567..bcc65c47 100644 --- a/src/playermat/Playmat.ttslua +++ b/src/playermat/Playmat.ttslua @@ -361,6 +361,11 @@ function gainResources(amount) RESOURCE_COUNTER.call("updateVal", count + add) end +-- returns the resource counter amount +function getResourceCount() + return RESOURCE_COUNTER.getVar("val") +end + -- function for "draw 1 button" (that can be added via option panel) function doDrawOne(_, color) -- send messages to player who clicked button if no seated player found diff --git a/src/playermat/PlaymatApi.ttslua b/src/playermat/PlaymatApi.ttslua index 84a1ffd0..2866f39d 100644 --- a/src/playermat/PlaymatApi.ttslua +++ b/src/playermat/PlaymatApi.ttslua @@ -178,6 +178,15 @@ do end end + -- Returns the resource counter amount for the requested playermat + PlaymatApi.getResourceCount = function(matColor) + local resources = 0 + for _, mat in ipairs(internal.getMatForColor(matColor)) do + resources = resources + mat.call("getResourceCount") + end + return resources + end + -- Discard a non-hidden card from the corresponding player's hand PlaymatApi.doDiscardOne = function(matColor) for _, mat in ipairs(internal.getMatForColor(matColor)) do From c678537581fda2982908bb5aa5ba8d6c9658f024 Mon Sep 17 00:00:00 2001 From: bankey Date: Wed, 30 Aug 2023 09:37:23 -0400 Subject: [PATCH 2/3] both Well Connected 0 and Well Connected 3 now share the same script, removes unecessary index from button, updates getResourceCount playmat api function, adds onSave() function, updates button look and position --- .../WellConnected3.170127.json | 2 +- src/playercards/cards/WellConnected.ttslua | 38 ++++++++++---- src/playercards/cards/WellConnected3.ttslua | 52 ------------------- src/playermat/PlaymatApi.ttslua | 7 +-- 4 files changed, 31 insertions(+), 68 deletions(-) delete mode 100644 src/playercards/cards/WellConnected3.ttslua diff --git a/objects/AllPlayerCards.15bb07/WellConnected3.170127.json b/objects/AllPlayerCards.15bb07/WellConnected3.170127.json index 7f2975ca..1c99a2e6 100644 --- a/objects/AllPlayerCards.15bb07/WellConnected3.170127.json +++ b/objects/AllPlayerCards.15bb07/WellConnected3.170127.json @@ -33,7 +33,7 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, - "LuaScript": "require(\"playercards/cards/WellConnected3\")", + "LuaScript": "require(\"playercards/cards/WellConnected\")", "LuaScriptState": "", "MeasureMovement": false, "Name": "Card", diff --git a/src/playercards/cards/WellConnected.ttslua b/src/playercards/cards/WellConnected.ttslua index 9a6a8d45..bde1ae32 100644 --- a/src/playercards/cards/WellConnected.ttslua +++ b/src/playercards/cards/WellConnected.ttslua @@ -1,23 +1,41 @@ +-- this script is shared between both the level 0 and the upgraded level 3 version of the card local playmatApi = require("playermat/PlaymatApi") local display = false local count = 0 -local modValue = 5 +local modValue = 5 -- level 0 Well Connected local loopId = nil local b_display = { - index = 0, - click_function = 'none', + click_function = "toggleCounter", function_owner = self, - position = {-0.25,1,-0.75}, - font_color = {1,1,1,100}, - font_size = 250, - width = 0, - height = 0 + position = {0.88,0.5,-1.33}, + font_size = 150, + width = 175, + height = 175 } -function onLoad() - self.addContextMenuItem('Toggle Counter', toggleCounter) +function onLoad(saved_data) + local notes = JSON.decode(self.getGMNotes()) + + if notes.id == "54006" then -- hardcoded card id for upgraded Well Connected (3) + modValue = 4 -- Well Connected (3) + end + + if saved_data != '' then + local loaded_data = JSON.decode(saved_data) + display = not loaded_data.saved_display + + toggleCounter() + end + + self.addContextMenuItem('Toggle Counter', toggleCounter) +end + +function onSave() + local data_to_save = {saved_display = display} + local saved_data = JSON.encode(data_to_save) + return saved_data end function toggleCounter() diff --git a/src/playercards/cards/WellConnected3.ttslua b/src/playercards/cards/WellConnected3.ttslua deleted file mode 100644 index f412610a..00000000 --- a/src/playercards/cards/WellConnected3.ttslua +++ /dev/null @@ -1,52 +0,0 @@ -local playmatApi = require("playermat/PlaymatApi") - -local display = false -local count = 0 -local modValue = 4 -local loopId = nil - -local b_display = { - index = 0, - click_function = 'none', - function_owner = self, - position = {-0.25,1,-0.75}, - font_color = {1,1,1,100}, - font_size = 250, - width = 0, - height = 0 -} - -function onLoad() - self.addContextMenuItem('Toggle Counter', toggleCounter) -end - -function toggleCounter() - display = not display - - if display then - createUpdateDisplay() - loopId = Wait.time(|| createUpdateDisplay(), 2, -1) - else - Wait.stop(loopId) - self.clearButtons() - loopId = nil - end -end - -function createUpdateDisplay() - count = math.max(math.floor(getPlayerResources() / modValue), 0) - - b_display.label = tostring(count) - - if loopId == nil then - self.createButton(b_display) - else - self.editButton(b_display) - end -end - -function getPlayerResources() - local matColor = playmatApi.getMatColorByPosition(self.getPosition()) - - return playmatApi.getResourceCount(matColor) -end \ No newline at end of file diff --git a/src/playermat/PlaymatApi.ttslua b/src/playermat/PlaymatApi.ttslua index 2866f39d..9df18be2 100644 --- a/src/playermat/PlaymatApi.ttslua +++ b/src/playermat/PlaymatApi.ttslua @@ -180,11 +180,8 @@ do -- Returns the resource counter amount for the requested playermat PlaymatApi.getResourceCount = function(matColor) - local resources = 0 - for _, mat in ipairs(internal.getMatForColor(matColor)) do - resources = resources + mat.call("getResourceCount") - end - return resources + local mat = getObjectFromGUID(MAT_IDS[matColor]) + return mat.call("getResourceCount") end -- Discard a non-hidden card from the corresponding player's hand From 1ef635fcfe4b807511f79c23e285994f8c03b72c Mon Sep 17 00:00:00 2001 From: bankey Date: Fri, 1 Sep 2023 15:51:47 -0400 Subject: [PATCH 3/3] fixes issue where saving and loading would cause an error when the display is not enabled, minor code cleanup --- src/playercards/cards/WellConnected.ttslua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/playercards/cards/WellConnected.ttslua b/src/playercards/cards/WellConnected.ttslua index bde1ae32..7e7fda73 100644 --- a/src/playercards/cards/WellConnected.ttslua +++ b/src/playercards/cards/WellConnected.ttslua @@ -26,6 +26,7 @@ function onLoad(saved_data) local loaded_data = JSON.decode(saved_data) display = not loaded_data.saved_display + self.clearButtons() toggleCounter() end @@ -33,9 +34,7 @@ function onLoad(saved_data) end function onSave() - local data_to_save = {saved_display = display} - local saved_data = JSON.encode(data_to_save) - return saved_data + return JSON.encode({saved_display = display}) end function toggleCounter() @@ -45,7 +44,10 @@ function toggleCounter() createUpdateDisplay() loopId = Wait.time(|| createUpdateDisplay(), 2, -1) else - Wait.stop(loopId) + if loopId ~= nil then + Wait.stop(loopId) + end + self.clearButtons() loopId = nil end