Addressed various PR suggestions. Additionally, implemented ChaosBagApi to follow API design pattern suggested for the codebase.

This commit is contained in:
Entrox-Licher 2023-06-18 00:54:03 -04:00
parent b537b13ae6
commit 838c0be0fc
3 changed files with 276 additions and 258 deletions

View File

@ -1,302 +1,263 @@
local campaignTokenData = { local campaignTokenData = {
GUID = "51b1c9", GUID = "51b1c9",
Name = "Custom_Model", Name = "Custom_Model",
Transform = { Transform = {
posX = 2.80123329, posX = -21.25,
posY = 1.681688, posY = 1.68,
posZ = -23.6498032, posZ = 55.59,
rotX = -2.22745348E-06, rotX = 0,
rotY = 269.999664, rotY = 270,
rotZ = -1.6614282E-06, rotZ = 0,
scaleX = 2.00499868, scaleX = 2,
scaleY = 2.00499868, scaleY = 2,
scaleZ = 2.00499868 scaleZ = 2
},
Nickname = "Arkham Coin",
Description = "SCED Importer Token",
GMNotes = "",
Tags = {
"ImporterToken"
},
CustomMesh = {
MeshURL = "http://cloud-3.steamusercontent.com/ugc/943949966265929204/A38BB5D72419E6298385556D931877C0A1A55C17/",
DiffuseURL = "http://cloud-3.steamusercontent.com/ugc/254843371583188147/920981125E37B5CEB6C400E3FD353A2C428DA969/",
NormalURL = "",
ColliderURL = "http://cloud-3.steamusercontent.com/ugc/943949966265929204/A38BB5D72419E6298385556D931877C0A1A55C17/",
Convex = true,
MaterialIndex = 2,
TypeIndex = 0,
CustomShader = {
SpecularColor = {
r = 0.7222887,
g = 0.507659256,
b = 0.339915335
}, },
Nickname = "Arkham Coin", SpecularIntensity = 0.4,
Description = "SCED Importer Token", SpecularSharpness = 7.0,
GMNotes = "", FresnelStrength = 0.0
AltLookAngle = {
x = 0.0,
y = 0.0,
z = 0.0
}, },
ColorDiffuse = { CastShadows = true
r = 1.0, }
g = 1.0,
b = 1.0
},
Tags = {
"ImporterToken"
},
LayoutGroupSortIndex = 0,
Value = 0,
Locked = false,
Grid = true,
Snap = true,
IgnoreFoW = false,
MeasureMovement = false,
DragSelectable = true,
Autoraise = true,
Sticky = true,
Tooltip = true,
GridProjection = false,
HideWhenFaceDown = false,
Hands = false,
CustomMesh = {
MeshURL = "http://cloud-3.steamusercontent.com/ugc/943949966265929204/A38BB5D72419E6298385556D931877C0A1A55C17/",
DiffuseURL = "http://cloud-3.steamusercontent.com/ugc/254843371583188147/920981125E37B5CEB6C400E3FD353A2C428DA969/",
NormalURL = "",
ColliderURL = "http://cloud-3.steamusercontent.com/ugc/943949966265929204/A38BB5D72419E6298385556D931877C0A1A55C17/",
Convex = true,
MaterialIndex = 2,
TypeIndex = 0,
CustomShader = {
SpecularColor = {
r = 0.7222887,
g = 0.507659256,
b = 0.339915335
},
SpecularIntensity = 0.4,
SpecularSharpness = 7.0,
FresnelStrength = 0.0
},
CastShadows = true
},
LuaScript = "",
LuaScriptState = "",
XmlUI = ""
} }
-- colors and order for following tables
local COLORS = { "White"; "Orange"; "Green"; "Red"; "Agenda" }
-- counter GUIDS (4x damage and 4x horror) -- counter GUIDS (4x damage and 4x horror)
local DAMAGE_HORROR_GUIDS = { local DAMAGE_HORROR_GUIDS = {
"eb08d6"; "e64eec"; "1f5a0a"; "591a45"; "eb08d6"; "e64eec"; "1f5a0a"; "591a45";
"468e88"; "0257d9"; "7b5729"; "beb964"; "468e88"; "0257d9"; "7b5729"; "beb964";
} }
local campaignData = {} local tokenZone
local deckImporter
local investigatorCounter
local tokenZone = getObjectFromGUID("ef3b5f") local chaosBagApi
local deckImporter = getObjectFromGUID("a28140")
local investigatorCounter = getObjectFromGUID("f182ee")
local campaignBoxGUID = "" local campaignBoxGUID
local traumaValues = false
local deckIds = false
local campaignGuidePage = false
function onLoad(save_state) function onLoad(save_state)
self.createButton({ chaosBagApi = require("chaosbag/ChaosBagApi")
click_function = "findCampaignFromToken", campaignBoxGUID = ""
function_owner = self,
label = "Import", tokenZone = getObjectFromGUID("ef3b5f")
position = {x=-1, y=0.2, z=0}, deckImporter = getObjectFromGUID("a28140")
width = 350, investigatorCounter = getObjectFromGUID("f182ee")
height = 150, self.createButton({
scale = {2, 1, 2}, click_function = "findCampaignFromToken",
}) function_owner = self,
self.createButton({ label = "Import",
click_function = "createCampaignToken", position = {x=-1, y=0.2, z=0},
function_owner = self, font_size = 400,
label = "Export", width = 1400,
position = {x=1, y=0.2, z=0}, height = 600,
width = 350, scale = {0.5, 1, 0.5},
height = 150, })
scale = {2, 1, 2}, self.createButton({
}) click_function = "createCampaignToken",
function_owner = self,
label = "Export",
position = {x=1, y=0.2, z=0},
font_size = 400,
width = 1400,
height = 600,
scale = {0.5, 1, 0.5},
})
end end
function findCampaignFromToken(_, _, _) function findCampaignFromToken(_, _, _)
local coin = nil local coin = nil
for _, obj in ipairs(tokenZone.getObjects()) do for _, obj in ipairs(tokenZone.getObjects()) do
if obj.hasTag("ImporterToken") then if obj.hasTag("ImporterToken") then
coin = obj coin = obj
end end
end
if coin == nil then
broadcastToAll("Could not find importer token", Color.Red)
else
local importData = JSON.decode(coin.getGMNotes())
campaignBoxGUID = importData[1]
local campaignBox = getObjectFromGUID(campaignBoxGUID)
if campaignBox.type == "Generic" then
campaignBox.call("buttonClick_download")
end end
if coin == nil then Wait.condition(
broadcastToAll("Could not find importer token", Color.Red) function()
else if #campaignBox.getObjects() > 0 then
local importData = JSON.decode(coin.getGMNotes()) placeCampaignFromToken(importData)
campaignBoxGUID = importData[1] else
local campaignBox = getObjectFromGUID(campaignBoxGUID) createCampaignFromToken(importData)
if campaignBox.type == "Generic" then
campaignBox.call("buttonClick_download")
end end
Wait.condition( end,
function() function()
if #campaignBox.getObjects() > 0 then local obj = getObjectFromGUID(campaignBoxGUID)
placeCampaignFromToken(importData) if obj == nil then
else return false
createCampaignFromToken(importData) else
end return obj.type == "Bag" and obj.getLuaScript() ~= ""
end, end
function() end,
local obj = getObjectFromGUID(campaignBoxGUID) 2,
if obj == nil then function() broadcastToAll("Error loading campaign box") end
return false )
else end
return obj.type == "Bag" and obj.getLuaScript() ~= ""
end
end,
2,
function() broadcastToAll("Error loading campaign box") end
)
end
end end
function placeCampaignFromToken(importData) function placeCampaignFromToken(importData)
getObjectFromGUID(campaignBoxGUID).call("buttonClick_place") getObjectFromGUID(campaignBoxGUID).call("buttonClick_place")
Wait.condition( Wait.condition(
function() createCampaignFromToken(importData) end, function() createCampaignFromToken(importData) end,
function() return findCampaignLog() ~= nil end, function() return findCampaignLog() ~= nil end,
2, 2,
function() broadcastToAll("Error placing campaign box") end function() broadcastToAll("Error placing campaign box") end
) )
end end
function createCampaignFromToken(importData) function createCampaignFromToken(importData)
findCampaignLog().destruct() findCampaignLog().destruct()
findChaosBag().destruct() --create campaign log
--create campaign log spawnObjectData({data = importData[2]})
spawnObjectData({data = importData[2]}) --create chaos bag
--create chaos bag chaosBagApi.setChaosBagState(importData[3])
spawnObjectData({data = importData[3]}) --populate trauma values
--populate trauma values if importData[4] then
if importData[4] then updateCounters(importData[4])
updateCounters(importData[4]) end
end --populate ArkhamDB deck IDs
--populate ArkhamDB deck IDs if importData[5] then
if importData[5] then deckImporter.call("updateDeckIds", importData[5])
deckImporter.call("updateDeckIds", importData[5]) end
end --set investigator count
--set investigator count investigatorCounter.call("updateVal", importData[6])
investigatorCounter.call("updateVal", importData[6]) --set campaign guide page
--set campaign guide page if findCampaignGuide() then
if findCampaignGuide() then Wait.condition(
Wait.condition( function()
function() --log("Campaign Guide import successful!")
--log("Campaign Guide import successful!") end,
end, function()
function() findCampaignGuide().Book.setPage(importData[7])
findCampaignGuide().Book.setPage(importData[7]) return findCampaignGuide().Book.getPage() == importData[7]
return findCampaignGuide().Book.getPage() == importData[7] end,
end, 1,
1, function()
function() --log("Campaign Guide import failed!")
--log("Campaign Guide import failed!") end
end )
) end
end Global.call("loadSettings", importData[8])
Global.call("loadSettings", importData[8]) broadcastToAll("Campaign successfully imported!", Color.Green)
broadcastToAll("Campaign successfully imported!", Color.Green)
end end
function createCampaignToken(_, _, _) function createCampaignToken(_, _, _)
local campaignBoxGUID = "" local campaignBoxGUID = ""
-- find active campaign -- find active campaign
for _, obj in ipairs(getObjectsWithTag("CampaignBox")) do for _, obj in ipairs(getObjectsWithTag("CampaignBox")) do
if obj.type == "Bag" and #obj.getObjects() == 0 then if obj.type == "Bag" and #obj.getObjects() == 0 then
campaignBoxGUID = obj.getGUID() campaignBoxGUID = obj.getGUID()
end
end end
if campaignBoxGUID == "" then end
broadcastToAll("Campaign box with all placed objects not found!", Color.Red) if campaignBoxGUID == "" then
return broadcastToAll("Campaign box with all placed objects not found!", Color.Red)
return
end
local campaignLog = findCampaignLog()
if campaignLog == nil then
broadcastToAll("Campaign log not found!", Color.Red)
return
end
local counterData = campaignLog.getVar("ref_buttonData")
if counterData ~= nil then
local traumaValues = {}
printToAll("Trauma values found in campaign log!", "Green")
for i = 1, 10, 3 do
traumaValues[1 + (i - 1) / 3] = counterData.counter[i].value
traumaValues[5 + (i - 1) / 3] = counterData.counter[i + 1].value
end end
local campaignLog = findCampaignLog() else
if campaignLog == nil then printToAll("Trauma values could not be found in campaign log!", "Yellow")
broadcastToAll("Campaign log not found!", Color.Red) printToAll("Default values for health and sanity loaded.", "Yellow")
return end
end local deckIds = {deckImporter.getInputs()[1].value, deckImporter.getInputs()[2].value, deckImporter.getInputs()[3].value, deckImporter.getInputs()[4].value}
local chaosBag = findChaosBag() local campaignGuide = findCampaignGuide()
if chaosBag == nil then if campaignGuide == nil then
broadcastToAll("Chaos bag not found in standard position!", Color.Red) broadcastToAll("Campaign guide not found!", Color.Red)
return return
end end
local counterData = campaignLog.getVar("ref_buttonData") local campaignGuidePage = campaignGuide.Book.getPage()
if counterData ~= nil then
traumaValues = {}
printToAll("Trauma values found in campaign log!", "Green")
for i = 1, 10, 3 do
traumaValues[1 + (i - 1) / 3] = counterData.counter[i].value
traumaValues[5 + (i - 1) / 3] = counterData.counter[i + 1].value
end
else
printToAll("Trauma values could not be found in campaign log!", "Yellow")
printToAll("Default values for health and sanity loaded.", "Yellow")
end
deckIds = {deckImporter.getInputs()[1].value, deckImporter.getInputs()[2].value, deckImporter.getInputs()[3].value, deckImporter.getInputs()[4].value}
local campaignGuide = findCampaignGuide()
if campaignGuide == nil then
broadcastToAll("Campaign loguideg not found!", Color.Red)
return
end
local campaignGuidePage = campaignGuide.Book.getPage()
campaignData = { local campaignData = {
campaignBoxGUID, campaignBoxGUID,
campaignLog.getData(), campaignLog.getData(),
chaosBag.getData(), chaosBagApi.getChaosBagState(),
traumaValues, traumaValues,
deckIds, deckIds,
investigatorCounter.getVar("val"), require("core/PlayAreaApi").getInvestigatorCount(),
campaignGuidePage, campaignGuidePage,
Global.getTable("optionPanel") Global.getTable("optionPanel")
} }
campaignTokenData.GMNotes = JSON.encode(campaignData) campaignTokenData.GMNotes = JSON.encode(campaignData)
campaignTokenData.Nickname = getObjectFromGUID(campaignBoxGUID).getName() .. os.date(" Save: %b %d") campaignTokenData.Nickname = os.date(" Save: %b %d") .. getObjectFromGUID(campaignBoxGUID).getName()
spawnObjectData({ spawnObjectData({
data = campaignTokenData, data = campaignTokenData,
position = {-21.25, 1.68, 55.59} position = {-21.25, 1.68, 55.59}
}) })
broadcastToAll("Campaign successfully exported! Save coin object to import on a fresh save", Color.Green) broadcastToAll("Campaign successfully exported! Save coin object to import on a fresh save", Color.Green)
end end
-- helper functions -- helper functions
function findCampaignLog() function findCampaignLog()
if getObjectsWithTag("CampaignLog") then if getObjectsWithTag("CampaignLog") then
return getObjectsWithTag("CampaignLog")[1] return getObjectsWithTag("CampaignLog")[1]
else else
return nil return nil
end end
end end
function findCampaignGuide() function findCampaignGuide()
if getObjectsWithTag("CampaignGuide") then if getObjectsWithTag("CampaignGuide") then
return getObjectsWithTag("CampaignGuide")[1] return getObjectsWithTag("CampaignGuide")[1]
else else
return nil return nil
end end
end
-- checks scripting zone for chaos bag
function findChaosBag()
for _, item in ipairs(getObjectFromGUID("83ef06").getObjects()) do
if item.getDescription() == "Chaos Bag" then
return item
end
end
end end
function updateCounters(tableOfNewValues) function updateCounters(tableOfNewValues)
if tonumber(tableOfNewValues) then if tonumber(tableOfNewValues) then
local value = tableOfNewValues local value = tableOfNewValues
tableOfNewValues = {} tableOfNewValues = {}
for i = 1, #DAMAGE_HORROR_GUIDS do for i = 1, #DAMAGE_HORROR_GUIDS do
table.insert(tableOfNewValues, value) table.insert(tableOfNewValues, value)
end
end
for i, guid in ipairs(DAMAGE_HORROR_GUIDS) do
local TOKEN = getObjectFromGUID(guid)
if TOKEN ~= nil then
TOKEN.call("updateVal", tableOfNewValues[i])
else
printToAll(": No. " .. i .. " could not be found.", "Yellow")
end
end end
end end
for i, guid in ipairs(DAMAGE_HORROR_GUIDS) do
local TOKEN = getObjectFromGUID(guid)
if TOKEN ~= nil then
TOKEN.call("updateVal", tableOfNewValues[i])
else
printToAll(": No. " .. i .. " could not be found.", "Yellow")
end
end
end

View File

@ -0,0 +1,27 @@
do
local ChaosBagApi = {}
-- respawns the chaos bag with a new state of tokens
---@param tokenList Table List of chaos token ids
ChaosBagApi.setChaosBagState = function(tokenList)
return Global.call("setChaosBagState", tokenList)
end
-- returns a Table List of chaos token ids in the current chaos bag
-- requires copying the data into a new table because TTS is weird about handling table return values in Global
ChaosBagApi.getChaosBagState = function()
local chaosBagContentsCatcher = Global.call("getChaosBagState")
local chaosBagContents = {}
for _, v in ipairs(chaosBagContentsCatcher) do
table.insert(chaosBagContents, v)
end
return chaosBagContents
end
-- checks scripting zone for chaos bag (also called by a lot of objects!)
ChaosBagApi.findChaosBag = function()
return Global.call("findChaosBag")
end
return ChaosBagApi
end

View File

@ -537,8 +537,38 @@ function getDataValue(storage, key)
end end
end end
function createChaosTokenNameLookupTable()
local namesToIds = {}
for k, v in pairs(ID_URL_MAP) do
namesToIds[v.name] = k
end
return namesToIds
end
-- returns a Table List of chaos token ids in the current chaos bag
---@api chaosbag/ChaosBagApi
function getChaosBagState()
local tokens = {}
local invertedTable = createChaosTokenNameLookupTable()
log(invertedTable)
local chaosbag = findChaosBag()
for _, v in ipairs(chaosbag.getObjects()) do
local id = invertedTable[v.name]
if id then
table.insert(tokens, id)
else
printToAll(v.name .. " token not recognized. Will not be recorded.", "Yellow")
end
end
return tokens
end
-- respawns the chaos bag with a new state of tokens -- respawns the chaos bag with a new state of tokens
---@param tokenList Table List of chaos token ids ---@param tokenList Table List of chaos token ids
---@api chaosbag/ChaosBagApi
function setChaosBagState(tokenList) function setChaosBagState(tokenList)
if not canTouchChaosTokens() then return end if not canTouchChaosTokens() then return end