From d73b1a3f4348486ff346b292a0d7148203566185 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Fri, 29 Mar 2024 18:46:44 +0100 Subject: [PATCH] Too much stuff to fix. --- media/lua/client/TOC/ClientRelayCommands.lua | 8 +++ .../client/TOC/Controllers/DataController.lua | 56 ++++++++++++++----- .../client/TOC/Handlers/CachedDataHandler.lua | 6 +- .../TOC/TimedActions/CleanWoundAction.lua | 9 ++- .../client/TOC/TimedActions/CutLimbAction.lua | 7 ++- media/lua/client/TOC/UI/HealthPanel.lua | 6 +- .../CutLimbInteractionHandler.lua | 6 +- .../Interactions/HealthPanelBaseHandler.lua | 2 + .../WoundCleaningInteractionHandler.lua | 1 + media/lua/server/TOC/ServerDataHandler.lua | 33 ++++++++++- media/lua/server/TOC/ServerRelayCommands.lua | 1 - media/lua/shared/TOC/CommandsData.lua | 9 ++- media/lua/shared/TOC/StaticData.lua | 4 +- 13 files changed, 120 insertions(+), 28 deletions(-) diff --git a/media/lua/client/TOC/ClientRelayCommands.lua b/media/lua/client/TOC/ClientRelayCommands.lua index 327f60a..35b2f49 100644 --- a/media/lua/client/TOC/ClientRelayCommands.lua +++ b/media/lua/client/TOC/ClientRelayCommands.lua @@ -30,6 +30,14 @@ function ClientRelayCommands.ReceiveExecuteAmputationAction(args) end +--* APPLY RELAY *-- + +function ClientRelayCommands.ReceiveApplyFromServer() + TOC_DEBUG.print("Received forced re-apply from server") + local key = CommandsData.GetKey(getPlayer():getUsername()) + ModData.request(key) +end + --* TRIGGERED BY ADMINS *-- diff --git a/media/lua/client/TOC/Controllers/DataController.lua b/media/lua/client/TOC/Controllers/DataController.lua index db19caf..0625d5d 100644 --- a/media/lua/client/TOC/Controllers/DataController.lua +++ b/media/lua/client/TOC/Controllers/DataController.lua @@ -78,8 +78,8 @@ function DataController:setup(key) end -- Initialize prostheses stuff - for i=1, #StaticData.PROSTHESES_GROUPS_STR do - local group = StaticData.PROSTHESES_GROUPS_STR[i] + for i=1, #StaticData.AMP_GROUPS_STR do + local group = StaticData.AMP_GROUPS_STR[i] self.tocData.prostheses[group] = { isProstEquipped = false, prostFactor = 0, @@ -103,7 +103,7 @@ end function DataController:loadLocalData(key) self.tocData = ModData.get(key) - 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") @@ -345,13 +345,19 @@ end --* Global Mod Data Handling *-- function DataController:apply() + TOC_DEBUG.print("Applying data for " .. self.username) ModData.transmit(CommandsData.GetKey(self.username)) + + -- if getPlayer():getUsername() ~= self.username then + -- sendClientCommand(CommandsData.modules.TOC_RELAY, CommandsData.server.Relay.RelayApplyFromOtherClient, {patientUsername = self.username} ) + -- -- force request from the server for that other client... + -- end end ---Online only, Global Mod Data doesn't trigger this in SP ----@param key any ----@param data any +---@param key string +---@param data tocModDataType 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 @@ -370,25 +376,47 @@ function DataController.ReceiveData(key, data) -- 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 username == getPlayer():getUsername() and not handler.isResetForced then - handler:loadLocalData(key) - elseif handler.isResetForced or data == nil then - TOC_DEBUG.print("Data is nil or empty!?") - TOC_DEBUG.printTable(data) + + -- TODO Add update from server scenario + + if handler.isResetForced then handler:setup(key) - elseif data and data.limbs then + elseif data then + if data.isUpdateFromServer then + TOC_DEBUG.print("Update from the server") + end handler:applyOnlineData(data) + elseif username == getPlayer():getUsername() then + handler:loadLocalData(key) end + handler:setIsResetForced(false) handler:setIsDataReady(true) - -- Event, triggers caching triggerEvent("OnReceivedTocData", handler.username) + + + -- if username == getPlayer():getUsername() and not handler.isResetForced then + -- handler:loadLocalData(key) + -- elseif handler.isResetForced or data == nil then + -- TOC_DEBUG.print("Data is nil or empty!?") + -- TOC_DEBUG.printTable(data) + -- handler:setup(key) + -- elseif data and data.limbs then + -- handler:applyOnlineData(data) + -- end + + -- handler:setIsResetForced(false) + -- handler:setIsDataReady(true) + + -- -- Event, triggers caching + -- triggerEvent("OnReceivedTocData", handler.username) + -- Transmit it back to the server - ModData.transmit(key) - TOC_DEBUG.print("Transmitting data after receiving it for: " .. handler.username) + --ModData.transmit(key) + --TOC_DEBUG.print("Transmitting data after receiving it for: " .. handler.username) end diff --git a/media/lua/client/TOC/Handlers/CachedDataHandler.lua b/media/lua/client/TOC/Handlers/CachedDataHandler.lua index e769070..77aa059 100644 --- a/media/lua/client/TOC/Handlers/CachedDataHandler.lua +++ b/media/lua/client/TOC/Handlers/CachedDataHandler.lua @@ -22,7 +22,11 @@ end ---Will calculate all the values that we need function CachedDataHandler.CalculateCacheableValues(username) CachedDataHandler.CalculateHighestAmputatedLimbs(username) - CachedDataHandler.CalculateBothHandsFeasibility() + + -- FIX This should be run ONLY on the actual client, never on other clients. Just a placeholder fix for now + if getPlayer():getUsername() == username then + CachedDataHandler.CalculateBothHandsFeasibility() + end end diff --git a/media/lua/client/TOC/TimedActions/CleanWoundAction.lua b/media/lua/client/TOC/TimedActions/CleanWoundAction.lua index d445e04..66bc78f 100644 --- a/media/lua/client/TOC/TimedActions/CleanWoundAction.lua +++ b/media/lua/client/TOC/TimedActions/CleanWoundAction.lua @@ -25,8 +25,10 @@ function CleanWoundAction:new(doctor, otherPlayer, bandage, bodyPart) o.bandage = bandage o.stopOnWalk = true o.stopOnRun = true + o.bandagedPlayerX = otherPlayer:getX() o.bandagedPlayerY = otherPlayer:getY() + o.maxTime = 250 - (o.doctorLevel * 6) if doctor:isTimedActionInstant() then o.maxTime = 1 @@ -79,8 +81,8 @@ function CleanWoundAction:stop() end function CleanWoundAction:perform() - -- needed to remove from queue / start next. - ISBaseTimedAction.perform(self) + + TOC_DEBUG.print("CleanWound for " .. self.otherPlayer:getUsername()) if self.character:HasTrait("Hemophobic") then self.character:getStats():setPanic(self.character:getStats():getPanic() + 15) @@ -113,6 +115,9 @@ function CleanWoundAction:perform() visual:setBlood(bbptEnum, 0) ISHealthPanel.setBodyPartActionForPlayer(self.otherPlayer, self.bodyPart, nil, nil, nil) + + -- needed to remove from queue / start next. + ISBaseTimedAction.perform(self) end return CleanWoundAction \ No newline at end of file diff --git a/media/lua/client/TOC/TimedActions/CutLimbAction.lua b/media/lua/client/TOC/TimedActions/CutLimbAction.lua index 4cdcb8b..4db7f04 100644 --- a/media/lua/client/TOC/TimedActions/CutLimbAction.lua +++ b/media/lua/client/TOC/TimedActions/CutLimbAction.lua @@ -7,6 +7,8 @@ local CommandsData = require("TOC/CommandsData") ---@class CutLimbAction : ISBaseTimedAction ---@field patient IsoPlayer ---@field character IsoPlayer +---@field patientX number +---@field patientY number ---@field limbName string ---@field item InventoryItem ---@field stitchesItem InventoryItem? @@ -32,6 +34,9 @@ function CutLimbAction:new(surgeon, patient, limbName, item, stitchesItem, banda o.limbName = limbName o.item = item + o.patientX = patient:getX() + o.patientY = patient:getY() + o.stitchesItem = stitchesItem or nil o.bandageItem = bandageItem or nil @@ -45,7 +50,7 @@ function CutLimbAction:new(surgeon, patient, limbName, item, stitchesItem, banda end function CutLimbAction:isValid() - return not ISHealthPanel.DidPatientMove(self.patient, self.character, self.patient:getX(), self.patient:getY()) + return not ISHealthPanel.DidPatientMove(self.character,self.patient, self.patientX, self.patientY) end function CutLimbAction:start() diff --git a/media/lua/client/TOC/UI/HealthPanel.lua b/media/lua/client/TOC/UI/HealthPanel.lua index ee3b1c2..895cfa9 100644 --- a/media/lua/client/TOC/UI/HealthPanel.lua +++ b/media/lua/client/TOC/UI/HealthPanel.lua @@ -133,13 +133,15 @@ function ISHealthPanel:update() og_ISHealthPanel_update(self) -- TODO Listen for changes on other player side instead of looping this + + -- FIX Re-enable it, just for test if self.character then local locPlUsername = getPlayer():getUsername() local remPlUsername = self.character:getUsername() if locPlUsername ~= remPlUsername and self:isReallyVisible() then - -- Request update for TOC DATA? + -- Request update for TOC DATA local key = CommandsData.GetKey(remPlUsername) - ModData.request(key) + --ModData.request(key) end end end diff --git a/media/lua/client/TOC/UI/Interactions/CutLimbInteractionHandler.lua b/media/lua/client/TOC/UI/Interactions/CutLimbInteractionHandler.lua index fb79856..4e26d42 100644 --- a/media/lua/client/TOC/UI/Interactions/CutLimbInteractionHandler.lua +++ b/media/lua/client/TOC/UI/Interactions/CutLimbInteractionHandler.lua @@ -208,7 +208,8 @@ end function CutLimbInteractionHandler:addToMenu(context) --TOC_DEBUG.print("CutLimbInteractionHandler addToMenu") local types = self:getAllItemTypes(self.items.ITEMS) - if #types > 0 and StaticData.BODYLOCS_IND_BPT[self.limbName] and not DataController.GetInstance():getIsCut(self.limbName) then + local patientUsername = self:getPatient():getUsername() + if #types > 0 and StaticData.BODYLOCS_IND_BPT[self.limbName] and not DataController.GetInstance(patientUsername):getIsCut(self.limbName) then TOC_DEBUG.print("addToMenu, types > 0") for i=1, #types do context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected, types[i]) @@ -230,7 +231,8 @@ end function CutLimbInteractionHandler:isValid() --TOC_DEBUG.print("CutLimbInteractionHandler isValid") self:checkItems() - return not DataController.GetInstance():getIsCut(self.limbName) + local patientUsername = self:getPatient():getUsername() + return not DataController.GetInstance(patientUsername):getIsCut(self.limbName) end function CutLimbInteractionHandler:perform(previousAction, itemType) diff --git a/media/lua/client/TOC/UI/Interactions/HealthPanelBaseHandler.lua b/media/lua/client/TOC/UI/Interactions/HealthPanelBaseHandler.lua index ecfcb8a..a95545a 100644 --- a/media/lua/client/TOC/UI/Interactions/HealthPanelBaseHandler.lua +++ b/media/lua/client/TOC/UI/Interactions/HealthPanelBaseHandler.lua @@ -126,10 +126,12 @@ function BaseHandler:toPlayerInventory(item, previousAction) return previousAction end +---@return IsoPlayer function BaseHandler:getDoctor() return self.panel.otherPlayer or self.panel.character end +---@return IsoPlayer function BaseHandler:getPatient() return self.panel.character end diff --git a/media/lua/client/TOC/UI/Interactions/WoundCleaningInteractionHandler.lua b/media/lua/client/TOC/UI/Interactions/WoundCleaningInteractionHandler.lua index 192dba7..d350aee 100644 --- a/media/lua/client/TOC/UI/Interactions/WoundCleaningInteractionHandler.lua +++ b/media/lua/client/TOC/UI/Interactions/WoundCleaningInteractionHandler.lua @@ -35,6 +35,7 @@ end function WoundCleaningInteractionHandler:addToMenu(context) --TOC_DEBUG.print("WoundCleaningInteraction addToMenu") + local types = self:getAllItemTypes(self.items.ITEMS) if #types > 0 and self:isValid() then TOC_DEBUG.print("WoundCleaningInteraction inside addToMenu") diff --git a/media/lua/server/TOC/ServerDataHandler.lua b/media/lua/server/TOC/ServerDataHandler.lua index aa5db52..253ff67 100644 --- a/media/lua/server/TOC/ServerDataHandler.lua +++ b/media/lua/server/TOC/ServerDataHandler.lua @@ -1,7 +1,12 @@ if isClient() then return end -- The event makes this necessary to prevent clients from running this file local StaticData = require("TOC/StaticData") +local CommandsData = require("TOC/CommandsData") ------------------------ + + + + local ServerDataHandler = {} ServerDataHandler.modData = {} @@ -21,10 +26,36 @@ function ServerDataHandler.AddTable(key, table) if not luautils.stringStarts(key, StaticData.MOD_NAME .. "_") then return end TOC_DEBUG.print("Received TOC ModData: " .. tostring(key)) - TOC_DEBUG.printTable(table) + --TOC_DEBUG.printTable(table) + + -- Set that the data has been modified and it's updated on the server + table.isUpdateFromServer = true ModData.add(key, table) -- Add it to the server mod data ServerDataHandler.modData[key] = table + + + -- 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 + local username = CommandsData.GetUsername(key) + TOC_DEBUG.print("Reapplying to " .. username) + + -- Since getPlayerFromUsername doesn't work in mp, we're gonna do this workaround. ew + local onlinePlayers = getOnlinePlayers() + local selectedPlayer + for i=0, onlinePlayers:size() - 1 do + + ---@type IsoPlayer + local player = onlinePlayers:get(i) + if player:getUsername() == username then + selectedPlayer = player + break + end + end + + TOC_DEBUG.print("Player username from IsoPlayer: " .. selectedPlayer:getUsername()) + sendServerCommand(selectedPlayer, CommandsData.modules.TOC_RELAY, CommandsData.client.Relay.ReceiveApplyFromServer, {}) + end Events.OnReceiveGlobalModData.Add(ServerDataHandler.AddTable) diff --git a/media/lua/server/TOC/ServerRelayCommands.lua b/media/lua/server/TOC/ServerRelayCommands.lua index 517d56e..890c5be 100644 --- a/media/lua/server/TOC/ServerRelayCommands.lua +++ b/media/lua/server/TOC/ServerRelayCommands.lua @@ -28,7 +28,6 @@ function ServerRelayCommands.RelayExecuteAmputationAction(surgeonPl, args) sendServerCommand(patientPl, CommandsData.modules.TOC_RELAY, CommandsData.client.Relay.ReceiveExecuteAmputationAction, params) end - --* ADMIN ONLY *-- ---Relay a local init from another client ---@param adminObj IsoPlayer diff --git a/media/lua/shared/TOC/CommandsData.lua b/media/lua/shared/TOC/CommandsData.lua index acfae51..e937d48 100644 --- a/media/lua/shared/TOC/CommandsData.lua +++ b/media/lua/shared/TOC/CommandsData.lua @@ -13,6 +13,9 @@ CommandsData.client = { ReceiveDamageDuringAmputation = "ReceiveDamageDuringAmputation", ---@alias receiveDamageDuringAmputationParams { limbName : string} ReceiveExecuteAmputationAction = "ReceiveExecuteAmputationAction", ---@alias receiveExecuteAmputationActionParams {surgeonNum : number, limbName : string} + --* APPLY *-- + ReceiveApplyFromServer = "ReceiveApplyFromServer", + --* ADMIN ONLY --* ReceiveExecuteInitialization = "ReceiveExecuteInitialization" @@ -28,8 +31,6 @@ CommandsData.server = { Relay = { RelayDamageDuringAmputation = "RelayDamageDuringAmputation", ---@alias relayDamageDuringAmputationParams {patientNum : number, limbName : string} RelayExecuteAmputationAction = "RelayExecuteAmputationAction", ---@alias relayExecuteAmputationActionParams {patientNum : number, limbName : string} - - --* ADMIN ONLY *-- RelayExecuteInitialization = "RelayExecuteInitialization" ---@alias relayExecuteInitializationParams {patientNum : number} } @@ -42,6 +43,10 @@ function CommandsData.GetKey(username) return StaticData.MOD_NAME .. "_" .. username end +function CommandsData.GetUsername(key) + return string.sub(key, #StaticData.MOD_NAME + 2, #key) -- Not sure why +2... Something with kahlua, it should be +1 +end + function CommandsData.GetZombieKey() return StaticData.MOD_NAME .. "_ZOMBIES" end diff --git a/media/lua/shared/TOC/StaticData.lua b/media/lua/shared/TOC/StaticData.lua index 91f041f..e20735c 100644 --- a/media/lua/shared/TOC/StaticData.lua +++ b/media/lua/shared/TOC/StaticData.lua @@ -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 prosthesisData {isProstEquipped : boolean, prostFactor : number } ---@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 } +---@alias tocModDataType { limbs : limbsTable, prostheses : prosthesesTable, isIgnoredPartInfected : boolean, isAnyLimbCut : boolean, isUpdateFromServer : boolean } --------------------------- @@ -174,7 +174,7 @@ end StaticData.TOURNIQUET_BODYLOCS_TO_GROUPS_IND_STR = { ["HandsLeft"] = StaticData.AMP_GROUPS_IND_STR.Top_L, - ["HandsRight"] = StaticData.AMP_GROUPS.IND_STR.Top_R + ["HandsRight"] = StaticData.AMP_GROUPS_IND_STR.Top_R }