From d313317cf5d02bf202e7ff74e629cd75b7838ace Mon Sep 17 00:00:00 2001 From: huwpascoe Date: Fri, 21 Nov 2014 02:09:09 +0000 Subject: [PATCH] Cleaned up the server browser --- OpenRA.Game/Network/GameServer.cs | 7 +- .../ServerTraits/MasterServerPinger.cs | 3 +- .../Widgets/Logic/ServerBrowserLogic.cs | 108 +++--------------- 3 files changed, 23 insertions(+), 95 deletions(-) diff --git a/OpenRA.Game/Network/GameServer.cs b/OpenRA.Game/Network/GameServer.cs index f77e1b5885..4e6d565ea7 100644 --- a/OpenRA.Game/Network/GameServer.cs +++ b/OpenRA.Game/Network/GameServer.cs @@ -17,11 +17,14 @@ namespace OpenRA.Network public readonly string Address = null; public readonly int State = 0; public readonly int Players = 0; + public readonly int MaxPlayers = 0; + public readonly int Bots = 0; + public readonly int Spectators = 0; public readonly string Map = null; - - // Retained name compatibility with the master server public readonly string Mods = ""; public readonly int TTL = 0; + public readonly bool Protected = false; + public readonly string Started = null; public bool CanJoin() { diff --git a/OpenRA.Mods.Common/ServerTraits/MasterServerPinger.cs b/OpenRA.Mods.Common/ServerTraits/MasterServerPinger.cs index 73f0edab28..9920780207 100644 --- a/OpenRA.Mods.Common/ServerTraits/MasterServerPinger.cs +++ b/OpenRA.Mods.Common/ServerTraits/MasterServerPinger.cs @@ -58,6 +58,7 @@ namespace OpenRA.Mods.Common.Server var numPlayers = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null && c1.Slot != null).Count(); var numBots = server.LobbyInfo.Clients.Where(c1 => c1.Bot != null).Count(); var numSpectators = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null && c1.Slot == null).Count(); + var numSlots = server.LobbyInfo.Slots.Where(s => !s.Value.Closed).Count() - numBots; var passwordProtected = string.IsNullOrEmpty(server.Settings.Password) ? 0 : 1; var clients = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null).Select(c => Convert.ToBase64String(Encoding.UTF8.GetBytes(c.Name))).ToArray(); @@ -79,7 +80,7 @@ namespace OpenRA.Mods.Common.Server numBots, "{0}@{1}".F(mod.Id, mod.Version), server.LobbyInfo.GlobalSettings.Map, - server.Map.PlayerCount, + numSlots, numSpectators, passwordProtected, string.Join(",", clients))); diff --git a/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs index 65da1d1834..2ef848bf17 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs @@ -138,10 +138,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic Game.RunAfterTick(() => RefreshServerListInner(games)); }; - currentQuery = new Download(Game.Settings.Server.MasterServer + "list", _ => {}, onComplete); + currentQuery = new Download(Game.Settings.Server.MasterServer + "games", _ => {}, onComplete); } - public void RefreshServerListInner(IEnumerable games) + void RefreshServerListInner(IEnumerable games) { if (games == null) return; @@ -166,7 +166,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic var title = item.GetOrNull("TITLE"); if (title != null) { - title.GetText = () => game.Name; + title.GetText = () => game.Protected ? ("(Password) " + game.Name) : game.Name; title.GetColor = () => canJoin ? title.TextColor : Color.Gray; } @@ -180,7 +180,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic var players = item.GetOrNull("PLAYERS"); if (players != null) { - players.GetText = () => "{0} / {1}".F(game.Players, map.PlayerCount); + players.GetText = () => "{0} / {1}".F(game.Players, game.MaxPlayers) + + (game.Spectators > 0 ? " ({0} Spectator{1})".F(game.Spectators, game.Spectators > 1 ? "s" : "") : ""); players.GetColor = () => canJoin ? players.TextColor : Color.Gray; } @@ -287,15 +288,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic ConnectionLogic.Connect(host, port, "", OpenLobby, DoNothing); } - static string GetPlayersLabel(GameServer game) - { - if (game == null || game.Players == 0) - return ""; - - var map = Game.modData.MapCache[game.Map]; - return "{0} / {1}".F(game.Players, map.PlayerCount == 0 ? "?" : map.PlayerCount.ToString()); - } - static string GetStateLabel(GameServer game) { if (game == null) @@ -304,7 +296,17 @@ namespace OpenRA.Mods.RA.Widgets.Logic if (game.State == (int)ServerState.WaitingPlayers) return "Waiting for players"; if (game.State == (int)ServerState.GameStarted) - return "Playing"; + { + try + { + var runTime = DateTime.Now - System.DateTime.Parse(game.Started); + return "In progress for {0} minute{1}".F(runTime.Minutes, runTime.Minutes > 1 ? "s" : ""); + } + catch (Exception) + { + return "In progress"; + } + } if (game.State == (int)ServerState.ShuttingDown) return "Server shutting down"; @@ -338,83 +340,5 @@ namespace OpenRA.Mods.RA.Widgets.Logic return false; } - - public void RefreshServerList(Widget panel, IEnumerable games) - { - var sl = panel.Get("SERVER_LIST"); - - searchStatus = SearchStatus.Fetching; - - sl.RemoveChildren(); - currentServer = null; - - if (games == null) - { - searchStatus = SearchStatus.Failed; - return; - } - - if (!games.Any()) - { - searchStatus = SearchStatus.NoGames; - return; - } - - searchStatus = SearchStatus.Hidden; - currentServer = games.FirstOrDefault(); - - foreach (var loop in games.OrderByDescending(g => g.CanJoin()).ThenByDescending(g => g.Players)) - { - var game = loop; - - var canJoin = game.CanJoin(); - - var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game, () => Join(game)); - - var map = Game.modData.MapCache[game.Map]; - var preview = item.Get("MAP_PREVIEW"); - preview.Preview = () => map; - - var title = item.Get("TITLE"); - title.GetText = () => game.Name; - - // TODO: Use game.MapTitle once the server supports it - var maptitle = item.Get("MAP"); - maptitle.GetText = () => map.Title; - - // TODO: Use game.MaxPlayers once the server supports it - var players = item.Get("PLAYERS"); - players.GetText = () => GetPlayersLabel(game); - - var state = item.Get("STATE"); - state.GetText = () => GetStateLabel(game); - - var ip = item.Get("IP"); - ip.GetText = () => game.Address; - - var version = item.Get("VERSION"); - version.GetText = () => GenerateModLabel(game); - version.IsVisible = () => !game.CompatibleVersion(); - - var location = item.Get("LOCATION"); - var cachedServerLocation = LobbyUtils.LookupCountry(game.Address.Split(':')[0]); - location.GetText = () => cachedServerLocation; - location.IsVisible = () => game.CompatibleVersion(); - - if (!canJoin) - { - title.GetColor = () => Color.Gray; - maptitle.GetColor = () => Color.Gray; - players.GetColor = () => Color.Gray; - state.GetColor = () => Color.Gray; - ip.GetColor = () => Color.Gray; - version.GetColor = () => Color.Gray; - location.GetColor = () => Color.Gray; - } - - if (!Filtered(game)) - sl.AddChild(item); - } - } } }