Reworked caching

This commit is contained in:
ZioPao
2023-11-13 03:35:07 +01:00
parent deb6dcc056
commit e374601402
6 changed files with 117 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
local ModDataHandler = require("TOC/Handlers/ModDataHandler") local ModDataHandler = require("TOC/Handlers/ModDataHandler")
local ItemsHandler = require("TOC/Handlers/ItemsHandler") local ItemsHandler = require("TOC/Handlers/ItemsHandler")
local PlayerHandler = require("TOC/Handlers/PlayerHandler") local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
local StaticData = require("TOC/StaticData") 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 -- TODO Send server command to manage items and spawn on another player
end end
-- Add it to the list of cut limbs on this local client -- 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 end
---Deletes the instance ---Deletes the instance
@@ -98,6 +98,7 @@ function AmputationHandler:close()
end end
--* Events *-- --* Events *--
---Updates the cicatrization process, run when a limb has been cut
function AmputationHandler.UpdateCicatrization() function AmputationHandler.UpdateCicatrization()
if ModDataHandler.GetInstance():getIsAnyLimbCut() == false then return end if ModDataHandler.GetInstance():getIsAnyLimbCut() == false then return end

View File

@@ -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

View File

@@ -176,7 +176,6 @@ function ModDataHandler:apply()
ModData.transmit(CommandsData.GetKey(self.username)) ModData.transmit(CommandsData.GetKey(self.username))
end end
function ModDataHandler.ReceiveData(key, table) function ModDataHandler.ReceiveData(key, table)
TOC_DEBUG.print("receive data for " .. key) TOC_DEBUG.print("receive data for " .. key)
if table == {} or table == nil then if table == {} or table == nil then
@@ -189,6 +188,7 @@ function ModDataHandler.ReceiveData(key, table)
end end
Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData) Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData)
-------------------
---@param username string? ---@param username string?
---@return ModDataHandler ---@return ModDataHandler

View File

@@ -1,17 +1,22 @@
local ModDataHandler = require("TOC/Handlers/ModDataHandler") local ModDataHandler = require("TOC/Handlers/ModDataHandler")
local CommonMethods = require("TOC/CommonMethods") local CommonMethods = require("TOC/CommonMethods")
local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
local StaticData = require("TOC/StaticData") 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 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 -- 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 -- 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) -- Update current player status (infection checks)
-- handle stats increase\decrease -- handle stats increase\decrease
---@class PlayerHandler ---@class PlayerHandler
---@field modDataHandler ModDataHandler
---@field playerObj IsoPlayer ---@field playerObj IsoPlayer
local PlayerHandler = {} local PlayerHandler = {}
@@ -22,14 +27,13 @@ PlayerHandler.amputatedLimbs = {}
---@param playerObj IsoPlayer ---@param playerObj IsoPlayer
---@param isForced boolean? ---@param isForced boolean?
function PlayerHandler.InitializePlayer(playerObj, isForced) function PlayerHandler.InitializePlayer(playerObj, isForced)
local username = playerObj:getUsername() local username = playerObj:getUsername()
TOC_DEBUG.print("initializing " .. username) TOC_DEBUG.print("initializing local player: " .. username)
local modDataHandler = ModDataHandler:new(username, isForced) ModDataHandler:new(username, isForced)
PlayerHandler.playerObj = playerObj PlayerHandler.playerObj = playerObj
-- Calculate amputated limbs at startup -- 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 -- Since isForced is used to reset an existing player data, we're gonna clean their ISHealthPanel table too
if isForced then if isForced then
@@ -54,34 +58,34 @@ function PlayerHandler.ManageTraits(playerObj)
end end
end end
---Cycle through all the limbs and caches the ones that the player cut off -- ---Cycle through all the limbs and caches the ones that the player cut off
---@param username string -- ---@param username string
function PlayerHandler.CacheAmputatedLimbs(username) -- function PlayerHandler.CacheAmputatedLimbs(username)
PlayerHandler.amputatedLimbs[username] = {} -- PlayerHandler.amputatedLimbs[username] = {}
local modDataHandler = ModDataHandler.GetInstance(username) -- local modDataHandler = ModDataHandler.GetInstance(username)
for i=1, #StaticData.LIMBS_STRINGS do -- for i=1, #StaticData.LIMBS_STRINGS do
local limbName = StaticData.LIMBS_STRINGS[i] -- local limbName = StaticData.LIMBS_STRINGS[i]
if modDataHandler:getIsCut(limbName) then -- if modDataHandler:getIsCut(limbName) then
PlayerHandler.AddLocalAmputatedLimb(username, limbName) -- PlayerHandler.AddLocalAmputatedLimb(username, limbName)
end -- end
end -- end
end -- end
---Cache the currently amputated limbs -- ---Cache the currently amputated limbs
---@param limbName string -- ---@param limbName string
function PlayerHandler.AddLocalAmputatedLimb(username, limbName) -- function PlayerHandler.AddLocalAmputatedLimb(username, limbName)
TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username) -- 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 -- table.insert(PlayerHandler.amputatedLimbs[username], limbName) -- TODO This should be player specific, not generic
end -- end
--* Getters *-- -- --* Getters *--
---Get a table with the strings of the cached amputated limbs -- ---Get a table with the strings of the cached amputated limbs
---@return table -- ---@return table
function PlayerHandler.GetAmputatedLimbs() -- function PlayerHandler.GetAmputatedLimbs()
return PlayerHandler.amputatedLimbs or {} -- return PlayerHandler.amputatedLimbs or {}
end -- end
--* Events *-- --* Events *--

View File

@@ -1,5 +1,5 @@
local CommonMethods = require("TOC/CommonMethods") 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") TOC_DEBUG.print("current item is a prosthesis")
local side = CommonMethods.GetSide(bodyLocation) local side = CommonMethods.GetSide(bodyLocation)
for i=1, #PlayerHandler.amputatedLimbs do local amputatedLimbs = CachedDataHandler.GetAmputatedLimbs(getPlayer():getUsername())
local limbName = PlayerHandler.amputatedLimbs[i] for i=1, #amputatedLimbs do
local limbName = amputatedLimbs[i]
if string.contains(limbName, side) and not string.contains(limbName, "UpperArm") then if string.contains(limbName, side) and not string.contains(limbName, "UpperArm") then
return true return true
end end

View File

@@ -1,7 +1,6 @@
local PlayerHandler = require("TOC/Handlers/PlayerHandler")
local StaticData = require("TOC/StaticData") local StaticData = require("TOC/StaticData")
local CommonMethods = require("TOC/CommonMethods")
local ModDataHandler = require("TOC/Handlers/ModDataHandler") local ModDataHandler = require("TOC/Handlers/ModDataHandler")
local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
---@diagnostic disable: duplicate-set-field ---@diagnostic disable: duplicate-set-field
local CutLimbHandler = require("TOC/UI/CutLimbInteractions") local CutLimbHandler = require("TOC/UI/CutLimbInteractions")
@@ -44,37 +43,6 @@ end
--* Modifications to handle visible amputation on the health menu *-- --* 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 local og_ISHealthPanel_initialise = ISHealthPanel.initialise
function ISHealthPanel:initialise() function ISHealthPanel:initialise()
if self.character:isFemale() then if self.character:isFemale() then
@@ -83,9 +51,7 @@ function ISHealthPanel:initialise()
self.sexPl = "Male" self.sexPl = "Male"
end end
self.highestAmputations = {} self.highestAmputations = CachedDataHandler.GetAmputatedLimbs(self.character:getUsername())
self:setHighestAmputation()
og_ISHealthPanel_initialise(self) og_ISHealthPanel_initialise(self)
end end
@@ -104,20 +70,16 @@ end
local og_ISHealthPanel_render = ISHealthPanel.render local og_ISHealthPanel_render = ISHealthPanel.render
function ISHealthPanel:render() function ISHealthPanel:render()
og_ISHealthPanel_render(self) og_ISHealthPanel_render(self)
if self.highestAmputations ~= nil then
--print("Rendering ISHealthPanel")
if self.highestAmputations ~= nil and self.highestAmputations[self.tocUsername] ~= nil then
-- Left Texture -- Left Texture
if self.highestAmputations[self.tocUsername]["L"] then if self.highestAmputations["L"] then
local textureL = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][self.highestAmputations[self.tocUsername]["L"]] 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) self:drawTexture(textureL, self.healthPanel.x/2 - 2, self.healthPanel.y/2, 1, 1, 0, 0)
end end
-- Right Texture -- Right Texture
if self.highestAmputations["R"] then 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) self:drawTexture(textureR, self.healthPanel.x/2 + 2, self.healthPanel.y/2, 1, 1, 0, 0)
end end
else else
@@ -166,6 +128,6 @@ function ISMedicalCheckAction:perform()
ModDataHandler.GetInstance(username) ModDataHandler.GetInstance(username)
-- We need to recalculate them here before we can create the highest amputations point -- We need to recalculate them here before we can create the highest amputations point
PlayerHandler.CacheAmputatedLimbs(username) CachedDataHandler.CalculateAmputatedLimbs(username)
og_ISMedicalCheckAction_perform(self) og_ISMedicalCheckAction_perform(self)
end end