Beispiel #1
0
        private void SortList(string sortBy, bool toggle)
        {
            GUIButton button = labelHolder.GetChildByUserData(sortBy) as GUIButton;

            if (button == null)
            {
                return;
            }

            sortedBy = sortBy;

            var arrowUp   = button.GetChildByUserData("arrowup");
            var arrowDown = button.GetChildByUserData("arrowdown");

            //disable arrow buttons in other labels
            foreach (var child in button.Parent.Children)
            {
                if (child != button)
                {
                    child.GetChildByUserData("arrowup").Visible   = false;
                    child.GetChildByUserData("arrowdown").Visible = false;
                }
            }

            bool ascending = arrowUp.Visible;

            if (toggle)
            {
                ascending = !ascending;
            }

            arrowUp.Visible   = ascending;
            arrowDown.Visible = !ascending;
            serverList.Content.RectTransform.SortChildren((c1, c2) =>
            {
                ServerInfo s1 = c1.GUIComponent.UserData as ServerInfo;
                ServerInfo s2 = c2.GUIComponent.UserData as ServerInfo;

                if (s1 == null && s2 == null)
                {
                    return(0);
                }
                else if (s1 == null)
                {
                    return(ascending ? 1 : -1);
                }
                else if (s2 == null)
                {
                    return(ascending ? -1 : 1);
                }

                switch (sortBy)
                {
                case "ServerListCompatible":
                    bool?s1Compatible = NetworkMember.IsCompatible(GameMain.Version.ToString(), s1.GameVersion);
                    if (!s1.ContentPackageHashes.Any())
                    {
                        s1Compatible = null;
                    }
                    if (s1Compatible.HasValue)
                    {
                        s1Compatible = s1Compatible.Value && s1.ContentPackagesMatch(GameMain.SelectedPackages);
                    }
                    ;

                    bool?s2Compatible = NetworkMember.IsCompatible(GameMain.Version.ToString(), s2.GameVersion);
                    if (!s2.ContentPackageHashes.Any())
                    {
                        s2Compatible = null;
                    }
                    if (s2Compatible.HasValue)
                    {
                        s2Compatible = s2Compatible.Value && s2.ContentPackagesMatch(GameMain.SelectedPackages);
                    }
                    ;

                    //convert to int to make sorting easier
                    //1 Compatible
                    //0 Unknown
                    //-1 Incompatible
                    int s1CompatibleInt = s1Compatible.HasValue ?
                                          (s1Compatible.Value ? 1 : -1) :
                                          0;
                    int s2CompatibleInt = s2Compatible.HasValue ?
                                          (s2Compatible.Value ? 1 : -1) :
                                          0;
                    return(s2CompatibleInt.CompareTo(s1CompatibleInt) * (ascending ? 1 : -1));

                case "ServerListHasPassword":
                    if (s1.HasPassword == s2.HasPassword)
                    {
                        return(0);
                    }
                    return((s1.HasPassword ? 1 : -1) * (ascending ? 1 : -1));

                case "ServerListName":
                    return(string.Compare(s1.ServerName, s2.ServerName) * (ascending ? 1 : -1));

                case "ServerListRoundStarted":
                    if (s1.GameStarted == s2.GameStarted)
                    {
                        return(0);
                    }
                    return((s1.GameStarted ? 1 : -1) * (ascending ? 1 : -1));

                case "ServerListPlayers":
                    return(s2.PlayerCount.CompareTo(s1.PlayerCount) * (ascending ? 1 : -1));

                case "ServerListPing":
                    return(s2.Ping.CompareTo(s1.Ping) * (ascending ? 1 : -1));

                default:
                    return(0);
                }
            });
        }