Example #1
0
        public CncServerBrowserLogic([ObjectCreator.Param] Widget widget,
		                            [ObjectCreator.Param] Action openLobby,
		                            [ObjectCreator.Param] Action onExit)
        {
            var panel = widget.GetWidget("SERVERBROWSER_PANEL");
            var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");

            // Menu buttons
            var refreshButton = panel.GetWidget<ButtonWidget>("REFRESH_BUTTON");
            refreshButton.IsDisabled = () => searchStatus == SearchStatus.Fetching;
            refreshButton.OnClick = () =>
            {
                searchStatus = SearchStatus.Fetching;
                sl.RemoveChildren();
                currentServer = null;
                ServerList.Query(games => RefreshServerList(panel, games));
            };

            var join = panel.GetWidget<ButtonWidget>("JOIN_BUTTON");
            join.IsDisabled = () => currentServer == null || !currentServer.CanJoin();
            join.OnClick = () =>
            {
                if (currentServer == null)
                    return;

                var host = currentServer.Address.Split(':')[0];
                var port = int.Parse(currentServer.Address.Split(':')[1]);

                Widget.CloseWindow();
                CncConnectingLogic.Connect(host, port, openLobby, onExit);
            };

            panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };

            // Server list
            serverTemplate = sl.GetWidget<ScrollItemWidget>("SERVER_TEMPLATE");

            // Display the progress label over the server list
            // The text is only visible when the list is empty
            var progressText = panel.GetWidget<LabelWidget>("PROGRESS_LABEL");
            progressText.IsVisible = () => searchStatus != SearchStatus.Hidden;
            progressText.GetText = ProgressLabelText;

            // Map preview
            var preview = panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW");
            preview.Map = () => CurrentMap();
            preview.IsVisible = () => CurrentMap() != null;

            // Server info
            var infoPanel = panel.GetWidget("SERVER_INFO");
            infoPanel.IsVisible = () => currentServer != null;
            infoPanel.GetWidget<LabelWidget>("SERVER_IP").GetText = () => currentServer.Address;
            infoPanel.GetWidget<LabelWidget>("SERVER_MODS").GetText = () => ServerBrowserLogic.GenerateModsLabel(currentServer);
            infoPanel.GetWidget<LabelWidget>("MAP_TITLE").GetText = () => (CurrentMap() != null) ? CurrentMap().Title : "Unknown";
            infoPanel.GetWidget<LabelWidget>("MAP_PLAYERS").GetText = () => GetPlayersLabel(currentServer);

            ServerList.Query(games => RefreshServerList(panel, games));
        }
Example #2
0
        public ServerBrowserLogic(Widget widget, Action openLobby, Action onExit)
        {
            var panel = widget;
            var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");

            // Menu buttons
            var refreshButton = panel.GetWidget<ButtonWidget>("REFRESH_BUTTON");
            refreshButton.IsDisabled = () => searchStatus == SearchStatus.Fetching;
            refreshButton.OnClick = () =>
            {
                searchStatus = SearchStatus.Fetching;
                sl.RemoveChildren();
                currentServer = null;
                ServerList.Query(games => RefreshServerList(panel, games));
            };

            var join = panel.GetWidget<ButtonWidget>("JOIN_BUTTON");
            join.IsDisabled = () => currentServer == null || !currentServer.CanJoin();
            join.OnClick = () =>
            {
                if (currentServer == null)
                    return;

                var host = currentServer.Address.Split(':')[0];
                var port = int.Parse(currentServer.Address.Split(':')[1]);

                Ui.CloseWindow();
                ConnectionLogic.Connect(host, port, openLobby, onExit);
            };

            panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };

            // Server list
            serverTemplate = sl.GetWidget<ScrollItemWidget>("SERVER_TEMPLATE");

            // Display the progress label over the server list
            // The text is only visible when the list is empty
            var progressText = panel.GetWidget<LabelWidget>("PROGRESS_LABEL");
            progressText.IsVisible = () => searchStatus != SearchStatus.Hidden;
            progressText.GetText = ProgressLabelText;

            ServerList.Query(games => RefreshServerList(panel, games));
        }
Example #3
0
 public static string GenerateModsLabel(GameServer s)
 {
     return s.UsefulMods.Select(m => GenerateModLabel(m)).JoinWith("\n");
 }
Example #4
0
        string GetStateLabel(GameServer game)
        {
            if (game == null)
                return "";

            if (game.State == 1) return "Waiting for players";
            if (game.State == 2) return "Playing";
            else return "Unknown";
        }
Example #5
0
        string GetPlayersLabel(GameServer game)
        {
            if (game == null || game.Players == 0)
                return "";

            var map = Game.modData.FindMapByUid(game.Map);

            var maxPlayers = map == null ? "?" : (object)map.PlayerCount;
            return "{0} / {1}".F(game.Players, maxPlayers);
        }
Example #6
0
 Map GetMapPreview(GameServer game)
 {
     return (game == null) ? null : Game.modData.FindMapByUid(game.Map);
 }
Example #7
0
        public void RefreshServerList(Widget panel, IEnumerable<GameServer> games)
        {
            var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");

            sl.RemoveChildren();
            currentServer = null;

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

            if (games.Count() == 0)
            {
                searchStatus = SearchStatus.NoGames;
                return;
            }

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

            foreach (var loop in games)
            {
                var game = loop;

                var canJoin = game.CanJoin();

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

                var preview = item.GetWidget<MapPreviewWidget>("MAP_PREVIEW");
                preview.Map = () => GetMapPreview(game);
                preview.IsVisible = () => GetMapPreview(game) != null;

                var title = item.GetWidget<LabelWidget>("TITLE");
                title.GetText = () => game.Name;

                // TODO: Use game.MapTitle once the server supports it
                var maptitle = item.GetWidget<LabelWidget>("MAP");
                maptitle.GetText = () =>
                {
                    var map = Game.modData.FindMapByUid(game.Map);
                    return map == null ? "Unknown Map" : map.Title;
                };

                // TODO: Use game.MaxPlayers once the server supports it
                var players = item.GetWidget<LabelWidget>("PLAYERS");
                players.GetText = () => GetPlayersLabel(game);

                var state = item.GetWidget<LabelWidget>("STATE");
                state.GetText = () => GetStateLabel(game);

                var ip = item.GetWidget<LabelWidget>("IP");
                ip.GetText = () => game.Address;

                var version = item.GetWidget<LabelWidget>("VERSION");
                version.GetText = () => GenerateModsLabel(game);
                version.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;
                }

                sl.AddChild(item);
            }
        }
        public ServerBrowserLogic( [ObjectCreator.Param] Widget widget )
        {
            var bg = widget.GetWidget("JOINSERVER_BG");

            bg.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
            bg.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Fetching game list...";

            ServerList.Query(RefreshServerList);

            bg.GetWidget("SERVER_INFO").IsVisible = () => currentServer != null;
            var preview = bg.GetWidget<MapPreviewWidget>("MAP_PREVIEW");
            preview.Map = () => CurrentMap();
            preview.IsVisible = () => CurrentMap() != null;

            bg.GetWidget<LabelWidget>("SERVER_IP").GetText = () => currentServer.Address;
            bg.GetWidget<LabelWidget>("SERVER_MODS").GetText = () => GenerateModsLabel(currentServer);
            bg.GetWidget<LabelWidget>("MAP_TITLE").GetText = () => (CurrentMap() != null) ? CurrentMap().Title : "Unknown";
            bg.GetWidget<LabelWidget>("MAP_PLAYERS").GetText = () =>
            {
                if (currentServer == null)
                    return "";
                string ret = currentServer.Players.ToString();
                if (CurrentMap() != null)
                    ret += "/" + CurrentMap().PlayerCount.ToString();
                return ret;
            };

            var sl = bg.GetWidget<ScrollPanelWidget>("SERVER_LIST");
            ServerTemplate = sl.GetWidget<ScrollItemWidget>("SERVER_TEMPLATE");

            bg.GetWidget<ButtonWidget>("REFRESH_BUTTON").OnClick = () =>
            {
                bg.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
                bg.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Fetching game list...";
                sl.RemoveChildren();
                currentServer = null;

                ServerList.Query(RefreshServerList);
            };

            bg.GetWidget<ButtonWidget>("CANCEL_BUTTON").OnClick = () => Widget.CloseWindow();
            bg.GetWidget<ButtonWidget>("DIRECTCONNECT_BUTTON").OnClick = () =>
            {
                Widget.CloseWindow();
                Widget.OpenWindow("DIRECTCONNECT_BG");
            };

            bg.GetWidget<ButtonWidget>("JOIN_BUTTON").OnClick = () =>
            {
                if (currentServer == null)
                    return;

                Widget.CloseWindow();
                Game.JoinServer(currentServer.Address.Split(':')[0], int.Parse(currentServer.Address.Split(':')[1]));
            };
        }
        void RefreshServerList(IEnumerable<GameServer> games)
        {
            var r = Widget.RootWidget;
            var bg = r.GetWidget("JOINSERVER_BG");

            if (bg == null) // We got a MasterServer reply AFTER the browser is gone, just return to prevent crash - Gecko
                return;

            var sl = bg.GetWidget<ScrollPanelWidget>("SERVER_LIST");

            sl.RemoveChildren();
            currentServer = null;

            if (games == null)
            {
                r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
                r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "Failed to contact master server.";
                return;
            }

            var gamesWaiting = games.Where(g => CanJoin(g));

            if (gamesWaiting.Count() == 0)
            {
                r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = true;
                r.GetWidget<LabelWidget>("JOINSERVER_PROGRESS_TITLE").Text = "No games found.";
                return;
            }

            r.GetWidget("JOINSERVER_PROGRESS_TITLE").Visible = false;

            int i = 0;
            foreach (var loop in gamesWaiting)
            {
                var game = loop;
                var item = ScrollItemWidget.Setup(ServerTemplate, () => currentServer == game, () => currentServer = game);
                item.GetWidget<LabelWidget>("TITLE").GetText = () => "{0} ({1})".F(game.Name, game.Address);
                sl.AddChild(item);
                if (i == 0) currentServer = game;
                i++;
            }
        }
 public static string GenerateModsLabel(GameServer s)
 {
     return string.Join("\n", s.UsefulMods
         .Select(m =>
            Mod.AllMods.ContainsKey(m.Key) ? string.Format("{0} ({1})", Mod.AllMods[m.Key].Title, m.Value)
                                        : string.Format("Unknown Mod: {0}",m.Key)).ToArray());
 }
        public static bool CanJoin(GameServer game)
        {
            //"waiting for players"
            if (game.State != 1)
                return false;

            // Mods won't match if there are a different number
            if (Game.CurrentMods.Count != game.Mods.Count())
                return false;

            return game.Mods.All( m => m.Contains('@')) &&  game.Mods.Select( m => Pair.New(m.Split('@')[0], m.Split('@')[1]))
                .All(kv => Game.CurrentMods.ContainsKey(kv.First) &&
                     (kv.Second == "{DEV_VERSION}" || Game.CurrentMods[kv.First].Version == "{DEV_VERSION}" || kv.Second == Game.CurrentMods[kv.First].Version));
        }
Example #12
0
        string GetPlayersLabel(GameServer game)
        {
            if (game == null)
                return "";

            var map = Game.modData.FindMapByUid(game.Map);
            return map == null ? "{0}".F(currentServer.Players) : "{0} / {1}".F(currentServer.Players, map.PlayerCount);
        }
Example #13
0
        public void RefreshServerList(Widget panel, IEnumerable<GameServer> games)
        {
            var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");

            sl.RemoveChildren();
            currentServer = null;

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

            var gamesWaiting = games.Where(g => g.CanJoin());

            if (gamesWaiting.Count() == 0)
            {
                searchStatus = SearchStatus.NoGames;
                return;
            }

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

            foreach (var loop in gamesWaiting)
            {
                var game = loop;

                var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game);
                item.GetWidget<LabelWidget>("TITLE").GetText = () => game.Name;
                // TODO: Use game.MapTitle once the server supports it
                item.GetWidget<LabelWidget>("MAP").GetText = () =>
                {
                    var map = Game.modData.FindMapByUid(game.Map);
                    return map == null ? "Unknown" : map.Title;
                };
                // TODO: Use game.MaxPlayers once the server supports it
                item.GetWidget<LabelWidget>("PLAYERS").GetText = () => GetPlayersLabel(game);
                item.GetWidget<LabelWidget>("IP").GetText = () => game.Address;
                sl.AddChild(item);
            }
        }