diff --git a/LuaRules/Gadgets/start_pregame_timer.lua b/LuaRules/Gadgets/start_pregame_timer.lua new file mode 100644 index 0000000000..84d788fbc7 --- /dev/null +++ b/LuaRules/Gadgets/start_pregame_timer.lua @@ -0,0 +1,68 @@ +function gadget:GetInfo() + return { + name = "Pregame Timer", + desc = "Listens to server pregame timekeeping messages", + author = "Sprung", + date = "2024", + license = "Public Domain", + layer = 0, + enabled = true, + } +end + +if gadgetHandler:IsSyncedCode() then + + function gadget:Initialize() + if Spring.GetGameFrame() > 0 then + gadgetHandler:RemoveGadget() + return + end + end + + function gadget:GameStart() + Spring.SetGameRulesParam("pregame_timer_seconds", nil) + gadgetHandler:RemoveGadget() + end + + function gadget:GotChatMsg (msg, senderID) + if string.find(msg, "pregame_timer_seconds") ~= 1 then + return + end + + if senderID ~= 255 then -- autohost + return + end + + local secondsUntilStart = tonumber(string.sub(msg, 23)) + if not secondsUntilStart then + return + end + + Spring.SetGameRulesParam("pregame_timer_seconds", secondsUntilStart) + SendToUnsynced("PreGameTimekeeping", secondsUntilStart) + end + +else -- unsynced + + function gadget:Initialize() + if Spring.GetGameFrame() > 0 then + gadgetHandler:RemoveGadget() + return + end + + gadgetHandler:AddSyncAction("PreGameTimekeeping", function (_, secondsUntilStart) + if Script.LuaUI("PreGameTimekeeping") then + Script.LuaUI.PreGameTimekeeping(secondsUntilStart) + end + end) + end + + function gadget:GameStart() + gadgetHandler:RemoveGadget() + end + + function gadget:Shutdown() + gadgetHandler:RemoveSyncAction("PreGameTimekeeping") + end + +end diff --git a/LuaUI/Widgets/gui_epicmenu.lua b/LuaUI/Widgets/gui_epicmenu.lua index bd4fd4457c..3a390894d4 100644 --- a/LuaUI/Widgets/gui_epicmenu.lua +++ b/LuaUI/Widgets/gui_epicmenu.lua @@ -3334,6 +3334,12 @@ function widget:GameFrame(n) end end +function widget:PreGameTimekeeping(secondsUntilStart) + if lbl_gtime then + lbl_gtime:SetCaption("-"..GetTimeString(secondsUntilStart)) + end +end + function widget:KeyPress(key, modifier, isRepeat, label) if not get_key_bind_mod then if key == KEYSYMS.LCTRL diff --git a/LuaUI/cawidgets.lua b/LuaUI/cawidgets.lua index 92cf55b785..514e2aa1b3 100644 --- a/LuaUI/cawidgets.lua +++ b/LuaUI/cawidgets.lua @@ -252,6 +252,7 @@ local flexCallIns = { "UnitStructureMoved", 'MissileFired', 'MissileDestroyed', + "PreGameTimekeeping", } local flexCallInMap = {} for _, ci in ipairs(flexCallIns) do @@ -2657,6 +2658,13 @@ function widgetHandler:Load(zip) end end + +function widgetHandler:PreGameTimekeeping(secondsUntilStart) + for _,w in r_ipairs(self.PreGameTimekeepingList) do + w:PreGameTimekeeping(secondsUntilStart) + end +end + -------------------------------------------------------------------------------- --------------------------------------------------------------------------------