Fixed SP, added some tests

This commit is contained in:
ZioPao
2024-01-10 23:43:36 +01:00
parent 02c992b12a
commit f984a834fc
6 changed files with 76 additions and 22 deletions

View File

@@ -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,7 +356,7 @@ 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
@@ -355,14 +364,12 @@ function DataController.ReceiveData(key, data)
else
handler:applyOnlineData(data)
end
else
handler:loadLocalData(key)
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?

View File

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

View File

@@ -13,7 +13,6 @@ local StaticData = require("TOC/StaticData")
---@field surgeonPl IsoPlayer?
local AmputationHandler = {}
---@param limbName string
---@param surgeonPl IsoPlayer?
---@return AmputationHandler

View File

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

View File

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

View File

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