small code update and resolving for player that clicks

This commit is contained in:
Chr1Z93 2024-03-04 00:10:27 +01:00
parent da9f955a59
commit 93a6957c9e
2 changed files with 87 additions and 67 deletions

View File

@ -2,10 +2,6 @@ local chaosBagApi = require("chaosbag/ChaosBagApi")
local guidReferenceApi = require("core/GUIDReferenceApi")
local playmatApi = require("playermat/PlaymatApi")
function onSave()
return JSON.encode({ loopId = loopId })
end
function onLoad(savedData)
self.addContextMenuItem("Enable Helper", createButtons)
if savedData ~= "" then
@ -17,39 +13,57 @@ function onLoad(savedData)
end
function deleteButtons()
self.UI.setAttribute("inactives", "active", false)
self.UI.setAttribute("actives", "active", false)
self.clearContextMenu()
self.addContextMenuItem("Enable Helper", createButtons)
Wait.stop(loopId)
self.UI.setAttribute("inactives", "active", false)
self.UI.setAttribute("actives", "active", false)
if loopId then Wait.stop(loopId) end
loopId = nil
self.script_state = JSON.encode({ loopId = loopId })
end
-- Create buttons and begin monitoring chaos bag for curse and bless tokens
-- create buttons and begin monitoring chaos bag for curse and bless tokens
function createButtons()
self.clearContextMenu()
self.addContextMenuItem("Clear Helper", deleteButtons)
self.UI.setAttribute("inactives", "active", true)
self.UI.setAttribute("actives", "active", true)
self.UI.show("inactiveBless")
self.UI.show("inactiveCurse")
self.UI.hide("Bless")
self.UI.hide("Curse")
currentState = "Empty"
loopId = Wait.time(countBlessCurse, 1, -1)
loopId = Wait.time(maybeUpdateButtonState, 1, -1)
self.script_state = JSON.encode({ loopId = loopId })
end
function resolveToken(_, _, tokenType)
local closestMatColor = playmatApi.getMatColorByPosition(self.getPosition())
local mat = guidReferenceApi.getObjectByOwnerAndType(closestMatColor, "Playermat")
function resolveToken(player, _, tokenType)
local matColor
if player.color == "Black" then
matColor = playmatApi.getMatColorByPosition(self.getPosition())
else
matColor = playmatApi.getMatColor(player.color)
end
local mat = guidReferenceApi.getObjectByOwnerAndType(matColor, "Playermat")
chaosBagApi.drawChaosToken(mat, true, tokenType)
end
-- count tokens in the bag and show appropriate buttons
function countBlessCurse()
function maybeUpdateButtonState()
local numInBag = getBlessCurseInBag()
local state = { Bless = false, Curse = false }
if numInBag.Bless >= numInBag.Curse and numInBag.Bless > 0 then
state.Bless = true
end
if numInBag.Curse >= numInBag.Bless and numInBag.Curse > 0 then
state.Curse = true
end
setUiState(state)
end
function getBlessCurseInBag()
local numInBag = { Bless = 0, Curse = 0 }
local chaosBag = chaosBagApi.findChaosBag()
local tokens = {}
for _, v in ipairs(chaosBag.getObjects()) do
if v.name == "Bless" then
numInBag.Bless = numInBag.Bless + 1
@ -57,48 +71,38 @@ function countBlessCurse()
numInBag.Curse = numInBag.Curse + 1
end
end
if numInBag.Bless > numInBag.Curse then
if currentState ~= "More Bless" then
self.UI.show("Bless")
self.UI.hide("inactiveBless")
self.UI.show("inactiveCurse")
self.UI.hide("Curse")
end
currentState = "More Bless"
elseif numInBag.Curse > numInBag.Bless then
if currentState ~= "More Curse" then
self.UI.show("Curse")
self.UI.hide("inactiveCurse")
self.UI.show("inactiveBless")
self.UI.hide("Bless")
end
currentState = "More Curse"
elseif numInBag.Curse == 0 then
if currentState ~= "Empty" then
self.UI.show("inactiveBless")
self.UI.hide("Bless")
self.UI.show("inactiveCurse")
self.UI.hide("Curse")
end
currentState = "Empty"
return numInBag
end
function setUiState(params)
-- set bless state
if params.Bless then
self.UI.show("Bless")
self.UI.hide("inactiveBless")
else
if currentState ~= "Equal" then
self.UI.show("Bless")
self.UI.hide("inactiveBless")
self.UI.show("Curse")
self.UI.hide("inactiveCurse")
end
currentState = "Equal"
self.UI.show("inactiveBless")
self.UI.hide("Bless")
end
-- set curse state
if params.Curse then
self.UI.show("Curse")
self.UI.hide("inactiveCurse")
else
self.UI.show("inactiveCurse")
self.UI.hide("Curse")
end
end
function errorMessage ()
if currentState == "Empty" then
broadcastToAll("There are no Bless or Curse tokens in the chaos bag.","Red")
elseif currentState == "More Bless" then
broadcastToAll("There are more Bless tokens than Curse tokens in the chaos bag.","Red")
function errorMessage()
local numInBag = getBlessCurseInBag()
if numInBag.Bless == 0 and numInBag.Curse == 0 then
broadcastToAll("There are no Bless or Curse tokens in the chaos bag.", "Red")
elseif numInBag.Bless > numInBag.Curse then
broadcastToAll("There are more Bless tokens than Curse tokens in the chaos bag.", "Red")
else
broadcastToAll("There are more Curse tokens than Bless tokens in the chaos bag.","Red")
broadcastToAll("There are more Curse tokens than Bless tokens in the chaos bag.", "Red")
end
end

View File

@ -3,10 +3,20 @@
font="font_teutonic-arkham"
fontSize="300"
iconWidth="400"
iconAlignment="Right"/>
iconAlignment="Right"
text="Resolve"/>
<Button class="inactive"
onClick="errorMessage"
color="#353535E6"
textColor="#A0A0A0"/>
<Button class="active"
onClick="resolveToken"
textColor="white"
active="false"/>
<Panel position="0 -55 -22"
rotation="0 0 180"
height="900" width="1400"
height="900"
width="1400"
scale="0.1 0.1 1"/>
<TableLayout active="false"
cellSpacing="80"
@ -17,14 +27,18 @@
<TableLayout id="actives">
<Row>
<Cell>
<Button id="Bless" icon="bless" textColor="White"
onClick="resolveToken" color="#9D702CE6" iconAlignment="Right">Resolve</Button>
<Button id="Bless"
icon="bless"
color="#9D702CE6"
class="active"/>
</Cell>
</Row>
<Row>
<Cell>
<Button id="Curse" icon="curse" textColor="White"
onClick="resolveToken" color="#633A84E6">Resolve</Button>
<Button id="Curse"
icon="curse"
color="#633A84E6"
class="active"/>
</Cell>
</Row>
</TableLayout>
@ -34,14 +48,16 @@
<TableLayout id="inactives">
<Row>
<Cell>
<Button id="inactiveBless" icon="bless" textColor="#A0A0A0"
onClick="errorMessage" color="#353535E6">Resolve</Button>
<Button id="inactiveBless"
icon="bless"
class="inactive"/>
</Cell>
</Row>
<Row>
<Cell>
<Button id="inactiveCurse" icon="curse" textColor="#A0A0A0"
onClick="errorMessage" color="#353535E6">Resolve</Button>
<Button id="inactiveCurse"
icon="curse"
class="inactive"/>
</Cell>
</Row>
</TableLayout>