Init for SP and MP working, may rework it though

This commit is contained in:
ZioPao
2024-01-11 01:27:19 +01:00
parent f984a834fc
commit 1028cef704
3 changed files with 48 additions and 21 deletions

View File

@@ -96,11 +96,14 @@ end
---@param key string ---@param key string
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
TOC_DEBUG.printTable(self.tocData) TOC_DEBUG.printTable(self.tocData)
if self.tocData and self.tocData.limbs then
TOC_DEBUG.print("Found and loaded local data") TOC_DEBUG.print("Found and loaded local data")
else else
TOC_DEBUG.print("Local data failed to load!") TOC_DEBUG.print("Local data failed to load! Running setup")
self:setup(key)
end end
end end
----------------- -----------------
@@ -196,13 +199,14 @@ end
---Set a generic boolean that toggles varies function of the mod ---Set a generic boolean that toggles varies function of the mod
---@return boolean ---@return boolean
function DataController:getIsAnyLimbCut() function DataController:getIsAnyLimbCut()
--if self.isDataReady == false then return false end if not self.isDataReady then return false end
return self.tocData.isAnyLimbCut return self.tocData.isAnyLimbCut
end end
---Get isIgnoredPartInfected ---Get isIgnoredPartInfected
---@return boolean ---@return boolean
function DataController:getIsIgnoredPartInfected() function DataController:getIsIgnoredPartInfected()
if not self.isDataReady then return false end
return self.tocData.isIgnoredPartInfected return self.tocData.isIgnoredPartInfected
end end
@@ -211,7 +215,6 @@ end
---@return boolean ---@return boolean
function DataController:getIsCut(limbName) function DataController:getIsCut(limbName)
if not self.isDataReady then return false end if not self.isDataReady then return false end
if self.tocData.limbs[limbName] then if self.tocData.limbs[limbName] then
return self.tocData.limbs[limbName].isCut return self.tocData.limbs[limbName].isCut
else else
@@ -224,7 +227,6 @@ end
---@return boolean ---@return boolean
function DataController:getIsVisible(limbName) function DataController:getIsVisible(limbName)
if not self.isDataReady then return false end if not self.isDataReady then return false end
return self.tocData.limbs[limbName].isVisible return self.tocData.limbs[limbName].isVisible
end end
@@ -232,6 +234,7 @@ end
---@param limbName string ---@param limbName string
---@return boolean ---@return boolean
function DataController:getIsCicatrized(limbName) function DataController:getIsCicatrized(limbName)
if not self.isDataReady then return false end
return self.tocData.limbs[limbName].isCicatrized return self.tocData.limbs[limbName].isCicatrized
end end
@@ -239,6 +242,7 @@ end
---@param limbName string ---@param limbName string
---@return boolean ---@return boolean
function DataController:getIsCauterized(limbName) function DataController:getIsCauterized(limbName)
if not self.isDataReady then return false end
return self.tocData.limbs[limbName].isCauterized return self.tocData.limbs[limbName].isCauterized
end end
@@ -246,6 +250,7 @@ end
---@param limbName string ---@param limbName string
---@return number ---@return number
function DataController:getWoundDirtyness(limbName) function DataController:getWoundDirtyness(limbName)
if not self.isDataReady then return -1 end
return self.tocData.limbs[limbName].woundDirtyness return self.tocData.limbs[limbName].woundDirtyness
end end
@@ -254,6 +259,7 @@ end
---@param limbName string ---@param limbName string
---@return number ---@return number
function DataController:getCicatrizationTime(limbName) function DataController:getCicatrizationTime(limbName)
if not self.isDataReady then return -1 end
return self.tocData.limbs[limbName].cicatrizationTime return self.tocData.limbs[limbName].cicatrizationTime
end end
@@ -345,7 +351,7 @@ function DataController.ReceiveData(key, data)
TOC_DEBUG.print("ReceiveData for " .. key) TOC_DEBUG.print("ReceiveData for " .. key)
if data == {} or data == nil then if data == {} or data == nil then
error("Data is nil, something is very wrong") error("Data is nil, new character or something is wrong")
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
@@ -357,10 +363,11 @@ function DataController.ReceiveData(key, data)
-- 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 handler.isResetForced or data == nil or data == {} or data == false then if username == getPlayer():getUsername() and not handler.isResetForced then
handler:setup(key)
elseif username == getPlayer():getUsername() then
handler:loadLocalData(key) handler:loadLocalData(key)
elseif handler.isResetForced or data == nil or #data == 0 then
TOC_DEBUG.print("Data is nil or empty!")
handler:setup(key)
else else
handler:applyOnlineData(data) handler:applyOnlineData(data)
end end

View File

@@ -38,9 +38,6 @@ function Main.Start()
TOC_DEBUG.print("running Start method") TOC_DEBUG.print("running Start method")
Main.SetupTraits() Main.SetupTraits()
Main.SetupEvents() Main.SetupEvents()
-- Starts initialization for local client
Events.OnGameStart.Add(Main.Initialize)
end end
function Main.SetupEvents() function Main.SetupEvents()
@@ -48,7 +45,7 @@ function Main.SetupEvents()
Events.OnReceivedTocData.Add(CachedDataHandler.CalculateHighestAmputatedLimbs) Events.OnReceivedTocData.Add(CachedDataHandler.CalculateHighestAmputatedLimbs)
end end
function Main.Initialize() function Main.InitializePlayer()
---Looop until we've successfully initialized the mod ---Looop until we've successfully initialized the mod
local function TryToInitialize() local function TryToInitialize()
local pl = getPlayer() local pl = getPlayer()
@@ -66,16 +63,30 @@ end
---Clean the TOC table for that SP player, to prevent from clogging it up ---Clean the TOC table for that SP player, to prevent from clogging it up
---@param player IsoPlayer ---@param player IsoPlayer
function Main.WipeSinglePlayerData(player) function Main.WipeData(player)
TOC_DEBUG.print("Wiping data after death")
local key = CommandsData.GetKey(player:getUsername()) local key = CommandsData.GetKey(player:getUsername())
ModData.remove(key)
ModData.transmit(key) --ModData.remove(key)
if not isClient() then
-- For SP, it's enough just removing the data this way
ModData.remove(key)
else
-- Different story for MP, we're gonna 'force' it to reload it
-- at the next character by passing an empty mod data
ModData.add(key, {})
ModData.transmit(key)
end
end end
--* Events *-- --* Events *--
Events.OnGameBoot.Add(Main.Start) -- TODO Does this apply after death? Events.OnGameStart.Add(Main.Start)
Events.OnCreatePlayer.Add(Main.InitializePlayer)
Events.OnPlayerDeath.Add(Main.WipeData)
-- TODO Disable windows interaction when no hands
if not isClient() and not isServer() then
Events.OnPlayerDeath.Add(Main.WipeSinglePlayerData)
end

View File

@@ -143,6 +143,15 @@ TestFramework.registerTestModule("TimedActions", "CauterizeAction", function()
end) end)
TestFramework.registerTestModule("Various", "Player", function()
local Tests = {}
function Tests.Kill()
getPlayer():Kill(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
local PerfTest = require("PerfTest/main") -- SHould be global anyway local PerfTest = require("PerfTest/main") -- SHould be global anyway