From 6be0b197a4c96f0adf6521fa9ac8529b8d4fa5a7 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Sat, 3 Aug 2024 23:57:57 +0200 Subject: [PATCH] implemented call function --- src/core/Global.ttslua | 16 +++++++++ src/core/token/TokenManagerApi.ttslua | 48 ++++++++++++++++++--------- src/playermat/Playermat.ttslua | 4 +-- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index 18fac3d0..68d939ce 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -2383,6 +2383,22 @@ end -- Utility functions --------------------------------------------------------- +-- allows calling a function inside of a table (like the TokenManager) +function callTable(params) + local keys = params[1] or {} + local arg = params[2] or nil + local var = _G + for _, key in ipairs(keys) do + var = var[key] + if type(var) ~= "table" then break end + end + if type(var) ~= "function" then + log("resulting var was not a function " .. table.concat(keys, "->")) + return + end + return var(arg) +end + -- removes a value from a table function removeValueFromTable(t, val) for i, v in ipairs(t) do diff --git a/src/core/token/TokenManagerApi.ttslua b/src/core/token/TokenManagerApi.ttslua index c3d680e0..1b9e42d6 100644 --- a/src/core/token/TokenManagerApi.ttslua +++ b/src/core/token/TokenManagerApi.ttslua @@ -4,13 +4,19 @@ do -- Pushes new location data into the local copy of the Data Helper location data. ---@param dataTable table Key/Value pairs following the DataHelper style function TokenManagerApi.addLocationData(dataTable) - Global.call("TokenManager.addLocationData", dataTable) + Global.call("callTable", { + { "TokenManager", "addLocationData" }, + dataTable + }) end -- Pushes new player card data into the local copy of the Data Helper player data. ---@param dataTable table Key/Value pairs following the DataHelper style function TokenManagerApi.addPlayerCardData(dataTable) - Global.call("TokenManager.addPlayerCardData", dataTable) + Global.call("callTable", { + { "TokenManager", "addPlayerCardData" }, + dataTable + }) end -- Spawns tokens for the card. This function is built to just throw a card at it and let it do @@ -21,7 +27,10 @@ do ---@param extraUses table A table of = which will modify the number of tokens --- spawned for that type. e.g. Akachi's playermat should pass "Charge"=1 function TokenManagerApi.spawnForCard(card, extraUses) - Global.call("TokenManager.spawnForCard", { card = card, extraUses = extraUses }) + Global.call("callTable", { + { "TokenManager", "spawnForCard" }, + { card = card, extraUses = extraUses } + }) end -- Spawns a single token at the given global position by copying it from the template bag. @@ -31,11 +40,14 @@ do -- x and z will use the default rotation from the source bag ---@param callback? function A callback function triggered after the new token is spawned function TokenManagerApi.spawnToken(position, tokenType, rotation, callback) - Global.call("TokenManager.spawnToken", { - position = position, - tokenType = tokenType, - rotation = rotation, - callback = callback + Global.call("callTable", { + { "TokenManager", "spawnToken" }, + { + position = position, + tokenType = tokenType, + rotation = rotation, + callback = callback + } }) end @@ -47,12 +59,15 @@ do ---@param shiftDown? number An offset for the z-value of this group of tokens ---@param subType? string Subtype of token to spawn. This will only differ from the tokenName for resource tokens function TokenManagerApi.spawnTokenGroup(card, tokenType, tokenCount, shiftDown, subType) - Global.call("TokenManager.spawnTokenGroup", { - card = card, - tokenType = tokenType, - tokenCount = tokenCount, - shiftDown = shiftDown, - subType = subType + Global.call("callTable", { + { "TokenManager", "spawnTokenGroup" }, + { + card = card, + tokenType = tokenType, + tokenCount = tokenCount, + shiftDown = shiftDown, + subType = subType + } }) end @@ -60,7 +75,10 @@ do ---@param card tts__Object Card object to be replenished ---@param uses table The already decoded metadata.uses (to avoid decoding again) function TokenManagerApi.maybeReplenishCard(card, uses) - Global.call("TokenManager.maybeReplenishCard", { card = card, uses = uses }) + Global.call("callTable", { + { "TokenManager", "maybeReplenishCard" }, + { card = card, uses = uses } + }) end return TokenManagerApi diff --git a/src/playermat/Playermat.ttslua b/src/playermat/Playermat.ttslua index 3f714198..07f98703 100644 --- a/src/playermat/Playermat.ttslua +++ b/src/playermat/Playermat.ttslua @@ -1129,9 +1129,7 @@ end -- checks if tokens should be spawned for the provided card function shouldSpawnTokens(card) - if card.is_face_down then - return false - end + if card.is_face_down then return false end local localCardPos = self.positionToLocal(card.getPosition()) local metadata = JSON.decode(card.getGMNotes())