well connected bugfix and refactor

This commit is contained in:
Chr1Z93 2023-09-30 19:37:56 +02:00
parent a5589907db
commit 93bf6a8b61
2 changed files with 28 additions and 46 deletions

View File

@ -1,13 +1,10 @@
-- this script is shared between both the level 0 and the upgraded level 3 version of the card -- this script is shared between both the level 0 and the upgraded level 3 version of the card
local playmatApi = require("playermat/PlaymatApi") local playmatApi = require("playermat/PlaymatApi")
local display = false local modValue, loopId
local count = 0 local buttonParameters = {
local modValue = 5 -- level 0 Well Connected
local loopId = nil
local b_display = {
click_function = "toggleCounter", click_function = "toggleCounter",
tooltip = "disable counter",
function_owner = self, function_owner = self,
position = { 0.88, 0.5, -1.33 }, position = { 0.88, 0.5, -1.33 },
font_size = 150, font_size = 150,
@ -15,57 +12,42 @@ local b_display = {
height = 175 height = 175
} }
function onLoad(saved_data) function onSave() return JSON.encode({ loopId = loopId }) end
local notes = JSON.decode(self.getGMNotes())
if notes.id == "54006" then -- hardcoded card id for upgraded Well Connected (3) function onLoad(savedData)
modValue = 4 -- Well Connected (3) -- use metadata to detect level and adjust modValue accordingly
if JSON.decode(self.getGMNotes()).level == 0 then
modValue = 5
else
modValue = 4
end end
if saved_data != '' then if savedData ~= "" then
local loaded_data = JSON.decode(saved_data) local loadedData = JSON.decode(savedData)
display = not loaded_data.saved_display if loadedData.loopId then
self.createButton(buttonParameters)
self.clearButtons() loopId = Wait.time(updateDisplay, 2, -1)
toggleCounter() end
end end
self.addContextMenuItem('Toggle Counter', toggleCounter) self.addContextMenuItem("Toggle Counter", toggleCounter)
end
function onSave()
return JSON.encode({ saved_display = display })
end end
function toggleCounter() function toggleCounter()
display = not display if loopId ~= nil then
Wait.stop(loopId)
if display then
createUpdateDisplay()
loopId = Wait.time(createUpdateDisplay, 2, -1)
else
if loopId ~= nil then
Wait.stop(loopId)
end
self.clearButtons()
loopId = nil loopId = nil
end self.clearButtons()
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 else
self.editButton(b_display) self.createButton(buttonParameters)
updateDisplay()
loopId = Wait.time(updateDisplay, 2, -1)
end end
end end
function getPlayerResources() function updateDisplay()
local matColor = playmatApi.getMatColorByPosition(self.getPosition()) local matColor = playmatApi.getMatColorByPosition(self.getPosition())
return playmatApi.getCounterValue(matColor, "ResourceCounter") local resources = playmatApi.getCounterValue(matColor, "ResourceCounter")
local count = tostring(math.floor(resources / modValue))
self.editButton({ index = 0, label = count })
end end

View File

@ -798,7 +798,7 @@ end
-- returns the resource counter amount -- returns the resource counter amount
---@param type String Counter to target ---@param type String Counter to target
function getCounterValue(type) function getCounterValue(type)
return ownedObjects.type.getVar("val") return ownedObjects[type].getVar("val")
end end
-- set investigator skill tracker to "1, 1, 1, 1" -- set investigator skill tracker to "1, 1, 1, 1"