From e374601402900ea4d7e3516a3aaec35371268998 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Mon, 13 Nov 2023 03:35:07 +0100 Subject: [PATCH] Reworked caching --- .../client/TOC/Handlers/AmputationHandler.lua | 5 +- .../client/TOC/Handlers/CachedDataHandler.lua | 69 +++++++++++++++++++ .../client/TOC/Handlers/ModDataHandler.lua | 2 +- .../lua/client/TOC/Handlers/PlayerHandler.lua | 62 +++++++++-------- .../client/TOC/Handlers/ProsthesisHandler.lua | 7 +- media/lua/client/TOC/UI/HealthPanel.lua | 52 ++------------ 6 files changed, 117 insertions(+), 80 deletions(-) create mode 100644 media/lua/client/TOC/Handlers/CachedDataHandler.lua diff --git a/media/lua/client/TOC/Handlers/AmputationHandler.lua b/media/lua/client/TOC/Handlers/AmputationHandler.lua index cee9d68..b9c1c22 100644 --- a/media/lua/client/TOC/Handlers/AmputationHandler.lua +++ b/media/lua/client/TOC/Handlers/AmputationHandler.lua @@ -1,6 +1,6 @@ local ModDataHandler = require("TOC/Handlers/ModDataHandler") local ItemsHandler = require("TOC/Handlers/ItemsHandler") -local PlayerHandler = require("TOC/Handlers/PlayerHandler") +local CachedDataHandler = require("TOC/Handlers/CachedDataHandler") local StaticData = require("TOC/StaticData") --------------------------- @@ -89,7 +89,7 @@ function AmputationHandler:execute(damagePlayer) -- TODO Send server command to manage items and spawn on another player end -- Add it to the list of cut limbs on this local client - PlayerHandler.AddLocalAmputatedLimb(self.patientPl:getUsername(), self.limbName) + CachedDataHandler.AddAmputatedLimb(self.patientPl:getUsername(), self.limbName) end ---Deletes the instance @@ -98,6 +98,7 @@ function AmputationHandler:close() end --* Events *-- +---Updates the cicatrization process, run when a limb has been cut function AmputationHandler.UpdateCicatrization() if ModDataHandler.GetInstance():getIsAnyLimbCut() == false then return end diff --git a/media/lua/client/TOC/Handlers/CachedDataHandler.lua b/media/lua/client/TOC/Handlers/CachedDataHandler.lua new file mode 100644 index 0000000..51dc69f --- /dev/null +++ b/media/lua/client/TOC/Handlers/CachedDataHandler.lua @@ -0,0 +1,69 @@ +local StaticData = require("TOC/StaticData") +local ModDataHandler = require("TOC/Handlers/ModDataHandler") +local CommonMethods = require("TOC/CommonMethods") + +---@class CachedDataHandler +local CachedDataHandler = {} + + + +--* Amputated Limbs caching *-- +CachedDataHandler.amputatedLimbs = {} + +function CachedDataHandler.CalculateAmputatedLimbs(username) + local modDataHandler = ModDataHandler.GetInstance(username) + for i=1, #StaticData.LIMBS_STRINGS do + local limbName = StaticData.LIMBS_STRINGS[i] + if modDataHandler:getIsCut(limbName) then + CachedDataHandler.AddAmputatedLimb(username, limbName) + end + end +end + +function CachedDataHandler.AddAmputatedLimb(username, limbName) + TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username) + table.insert(CachedDataHandler.amputatedLimbs[username], limbName) +end + +function CachedDataHandler.GetAmputatedLimbs(username) + return CachedDataHandler.amputatedLimbs[username] +end + + +--* Highest amputated limb per side caching *-- +CachedDataHandler.highestAmputatedLimbs = {} + + +function CachedDataHandler.CalculateHighestAmputatedLimbs(username) + if CachedDataHandler.amputatedLimbs == nil or CachedDataHandler.amputatedLimbs[username] == nil then + TOC_DEBUG.print("Amputated limbs weren't calculated. Trying to calculate them now for " .. username) + CachedDataHandler.CalculateAmputatedLimbs(username) + return + end + + local amputatedLimbs = CachedDataHandler.amputatedLimbs[username] + CachedDataHandler.highestAmputatedLimbs[username] = {} + TOC_DEBUG.print("Searching highest amputations for " .. username) + local modDataHandler = ModDataHandler.GetInstance(username) + if modDataHandler == nil then + TOC_DEBUG.print("ModDataHandler not found for " .. username) + return + end + + for i=1, #amputatedLimbs do + local limbName = amputatedLimbs[i] + local index = CommonMethods.GetSide(limbName) + if modDataHandler:getIsCut(limbName) and modDataHandler:getIsVisible(limbName) then + TOC_DEBUG.print("found high amputation " .. limbName) + CachedDataHandler.highestAmputatedLimbs[username][index] = limbName + end + end +end + +function CachedDataHandler.GetHighestAmputatedLimbs(username) + return CachedDataHandler.highestAmputatedLimbs[username] +end + + + +return CachedDataHandler \ No newline at end of file diff --git a/media/lua/client/TOC/Handlers/ModDataHandler.lua b/media/lua/client/TOC/Handlers/ModDataHandler.lua index efc038e..ff10549 100644 --- a/media/lua/client/TOC/Handlers/ModDataHandler.lua +++ b/media/lua/client/TOC/Handlers/ModDataHandler.lua @@ -176,7 +176,6 @@ function ModDataHandler:apply() ModData.transmit(CommandsData.GetKey(self.username)) end - function ModDataHandler.ReceiveData(key, table) TOC_DEBUG.print("receive data for " .. key) if table == {} or table == nil then @@ -189,6 +188,7 @@ function ModDataHandler.ReceiveData(key, table) end Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData) +------------------- ---@param username string? ---@return ModDataHandler diff --git a/media/lua/client/TOC/Handlers/PlayerHandler.lua b/media/lua/client/TOC/Handlers/PlayerHandler.lua index 1ed55bd..b398f7a 100644 --- a/media/lua/client/TOC/Handlers/PlayerHandler.lua +++ b/media/lua/client/TOC/Handlers/PlayerHandler.lua @@ -1,17 +1,22 @@ local ModDataHandler = require("TOC/Handlers/ModDataHandler") local CommonMethods = require("TOC/CommonMethods") +local CachedDataHandler = require("TOC/Handlers/CachedDataHandler") + local StaticData = require("TOC/StaticData") ----------- -- TODO We should instantiate this anyway if we want to keep track of cut limbs here. Doing so, we would be able to handle other players too + +-- TODO THIS SHOULD BE LOCAL ONLY! WE'RE MANAGING EVENTS AND INITIALIZATION STUFF! MOVE ONLINE STUFF AWAY! + + -- LIST OF STUFF THAT THIS CLASS NEEDS TO DO -- Keep track of cut limbs so that we don't have to loop through all of them all the time -- Update current player status (infection checks) -- handle stats increase\decrease ---@class PlayerHandler ----@field modDataHandler ModDataHandler ---@field playerObj IsoPlayer local PlayerHandler = {} @@ -22,14 +27,13 @@ PlayerHandler.amputatedLimbs = {} ---@param playerObj IsoPlayer ---@param isForced boolean? function PlayerHandler.InitializePlayer(playerObj, isForced) - local username = playerObj:getUsername() - TOC_DEBUG.print("initializing " .. username) - local modDataHandler = ModDataHandler:new(username, isForced) + TOC_DEBUG.print("initializing local player: " .. username) + ModDataHandler:new(username, isForced) PlayerHandler.playerObj = playerObj -- Calculate amputated limbs at startup - PlayerHandler.CacheAmputatedLimbs(username) + CachedDataHandler.CalculateAmputatedLimbs(username) -- Since isForced is used to reset an existing player data, we're gonna clean their ISHealthPanel table too if isForced then @@ -54,34 +58,34 @@ function PlayerHandler.ManageTraits(playerObj) end end ----Cycle through all the limbs and caches the ones that the player cut off ----@param username string -function PlayerHandler.CacheAmputatedLimbs(username) - PlayerHandler.amputatedLimbs[username] = {} - local modDataHandler = ModDataHandler.GetInstance(username) - for i=1, #StaticData.LIMBS_STRINGS do - local limbName = StaticData.LIMBS_STRINGS[i] - if modDataHandler:getIsCut(limbName) then - PlayerHandler.AddLocalAmputatedLimb(username, limbName) - end - end -end +-- ---Cycle through all the limbs and caches the ones that the player cut off +-- ---@param username string +-- function PlayerHandler.CacheAmputatedLimbs(username) +-- PlayerHandler.amputatedLimbs[username] = {} +-- local modDataHandler = ModDataHandler.GetInstance(username) +-- for i=1, #StaticData.LIMBS_STRINGS do +-- local limbName = StaticData.LIMBS_STRINGS[i] +-- if modDataHandler:getIsCut(limbName) then +-- PlayerHandler.AddLocalAmputatedLimb(username, limbName) +-- end +-- end +-- end ----Cache the currently amputated limbs ----@param limbName string -function PlayerHandler.AddLocalAmputatedLimb(username, limbName) - TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username) - table.insert(PlayerHandler.amputatedLimbs[username], limbName) -- TODO This should be player specific, not generic -end +-- ---Cache the currently amputated limbs +-- ---@param limbName string +-- function PlayerHandler.AddLocalAmputatedLimb(username, limbName) +-- TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username) +-- table.insert(PlayerHandler.amputatedLimbs[username], limbName) -- TODO This should be player specific, not generic +-- end ---* Getters *-- +-- --* Getters *-- ----Get a table with the strings of the cached amputated limbs ----@return table -function PlayerHandler.GetAmputatedLimbs() - return PlayerHandler.amputatedLimbs or {} -end +-- ---Get a table with the strings of the cached amputated limbs +-- ---@return table +-- function PlayerHandler.GetAmputatedLimbs() +-- return PlayerHandler.amputatedLimbs or {} +-- end --* Events *-- diff --git a/media/lua/client/TOC/Handlers/ProsthesisHandler.lua b/media/lua/client/TOC/Handlers/ProsthesisHandler.lua index f169a7f..3d72860 100644 --- a/media/lua/client/TOC/Handlers/ProsthesisHandler.lua +++ b/media/lua/client/TOC/Handlers/ProsthesisHandler.lua @@ -1,5 +1,5 @@ local CommonMethods = require("TOC/CommonMethods") -local PlayerHandler = require("TOC/Handlers/PlayerHandler") +local CachedDataHandler = require("TOC/Handlers/CachedDataHandler") ------------------------- @@ -19,8 +19,9 @@ function ProsthesisHandler.CheckIfEquippable(bodyLocation) TOC_DEBUG.print("current item is a prosthesis") local side = CommonMethods.GetSide(bodyLocation) - for i=1, #PlayerHandler.amputatedLimbs do - local limbName = PlayerHandler.amputatedLimbs[i] + local amputatedLimbs = CachedDataHandler.GetAmputatedLimbs(getPlayer():getUsername()) + for i=1, #amputatedLimbs do + local limbName = amputatedLimbs[i] if string.contains(limbName, side) and not string.contains(limbName, "UpperArm") then return true end diff --git a/media/lua/client/TOC/UI/HealthPanel.lua b/media/lua/client/TOC/UI/HealthPanel.lua index ee3584c..5340a9a 100644 --- a/media/lua/client/TOC/UI/HealthPanel.lua +++ b/media/lua/client/TOC/UI/HealthPanel.lua @@ -1,7 +1,6 @@ -local PlayerHandler = require("TOC/Handlers/PlayerHandler") local StaticData = require("TOC/StaticData") -local CommonMethods = require("TOC/CommonMethods") local ModDataHandler = require("TOC/Handlers/ModDataHandler") +local CachedDataHandler = require("TOC/Handlers/CachedDataHandler") ---@diagnostic disable: duplicate-set-field local CutLimbHandler = require("TOC/UI/CutLimbInteractions") @@ -44,37 +43,6 @@ end --* Modifications to handle visible amputation on the health menu *-- -function ISHealthPanel:setHighestAmputation() - - --TOC_DEBUG.print("setHighestAmputation for " .. self.tocUsername) - - -- character is always the patient. - self.tocUsername = self.character:getUsername() - - if PlayerHandler.amputatedLimbs == nil or PlayerHandler.amputatedLimbs[self.tocUsername] == nil then - TOC_DEBUG.print("PlayerHandler.amputatedLimbs is still nil or wasn't initialized for " .. self.tocUsername) - return - end - local amputatedLimbs = PlayerHandler.amputatedLimbs[self.tocUsername] - - self.highestAmputations[self.tocUsername] = {} - TOC_DEBUG.print("Searching highest amputations for " .. self.tocUsername) - local modDataHandler = ModDataHandler.GetInstance(self.tocUsername) - if modDataHandler == nil then - TOC_DEBUG.print("ModDataHandler not found for " .. self.tocUsername) - return - end - - for i=1, #amputatedLimbs do - local limbName = amputatedLimbs[i] - local index = CommonMethods.GetSide(limbName) - if modDataHandler:getIsCut(limbName) and modDataHandler:getIsVisible(limbName) then - TOC_DEBUG.print("found high amputation " .. limbName) - self.highestAmputations[self.tocUsername][index] = limbName - end - end -end - local og_ISHealthPanel_initialise = ISHealthPanel.initialise function ISHealthPanel:initialise() if self.character:isFemale() then @@ -83,9 +51,7 @@ function ISHealthPanel:initialise() self.sexPl = "Male" end - self.highestAmputations = {} - self:setHighestAmputation() - + self.highestAmputations = CachedDataHandler.GetAmputatedLimbs(self.character:getUsername()) og_ISHealthPanel_initialise(self) end @@ -104,20 +70,16 @@ end local og_ISHealthPanel_render = ISHealthPanel.render function ISHealthPanel:render() og_ISHealthPanel_render(self) - - --print("Rendering ISHealthPanel") - - - if self.highestAmputations ~= nil and self.highestAmputations[self.tocUsername] ~= nil then + if self.highestAmputations ~= nil then -- Left Texture - if self.highestAmputations[self.tocUsername]["L"] then - local textureL = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][self.highestAmputations[self.tocUsername]["L"]] + if self.highestAmputations["L"] then + local textureL = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][self.highestAmputations["L"]] self:drawTexture(textureL, self.healthPanel.x/2 - 2, self.healthPanel.y/2, 1, 1, 0, 0) end -- Right Texture if self.highestAmputations["R"] then - local textureR = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][self.highestAmputations[self.tocUsername]["R"]] + local textureR = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][self.highestAmputations["R"]] self:drawTexture(textureR, self.healthPanel.x/2 + 2, self.healthPanel.y/2, 1, 1, 0, 0) end else @@ -166,6 +128,6 @@ function ISMedicalCheckAction:perform() ModDataHandler.GetInstance(username) -- We need to recalculate them here before we can create the highest amputations point - PlayerHandler.CacheAmputatedLimbs(username) + CachedDataHandler.CalculateAmputatedLimbs(username) og_ISMedicalCheckAction_perform(self) end \ No newline at end of file