adding more functionality to player card sealing
This commit is contained in:
parent
ca214f7ecd
commit
e8d77d35b0
@ -1,29 +1,91 @@
|
||||
--[[ Library for cards that seal tokens
|
||||
This file is used to add sealing option to cards' context menu.
|
||||
Valid options (set before requiring this file):
|
||||
SHOW_READ_BAG -- boolean
|
||||
SHOW_SINGLE_RELEASE -- boolean
|
||||
SHOW_MULTI_RELEASE -- number (amount of tokens to release at once)
|
||||
VALID_TOKENS -- table ([tokenName] = true)
|
||||
INVALID_TOKENS -- table ([tokenName] = true)]]
|
||||
|
||||
local blessCurseManagerApi = require("chaosbag/BlessCurseManagerApi")
|
||||
local tokenArrangerApi = require("accessories/TokenArrangerApi")
|
||||
|
||||
function onSave() return JSON.encode(sealedTokens) end
|
||||
|
||||
function onLoad(savedData)
|
||||
sealedTokens = JSON.decode(savedData) or {}
|
||||
ID_URL_MAP = Global.getTable("ID_URL_MAP")
|
||||
generateContextMenu()
|
||||
end
|
||||
|
||||
-- add menu items
|
||||
-- builds the context menu
|
||||
function generateContextMenu()
|
||||
self.clearContextMenu()
|
||||
|
||||
-- only show this for cards that need a dynamic list of tokens (for example 'Unrelenting')
|
||||
if SHOW_READ_BAG then
|
||||
self.addContextMenuItem("Update list", generateContextMenu)
|
||||
readBag()
|
||||
end
|
||||
|
||||
-- conditional single or multi release options
|
||||
if SHOW_SINGLE_RELEASE then
|
||||
self.addContextMenuItem("Release token", releaseOneToken)
|
||||
elseif SHOW_MULTI_RELEASE then
|
||||
self.addContextMenuItem("Release " .. SHOW_MULTI_RELEASE .. " token(s)", function(playerColor)
|
||||
if SHOW_MULTI_RELEASE >= #sealedTokens then
|
||||
for i = 1, SHOW_MULTI_RELEASE do
|
||||
releaseOneToken(playerColor)
|
||||
end
|
||||
else
|
||||
printToColor("Not enough " .. name .. " tokens sealed.", playerColor)
|
||||
end
|
||||
end)
|
||||
else
|
||||
self.addContextMenuItem("Release token(s)", releaseTokens)
|
||||
end
|
||||
|
||||
-- main context menu options to seal tokens
|
||||
for _, map in pairs(ID_URL_MAP) do
|
||||
if VALID_TOKENS[map.name] ~= nil then
|
||||
self.addContextMenuItem("Seal " .. map.name, function(playerColor) sealToken(map.name, playerColor) end)
|
||||
if (VALID_TOKENS[map.name] ~= nil) or (SHOW_READ_BAG and tokensInBag[map.name] and not INVALID_TOKENS[map.name]) then
|
||||
if not SHOW_MULTI_SEAL then
|
||||
self.addContextMenuItem("Seal " .. map.name, function(playerColor)
|
||||
sealToken(map.name, playerColor)
|
||||
end)
|
||||
else
|
||||
self.addContextMenuItem("Seal " .. SHOW_MULTI_SEAL .. " " .. map.name, function(playerColor)
|
||||
readBag()
|
||||
local allowed = true
|
||||
local notFound
|
||||
|
||||
for name, _ in pairs(VALID_TOKENS) do
|
||||
if (tokensInBag[name] or 0) < SHOW_MULTI_SEAL then
|
||||
allowed = false
|
||||
notFound = name
|
||||
end
|
||||
end
|
||||
|
||||
if allowed then
|
||||
for i = 1, SHOW_MULTI_SEAL do
|
||||
sealToken(map.name, playerColor)
|
||||
end
|
||||
else
|
||||
printToColor("Not enough " .. notFound .. " tokens in the chaos bag.", playerColor)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- generates a list of chaos tokens that is in the chaos bag
|
||||
function readBag()
|
||||
local chaosbag = Global.call("findChaosBag")
|
||||
tokensInBag = {}
|
||||
|
||||
for _, token in ipairs(chaosbag.getObjects()) do
|
||||
tokensInBag[token.name] = (tokensInBag[token.name] or 0) + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- seals the named token on this card
|
||||
@ -39,7 +101,7 @@ function sealToken(name, playerColor)
|
||||
callback_function = function(token)
|
||||
local guid = token.getGUID()
|
||||
table.insert(sealedTokens, guid)
|
||||
|
||||
tokenArrangerApi.layout()
|
||||
if name == "Bless" or name == "Curse" then
|
||||
blessCurseManagerApi.sealedToken(name, guid)
|
||||
end
|
||||
@ -53,18 +115,11 @@ end
|
||||
|
||||
-- release the last sealed token
|
||||
function releaseOneToken(playerColor)
|
||||
if sealedTokens == {} then
|
||||
if sealedTokens == {} or #sealedTokens == 0 then
|
||||
printToColor("No sealed token(s) found", playerColor)
|
||||
else
|
||||
printToColor("Releasing token", playerColor)
|
||||
local chaosbag = Global.call("findChaosBag")
|
||||
local guid = table.remove(sealedTokens)
|
||||
local token = getObjectFromGUID(guid)
|
||||
local name = token.getName()
|
||||
chaosbag.putObject(token)
|
||||
if name == "Bless" or name == "Curse" then
|
||||
blessCurseManagerApi.releasedToken(name, guid)
|
||||
end
|
||||
putTokenAway(table.remove(sealedTokens))
|
||||
end
|
||||
end
|
||||
|
||||
@ -74,15 +129,23 @@ function releaseTokens(playerColor)
|
||||
printToColor("No sealed token(s) found", playerColor)
|
||||
else
|
||||
printToColor("Releasing token(s)", playerColor)
|
||||
local chaosbag = Global.call("findChaosBag")
|
||||
for _, guid in ipairs(sealedTokens) do
|
||||
local token = getObjectFromGUID(guid)
|
||||
local name = token.getName()
|
||||
chaosbag.putObject(token)
|
||||
if name == "Bless" or name == "Curse" then
|
||||
blessCurseManagerApi.releasedToken(name, guid)
|
||||
end
|
||||
putTokenAway(guid)
|
||||
end
|
||||
sealedTokens = {}
|
||||
end
|
||||
end
|
||||
|
||||
-- returns the token (referenced by GUID) to the chaos bag
|
||||
function putTokenAway(guid)
|
||||
local token = getObjectFromGUID(guid)
|
||||
if not token then return end
|
||||
|
||||
local name = token.getName()
|
||||
local chaosbag = Global.call("findChaosBag")
|
||||
chaosbag.putObject(token)
|
||||
tokenArrangerApi.layout()
|
||||
if name == "Bless" or name == "Curse" then
|
||||
blessCurseManagerApi.releasedToken(name, guid)
|
||||
end
|
||||
end
|
||||
|
@ -1,93 +1,8 @@
|
||||
function onload()
|
||||
mode = "Bless"
|
||||
chaosbag = getChaosBag()
|
||||
manager = getObjectFromGUID("5933fb")
|
||||
sealedTokens = { }
|
||||
IMAGE_TOKEN_MAP = { }
|
||||
for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do
|
||||
IMAGE_TOKEN_MAP[i] = v
|
||||
end
|
||||
VALID_TOKENS = {
|
||||
["Bless"] = true
|
||||
}
|
||||
|
||||
-- add menu items
|
||||
self.clearContextMenu()
|
||||
self.addContextMenuItem("Release Token", releaseTokens, true)
|
||||
for url,name in pairs(IMAGE_TOKEN_MAP) do
|
||||
if name == mode then
|
||||
self.addContextMenuItem("Seal 2 " .. mode, function(playerColor) sealToken(url, playerColor) end, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
SHOW_SINGLE_RELEASE = true
|
||||
SHOW_MULTI_SEAL = 2
|
||||
|
||||
function sealToken(url, playerColor)
|
||||
local pos = self.getPosition()
|
||||
|
||||
local name = IMAGE_TOKEN_MAP[url]
|
||||
local indexes = {}
|
||||
|
||||
for i,obj in ipairs(chaosbag.getObjects()) do
|
||||
if obj.name == name then
|
||||
table.insert(indexes, obj.index)
|
||||
end
|
||||
end
|
||||
if #indexes < 2 then
|
||||
printToColor("Fewer than 2 " .. name .. " tokens in bag", playerColor)
|
||||
return
|
||||
end
|
||||
table.sort(indexes)
|
||||
chaosbag.takeObject({
|
||||
position={ pos.x, pos.y + 1, pos.z },
|
||||
index=indexes[#indexes],
|
||||
smooth=false,
|
||||
callback_function=_sealToken
|
||||
})
|
||||
chaosbag.takeObject({
|
||||
position={ pos.x, pos.y + 2, pos.z },
|
||||
index=indexes[#indexes-1],
|
||||
smooth=false,
|
||||
callback_function=_sealToken
|
||||
})
|
||||
end
|
||||
|
||||
function _sealToken(obj)
|
||||
table.insert(sealedTokens, obj)
|
||||
local guid = obj.getGUID()
|
||||
local tokensTaken = manager.getVar("tokensTaken")
|
||||
table.insert(tokensTaken[mode], guid)
|
||||
manager.setVar("tokensTaken", tokensTaken)
|
||||
manager.setVar("mode", mode)
|
||||
printToAll("Sealing " .. mode .. " token " .. manager.call("getTokenCount"))
|
||||
end
|
||||
|
||||
function releaseTokens(playerColor)
|
||||
if #sealedTokens == 0 then return end
|
||||
local token = sealedTokens[#sealedTokens]
|
||||
if token ~= nil then
|
||||
local guid = token.getGUID()
|
||||
chaosbag.putObject(token)
|
||||
local tokensTaken = manager.getVar("tokensTaken")
|
||||
for i,v in ipairs(tokensTaken[mode]) do
|
||||
if v == guid then
|
||||
table.remove(tokensTaken[mode], i)
|
||||
break
|
||||
end
|
||||
end
|
||||
manager.setVar("tokensTaken", tokensTaken)
|
||||
manager.setVar("mode", mode)
|
||||
printToAll("Releasing " .. mode .. " token" .. manager.call("getTokenCount"))
|
||||
end
|
||||
|
||||
table.remove(sealedTokens)
|
||||
end
|
||||
|
||||
function getChaosBag()
|
||||
local items = getObjectFromGUID("83ef06").getObjects()
|
||||
local chaosbag = nil
|
||||
for i,v in ipairs(items) do
|
||||
if v.getDescription() == "Chaos Bag" then
|
||||
chaosbag = getObjectFromGUID(v.getGUID())
|
||||
break
|
||||
end
|
||||
end
|
||||
if chaosbag == nil then printToAll("No chaos bag found") end
|
||||
return chaosbag
|
||||
end
|
||||
require("playercards/CardsThatSealTokens")
|
||||
|
@ -1,75 +1,9 @@
|
||||
function onload()
|
||||
chaosbag = getChaosBag()
|
||||
sealedTokens = { }
|
||||
IMAGE_TOKEN_MAP = { }
|
||||
for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do
|
||||
IMAGE_TOKEN_MAP[i] = v
|
||||
end
|
||||
readBag()
|
||||
end
|
||||
VALID_TOKENS = {}
|
||||
|
||||
function readBag(playerColor)
|
||||
if playerColor ~= nil then
|
||||
printToColor("Reading chaos bag", playerColor)
|
||||
end
|
||||
INVALID_TOKENS = {
|
||||
["Auto-fail"] = true
|
||||
}
|
||||
|
||||
local tokensInBag = { }
|
||||
for i,token in ipairs(chaosbag.getObjects()) do
|
||||
if token.name ~= nil and token.name ~= "" then
|
||||
tokensInBag[token.name] = true
|
||||
end
|
||||
end
|
||||
SHOW_READ_BAG = true
|
||||
|
||||
-- add menu items
|
||||
self.clearContextMenu()
|
||||
self.addContextMenuItem("Release Token", releaseTokens)
|
||||
for url,name in pairs(IMAGE_TOKEN_MAP) do
|
||||
if tokensInBag[name] and name ~= "Auto-fail" then
|
||||
self.addContextMenuItem("Seal " .. name, function(playerColor) sealToken(url, playerColor) end)
|
||||
end
|
||||
end
|
||||
self.addContextMenuItem("Read Chaos Bag", readBag)
|
||||
end
|
||||
|
||||
function sealToken(url, playerColor)
|
||||
local pos = self.getPosition()
|
||||
|
||||
local name = IMAGE_TOKEN_MAP[url]
|
||||
for i,obj in ipairs(chaosbag.getObjects()) do
|
||||
if obj.name == name then
|
||||
chaosbag.takeObject({
|
||||
position={ pos.x, pos.y + 1, pos.z },
|
||||
index=i-1,
|
||||
smooth=false,
|
||||
callback_function=_sealToken
|
||||
})
|
||||
return
|
||||
end
|
||||
end
|
||||
printToColor(name .. " token not found in bag", playerColor)
|
||||
end
|
||||
|
||||
function _sealToken(obj)
|
||||
table.insert(sealedTokens, obj)
|
||||
end
|
||||
|
||||
function releaseTokens(playerColor)
|
||||
printToColor("Releasing token", playerColor)
|
||||
for i,obj in ipairs(sealedTokens) do
|
||||
chaosbag.putObject(obj)
|
||||
end
|
||||
sealedTokens = { }
|
||||
end
|
||||
|
||||
function getChaosBag()
|
||||
local items = getObjectFromGUID("83ef06").getObjects()
|
||||
local chaosbag = nil
|
||||
for i,v in ipairs(items) do
|
||||
if v.getDescription() == "Chaos Bag" then
|
||||
chaosbag = getObjectFromGUID(v.getGUID())
|
||||
break
|
||||
end
|
||||
end
|
||||
if chaosbag == nil then printToAll("No chaos bag found") end
|
||||
return chaosbag
|
||||
end
|
||||
require("playercards/CardsThatSealTokens")
|
||||
|
@ -1,97 +1,6 @@
|
||||
function onload()
|
||||
chaosbag = getChaosBag()
|
||||
manager = getObjectFromGUID("5933fb")
|
||||
sealedTokens = { }
|
||||
IMAGE_TOKEN_MAP = { }
|
||||
for i,v in pairs(Global.getVar("IMAGE_TOKEN_MAP")) do
|
||||
IMAGE_TOKEN_MAP[i] = v
|
||||
end
|
||||
VALID_TOKENS = {}
|
||||
INVALID_TOKENS = {}
|
||||
|
||||
readBag()
|
||||
end
|
||||
SHOW_READ_BAG = true
|
||||
|
||||
function sealToken(url, playerColor)
|
||||
local pos = self.getPosition()
|
||||
|
||||
local name = IMAGE_TOKEN_MAP[url]
|
||||
for i,obj in ipairs(chaosbag.getObjects()) do
|
||||
if obj.name == name then
|
||||
chaosbag.takeObject({
|
||||
position={ pos.x, pos.y + 1, pos.z },
|
||||
index=i-1,
|
||||
smooth=false,
|
||||
callback_function=_sealToken
|
||||
})
|
||||
return
|
||||
end
|
||||
end
|
||||
printToColor(name .. " token not found in bag", playerColor)
|
||||
end
|
||||
|
||||
function _sealToken(obj)
|
||||
table.insert(sealedTokens, obj)
|
||||
local guid = obj.getGUID()
|
||||
local name = obj.getName()
|
||||
if name == "Bless" or name == "Curse" then
|
||||
local tokensTaken = manager.getVar("tokensTaken")
|
||||
table.insert(tokensTaken[name], guid)
|
||||
manager.setVar("tokensTaken", tokensTaken)
|
||||
manager.setVar("mode", name)
|
||||
printToAll("Sealing " .. name .. " token " .. manager.call("getTokenCount"))
|
||||
end
|
||||
end
|
||||
|
||||
function releaseTokens(playerColor)
|
||||
if #sealedTokens == 0 then return end
|
||||
for i,token in ipairs(sealedTokens) do
|
||||
local guid = token.getGUID()
|
||||
local name = token.getName()
|
||||
chaosbag.putObject(token)
|
||||
if name == "Bless" or name == "Curse" then
|
||||
local tokensTaken = manager.getVar("tokensTaken")
|
||||
for i,v in ipairs(tokensTaken[name]) do
|
||||
if v == guid then
|
||||
table.remove(tokensTaken[name], i)
|
||||
break
|
||||
end
|
||||
end
|
||||
manager.setVar("tokensTaken", tokensTaken)
|
||||
manager.setVar("mode", name)
|
||||
printToAll("Releasing " .. name .. " token" .. manager.call("getTokenCount"))
|
||||
end
|
||||
end
|
||||
|
||||
sealedTokens = { }
|
||||
end
|
||||
|
||||
function getChaosBag()
|
||||
local items = getObjectFromGUID("83ef06").getObjects()
|
||||
local chaosbag = nil
|
||||
for i,v in ipairs(items) do
|
||||
if v.getDescription() == "Chaos Bag" then
|
||||
chaosbag = getObjectFromGUID(v.getGUID())
|
||||
break
|
||||
end
|
||||
end
|
||||
if chaosbag == nil then printToAll("No chaos bag found") end
|
||||
return chaosbag
|
||||
end
|
||||
|
||||
function readBag()
|
||||
-- add menu items
|
||||
self.clearContextMenu()
|
||||
self.addContextMenuItem("Release Tokens", releaseTokens)
|
||||
|
||||
local bagTokens = { }
|
||||
local tokens = chaosbag.getObjects()
|
||||
for i,token in ipairs(tokens) do
|
||||
bagTokens[token.name] = true
|
||||
end
|
||||
|
||||
for url,token in pairs(IMAGE_TOKEN_MAP) do
|
||||
if bagTokens[token] then
|
||||
self.addContextMenuItem("Seal " .. token, function(playerColor) sealToken(url, playerColor) end, true)
|
||||
end
|
||||
end
|
||||
self.addContextMenuItem("Refresh Seal Options", readBag)
|
||||
end
|
||||
require("playercards/CardsThatSealTokens")
|
||||
|
Loading…
Reference in New Issue
Block a user