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

View File

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