Updated ChaosBagApi Utilization

Changed all existing references to chaos bag functionality to utilize the new ChaosBagApi
This commit is contained in:
Entrox-Licher 2023-08-08 02:17:43 -04:00
parent 92c8d9c6d1
commit 7659de4c59
7 changed files with 57 additions and 21 deletions

View File

@ -34,6 +34,7 @@ buttonParameters.height = 300
local name
local tokens = {}
local chaosBagApi = require("chaosbag/ChaosBagApi")
function onLoad()
-- create buttons for tokens
@ -60,11 +61,11 @@ function buttonClick(index, isRightClick)
local tokenId = TOKEN_IDS[index]
if isRightClick then
Global.call("removeChaosToken", tokenId)
chaosBagApi.removeChaosToken(tokenId)
else
local tokens = {}
local name = BUTTON_TOOLTIP[index]
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
for _, v in ipairs(chaosbag.getObjects()) do
if v.name == name then table.insert(tokens, v.guid) end
@ -76,7 +77,7 @@ function buttonClick(index, isRightClick)
return
end
Global.call("spawnChaosToken", tokenId)
chaosBagApi.spawnChaosToken(tokenId)
printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White")
end
end

View File

@ -37,6 +37,8 @@ local TOKEN_NAMES = {
""
}
local chaosBagApi = require("chaosbag/ChaosBagApi")
-- saving the precedence settings and information on the most recently loaded data
function onSave()
return JSON.encode({
@ -287,7 +289,7 @@ function layout(_, _, isRightClick)
return
end
local chaosBag = Global.call("findChaosBag")
local chaosBag = chaosBagApi.findChaosBag()
local data = {}
-- clone tokens from chaos bag (default position above trash can)

View File

@ -23,6 +23,8 @@ local FONT_COLOR = {
local whitespace = " "
local updating
local chaosBagApi = require("chaosbag/ChaosBagApi")
---------------------------------------------------------
-- creating buttons and menus + initializing tables
---------------------------------------------------------
@ -79,7 +81,7 @@ function initializeState()
resetTables()
-- count tokens in the bag
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
local tokens = {}
for _, v in ipairs(chaosbag.getObjects()) do
if v.name == "Bless" then
@ -117,7 +119,7 @@ end
-- context menu function 1
function doRemove(color)
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
-- remove tokens from chaos bag
local count = { Bless = 0, Curse = 0 }
@ -224,7 +226,7 @@ end
-- function that is called by click_functions 1+2 and calls the other functions
function callFunctions(token, isRightClick)
if not Global.call("canTouchChaosTokens") then
if not chaosBagApi.canTouchChaosTokens() then
return
end
local success
@ -286,11 +288,11 @@ function addToken(type)
end
numInPlay[type] = numInPlay[type] + 1
printToAll("Adding " .. type .. " token " .. formatTokenCount(type))
return Global.call("spawnChaosToken", type)
return chaosBagApi.spawnChaosToken(type)
end
function takeToken(type, remove)
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
if not remove and not SEAL_CARD_MESSAGE then
broadcastToColor("For sealing tokens on cards try right-clicking on the card for seal options.", playerColor)
SEAL_CARD_MESSAGE = true
@ -335,7 +337,7 @@ function returnToken(type)
printToColor("Couldn't find token " .. guid .. ", not returning to bag", playerColor)
return 0
end
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
if chaosbag == nil then
return 0
end
@ -382,7 +384,7 @@ function addMenuOptions(parameters)
end
function sealToken(type, playerColor, enemy)
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
if chaosbag == nil then return end
local pos = enemy.getPosition()
@ -407,7 +409,7 @@ function sealToken(type, playerColor, enemy)
end
function releaseToken(type, playerColor, enemy)
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
if chaosbag == nil then return end
local tokens = sealedTokens[enemy.getGUID()]
if tokens == nil or #tokens == 0 then return end

View File

@ -28,5 +28,31 @@ do
return Global.call("releaseAllSealedTokens", playerColor)
end
-- removes the specified chaos token from the chaos bag
---@param id String ID of the chaos token
ChaosBagApi.removeChaosToken = function(id)
return Global.call("removeChaosToken", id)
end
-- spawns the specified chaos token and puts it into the chaos bag
---@param id String ID of the chaos token
ChaosBagApi.spawnChaosToken = function(id)
return Global.call("spawnChaosToken", id)
end
-- Checks to see if the chaos bag can be manipulated. If a player is searching the bag when tokens
-- are drawn or replaced a TTS bug can cause those tokens to vanish. Any functions which change the
-- contents of the bag should check this method before doing so.
-- This method will broadcast a message to all players if the bag is being searched.
---@return Boolean. True if the bag is manipulated, false if it should be blocked.
ChaosBagApi.canTouchChaosTokens = function()
return Global.call("canTouchChaosTokens")
end
-- called by playermats (by the "Draw chaos token" button)
ChaosBagApi.drawChaosToken = function(params)
return Global.call("drawChaosToken", params)
end
return ChaosBagApi
end

View File

@ -8,6 +8,7 @@ local highlightMissing = false
local highlightCounted = false
local TRASHCAN
local TRASHCAN_GUID = "70b9f6"
local chaosBagApi = require("chaosbag/ChaosBagApi")
-- button creation when loading the game
function onLoad()
@ -276,7 +277,7 @@ function placeCard(card)
if obj.tag == "Deck" or obj.tag == "Card" then
-- put chaos tokens back into bag
elseif tokenChecker.isChaosToken(obj) then
local chaosBag = Global.call("findChaosBag")
local chaosBag = chaosBagApi.findChaosBag()
chaosBag.putObject(obj)
elseif obj.memo ~= nil and obj.getLock() == false then
TRASHCAN.putObject(obj)

View File

@ -74,6 +74,8 @@ local sealedTokens = {}
local ID_URL_MAP = {}
local tokensInBag = {}
local chaosBagApi = require("chaosbag/ChaosBagApi")
function onSave() return JSON.encode(sealedTokens) end
function onLoad(savedData)
@ -129,7 +131,7 @@ end
-- generates a list of chaos tokens that is in the chaos bag
function readBag()
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
tokensInBag = {}
for _, token in ipairs(chaosbag.getObjects()) do
@ -148,8 +150,8 @@ end
-- seals the named token on this card
function sealToken(name, playerColor)
if not Global.call("canTouchChaosTokens") then return end
local chaosbag = Global.call("findChaosBag")
if not chaosBagApi.canTouchChaosTokens() then return end
local chaosbag = chaosBagApi.findChaosBag()
for i, obj in ipairs(chaosbag.getObjects()) do
if obj.name == name then
chaosbag.takeObject({
@ -174,7 +176,7 @@ end
-- release the last sealed token
function releaseOneToken(playerColor)
if not Global.call("canTouchChaosTokens") then return end
if not chaosBagApi.canTouchChaosTokens() then return end
if #sealedTokens == 0 then
printToColor("No sealed token(s) found", playerColor)
else
@ -197,7 +199,7 @@ end
-- releases all sealed tokens
function releaseAllTokens(playerColor)
if not Global.call("canTouchChaosTokens") then return end
if not chaosBagApi.canTouchChaosTokens() then return end
if #sealedTokens == 0 then
printToColor("No sealed token(s) found", playerColor)
else
@ -215,7 +217,7 @@ function putTokenAway(guid)
if not token then return end
local name = token.getName()
local chaosbag = Global.call("findChaosBag")
local chaosbag = chaosBagApi.findChaosBag()
chaosbag.putObject(token)
tokenArrangerApi.layout()
if name == "Bless" or name == "Curse" then

View File

@ -56,6 +56,8 @@ local TRASHCAN
local STAT_TRACKER
local RESOURCE_COUNTER
local chaosBagApi = require("chaosbag/ChaosBagApi")
-- global variable so it can be reset by the Clean Up Helper
activeInvestigatorId = "00000"
local isDrawButtonVisible = false
@ -210,7 +212,7 @@ function makeDiscardHandlerFor(searchPosition, discardPosition)
end
-- put chaos tokens back into bag (e.g. Unrelenting)
elseif tokenChecker.isChaosToken(obj) then
local chaosBag = Global.call("findChaosBag")
local chaosBag = chaosBagApi.findChaosBag()
chaosBag.putObject(obj)
-- don't touch the table or this playmat itself
elseif obj.guid ~= "4ee1f2" and obj ~= self then
@ -772,7 +774,7 @@ end
---------------------------------------------------------
function drawChaosTokenButton(_, _, isRightClick)
Global.call("drawChaosToken", {self, DRAWN_CHAOS_TOKEN_OFFSET, isRightClick})
chaosBagApi.drawChaosToken({self, DRAWN_CHAOS_TOKEN_OFFSET, isRightClick})
end
function drawEncountercard(_, _, isRightClick)