Example #1
0
        void RefreshServerListInner(IEnumerable <GameServer> games)
        {
            if (games == null)
            {
                return;
            }

            var mods = games.GroupBy(g => g.Mods)
                       .OrderByDescending(g => GroupSortOrder(g.First()))
                       .ThenByDescending(g => g.Count());

            var rows = new List <Widget>();

            foreach (var modGames in mods)
            {
                if (modGames.All(Filtered))
                {
                    continue;
                }

                var header = ScrollItemWidget.Setup(headerTemplate, () => true, () => { });

                var headerTitle = modGames.First().ModLabel;
                header.Get <LabelWidget>("LABEL").GetText = () => headerTitle;
                rows.Add(header);

                foreach (var loop in modGames.OrderByDescending(g => g.IsJoinable).ThenByDescending(g => g.Players))
                {
                    var game = loop;
                    if (game == null || Filtered(game))
                    {
                        continue;
                    }

                    var canJoin    = game.IsJoinable;
                    var compatible = game.IsCompatible;

                    var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game, () => Join(game));

                    var map     = Game.ModData.MapCache[game.Map];
                    var preview = item.GetOrNull <MapPreviewWidget>("MAP_PREVIEW");
                    if (preview != null)
                    {
                        preview.Preview = () => map;
                    }

                    var title = item.GetOrNull <LabelWidget>("TITLE");
                    if (title != null)
                    {
                        title.GetText  = () => game.Name;
                        title.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : title.TextColor;
                    }

                    var maptitle = item.GetOrNull <LabelWidget>("MAP");
                    if (title != null)
                    {
                        maptitle.GetText  = () => map.Title;
                        maptitle.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : maptitle.TextColor;
                    }

                    var players = item.GetOrNull <LabelWidget>("PLAYERS");
                    if (players != null)
                    {
                        players.GetText = () => "{0} / {1}".F(game.Players, game.MaxPlayers)
                                          + (game.Spectators > 0 ? "  ({0} Spectator{1})".F(game.Spectators, game.Spectators > 1 ? "s" : "") : "");
                        players.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : players.TextColor;
                    }

                    var state = item.GetOrNull <LabelWidget>("STATE");
                    if (state != null)
                    {
                        state.GetText  = () => GetStateLabel(game);
                        state.GetColor = () => GetStateColor(game, state, !compatible || !canJoin);
                    }

                    var ip = item.GetOrNull <LabelWidget>("IP");
                    if (ip != null)
                    {
                        ip.GetText  = () => game.Address;
                        ip.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : ip.TextColor;
                    }

                    var location = item.GetOrNull <LabelWidget>("LOCATION");
                    if (location != null)
                    {
                        var cachedServerLocation = LobbyUtils.LookupCountry(game.Address.Split(':')[0]);
                        location.GetText  = () => cachedServerLocation;
                        location.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : location.TextColor;
                    }

                    rows.Add(item);
                }
            }

            Game.RunAfterTick(() =>
            {
                serverList.RemoveChildren();
                currentServer = null;

                if (games == null)
                {
                    searchStatus = SearchStatus.Failed;
                    return;
                }

                if (!games.Any())
                {
                    searchStatus = SearchStatus.NoGames;
                    return;
                }

                currentServer = games.FirstOrDefault();
                searchStatus  = SearchStatus.Hidden;

                // Search for any unknown maps
                if (Game.Settings.Game.AllowDownloading)
                {
                    Game.ModData.MapCache.QueryRemoteMapDetails(games.Where(g => !Filtered(g)).Select(g => g.Map));
                }

                foreach (var row in rows)
                {
                    serverList.AddChild(row);
                }
            });
        }
Example #2
0
        public ClientTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, OrderManager orderManager, int clientIndex)
        {
            var admin     = widget.Get <LabelWidget>("ADMIN");
            var adminFont = Game.Renderer.Fonts[admin.Font];

            var latency     = widget.Get <LabelWidget>("LATENCY");
            var latencyFont = Game.Renderer.Fonts[latency.Font];

            var latencyPrefix     = widget.Get <LabelWidget>("LATENCY_PREFIX");
            var latencyPrefixFont = Game.Renderer.Fonts[latencyPrefix.Font];

            var ip          = widget.Get <LabelWidget>("IP");
            var addressFont = Game.Renderer.Fonts[ip.Font];

            var location     = widget.Get <LabelWidget>("LOCATION");
            var locationFont = Game.Renderer.Fonts[location.Font];

            var locationOffset = location.Bounds.Y;
            var addressOffset  = ip.Bounds.Y;
            var latencyOffset  = latency.Bounds.Y;
            var tooltipHeight  = widget.Bounds.Height;

            var margin = widget.Bounds.Width;

            tooltipContainer.IsVisible    = () => (orderManager.LobbyInfo.ClientWithIndex(clientIndex) != null);
            tooltipContainer.BeforeRender = () =>
            {
                var latencyPrefixSize = latencyPrefix.Bounds.X + latencyPrefixFont.Measure(latencyPrefix.GetText() + " ").X;
                var locationWidth     = locationFont.Measure(location.GetText()).X;
                var adminWidth        = adminFont.Measure(admin.GetText()).X;
                var addressWidth      = addressFont.Measure(ip.GetText()).X;
                var latencyWidth      = latencyPrefixSize + latencyFont.Measure(latency.GetText()).X;
                var width             = Math.Max(locationWidth, Math.Max(adminWidth, Math.Max(addressWidth, latencyWidth)));
                widget.Bounds.Width   = width + 2 * margin;
                latency.Bounds.Width  = widget.Bounds.Width;
                ip.Bounds.Width       = widget.Bounds.Width;
                admin.Bounds.Width    = widget.Bounds.Width;
                location.Bounds.Width = widget.Bounds.Width;

                ip.Bounds.Y          = addressOffset;
                latency.Bounds.Y     = latencyOffset;
                location.Bounds.Y    = locationOffset;
                widget.Bounds.Height = tooltipHeight;

                if (admin.IsVisible())
                {
                    ip.Bounds.Y          += admin.Bounds.Height;
                    latency.Bounds.Y     += admin.Bounds.Height;
                    location.Bounds.Y    += admin.Bounds.Height;
                    widget.Bounds.Height += admin.Bounds.Height;
                }

                latencyPrefix.Bounds.Y = latency.Bounds.Y;
                latency.Bounds.X       = latencyPrefixSize;
            };

            admin.IsVisible = () => orderManager.LobbyInfo.ClientWithIndex(clientIndex).IsAdmin;
            var client = orderManager.LobbyInfo.ClientWithIndex(clientIndex);
            var ping   = orderManager.LobbyInfo.PingFromClient(client);

            latency.GetText  = () => LobbyUtils.LatencyDescription(ping);
            latency.GetColor = () => LobbyUtils.LatencyColor(ping);
            var address = orderManager.LobbyInfo.ClientWithIndex(clientIndex).IpAddress;

            if (clientIndex == orderManager.LocalClient.Index && UPnP.NatDevice != null &&
                address == IPAddress.Loopback.ToString())
            {
                address = UPnP.NatDevice.GetExternalIP().ToString();
            }
            var cachedDescriptiveIP = LobbyUtils.DescriptiveIpAddress(address);

            ip.GetText = () => cachedDescriptiveIP;
            var cachedCountryLookup = LobbyUtils.LookupCountry(address);

            location.GetText = () => cachedCountryLookup;
        }