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.isResetForced = isResetForced
o.isDataReady = false o.isDataReady = false
local key = CommandsData.GetKey(username) -- We're gonna set it already from here, to prevent it from looping in SP (in case we need to fetch this instance)
ModData.request(key)
DataController.instances[username] = o DataController.instances[username] = o
local key = CommandsData.GetKey(username)
if isClient() then
ModData.request(key)
else
o:initSinglePlayer(key)
end
return o return o
end end
@@ -91,9 +97,10 @@ end
function DataController:loadLocalData(key) function DataController:loadLocalData(key)
self.tocData = ModData.get(key) self.tocData = ModData.get(key)
if self.tocData ~= nil and self.tocData ~= {} then if self.tocData ~= nil and self.tocData ~= {} then
TOC_DEBUG.printTable(self.tocData)
TOC_DEBUG.print("Found and loaded local data") TOC_DEBUG.print("Found and loaded local data")
else else
error("Local data failed to load!") TOC_DEBUG.print("Local data failed to load!")
end end
end end
----------------- -----------------
@@ -329,14 +336,16 @@ function DataController:apply()
ModData.transmit(CommandsData.GetKey(self.username)) ModData.transmit(CommandsData.GetKey(self.username))
end end
---Online only, Global Mod Data doesn't trigger this in SP
---@param key any
---@param data any
function DataController.ReceiveData(key, data) function DataController.ReceiveData(key, data)
-- During startup the game can return Bob as the player username, adding a useless ModData table -- During startup the game can return Bob as the player username, adding a useless ModData table
if key == "TOC_Bob" then return end if key == "TOC_Bob" then return end
TOC_DEBUG.print("ReceiveData for " .. key) TOC_DEBUG.print("ReceiveData for " .. key)
if data == {} or data == nil then if data == {} or data == nil then
TOC_DEBUG.print("table is nil... returning") error("Data is nil, something is very wrong")
return
end end
-- Get DataController instance if there was none for that user and reapply the correct ModData table as a reference -- 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, -- 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 -- 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 -- 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 if handler.isResetForced or data == nil or data == {} or data == false then
handler:setup(key) handler:setup(key)
elseif username == getPlayer():getUsername() then elseif username == getPlayer():getUsername() then
handler:loadLocalData(key)
else
handler:applyOnlineData(data)
end
else
handler:loadLocalData(key) handler:loadLocalData(key)
else
handler:applyOnlineData(data)
end end
handler.isResetForced = false -- TODO Add a setter
handler:setIsResetForced(false)
handler:setIsDataReady(true) handler:setIsDataReady(true)
-- Event -- Event, triggers caching
triggerEvent("OnReceivedTocData", handler.username) triggerEvent("OnReceivedTocData", handler.username)
-- Transmit it back to the server -- Transmit it back to the server
@@ -373,6 +380,20 @@ end
Events.OnReceiveGlobalModData.Add(DataController.ReceiveData) 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? ---@param username string?

View File

@@ -37,7 +37,7 @@ function LocalPlayerController.InitializePlayer(isForced)
if isForced then if isForced then
local ItemsController = require("TOC/Controllers/ItemsController") local ItemsController = require("TOC/Controllers/ItemsController")
ItemsController.Player.DeleteAllOldAmputationItems(playerObj) ItemsController.Player.DeleteAllOldAmputationItems(playerObj)
CachedDataHandler.Reset(username) CachedDataHandler.Setup(username)
end end
-- Set a bool to use an overriding GetDamagedParts -- Set a bool to use an overriding GetDamagedParts

View File

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

View File

@@ -9,7 +9,7 @@ local CachedDataHandler = {}
---Reset everything cache related for that specific user ---Reset everything cache related for that specific user
---@param username string ---@param username string
function CachedDataHandler.Reset(username) function CachedDataHandler.Setup(username)
CachedDataHandler.amputatedLimbs[username] = {} CachedDataHandler.amputatedLimbs[username] = {}
CachedDataHandler.highestAmputatedLimbs[username] = {} CachedDataHandler.highestAmputatedLimbs[username] = {}
end end
@@ -41,6 +41,9 @@ function CachedDataHandler.AddAmputatedLimb(username, limbName)
TOC_DEBUG.print("Added " .. limbName .. " to known amputated limbs for " .. username) TOC_DEBUG.print("Added " .. limbName .. " to known amputated limbs for " .. username)
-- Add it to the generic list -- Add it to the generic list
if CachedDataHandler.amputatedLimbs[username] == nil then
CachedDataHandler.amputatedLimbs[username] = {}
end
CachedDataHandler.amputatedLimbs[username][limbName] = limbName CachedDataHandler.amputatedLimbs[username][limbName] = limbName
end end

View File

@@ -111,6 +111,37 @@ TestFramework.registerTestModule("AmputationHandler", "Top Right", function()
return Tests return Tests
end) 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 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 -- We need to follow ISBaseTimedAction. self.character is gonna be the surgeon
o.character = character o.character = character
o.ovenObj = stoveObj o.stoveObj = stoveObj
o.limbName = limbName o.limbName = limbName
o.stopOnWalk = true o.stopOnWalk = true
o.stopOnRun = true o.stopOnRun = true
-- Max time depends on the strength -- Max time depends on the strength
o.maxTime = 100 o.maxTime = 20
if o.character:isTimedActionInstant() then o.maxTime = 1 end if o.character:isTimedActionInstant() then o.maxTime = 1 end
return o return o