Fixed sync with server after setup to prevent issues with medical check

This commit is contained in:
ZioPao
2024-05-05 00:14:37 +02:00
parent 20bed84910
commit aadbe02df4
3 changed files with 22 additions and 6 deletions

View File

@@ -57,6 +57,7 @@ function DataController:setup(key)
---@type tocModDataType ---@type tocModDataType
self.tocData = { self.tocData = {
-- Generic stuff that does not belong anywhere else -- Generic stuff that does not belong anywhere else
isInitializing = true,
isIgnoredPartInfected = false, isIgnoredPartInfected = false,
isAnyLimbCut = false, isAnyLimbCut = false,
limbs = {}, limbs = {},
@@ -86,9 +87,16 @@ function DataController:setup(key)
} }
end end
-- Add it to global mod data -- Add it to client global mod data
ModData.add(key, self.tocData) ModData.add(key, self.tocData)
-- Sync with the server
self:apply()
-- -- Disable lock
-- self.tocData.isInitializing = false
-- ModData.add(key, self.tocData)
end end
---In case of desync between the table on ModData and the table here ---In case of desync between the table on ModData and the table here
@@ -100,7 +108,7 @@ function DataController:applyOnlineData(tocData)
end end
---@param key string ---@param key string
function DataController:loadLocalData(key) function DataController:tryLoadLocalData(key)
self.tocData = ModData.get(key) self.tocData = ModData.get(key)
--TOC_DEBUG.printTable(self.tocData) --TOC_DEBUG.printTable(self.tocData)
@@ -389,6 +397,7 @@ function DataController.ReceiveData(key, data)
-- TODO Add update from server scenario -- TODO Add update from server scenario
if handler.isResetForced then if handler.isResetForced then
TOC_DEBUG.print("Forced reset")
handler:setup(key) handler:setup(key)
elseif data then elseif data then
if data.isUpdateFromServer then if data.isUpdateFromServer then
@@ -396,7 +405,8 @@ function DataController.ReceiveData(key, data)
end end
handler:applyOnlineData(data) handler:applyOnlineData(data)
elseif username == getPlayer():getUsername() then elseif username == getPlayer():getUsername() then
handler:loadLocalData(key) TOC_DEBUG.print("loading local data")
handler:tryLoadLocalData(key)
end end
@@ -436,7 +446,7 @@ Events.OnReceiveGlobalModData.Add(DataController.ReceiveData)
--- SP Only initialization --- SP Only initialization
---@param key string ---@param key string
function DataController:initSinglePlayer(key) function DataController:initSinglePlayer(key)
self:loadLocalData(key) self:tryLoadLocalData(key)
if self.tocData == nil or self.isResetForced then if self.tocData == nil or self.isResetForced then
self:setup(key) self:setup(key)
end end

View File

@@ -29,12 +29,18 @@ function ServerDataHandler.AddTable(key, table)
--TOC_DEBUG.printTable(table) --TOC_DEBUG.printTable(table)
-- Set that the data has been modified and it's updated on the server -- Set that the data has been modified and it's updated on the server
table.isUpdateFromServer = true table.isUpdateFromServer = true -- FIX this is useless
ModData.add(key, table) -- Add it to the server mod data ModData.add(key, table) -- Add it to the server mod data
ServerDataHandler.modData[key] = table ServerDataHandler.modData[key] = table
-- if table.isInitializing == true then
-- TOC_DEBUG.print("Applied data after setup")
-- table.isInitializing = false
-- end
-- Since this could be triggered by a different client than the one referenced in the key, we're gonna -- Since this could be triggered by a different client than the one referenced in the key, we're gonna
-- apply the changes back to the key client again to be sure that everything is in sync -- apply the changes back to the key client again to be sure that everything is in sync
local username = CommandsData.GetUsername(key) local username = CommandsData.GetUsername(key)

View File

@@ -2,7 +2,7 @@
---@alias limbsTable {Hand_L : partDataType, ForeArm_L : partDataType, UpperArm_L : partDataType, Hand_R : partDataType, ForeArm_R : partDataType, UpperArm_R : partDataType } ---@alias limbsTable {Hand_L : partDataType, ForeArm_L : partDataType, UpperArm_L : partDataType, Hand_R : partDataType, ForeArm_R : partDataType, UpperArm_R : partDataType }
---@alias prosthesisData {isProstEquipped : boolean, prostFactor : number } ---@alias prosthesisData {isProstEquipped : boolean, prostFactor : number }
---@alias prosthesesTable {Top_L : prosthesisData, Top_R : prosthesisData } -- TODO add Bottom_L and Bottom_R ---@alias prosthesesTable {Top_L : prosthesisData, Top_R : prosthesisData } -- TODO add Bottom_L and Bottom_R
---@alias tocModDataType { limbs : limbsTable, prostheses : prosthesesTable, isIgnoredPartInfected : boolean, isAnyLimbCut : boolean, isUpdateFromServer : boolean } ---@alias tocModDataType { limbs : limbsTable, prostheses : prosthesesTable, isIgnoredPartInfected : boolean, isAnyLimbCut : boolean, isUpdateFromServer : boolean, isInitializing : boolean}
--------------------------- ---------------------------