From 2d4dc07a25b7097cb455ca059497459ad338d5a8 Mon Sep 17 00:00:00 2001 From: "(Jip) Willem Wijnia" Date: Tue, 19 Mar 2024 15:03:16 +0100 Subject: [PATCH 1/3] Simplify the setup for moderator events --- engine/User.lua | 7 +- lua/SimCallbacks.lua | 16 +++ lua/keymap/misckeyactions.lua | 4 - lua/userInit.lua | 181 ++++++---------------------------- 4 files changed, 48 insertions(+), 160 deletions(-) diff --git a/engine/User.lua b/engine/User.lua index 98102bd077..2da18f0fcd 100644 --- a/engine/User.lua +++ b/engine/User.lua @@ -163,11 +163,10 @@ end function EntityCategoryFilterOut(category, units) end ---- Executes some Lua code in the sim +--- Executes some Lua code in the sim. Requires cheats to be enabled ---@param func function ----@param ... any this may actually be a comma-separated string of args instead of a vararg ----@return any -function ExecLuaInSim(func, ...) +---@param value any +function ExecLuaInSim(func, value) end --- Requests that the application shut down diff --git a/lua/SimCallbacks.lua b/lua/SimCallbacks.lua index 98495316bf..8b0f9917fd 100644 --- a/lua/SimCallbacks.lua +++ b/lua/SimCallbacks.lua @@ -1247,3 +1247,19 @@ Callbacks.AIPlatoonSimpleStructureBehavior = function(data, units) end --#endregion + +------------------------------------------------------------------------------- +--#region Moderator related functionality + +---@class CallbackModeratorEventData +---@field From number +---@field Message string + +---@param data CallbackModeratorEventData +Callbacks.ModeratorEvent = function(data) + -- show up in the game logs + local brain = GetArmyBrain(GetCurrentCommandSource()) + SPEW(string.format("Moderator event for %s: %s", tostring(brain.Nickname), repru(data, 10000))) +end + +--#endregion \ No newline at end of file diff --git a/lua/keymap/misckeyactions.lua b/lua/keymap/misckeyactions.lua index 3e611dba60..7ab142d2bc 100644 --- a/lua/keymap/misckeyactions.lua +++ b/lua/keymap/misckeyactions.lua @@ -662,7 +662,3 @@ SelectAllResourceConsumers = function(onscreen) SelectUnits(units) end - -TogglePerformanceMetricsWindow = function() - local instance = import("/lua/ui/lobby/sim-performance-popup.lua").OpenWindow() -end diff --git a/lua/userInit.lua b/lua/userInit.lua index 79bb8e72be..7e126115f1 100644 --- a/lua/userInit.lua +++ b/lua/userInit.lua @@ -155,67 +155,25 @@ do -- do a basic check if find(lower, 'setfocusarmy') then - local isCheatsEnabled = SessionGetScenarioInfo().Options.CheatsEnabled == "true" - - if not (SessionIsReplay() or isCheatsEnabled) then - local clients = GetSessionClients() - local localClient = nil - for k = 1, TableGetn(clients) do - if clients[k]["local"] then - localClient = clients[k] - break - end - end - + if not SessionIsReplay() then local currentFocusArmy = GetFocusArmy() local proposedFocusArmy = tonumber(match(command, '%d+')) - if find(command, '-') then proposedFocusArmy = proposedFocusArmy * -1 else proposedFocusArmy = proposedFocusArmy + 1 end - if localClient and TableGetn(clients) > 1 and proposedFocusArmy then - command = "SetFocusArmy " .. (currentFocusArmy - 1) - - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is trying to change focus army from %d to %d via ConExecute!", currentFocusArmy, proposedFocusArmy)} - }, + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format("Is changing focus army from %d to %d via ConExecute!", currentFocusArmy, proposedFocusArmy), }, - true - ) - - local tick = GameTick() - if tickstamp + 10 > tick then - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is (continously) trying to change focus army from %d to %d via ConExecute!", currentFocusArmy, proposedFocusArmy ) } - }, - }, - true - ) - end - - tickstamp = tick - end + } + ) end end @@ -245,67 +203,25 @@ do -- do a basic check if find(lower, 'setfocusarmy') then - local isCheatsEnabled = SessionGetScenarioInfo().Options.CheatsEnabled == "true" - - if not (SessionIsReplay() or isCheatsEnabled) then - local clients = GetSessionClients() - local localClient = nil - for k = 1, TableGetn(clients) do - if clients[k]["local"] then - localClient = clients[k] - break - end - end - + if not (SessionIsReplay()) then local currentFocusArmy = GetFocusArmy() local proposedFocusArmy = tonumber(match(command, '%d+')) - if find(command, '-') then proposedFocusArmy = proposedFocusArmy * -1 else proposedFocusArmy = proposedFocusArmy + 1 end - if localClient and TableGetn(clients) > 1 and proposedFocusArmy then - command = "SetFocusArmy " .. (currentFocusArmy - 1) - - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is trying to change focus army from %d to %d via ConExecuteSave!", currentFocusArmy, proposedFocusArmy) } - }, + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format("Is changing focus army from %d to %d via ConExecuteSave!", currentFocusArmy, proposedFocusArmy), }, - true - ) - - local tick = GameTick() - if tickstamp + 10 > tick then - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is (continously) trying to change focus army from %d to %d via ConExecuteSave!", currentFocusArmy, proposedFocusArmy) } - }, - }, - true - ) - end - - tickstamp = tick - end + } + ) end end @@ -658,58 +574,19 @@ do -- do a basic check local isCheatsEnabled = SessionGetScenarioInfo().Options.CheatsEnabled == "true" if not (SessionIsReplay() or isCheatsEnabled) then - local clients = GetSessionClients() - local localClient = nil - for k = 1, TableGetn(clients) do - if clients[k]["local"] then - localClient = clients[k] - break - end - end - local currentFocusArmy = GetFocusArmy() local proposedFocusArmy = number - if localClient and TableGetn(clients) > 1 and proposedFocusArmy then - number = currentFocusArmy - - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is trying to change focus army from %d to %d via SetFocusArmy!", currentFocusArmy, proposedFocusArmy) } - }, + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format("Is changing focus army from %d to %d via SetFocusArmy!", currentFocusArmy, proposedFocusArmy), }, - true - ) - - local tick = GameTick() - if tickstamp + 10 > tick then - -- try to inform moderators - SimCallback( - { - Func="GiveResourcesToPlayer", - Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Is (continously) trying to change focus army from %d to %d via SetFocusArmy!", currentFocusArmy, proposedFocusArmy) } - }, - }, - true - ) - end - - tickstamp = tick - end + } + ) end oldSetFocusArmy(number) From 7a9354dc41653d40edfa7737eaad2f47185001d7 Mon Sep 17 00:00:00 2001 From: "(Jip) Willem Wijnia" Date: Tue, 19 Mar 2024 19:07:33 +0100 Subject: [PATCH 2/3] Include the remaining events --- lua/UserSync.lua | 23 +++----------- lua/userInit.lua | 81 +++++++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/lua/UserSync.lua b/lua/UserSync.lua index 1419e50088..d60db9899b 100644 --- a/lua/UserSync.lua +++ b/lua/UserSync.lua @@ -111,32 +111,17 @@ function OnSync() -- Informs moderators that the focus army has changed for the local player if Sync.FocusArmyChanged then - local clients = GetSessionClients() - local localClient = nil - for k = 1, table.getn(clients) do - if clients[k]["local"] then - localClient = clients[k] - break - end - end - -- try to inform moderators SimCallback( { - Func="GiveResourcesToPlayer", + Func="ModeratorEvent", Args= { - From=Sync.FocusArmyChanged.old, - To=Sync.FocusArmyChanged.old, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Switched focus army from %s to %s!", tostring(Sync.FocusArmyChanged.old), tostring(Sync.FocusArmyChanged.new)) } + From = Sync.FocusArmyChanged.old, + Message = string.format("Switched focus army from %s to %s!", tostring(Sync.FocusArmyChanged.old), tostring(Sync.FocusArmyChanged.new)), }, - }, - true + } ) end - end -- old sync callbacks diff --git a/lua/userInit.lua b/lua/userInit.lua index 7e126115f1..2f461269c2 100644 --- a/lua/userInit.lua +++ b/lua/userInit.lua @@ -123,12 +123,9 @@ do local TableGetn = table.getn - local GameTick = GameTick local GetFocusArmy = GetFocusArmy local SessionIsReplay = SessionIsReplay - local SessionRequestPause = SessionRequestPause local GetSessionClients = GetSessionClients - local SessionGetScenarioInfo = SessionGetScenarioInfo local tickstamp = 0 @@ -146,11 +143,18 @@ do -- inform allies about self-destructed units if find(lower, 'killselectedunits') then local selectedUnits = GetSelectedUnits() - SessionSendChatMessage(import('/lua/ui/game/clientutils.lua').GetAllies(), { - to = 'allies', - text = string.format('Self-destructed %d units', TableGetn(selectedUnits)), - Chat = true, - }) + local currentFocusArmy = GetFocusArmy() + + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format('Self-destructed %d units', TableGetn(selectedUnits)), + }, + } + ) end -- do a basic check @@ -194,11 +198,18 @@ do -- inform allies about self-destructed units if find(lower, 'killselectedunits') then local selectedUnits = GetSelectedUnits() - SessionSendChatMessage(import('/lua/ui/game/clientutils.lua').GetAllies(), { - to = 'allies', - text = string.format('Self-destructed %d units', TableGetn(selectedUnits)), - Chat = true, - }) + local currentFocusArmy = GetFocusArmy() + + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format('Self-destructed %d units', TableGetn(selectedUnits)), + }, + } + ) end -- do a basic check @@ -246,44 +257,42 @@ do -- inform allies about self-destructed units if callback.Func == 'ToggleSelfDestruct' then local selectedUnits = GetSelectedUnits() - SessionSendChatMessage(import('/lua/ui/game/clientutils.lua').GetAllies(), { - to = 'allies', - text = string.format('Self-destructed %d units', TableGetn(selectedUnits)), - Chat = true, - }) + + -- try to inform moderators + SimCallback( + { + Func="ModeratorEvent", + Args= { + From = currentFocusArmy, + Message = string.format('Self-destructed %d units', TableGetn(selectedUnits)), + }, + } + ) end -- inform moderators about pings if callback.Func == 'SpawnPing' then if callback.Args.Marker then + -- try to inform moderators SimCallback( { - Func="GiveResourcesToPlayer", + Func="ModeratorEvent", Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Created a marker with the text: '%s'", tostring(callback.Args.Name)) } + From = currentFocusArmy, + Message = string.format("Created a marker with the text: '%s'", tostring(callback.Args.Name)), }, - }, - true + } ) else + -- try to inform moderators SimCallback( { - Func="GiveResourcesToPlayer", + Func="ModeratorEvent", Args= { - From=currentFocusArmy, - To=currentFocusArmy, - Mass=0, - Energy=0, - Sender=localClient.name, - Msg={ to='moderators', text = string.format("Created a ping of type '%s'", tostring(callback.Args.Type)) } + From = currentFocusArmy, + Message = string.format("Created a ping of type '%s'", tostring(callback.Args.Type)), }, - }, - true + } ) end end From 6a84c604bca3320cc540ede49a1994e6104180e1 Mon Sep 17 00:00:00 2001 From: "(Jip) Willem Wijnia" Date: Tue, 19 Mar 2024 19:19:35 +0100 Subject: [PATCH 3/3] Clean up the logs --- lua/SimPing.lua | 1 - lua/SimSync.lua | 2 -- 2 files changed, 3 deletions(-) diff --git a/lua/SimPing.lua b/lua/SimPing.lua index 21bd561dc5..2f5c6685dc 100644 --- a/lua/SimPing.lua +++ b/lua/SimPing.lua @@ -122,7 +122,6 @@ end function OnArmyChange() Sync.MaxPingMarkers = MaxPingMarkers - LOG('syncing max ping markers: ', MaxPingMarkers) --Flush all of the current markers on the UI side if not Sync.Ping then Sync.Ping = {} end table.insert(Sync.Ping, {Action = 'flush'}) diff --git a/lua/SimSync.lua b/lua/SimSync.lua index d69e94b66a..0db4c06a3c 100644 --- a/lua/SimSync.lua +++ b/lua/SimSync.lua @@ -122,8 +122,6 @@ function OnPostLoad() end function NoteFocusArmyChanged(new, old) - LOG("NoteFocusArmyChanged", new, old) - --LOG('NoteFocusArmyChanged(new=' .. repr(new) .. ', old=' .. repr(old) .. ')') import("/lua/simping.lua").OnArmyChange() import("/lua/sim/recall.lua").OnArmyChange() for entityID, data in UnitData do