From 9425fb1def3421a8eca11e5d59e0103ca03f3796 Mon Sep 17 00:00:00 2001 From: alzeih Date: Mon, 14 Jun 2010 12:49:01 +1200 Subject: [PATCH] Fix unnecessary duplication and get a better UI out of it --- OpenRA.Game/Server/Server.cs | 117 +++++------------- .../Widgets/Delegates/LobbyDelegate.cs | 36 +++--- 2 files changed, 47 insertions(+), 106 deletions(-) diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 3310641295..f5663fb6ba 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -264,19 +264,7 @@ namespace OpenRA.Server }}, { "name", s => - { - if (GetClient(conn).State == Session.ClientState.Ready) - { - SendChatTo( conn, "You can't change your name once you are marked as ready" ); - return true; - } - - if (GameStarted) - { - SendChatTo( conn, "You can't change your name after the game has started" ); - return true; - } - + { if (s.Trim() == "") { SendChatTo( conn, "Blank names are not permitted." ); @@ -302,19 +290,7 @@ namespace OpenRA.Server }}, { "race", s => - { - if (GetClient(conn).State == Session.ClientState.Ready) - { - SendChatTo( conn, "You can't change your race once you are marked as ready" ); - return true; - } - - if (GameStarted) - { - SendChatTo( conn, "You can't change your race after the game has started" ); - return true; - } - + { GetClient(conn).Country = s; SyncLobbyInfo(); return true; @@ -322,18 +298,6 @@ namespace OpenRA.Server { "team", s => { - if (GetClient(conn).State == Session.ClientState.Ready) - { - SendChatTo( conn, "You can't change your team once you are marked as ready" ); - return true; - } - - if (GameStarted) - { - SendChatTo( conn, "You can't change your team after the game has started" ); - return true; - } - int team; if (!int.TryParse(s, out team)) { Console.WriteLine("Invalid team: {0}", s ); return false; } @@ -344,18 +308,6 @@ namespace OpenRA.Server { "spawn", s => { - if (GetClient(conn).State == Session.ClientState.Ready) - { - SendChatTo( conn, "You can't change your spawn point once you are marked as ready" ); - return true; - } - - if (GameStarted) - { - SendChatTo( conn, "You can't change your spawn point after the game has started" ); - return true; - } - int spawnPoint; if (!int.TryParse(s, out spawnPoint) || spawnPoint < 0 || spawnPoint > 8) //TODO: SET properly! { @@ -376,17 +328,6 @@ namespace OpenRA.Server { "pal", s => { - if (GetClient(conn).State == Session.ClientState.Ready) - { - SendChatTo( conn, "You can't change your color once you are marked as ready" ); - return true; - } - - if (GameStarted) - { - SendChatTo( conn, "You can't change your color after the game has started" ); - return true; - } int pali; if (!int.TryParse(s, out pali)) @@ -413,16 +354,12 @@ namespace OpenRA.Server SendChatTo( conn, "Only the host can change the map" ); return true; } - - if (GameStarted) - { - SendChatTo( conn, "You can't change the map after the game has started" ); - return true; - } - lobbyInfo.GlobalSettings.Map = s; foreach(var client in lobbyInfo.Clients) + { client.SpawnPoint = 0; + client.State = Session.ClientState.NotReady; + } SyncLobbyInfo(); return true; @@ -430,12 +367,6 @@ namespace OpenRA.Server { "addpkg", s => { - if (GameStarted) - { - SendChatTo( conn, "You can't change packages after the game has started" ); - return true; - } - Console.WriteLine("** Added package: `{0}`", s); try { @@ -455,11 +386,6 @@ namespace OpenRA.Server { "mods", s => { - if (GameStarted) - { - SendChatTo( conn, "You can't change mods after the game has started" ); - return true; - } var args = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); lobbyInfo.GlobalSettings.Mods = args.GetRange(0,args.Count - 1).ToArray(); lobbyInfo.GlobalSettings.Map = args.Last(); @@ -495,20 +421,35 @@ namespace OpenRA.Server switch (so.Name) { case "Chat": - if (so.Data.StartsWith("/")) - { - if (!InterpretCommand(conn, so.Data.Substring(1))) - { - Console.WriteLine("Bad server command: {0}", so.Data.Substring(1)); - SendChatTo(conn, "Bad server command."); - } - } - else + if (!HandleAsCommand(conn, so)) + foreach (var c in conns.Except(conn).ToArray()) + DispatchOrdersToClient(c, GetClient(conn).Index, 0, so.Serialize()); + break; + //TODO: send to those with the same team + case "TeamChat": + if (!HandleAsCommand(conn, so)) foreach (var c in conns.Except(conn).ToArray()) DispatchOrdersToClient(c, GetClient(conn).Index, 0, so.Serialize()); break; } } + + static bool HandleAsCommand(Connection conn, ServerOrder so) + { + if (!so.Data.StartsWith("/")) return false; + + var cmd = so.Data.Substring(1); + if(GameStarted) + SendChatTo(conn, "Cannot change state when game started."); + else if (GetClient(conn).State == Session.ClientState.Ready && cmd != "ready") + SendChatTo(conn, "Cannot change state when marked as ready."); + else if (!InterpretCommand(conn, cmd)) + { + Console.WriteLine("Bad server command: {0}", cmd); + SendChatTo(conn, "Bad server command."); + } + return true; + } static Session.Client GetClient(Connection conn) { diff --git a/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs b/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs index 624b40e9d3..f9ef21e419 100644 --- a/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs @@ -63,14 +63,12 @@ namespace OpenRA.Widgets.Delegates foreach(var client in Game.LobbyInfo.Clients) { var c = client; - var template = (client.Index == Game.LocalClient.Index)? LocalPlayerTemplate.Clone() : RemotePlayerTemplate.Clone(); + Widget template; - template.Id = "PLAYER_{0}".F(c.Index); - template.Parent = Players; - template.GetWidget("NAME").GetText = () => c.Name; - - if(client.Index == Game.LocalClient.Index) + if(client.Index == Game.LocalClient.Index && c.State != Session.ClientState.Ready) { + template = LocalPlayerTemplate.Clone(); + var color = template.GetWidget("COLOR"); color.OnMouseUp = CyclePalette; @@ -95,6 +93,8 @@ namespace OpenRA.Widgets.Delegates } else { + template = RemotePlayerTemplate.Clone(); + var color = template.GetWidget("COLOR"); color.GetColor = () => Game.world.PlayerColors()[c.PaletteIndex % Game.world.PlayerColors().Count].Color; @@ -110,8 +110,13 @@ namespace OpenRA.Widgets.Delegates var status = template.GetWidget("STATUS"); status.Checked = () => c.State == Session.ClientState.Ready; + if (client.Index == Game.LocalClient.Index) status.OnMouseDown = CycleReady; } + template.Id = "PLAYER_{0}".F(c.Index); + template.Parent = Players; + template.GetWidget("NAME").GetText = () => c.Name; + template.Bounds = new Rectangle(0, offset, template.Bounds.Width, template.Bounds.Height); template.IsVisible = () => true; Players.AddChild(template); @@ -124,9 +129,7 @@ namespace OpenRA.Widgets.Delegates bool SpawnPointAvailable(int index) { return (index == 0) || Game.LobbyInfo.Clients.All(c => c.SpawnPoint != index); } bool CyclePalette(MouseInput mi) - { - if (Game.LocalClient.State == Session.ClientState.Ready) return false; - + { var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.PlayerColors().Count() - 1; var newIndex = ((int)Game.LocalClient.PaletteIndex + d) % Game.world.PlayerColors().Count(); @@ -141,9 +144,7 @@ namespace OpenRA.Widgets.Delegates } bool CycleRace(MouseInput mi) - { - if (Game.LocalClient.State == Session.ClientState.Ready) return false; - + { var countries = new[] { "Random" }.Concat(Game.world.GetCountries().Select(c => c.Name)); if (mi.Button == MouseButton.Right) @@ -164,14 +165,15 @@ namespace OpenRA.Widgets.Delegates bool CycleReady(MouseInput mi) { + //HACK: Can't set this as part of the fuction as LocalClient/State not initalised yet + Chrome.rootWidget.GetWidget("SERVER_LOBBY").GetWidget("CHANGEMAP_BUTTON").Visible + = (Game.IsHost && Game.LocalClient.State == Session.ClientState.Ready); Game.IssueOrder(Order.Chat("/ready")); return true; } bool CycleSpawnPoint(MouseInput mi) - { - if (Game.LocalClient.State == Session.ClientState.Ready) return false; - + { var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.SpawnPoints.Count(); var newIndex = (Game.LocalClient.SpawnPoint + d) % (Game.world.Map.SpawnPoints.Count()+1); @@ -185,9 +187,7 @@ namespace OpenRA.Widgets.Delegates } bool CycleTeam(MouseInput mi) - { - if (Game.LocalClient.State == Session.ClientState.Ready) return false; - + { var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.PlayerCount; var newIndex = (Game.LocalClient.Team + d) % (Game.world.Map.PlayerCount+1);