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

View File

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