diff --git a/media/lua/client/TOC/Controllers/DataController.lua b/media/lua/client/TOC/Controllers/DataController.lua index 01927f0..c7155a2 100644 --- a/media/lua/client/TOC/Controllers/DataController.lua +++ b/media/lua/client/TOC/Controllers/DataController.lua @@ -28,11 +28,17 @@ function DataController:new(username, isResetForced) o.isResetForced = isResetForced o.isDataReady = false - local key = CommandsData.GetKey(username) - ModData.request(key) - + -- We're gonna set it already from here, to prevent it from looping in SP (in case we need to fetch this instance) DataController.instances[username] = o + local key = CommandsData.GetKey(username) + + if isClient() then + ModData.request(key) + else + o:initSinglePlayer(key) + end + return o end @@ -91,9 +97,10 @@ end function DataController:loadLocalData(key) self.tocData = ModData.get(key) if self.tocData ~= nil and self.tocData ~= {} then + TOC_DEBUG.printTable(self.tocData) TOC_DEBUG.print("Found and loaded local data") else - error("Local data failed to load!") + TOC_DEBUG.print("Local data failed to load!") end end ----------------- @@ -329,14 +336,16 @@ function DataController:apply() ModData.transmit(CommandsData.GetKey(self.username)) end +---Online only, Global Mod Data doesn't trigger this in SP +---@param key any +---@param data any function DataController.ReceiveData(key, data) -- During startup the game can return Bob as the player username, adding a useless ModData table if key == "TOC_Bob" then return end TOC_DEBUG.print("ReceiveData for " .. key) if data == {} or data == nil then - TOC_DEBUG.print("table is nil... returning") - return + error("Data is nil, something is very wrong") end -- Get DataController instance if there was none for that user and reapply the correct ModData table as a reference @@ -347,22 +356,20 @@ function DataController.ReceiveData(key, data) -- but Zomboid Mod Data handling is too finnicky at best to be that reliable, in case of an unwanted disconnection and what not, -- so for now, I'm gonna assume that the local data (for the local client) is the -- most recent (and correct) one instead of trying to fetch it from the server every single time - if isClient() then - if handler.isResetForced or data == nil or data == {} or data == false then - handler:setup(key) - elseif username == getPlayer():getUsername() then - handler:loadLocalData(key) - else - handler:applyOnlineData(data) - end - else + + if handler.isResetForced or data == nil or data == {} or data == false then + handler:setup(key) + elseif username == getPlayer():getUsername() then handler:loadLocalData(key) + else + handler:applyOnlineData(data) end - handler.isResetForced = false -- TODO Add a setter + + handler:setIsResetForced(false) handler:setIsDataReady(true) - -- Event + -- Event, triggers caching triggerEvent("OnReceivedTocData", handler.username) -- Transmit it back to the server @@ -373,6 +380,20 @@ end Events.OnReceiveGlobalModData.Add(DataController.ReceiveData) + +--* SP Only initialization +function DataController:initSinglePlayer(key) + self:loadLocalData(key) + if self.tocData == nil or self.isResetForced then + self:setup(key) + end + + self:setIsDataReady(true) + self:setIsResetForced(false) + + -- Event, triggers caching + triggerEvent("OnReceivedTocData", self.username) +end ------------------- ---@param username string? diff --git a/media/lua/client/TOC/Controllers/LocalPlayerController.lua b/media/lua/client/TOC/Controllers/LocalPlayerController.lua index add35da..ab8c481 100644 --- a/media/lua/client/TOC/Controllers/LocalPlayerController.lua +++ b/media/lua/client/TOC/Controllers/LocalPlayerController.lua @@ -37,7 +37,7 @@ function LocalPlayerController.InitializePlayer(isForced) if isForced then local ItemsController = require("TOC/Controllers/ItemsController") ItemsController.Player.DeleteAllOldAmputationItems(playerObj) - CachedDataHandler.Reset(username) + CachedDataHandler.Setup(username) end -- Set a bool to use an overriding GetDamagedParts diff --git a/media/lua/client/TOC/Handlers/AmputationHandler.lua b/media/lua/client/TOC/Handlers/AmputationHandler.lua index 1c3b3f5..58cf2d6 100644 --- a/media/lua/client/TOC/Handlers/AmputationHandler.lua +++ b/media/lua/client/TOC/Handlers/AmputationHandler.lua @@ -13,7 +13,6 @@ local StaticData = require("TOC/StaticData") ---@field surgeonPl IsoPlayer? local AmputationHandler = {} - ---@param limbName string ---@param surgeonPl IsoPlayer? ---@return AmputationHandler diff --git a/media/lua/client/TOC/Handlers/CachedDataHandler.lua b/media/lua/client/TOC/Handlers/CachedDataHandler.lua index de45eda..e958546 100644 --- a/media/lua/client/TOC/Handlers/CachedDataHandler.lua +++ b/media/lua/client/TOC/Handlers/CachedDataHandler.lua @@ -9,7 +9,7 @@ local CachedDataHandler = {} ---Reset everything cache related for that specific user ---@param username string -function CachedDataHandler.Reset(username) +function CachedDataHandler.Setup(username) CachedDataHandler.amputatedLimbs[username] = {} CachedDataHandler.highestAmputatedLimbs[username] = {} end @@ -41,6 +41,9 @@ function CachedDataHandler.AddAmputatedLimb(username, limbName) TOC_DEBUG.print("Added " .. limbName .. " to known amputated limbs for " .. username) -- Add it to the generic list + if CachedDataHandler.amputatedLimbs[username] == nil then + CachedDataHandler.amputatedLimbs[username] = {} + end CachedDataHandler.amputatedLimbs[username][limbName] = limbName end diff --git a/media/lua/client/TOC/Tests.lua b/media/lua/client/TOC/Tests.lua index 678eeb4..b0401cb 100644 --- a/media/lua/client/TOC/Tests.lua +++ b/media/lua/client/TOC/Tests.lua @@ -111,6 +111,37 @@ TestFramework.registerTestModule("AmputationHandler", "Top Right", function() return Tests end) +TestFramework.registerTestModule("TimedActions", "CauterizeAction", function() + local Tests = {} + local CauterizeAction = require("TOC/TimedActions/CauterizeAction") + + function Tests.CauterizeLeftHand() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "Hand_L", getPlayer())) + end + + function Tests.CauterizeLefForeArm() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "ForeArm_L", getPlayer())) + end + + function Tests.CauterizeLeftUpperArm() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "UpperArm_L", getPlayer())) + end + + function Tests.CauterizeRightHand() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "Hand_R", getPlayer())) + end + + function Tests.CauterizeRightForeArm() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "ForeArm_R", getPlayer())) + end + + function Tests.CauterizeRightUpperArm() + ISTimedActionQueue.add(CauterizeAction:new(getPlayer(), "UpperArm_R", getPlayer())) + end + + return Tests + +end) -------------------------------------------------------------------------------------- if not getActivatedMods():contains("PerfTestFramework") or not isDebugEnabled() then return end diff --git a/media/lua/client/TOC/TimedActions/CauterizeAction.lua b/media/lua/client/TOC/TimedActions/CauterizeAction.lua index 506c7bc..a27333b 100644 --- a/media/lua/client/TOC/TimedActions/CauterizeAction.lua +++ b/media/lua/client/TOC/TimedActions/CauterizeAction.lua @@ -19,14 +19,14 @@ function CauterizeAction:new(character, limbName, stoveObj) -- We need to follow ISBaseTimedAction. self.character is gonna be the surgeon o.character = character - o.ovenObj = stoveObj + o.stoveObj = stoveObj o.limbName = limbName o.stopOnWalk = true o.stopOnRun = true -- Max time depends on the strength - o.maxTime = 100 + o.maxTime = 20 if o.character:isTimedActionInstant() then o.maxTime = 1 end return o