From aaea8bc709650609bcc6b01c7f48aab0e55bff44 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 24 Jul 2017 21:21:00 +0300 Subject: [PATCH] Dedicated servers can use autorestart --- .../BarotraumaClient/Source/DebugConsole.cs | 7 +++++ .../Source/Screens/NetLobbyScreen.cs | 20 ++++++++++++- .../BarotraumaServer/Source/DebugConsole.cs | 8 +++++ .../BarotraumaShared/Source/DebugConsole.cs | 30 +++++++++++++++++++ .../Source/Networking/GameServer.cs | 18 ++++++----- .../Source/Networking/GameServerSettings.cs | 14 +++++++-- .../Source/Screens/NetLobbyScreen.cs | 16 +--------- 7 files changed, 88 insertions(+), 25 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/DebugConsole.cs b/Barotrauma/BarotraumaClient/Source/DebugConsole.cs index 407a3f2f5..78954be65 100644 --- a/Barotrauma/BarotraumaClient/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaClient/Source/DebugConsole.cs @@ -327,6 +327,13 @@ namespace Barotrauma new GUIMessageBox("", string.Join(" ", args)); })); + commands.Add(new Command("autorestart", "autorestart: Toggle autorestart on/off when hosting a server.", (string[] args) => + { + if (GameMain.Server == null) return; + GameMain.NetLobbyScreen.ToggleAutoRestart(); + NewMessage(GameMain.Server.AutoRestart ? "Automatic restart enabled." : "Automatic restart disabled.", Color.White); + })); + commands.Add(new Command("debugdraw", "debugdraw: Toggle the debug drawing mode on/off.", (string[] args) => { GameMain.DebugDraw = !GameMain.DebugDraw; diff --git a/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs b/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs index fda6ab2f4..90a28fb9c 100644 --- a/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs +++ b/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs @@ -44,11 +44,12 @@ namespace Barotrauma private GUIDropDown shuttleList; - private Sprite backgroundSprite; private GUITextBox serverMessage; + private float autoRestartTimer; + public GUITextBox ServerMessage { get { return serverMessage; } @@ -154,6 +155,17 @@ namespace Barotrauma } } + public string AutoRestartText() + { + if (GameMain.Server != null) + { + if (!GameMain.Server.AutoRestart || GameMain.Server.ConnectedClients.Count == 0) return ""; + return "Restarting in " + ToolBox.SecondsToReadableTime(Math.Max(GameMain.Server.AutoRestartTimer, 0)); + } + + if (autoRestartTimer == 0.0f) return ""; + return "Restarting in " + ToolBox.SecondsToReadableTime(Math.Max(autoRestartTimer, 0)); + } public NetLobbyScreen() { @@ -596,6 +608,12 @@ namespace Barotrauma autoRestartTimer = timer; } + public void ToggleAutoRestart() + { + autoRestartBox.Selected = !autoRestartBox.Selected; + ToggleAutoRestart(autoRestartBox); + } + private bool ToggleAutoRestart(GUITickBox tickBox) { if (GameMain.Server == null) return false; diff --git a/Barotrauma/BarotraumaServer/Source/DebugConsole.cs b/Barotrauma/BarotraumaServer/Source/DebugConsole.cs index 84f035900..307399111 100644 --- a/Barotrauma/BarotraumaServer/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/Source/DebugConsole.cs @@ -131,6 +131,14 @@ namespace Barotrauma GameMain.Server.EndGame(); })); + commands.Add(new Command("autorestart", "autorestart: Toggle automatic round restarting on/off.", (string[] args) => + { + if (GameMain.Server == null) return; + + GameMain.Server.AutoRestart = !GameMain.Server.AutoRestart; + NewMessage(GameMain.Server.AutoRestart ? "Automatic restart enabled." : "Automatic restart disabled.", Color.White); + })); + commands.Add(new Command("entitydata", "", (string[] args) => { if (args.Length == 0) return; diff --git a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs index eb38274fe..fec268bcb 100644 --- a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs @@ -249,6 +249,36 @@ namespace Barotrauma NewMessage("Crew AI enabled", Color.White); })); + commands.Add(new Command("autorestartinterval", "autorestartinterval [seconds]: Set how long the server waits between rounds before automatically starting a new one.", (string[] args) => + { + if (GameMain.Server == null) return; + if (args.Length > 0) + { + int parsedInt = 0; + if (int.TryParse(args[0], out parsedInt) && parsedInt >= 0) + { + GameMain.Server.AutoRestartInterval = parsedInt; + NewMessage("Autorestart interval set to " + GameMain.Server.AutoRestartInterval + " seconds.", Color.White); + } + } + })); + + + commands.Add(new Command("autorestarttimer", "autorestarttimer [seconds]: Set the current autorestart countdown to the specified value.", (string[] args) => + { + if (GameMain.Server == null) return; + if (args.Length > 0) + { + int parsedInt = 0; + if (int.TryParse(args[0], out parsedInt) && parsedInt >= 0) + { + GameMain.Server.AutoRestartTimer = parsedInt; + GameMain.NetLobbyScreen.LastUpdateID++; + NewMessage("Autorestart timer set to " + GameMain.Server.AutoRestartTimer + " seconds.", Color.White); + } + } + })); + commands.Add(new Command("kick", "kick [name]: Kick a player out of the server.", (string[] args) => { if (GameMain.NetworkMember == null || args.Length == 0) return; diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs index 74b67a412..406776630 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs @@ -408,10 +408,10 @@ namespace Barotrauma.Networking initiatedStartGame = false; } } - else if (autoRestart && Screen.Selected == GameMain.NetLobbyScreen && connectedClients.Count>0) + else if (autoRestart && Screen.Selected == GameMain.NetLobbyScreen && connectedClients.Count > 0) { - AutoRestartTimer -= deltaTime; - if (AutoRestartTimer < 0.0f && GameMain.NetLobbyScreen.StartButtonEnabled) + AutoRestartTimer -= deltaTime; + if (AutoRestartTimer < 0.0f && !initiatedStartGame) { StartGame(); } @@ -1057,6 +1057,7 @@ namespace Barotrauma.Networking private IEnumerable InitiateStartGame(Submarine selectedSub, Submarine selectedShuttle, GameModePreset selectedMode) { + initiatedStartGame = true; GameMain.NetLobbyScreen.StartButtonEnabled = false; if (connectedClients.Any()) @@ -1113,9 +1114,7 @@ namespace Barotrauma.Networking } private IEnumerable StartGame(Submarine selectedSub, Submarine selectedShuttle, GameModePreset selectedMode) - { - initiatedStartGame = true; - + { entityEventManager.Clear(); GameMain.NetLobbyScreen.StartButtonEnabled = false; @@ -1353,7 +1352,12 @@ namespace Barotrauma.Networking Mission mission = GameMain.GameSession.Mission; GameMain.GameSession.gameMode.End(endMessage); - if (autoRestart) AutoRestartTimer = AutoRestartInterval; + if (autoRestart) + { + AutoRestartTimer = AutoRestartInterval; + //send a netlobby update to get the clients' autorestart timers up to date + GameMain.NetLobbyScreen.LastUpdateID++; + } if (SaveServerLogs) log.Save(); diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs index 20f86514c..265b47857 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs @@ -120,7 +120,7 @@ namespace Barotrauma.Networking public float AutoRestartInterval { get; - private set; + set; } [HasDefaultValue(true, true)] @@ -166,7 +166,7 @@ namespace Barotrauma.Networking public bool AutoRestart { - get { return (connectedClients.Count != 0) && autoRestart; } + get { return autoRestart; } set { autoRestart = value; @@ -236,6 +236,8 @@ namespace Barotrauma.Networking doc.Root.SetAttributeValue("maxplayers", maxPlayers); doc.Root.SetAttributeValue("enableupnp", config.EnableUPnP); + doc.Root.SetAttributeValue("autorestart", autoRestart); + doc.Root.SetAttributeValue("SubSelection", subSelectionMode.ToString()); doc.Root.SetAttributeValue("ModeSelection", modeSelectionMode.ToString()); @@ -279,6 +281,14 @@ namespace Barotrauma.Networking ObjectProperties = ObjectProperty.InitProperties(this, doc.Root); + AutoRestart = ToolBox.GetAttributeBool(doc.Root, "autorestart", false); +#if CLIENT + if (autoRestart) + { + GameMain.NetLobbyScreen.SetAutoRestart(autoRestart, AutoRestartInterval); + } +#endif + subSelectionMode = SelectionMode.Manual; Enum.TryParse(ToolBox.GetAttributeString(doc.Root, "SubSelection", "Manual"), out subSelectionMode); Voting.AllowSubVoting = subSelectionMode == SelectionMode.Vote; diff --git a/Barotrauma/BarotraumaShared/Source/Screens/NetLobbyScreen.cs b/Barotrauma/BarotraumaShared/Source/Screens/NetLobbyScreen.cs index 952087fc9..6a24009e4 100644 --- a/Barotrauma/BarotraumaShared/Source/Screens/NetLobbyScreen.cs +++ b/Barotrauma/BarotraumaShared/Source/Screens/NetLobbyScreen.cs @@ -12,7 +12,7 @@ namespace Barotrauma public UInt16 LastUpdateID { get { if (GameMain.Server != null && lastUpdateID < 1) lastUpdateID++; return lastUpdateID; } - set { if (GameMain.Server != null) return; lastUpdateID = value; } + set { lastUpdateID = value; } } //for guitextblock delegate @@ -23,20 +23,6 @@ namespace Barotrauma private string levelSeed = ""; - private float autoRestartTimer; - - public string AutoRestartText() - { - if (GameMain.Server != null) - { - if (!GameMain.Server.AutoRestart) return ""; - return "Restarting in " + ToolBox.SecondsToReadableTime(Math.Max(GameMain.Server.AutoRestartTimer, 0)); - } - - if (autoRestartTimer == 0.0f) return ""; - return "Restarting in " + ToolBox.SecondsToReadableTime(Math.Max(autoRestartTimer, 0)); - } - public void ToggleTraitorsEnabled(int dir) { if (GameMain.Server == null) return;