POC for health panels in MP

This commit is contained in:
ZioPao
2023-11-13 03:04:12 +01:00
parent 30f343a7de
commit 980545a07e
9 changed files with 155 additions and 83 deletions

View File

@@ -29,6 +29,8 @@ function ModDataHandler:new(username, isResetForced)
TOC_DEBUG.print("tocData in ModDataHandler for " .. username .. " is nil, creating it now") TOC_DEBUG.print("tocData in ModDataHandler for " .. username .. " is nil, creating it now")
self:setup(key) self:setup(key)
end end
-- Transmit it to the server
ModData.transmit(key)
ModDataHandler.instances[username] = o ModDataHandler.instances[username] = o
@@ -59,8 +61,7 @@ function ModDataHandler:setup(key)
-- Add it to global mod data -- Add it to global mod data
ModData.add(key, self.tocData) ModData.add(key, self.tocData)
-- Transmit it to the server
ModData.transmit(key)
end end
@@ -169,12 +170,26 @@ function ModDataHandler:setLimbParams(limbName, ampStatus, cicatrizationTime)
end end
--* Global Mod Data Apply *-- --* Global Mod Data Handling *--
function ModDataHandler:apply() function ModDataHandler:apply()
ModData.transmit(CommandsData.GetKey(self.username)) ModData.transmit(CommandsData.GetKey(self.username))
end end
function ModDataHandler.ReceiveData(key, table)
TOC_DEBUG.print("receive data for " .. key)
if table == {} or table == nil then
TOC_DEBUG.print("table is nil... returning")
return
end
ModData.add(key, table) -- Add it to the client mod data (not sure)
local username = key:sub(5)
ModDataHandler.GetInstance(username)
end
Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData)
---@param username string? ---@param username string?
---@return ModDataHandler ---@return ModDataHandler
function ModDataHandler.GetInstance(username) function ModDataHandler.GetInstance(username)

View File

@@ -18,7 +18,7 @@ local PlayerHandler = {}
PlayerHandler.amputatedLimbs = {} PlayerHandler.amputatedLimbs = {}
---Setup the Player Handler and modData ---Setup the Player Handler and modData, only for local client
---@param playerObj IsoPlayer ---@param playerObj IsoPlayer
---@param isForced boolean? ---@param isForced boolean?
function PlayerHandler.InitializePlayer(playerObj, isForced) function PlayerHandler.InitializePlayer(playerObj, isForced)
@@ -27,15 +27,9 @@ function PlayerHandler.InitializePlayer(playerObj, isForced)
TOC_DEBUG.print("initializing " .. username) TOC_DEBUG.print("initializing " .. username)
local modDataHandler = ModDataHandler:new(username, isForced) local modDataHandler = ModDataHandler:new(username, isForced)
PlayerHandler.playerObj = playerObj PlayerHandler.playerObj = playerObj
-- Calculate amputated limbs at startup
PlayerHandler.amputatedLimbs[username] = {}
for i=1, #StaticData.LIMBS_STRINGS do -- Calculate amputated limbs at startup
local limbName = StaticData.LIMBS_STRINGS[i] PlayerHandler.CacheAmputatedLimbs(username)
if modDataHandler:getIsCut(limbName) then
PlayerHandler.AddLocalAmputatedLimb(username, limbName)
end
end
-- Since isForced is used to reset an existing player data, we're gonna clean their ISHealthPanel table too -- Since isForced is used to reset an existing player data, we're gonna clean their ISHealthPanel table too
if isForced then if isForced then
@@ -60,8 +54,23 @@ function PlayerHandler.ManageTraits(playerObj)
end end
end end
---Cycle through all the limbs and caches the ones that the player cut off
---@param username string
function PlayerHandler.CacheAmputatedLimbs(username)
PlayerHandler.amputatedLimbs[username] = {}
local modDataHandler = ModDataHandler.GetInstance(username)
for i=1, #StaticData.LIMBS_STRINGS do
local limbName = StaticData.LIMBS_STRINGS[i]
if modDataHandler:getIsCut(limbName) then
PlayerHandler.AddLocalAmputatedLimb(username, limbName)
end
end
end
---Cache the currently amputated limbs ---Cache the currently amputated limbs
---@param limbName string ---@param limbName string
---@private
function PlayerHandler.AddLocalAmputatedLimb(username, limbName) function PlayerHandler.AddLocalAmputatedLimb(username, limbName)
TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username) TOC_DEBUG.print("added " .. limbName .. " to known amputated limbs for " .. username)
table.insert(PlayerHandler.amputatedLimbs[username], limbName) -- TODO This should be player specific, not generic table.insert(PlayerHandler.amputatedLimbs[username], limbName) -- TODO This should be player specific, not generic

View File

@@ -2,7 +2,6 @@ local PlayerHandler = require("TOC/Handlers/PlayerHandler")
local StaticData = require("TOC/StaticData") local StaticData = require("TOC/StaticData")
local CommonMethods = require("TOC/CommonMethods") local CommonMethods = require("TOC/CommonMethods")
local ModDataHandler = require("TOC/Handlers/ModDataHandler") local ModDataHandler = require("TOC/Handlers/ModDataHandler")
local CommandsData = require("TOC/CommandsData")
---@diagnostic disable: duplicate-set-field ---@diagnostic disable: duplicate-set-field
local CutLimbHandler = require("TOC/UI/CutLimbInteractions") local CutLimbHandler = require("TOC/UI/CutLimbInteractions")
@@ -49,14 +48,11 @@ function ISHealthPanel:setHighestAmputation()
--TOC_DEBUG.print("setHighestAmputation for " .. self.tocUsername) --TOC_DEBUG.print("setHighestAmputation for " .. self.tocUsername)
if self.otherPlayer ~= nil then -- character is always the patient.
self.tocUsername = self.otherPlayer:getUsername() self.tocUsername = self.character:getUsername()
else
self.tocUsername = self.character:getUsername()
end
if PlayerHandler.amputatedLimbs == nil or PlayerHandler.amputatedLimbs[self.tocUsername] == nil then if PlayerHandler.amputatedLimbs == nil or PlayerHandler.amputatedLimbs[self.tocUsername] == nil then
TOC_DEBUG.print("PlayerHandler.amputatedLimbs is still nil or wasn't initialized for that player") TOC_DEBUG.print("PlayerHandler.amputatedLimbs is still nil or wasn't initialized for " .. self.tocUsername)
return return
end end
local amputatedLimbs = PlayerHandler.amputatedLimbs[self.tocUsername] local amputatedLimbs = PlayerHandler.amputatedLimbs[self.tocUsername]
@@ -111,6 +107,8 @@ local og_ISHealthPanel_render = ISHealthPanel.render
function ISHealthPanel:render() function ISHealthPanel:render()
og_ISHealthPanel_render(self) og_ISHealthPanel_render(self)
--print("Rendering ISHealthPanel")
if self.highestAmputations ~= nil and self.highestAmputations[self.tocUsername] ~= nil then if self.highestAmputations ~= nil and self.highestAmputations[self.tocUsername] ~= nil then
-- Left Texture -- Left Texture
@@ -135,3 +133,44 @@ function ISCharacterInfoWindow:prerender()
og_ISCharacterInfoWindow_render(self) og_ISCharacterInfoWindow_render(self)
self.backgroundColor.a = 1 self.backgroundColor.a = 1
end end
-- We need to override this to force the alpha to 1 for the Medical Check in particular
local og_ISHealthPanel_prerender = ISHealthPanel.prerender
function ISHealthPanel:prerender()
og_ISHealthPanel_prerender(self)
self.backgroundColor.a = 1
end
--- The medical check wrap the health panel into this. We need to override its color
local overrideBackgroundColor = true
local og_ISUIElement_wrapInCollapsableWindow = ISUIElement.wrapInCollapsableWindow
function ISUIElement:wrapInCollapsableWindow(title, resizable, subClass)
local panel = og_ISUIElement_wrapInCollapsableWindow(self, title, resizable, subClass)
if overrideBackgroundColor then
TOC_DEBUG.print("Overriding color for panel - " .. title)
self.backgroundColor.a = 1
panel.backgroundColor.a = 1
end
return panel
end
-- This is run when a player is trying the Medical Check action on another player
local og_ISMedicalCheckAction_perform = ISMedicalCheckAction.perform
function ISMedicalCheckAction:perform()
local username = self.otherPlayer:getUsername()
TOC_DEBUG.print("creating instance for " .. username )
ModDataHandler.GetInstance(username)
-- We need to recalculate them here before we can create the highest amputations point
PlayerHandler.CacheAmputatedLimbs(username)
og_ISMedicalCheckAction_perform(self)
end

View File

@@ -1,15 +1,26 @@
local CommandsData = require("TOC/CommandsData") local CommandsData = require("TOC/CommandsData")
local ServerDataHandler = require("TOC/ServerDataHandler")
----------------------------
local DebugCommands = {} local DebugCommands = {}
---comment
---@param playerObj IsoPlayer
---@param args table
function DebugCommands.PrintAllTocData(playerObj, args)
TOC_DEBUG.printTable(ServerDataHandler.modData)
end
---comment ---comment
---@param playerObj IsoPlayer ---@param playerObj IsoPlayer
---@param args {username : string} ---@param args printTocDataParams
function DebugCommands.PrintTocData(playerObj, args) function DebugCommands.PrintTocData(playerObj, args)
local data = ModData.get(CommandsData.GetKey(args.username)) local key = CommandsData.GetKey(args.username)
TOC_DEBUG.printTable(data) local tocData = ServerDataHandler.GetTable(key)
TOC_DEBUG.printTable(tocData)
end end
-------------------- --------------------

View File

@@ -1,54 +0,0 @@
-- TODO Switch EVERYTHING to global mod data
local CommandsData = require("TOC/CommandsData")
local ServerDataCommands = {}
local moduleName = "test_sync"
local function PrintModDataTable(key, table)
print("Received key: " .. key)
end
Events.OnReceiveGlobalModData.Add(PrintModDataTable)
-- TODO Consider delays
-- TODO Use transmit from client
-- ---comment
-- ---@param playerObj IsoPlayer
-- ---@param args any
-- function ServerDataCommands.AddTable(playerObj, args)
-- ModData.add(GetKey(playerObj), args.tocData)
-- end
-- function ServerDataCommands.GetTable(playerObj, args)
-- local requestedPlayer = getSpecificPlayer(args.playerNum)
-- local data = ModData.get(CommandsData.GetKey(requestedPlayer))
-- -- TODO Request from that client again just to be sure that it's synced?
-- sendServerCommand()
-- end
------------------------------
-- local function OnClientDataCommand(module, command, playerObj, args)
-- if module == moduleName and ServerDataCommands[command] then
-- ServerDataCommands[command](playerObj, args)
-- end
-- end
-- Events.OnClientCommand.Add(OnClientDataCommand)

View File

@@ -0,0 +1,26 @@
if isClient() then return end
local ServerDataHandler = {}
ServerDataHandler.modData = {}
---Get the server mod data table containing that player TOC data
---@param key string
---@return tocModData
function ServerDataHandler.GetTable(key)
return ServerDataHandler.modData[key]
end
function ServerDataHandler.AddTable(key, table)
ModData.add(key, table) -- Add it to the server mod data
ServerDataHandler.modData[key] = table
end
Events.OnReceiveGlobalModData.Add(ServerDataHandler.AddTable)
return ServerDataHandler

View File

@@ -23,7 +23,8 @@ CommandsData.server = {
}, },
Debug = { Debug = {
PrintTocData = "PrintTocData" ---@alias printTocDataParams {username : string} PrintTocData = "PrintTocData", ---@alias printTocDataParams {username : string}
PrintAllTocData = "PrintAllTocData"
} }
} }
@@ -34,4 +35,13 @@ function CommandsData.GetKey(username)
return StaticData.MOD_NAME .. "_" .. username return StaticData.MOD_NAME .. "_" .. username
end end
-- ---comment
-- ---@param key string
-- ---@return string
-- function CommandsData.GetUsernameFromKey(key)
-- local subSize = #StaticData.MOD_NAME + 1
-- local username = key:sub(subSize)
-- return username
-- end
return CommandsData return CommandsData

View File

@@ -28,10 +28,16 @@ function TOC_DEBUG.printTable(table, indent)
end end
end end
function TOC_DEBUG.printGlobalModDataServer(username)
local CommandsData = require("TOC/CommandsData")
---@type printTocDataParams
local params = {username = username} --* Debug server commands *--
sendClientCommand(CommandsData.modules.TOC_DEBUG, CommandsData.server.Debug.PrintTocData, params)
local CommandsData = require("TOC/CommandsData")
function TOC_DEBUG.printPlayerServerModData(username)
sendClientCommand(CommandsData.modules.TOC_DEBUG, CommandsData.server.Debug.PrintTocData, {username = username})
end
function TOC_DEBUG.printAllServerModData()
sendClientCommand(CommandsData.modules.TOC_DEBUG, CommandsData.server.Debug.PrintAllTocData, {})
end end

View File

@@ -92,6 +92,16 @@ StaticData.HEALTH_PANEL_TEXTURES = {
ForeArm_L = getTexture("media/ui/ForeArm_L.png"), ForeArm_L = getTexture("media/ui/ForeArm_L.png"),
UpperArm_L = getTexture("media/ui/UpperArm_L.png"), UpperArm_L = getTexture("media/ui/UpperArm_L.png"),
Hand_R = getTexture("media/ui/Hand_R.png"),
ForeArm_R = getTexture("media/ui/ForeArm_R.png"),
UpperArm_R = getTexture("media/ui/UpperArm_R.png")
},
Male = {
Hand_L = getTexture("media/ui/Hand_L.png"),
ForeArm_L = getTexture("media/ui/ForeArm_L.png"),
UpperArm_L = getTexture("media/ui/UpperArm_L.png"),
Hand_R = getTexture("media/ui/Hand_R.png"), Hand_R = getTexture("media/ui/Hand_R.png"),
ForeArm_R = getTexture("media/ui/ForeArm_R.png"), ForeArm_R = getTexture("media/ui/ForeArm_R.png"),
UpperArm_R = getTexture("media/ui/UpperArm_R.png") UpperArm_R = getTexture("media/ui/UpperArm_R.png")