예제 #1
0
        void UpdatePlayerList()
        {
            var idx = 0;

            foreach (var kv in orderManager.LobbyInfo.Slots)
            {
                var    key      = kv.Key;
                var    slot     = kv.Value;
                var    client   = orderManager.LobbyInfo.ClientInSlot(key);
                Widget template = null;

                // get template for possible reuse
                if (idx < Players.Children.Count)
                {
                    template = Players.Children[idx];
                }

                // Empty slot
                if (client == null)
                {
                    if (template == null || template.Id != EmptySlotTemplate.Id)
                    {
                        template = EmptySlotTemplate.Clone();
                    }

                    if (Game.IsHost)
                    {
                        LobbyUtils.SetupEditableSlotWidget(template, slot, client, orderManager);
                    }
                    else
                    {
                        LobbyUtils.SetupSlotWidget(template, slot, client);
                    }

                    var join = template.Get <ButtonWidget>("JOIN");
                    join.IsVisible  = () => !slot.Closed;
                    join.IsDisabled = () => orderManager.LocalClient.IsReady;
                    join.OnClick    = () => orderManager.IssueOrder(Order.Command("slot " + key));
                }

                // Editable player in slot
                else if ((client.Index == orderManager.LocalClient.Index) ||
                         (client.Bot != null && Game.IsHost))
                {
                    if (template == null || template.Id != EditablePlayerTemplate.Id)
                    {
                        template = EditablePlayerTemplate.Clone();
                    }

                    LobbyUtils.SetupClientWidget(template, slot, client, orderManager, client.Bot == null);

                    if (client.Bot != null)
                    {
                        LobbyUtils.SetupEditableSlotWidget(template, slot, client, orderManager);
                    }
                    else
                    {
                        LobbyUtils.SetupEditableNameWidget(template, slot, client, orderManager);
                    }

                    LobbyUtils.SetupEditableColorWidget(template, slot, client, orderManager, colorPreview);
                    LobbyUtils.SetupEditableFactionWidget(template, slot, client, orderManager, CountryNames);
                    LobbyUtils.SetupEditableTeamWidget(template, slot, client, orderManager, Map.GetSpawnPoints().Length);
                    LobbyUtils.SetupEditableReadyWidget(template, slot, client, orderManager);
                }
                else
                {                       // Non-editable player in slot
                    if (template == null || template.Id != NonEditablePlayerTemplate.Id)
                    {
                        template = NonEditablePlayerTemplate.Clone();
                    }

                    LobbyUtils.SetupClientWidget(template, slot, client, orderManager, client.Bot == null);
                    LobbyUtils.SetupNameWidget(template, slot, client);
                    LobbyUtils.SetupKickWidget(template, slot, client, orderManager, lobby,
                                               () => panel = PanelType.Kick, () => panel = PanelType.Players);
                    LobbyUtils.SetupColorWidget(template, slot, client);
                    LobbyUtils.SetupFactionWidget(template, slot, client, CountryNames);
                    LobbyUtils.SetupTeamWidget(template, slot, client);
                    LobbyUtils.SetupReadyWidget(template, slot, client);
                }

                template.IsVisible = () => true;

                if (idx >= Players.Children.Count)
                {
                    Players.AddChild(template);
                }
                else if (Players.Children[idx].Id != template.Id)
                {
                    Players.ReplaceChild(Players.Children[idx], template);
                }

                idx++;
            }

            // Add spectators
            foreach (var client in orderManager.LobbyInfo.Clients.Where(client => client.Slot == null))
            {
                Widget template = null;
                var    c        = client;

                // get template for possible reuse
                if (idx < Players.Children.Count)
                {
                    template = Players.Children[idx];
                }

                // Editable spectator
                if (c.Index == orderManager.LocalClient.Index)
                {
                    if (template == null || template.Id != EditableSpectatorTemplate.Id)
                    {
                        template = EditableSpectatorTemplate.Clone();
                    }

                    LobbyUtils.SetupEditableNameWidget(template, null, c, orderManager);
                }
                // Non-editable spectator
                else
                {
                    if (template == null || template.Id != NonEditableSpectatorTemplate.Id)
                    {
                        template = NonEditableSpectatorTemplate.Clone();
                    }

                    LobbyUtils.SetupNameWidget(template, null, client);
                    LobbyUtils.SetupKickWidget(template, null, client, orderManager, lobby,
                                               () => panel = PanelType.Kick, () => panel = PanelType.Players);
                }

                LobbyUtils.SetupClientWidget(template, null, c, orderManager, true);
                template.IsVisible = () => true;

                if (idx >= Players.Children.Count)
                {
                    Players.AddChild(template);
                }
                else if (Players.Children[idx].Id != template.Id)
                {
                    Players.ReplaceChild(Players.Children[idx], template);
                }

                idx++;
            }

            // Spectate button
            if (orderManager.LocalClient.Slot != null)
            {
                Widget spec = null;
                if (idx < Players.Children.Count)
                {
                    spec = Players.Children[idx];
                }
                if (spec == null || spec.Id != NewSpectatorTemplate.Id)
                {
                    spec = NewSpectatorTemplate.Clone();
                }

                var btn = spec.Get <ButtonWidget>("SPECTATE");
                btn.OnClick    = () => orderManager.IssueOrder(Order.Command("spectate"));
                btn.IsDisabled = () => orderManager.LocalClient.IsReady;
                spec.IsVisible = () => true;

                if (idx >= Players.Children.Count)
                {
                    Players.AddChild(spec);
                }
                else if (Players.Children[idx].Id != spec.Id)
                {
                    Players.ReplaceChild(Players.Children[idx], spec);
                }

                idx++;
            }

            while (Players.Children.Count > idx)
            {
                Players.RemoveChild(Players.Children[idx]);
            }
        }