public void KeepClanMates()
        {
            //KEEP CLAN SQUADS START
            string DebugScoreList = "";
            string strTeam1 = "";
            string strTeam2 = "";

            int intTeamA = 0;
            int intTeamB = 0;
            int squadless = 0;
            double squadvalue = 0;
            int squadsize = 0;
            int squadIDnew = 0;
            List<string> KeepSquads = new List<string>();
            List<string> squadTags = new List<string>();

            List<int> PlayerTeamA = new List<int>();
            List<int> PlayerTeamB = new List<int>();
            List<int> SquadsTeamA = new List<int>();
            List<int> SquadsTeamB = new List<int>();
            List<int> squadplayers = new List<int>();

            List<int> toremoveKeys = new List<int>();
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }

                if (this.dicPlayerCache.ContainsKey(this.dicPlayerScore[kvp.Key].playerName))
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == 1)
                        intTeamA++;
                    if (this.dicPlayerScore[kvp.Key].teamID == 2)
                        intTeamB++;
                }
                else
                {
                    toremoveKeys.Add(kvp.Key);
                }
            }


            foreach (int removeKey in toremoveKeys)
            {
                this.dicPlayerScore.Remove(removeKey);
            }


            DebugScoreList = "Before Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugScoreList);     



            bool SWITCHsquad = false;
            bool SWITCHplayer = false;

            foreach(KeyValuePair<int, CPlayerScoreInf> kvpCheck in this.dicPlayerScore)
            {
                squadTags.Clear();

                if (this.dicPlayerScore[kvpCheck.Key].playerSquad == 0)
                {
                    squadless++;
                    
                    if (!SWITCHplayer)
                    {
                        SWITCHplayer = true;
                        PlayerTeamA.Add(kvpCheck.Key);
                    }
                    else if (SWITCHplayer)
                    {
                        SWITCHplayer = false;
                        PlayerTeamB.Add(kvpCheck.Key);
                    }

                }
                else
                {
                    squadvalue = this.dicPlayerScore[kvpCheck.Key].playerValue;
                    squadsize = 1;
                    squadplayers = new List<int>();
                    squadplayers.Add(kvpCheck.Key);
                    string CheckSquad = this.dicPlayerScore[kvpCheck.Key].teamID + "." + this.dicPlayerScore[kvpCheck.Key].playerSquad;
                       
                    if (this.dicPlayerScore[kvpCheck.Key].tag != "")
                    {
                        squadTags.Add(this.dicPlayerScore[kvpCheck.Key].tag);
                    }

                    if (KeepSquads.Contains(CheckSquad) == false)
                    {
                        bool deletesquad = true;
                        foreach (KeyValuePair<int, CPlayerScoreInf> kvp2 in this.dicPlayerScore)
                        {
                            if (kvpCheck.Key != kvp2.Key && this.dicPlayerScore[kvpCheck.Key].teamID == this.dicPlayerScore[kvp2.Key].teamID && this.dicPlayerScore[kvpCheck.Key].playerSquad == this.dicPlayerScore[kvp2.Key].playerSquad)
                            {
                                squadvalue = squadvalue + this.dicPlayerScore[kvp2.Key].playerValue;
                                squadsize++;
                                squadplayers.Add(kvp2.Key);
                                if ((  ( ((IList<string>)this.strAClantagWhitelistScrambler).Contains(this.dicPlayerScore[kvp2.Key].tag) && this.dicPlayerScore[kvp2.Key].tag != "" ) || ( ((IList<string>)this.strAClantagWhitelistScrambler).Contains(this.dicPlayerScore[kvpCheck.Key].tag) && this.dicPlayerScore[kvpCheck.Key].tag != "" ) ) && !KeepSquads.Contains(CheckSquad))
                                {
                                    KeepSquads.Add(this.dicPlayerScore[kvpCheck.Key].teamID + "." + this.dicPlayerScore[kvpCheck.Key].playerSquad);
                                    deletesquad = false;
                                    CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[kvpCheck.Key].teamID, this.dicPlayerScore[kvpCheck.Key].playerSquad, 0, 0, false);
                                    squadIDnew++;
                                    this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                                    DebugInfoSkill(string.Format("WL-Clantag detected, Clantag: ^b^2{0}^n^9 or Clantag: ^b^2{1}^n^9. Best Player: ^2{2}", this.dicPlayerScore[kvp2.Key].tag, this.dicPlayerScore[kvpCheck.Key].tag, this.dicPlayerScore[kvp2.Key].playerName)); 
                                    //BLEIBT BESTEHEN!
                                }
                                else if (squadTags.Contains(this.dicPlayerScore[kvp2.Key].tag) && this.dicPlayerScore[kvp2.Key].tag != "" && !KeepSquads.Contains(CheckSquad))
                                {

                                    KeepSquads.Add(this.dicPlayerScore[kvpCheck.Key].teamID + "." + this.dicPlayerScore[kvpCheck.Key].playerSquad);
                                    deletesquad = false;
                                    CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[kvpCheck.Key].teamID, this.dicPlayerScore[kvpCheck.Key].playerSquad, 0, 0, false);
                                    squadIDnew++;
                                    this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                                    DebugInfoSkill(string.Format("Clanmates detected, Clantag: ^b^2{0}^n^9. Best Player: ^2{1}", this.dicPlayerScore[kvp2.Key].tag, this.dicPlayerScore[kvpCheck.Key].playerName));
                                    //BLEIBT BESTEHEN!
                                }
                                else if (!KeepSquads.Contains(CheckSquad) && this.dicPlayerScore[kvp2.Key].tag != "")
                                {
                                    squadTags.Add(this.dicPlayerScore[kvp2.Key].tag);     
                                }
                            }
                        }

                        if (deletesquad)
                        {

                            this.dicPlayerScore[kvpCheck.Key].playerSquad = 0;
                            squadless++;

                            if (!SWITCHplayer)
                            {
                                SWITCHplayer = true;
                                PlayerTeamA.Add(kvpCheck.Key);
                            }
                            else if (SWITCHplayer)
                            {
                                SWITCHplayer = false;
                                PlayerTeamB.Add(kvpCheck.Key);
                            }

                        }
                        else
                        {
                            this.dicSquadScore[squadIDnew].squadScore = squadvalue;
                            this.dicSquadScore[squadIDnew].squadsize = squadsize;
                            
                            if (!SWITCHsquad)
                            {
                                SWITCHsquad = true;
                                PlayerTeamA.AddRange(squadplayers);
                                SquadsTeamA.Add(squadIDnew);
                            }
                            else if (SWITCHsquad)
                            {
                                SWITCHsquad = false;
                                PlayerTeamB.AddRange(squadplayers);
                                SquadsTeamB.Add(squadIDnew);
                            }
                            
                        }
                    }
                    

                }

            }

            int teamsizedifference = PlayerTeamA.Count - PlayerTeamB.Count;

            if (teamsizedifference >= 2)
            {
                DebugInfoSkill("Before. ^bTeamSizeA: " + PlayerTeamA.Count + "^n --- TeamSizeB: " + PlayerTeamB.Count);
                int fromsize = PlayerTeamA.Count - 1;
                for (int j = fromsize; j >= 0; j--)
                {
                    if (this.dicPlayerScore[PlayerTeamA[j]].playerSquad == 0 && (PlayerTeamA.Count - PlayerTeamB.Count) >= 2)
                    {
                        DebugInfoSkill("Player moved to even Teams, 1->2: " + "[" + this.dicPlayerScore[PlayerTeamA[j]].tag +"]^b" + this.dicPlayerScore[PlayerTeamA[j]].playerName);
                        PlayerTeamB.Add(PlayerTeamA[j]);
                        PlayerTeamA.RemoveAt(j);
                    }

                    if ((PlayerTeamA.Count - PlayerTeamB.Count) < 2)
                    {
                        DebugInfoSkill("Done!");
                        break;
                    }
                }

                DebugInfoSkill("^2After. TeamSizeA: " + PlayerTeamA.Count + "^n --- TeamSizeB: " + PlayerTeamB.Count);

            }
            else if (teamsizedifference <= -2)
            {
                DebugInfoSkill("Before. TeamSizeA: " + PlayerTeamA.Count + "^n --- ^bTeamSizeB: " + PlayerTeamB.Count);
                int fromsize = PlayerTeamB.Count - 1;
                for (int j = fromsize; j >= 0; j--)
                {
                    if (this.dicPlayerScore[PlayerTeamB[j]].playerSquad == 0 && (PlayerTeamA.Count - PlayerTeamB.Count) <= -2)
                    {
                        DebugInfoSkill("Player moved to even Teams, 2->1: " + "[" + this.dicPlayerScore[PlayerTeamB[j]].tag + "]^b" + this.dicPlayerScore[PlayerTeamB[j]].playerName);
                        PlayerTeamA.Add(PlayerTeamB[j]);
                        PlayerTeamB.RemoveAt(j);
                    }

                    if ((PlayerTeamA.Count - PlayerTeamB.Count) > -2)
                    {
                        DebugInfoSkill("Done!");
                        break;
                    }
                }

                DebugInfoSkill("^2After. TeamSizeA: " + PlayerTeamA.Count + "^n --- TeamSizeB: " + PlayerTeamB.Count);

            }
            else
            {
                DebugInfoSkill("^2TeamSize A: " + PlayerTeamA.Count + "TeamSizeB: " + PlayerTeamB.Count);
            }



            double TeamValueA = 0;
            int TeamSizeA = PlayerTeamA.Count;
            double TeamValueB = 0;
            int TeamSizeB = PlayerTeamB.Count;

            foreach (int PlayerIDa in PlayerTeamA)
            {
                TeamValueA = TeamValueA + this.dicPlayerScore[PlayerIDa].playerValue;
            }
            foreach (int PlayerIDb in PlayerTeamB)
            {
                TeamValueB = TeamValueB + this.dicPlayerScore[PlayerIDb].playerValue;
            }

            double AverageTeamA = TeamValueA / TeamSizeA;
            double AverageTeamB = TeamValueB / TeamSizeB;
            double AverageDiff = AverageTeamA - AverageTeamB;

            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue Before KeepClanMates Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference Before Adjustment: " + AverageDiff);
            }


            this.DebugInfoSkill("SortValue before adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference before adjustment: ^b^2" + AverageDiff);


            bool adjusted = false;
            do
            {
                adjusted = true;
                double tempTeamValueA = 0;
                double tempTeamValueB = 0;
                double tempAverageTeamA = 0;
                double tempAverageTeamB = 0;
                double tempAverageDiff = 0;
                double adjustvalue = AverageDiff;
                int moveIDA = 0;
                int moveIDB = 0;

                foreach (int playerIDa in PlayerTeamA)
                {
                    foreach (int playerIDb in PlayerTeamB)
                    {
                        if (adjustvalue > 0 && this.dicPlayerScore[playerIDa].playerValue > this.dicPlayerScore[playerIDb].playerValue
                            && this.dicPlayerScore[playerIDa].playerSquad == 0 && this.dicPlayerScore[playerIDb].playerSquad == 0)
                        {
                            tempTeamValueA = TeamValueA - this.dicPlayerScore[playerIDa].playerValue + this.dicPlayerScore[playerIDb].playerValue;
                            tempTeamValueB = TeamValueB - this.dicPlayerScore[playerIDb].playerValue + this.dicPlayerScore[playerIDa].playerValue;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = playerIDa;
                                moveIDB = playerIDb;
                                adjusted = false;
                            }

                        }
                        else if (adjustvalue < 0 && this.dicPlayerScore[playerIDa].playerValue < this.dicPlayerScore[playerIDb].playerValue
                            && this.dicPlayerScore[playerIDa].playerSquad == 0 && this.dicPlayerScore[playerIDb].playerSquad == 0)
                        {
                            tempTeamValueA = TeamValueA - this.dicPlayerScore[playerIDa].playerValue + this.dicPlayerScore[playerIDb].playerValue;
                            tempTeamValueB = TeamValueB - this.dicPlayerScore[playerIDb].playerValue + this.dicPlayerScore[playerIDa].playerValue;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = playerIDa;
                                moveIDB = playerIDb;
                                adjusted = false;
                            }

                        }

                    }
                }

                if (!adjusted)
                {
                    PlayerTeamA.Remove(moveIDA);
                    PlayerTeamA.Add(moveIDB);
                    TeamValueA = TeamValueA - this.dicPlayerScore[moveIDA].playerValue + this.dicPlayerScore[moveIDB].playerValue;
                    AverageTeamA = TeamValueA / TeamSizeA;

                    PlayerTeamB.Remove(moveIDB);
                    PlayerTeamB.Add(moveIDA);
                    TeamValueB = TeamValueB - this.dicPlayerScore[moveIDB].playerValue + this.dicPlayerScore[moveIDA].playerValue;
                    AverageTeamB = TeamValueB / TeamSizeB;

                    AverageDiff = AverageTeamA - AverageTeamB;

                    this.DebugInfoSkill("Adjustment: " + "^0[" + this.dicPlayerScore[moveIDA].tag + "]^b" + this.dicPlayerScore[moveIDA].playerName + "^n^9/^2" + this.dicPlayerScore[moveIDA].playerValue + " ^9 <--> " + "^0[" + this.dicPlayerScore[moveIDB].tag + "]^b" + this.dicPlayerScore[moveIDB].playerName + "n^9/^2" + this.dicPlayerScore[moveIDB].playerValue);
                }

            } while (!adjusted);
            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue After KeepClanMates Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference After Adjustment: " + AverageDiff);
            }

            this.DebugInfoSkill("SortValue ^bafter^n PLAYER adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference ^bafter ^nPLAYER adjustment: ^b^2" + AverageDiff);


            bool Sortiert;
            do
            {
                Sortiert = true;
                for (int j = 0; j < (PlayerTeamA.Count -1); j++)
                {
                    if (this.dicPlayerScore[PlayerTeamA[j]].playerValue < this.dicPlayerScore[PlayerTeamA[j+1]].playerValue)
                    {
                        int temp = PlayerTeamA[j];
                        PlayerTeamA[j] = PlayerTeamA[j + 1];
                        PlayerTeamA[j + 1] = temp;
                        Sortiert = false;
                    }
                }
            } while (!Sortiert);

            do
            {
                Sortiert = true;
                for (int j = 0; j < (PlayerTeamB.Count - 1); j++)
                {
                    if (this.dicPlayerScore[PlayerTeamB[j]].playerValue < this.dicPlayerScore[PlayerTeamB[j + 1]].playerValue)
                    {
                        int temp = PlayerTeamB[j];
                        PlayerTeamB[j] = PlayerTeamB[j + 1];
                        PlayerTeamB[j + 1] = temp;
                        Sortiert = false;
                    }
                }
            } while (!Sortiert);


            int count1 = 1;
            foreach (int playerIDa in PlayerTeamA)
            {
                if (this.dicPlayerScore[playerIDa].playerSquad == 0)
                {
                    if (count1 == 1)
                    {
                        squadIDnew++;
                        this.dicPlayerScore[playerIDa].teamID = 100;
                        this.dicPlayerScore[playerIDa].playerSquad = 100 + squadIDnew;

                        CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[playerIDa].teamID, this.dicPlayerScore[playerIDa].playerSquad, 1, this.dicPlayerScore[playerIDa].playerValue, false);
                        this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                        SquadsTeamA.Add(squadIDnew);

                    }
                    else
                    {
                        this.dicPlayerScore[playerIDa].teamID = 100;
                        this.dicPlayerScore[playerIDa].playerSquad = 100 + squadIDnew;
                        this.dicSquadScore[squadIDnew].squadsize++;
                        this.dicSquadScore[squadIDnew].squadScore = this.dicSquadScore[squadIDnew].squadScore + this.dicPlayerScore[playerIDa].playerValue;
                    }

                    count1++;
                    if (count1 == 5)
                        count1 = 1;
                }
            }


            count1 = 1;
            foreach (int playerIDb in PlayerTeamB)
            {
                if (this.dicPlayerScore[playerIDb].playerSquad == 0)
                {
                    if (count1 == 1)
                    {
                        squadIDnew++;
                        this.dicPlayerScore[playerIDb].teamID = 200;
                        this.dicPlayerScore[playerIDb].playerSquad = 200 + squadIDnew;

                        CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[playerIDb].teamID, this.dicPlayerScore[playerIDb].playerSquad, 1, this.dicPlayerScore[playerIDb].playerValue, false);
                        this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                        SquadsTeamB.Add(squadIDnew);

                    }
                    else
                    {
                        this.dicPlayerScore[playerIDb].teamID = 200;
                        this.dicPlayerScore[playerIDb].playerSquad = 200 + squadIDnew;
                        this.dicSquadScore[squadIDnew].squadsize++;
                        this.dicSquadScore[squadIDnew].squadScore = this.dicSquadScore[squadIDnew].squadScore + this.dicPlayerScore[playerIDb].playerValue;
                    }

                    count1++;
                    if (count1 == 5)
                        count1 = 1;
                }
            }



            adjusted = false;
            bool squadadjust = false;
            do
            {
                adjusted = true;
                double tempTeamValueA = 0;
                double tempTeamValueB = 0;
                double tempAverageTeamA = 0;
                double tempAverageTeamB = 0;
                double tempAverageDiff = 0;
                double adjustvalue = AverageDiff;
                int moveIDA = 0;
                int moveIDB = 0;

                foreach (int squadIDA in SquadsTeamA)
                {
                    foreach (int squadIDB in SquadsTeamB)
                    {
                        if (adjustvalue > 0 && this.dicSquadScore[squadIDA].squadScore > this.dicSquadScore[squadIDB].squadScore && this.dicSquadScore[squadIDA].squadsize == this.dicSquadScore[squadIDB].squadsize)
                        {
                            tempTeamValueA = TeamValueA - this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize +
                                this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize;
                            tempTeamValueB = TeamValueB - this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize +
                                this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = squadIDA;
                                moveIDB = squadIDB;
                                adjusted = false;
                            }

                        }
                        else if (adjustvalue < 0 && this.dicSquadScore[squadIDA].squadScore < this.dicSquadScore[squadIDB].squadScore && this.dicSquadScore[squadIDA].squadsize == this.dicSquadScore[squadIDB].squadsize)
                        {
                            tempTeamValueA = TeamValueA - this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize +
                                this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize;
                            tempTeamValueB = TeamValueB - this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize +
                                this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = squadIDA;
                                moveIDB = squadIDB;
                                adjusted = false;
                            }

                        }

                    }
                }



                if (!adjusted)
                {
                    squadadjust = true;
                    SquadsTeamA.Remove(moveIDA);
                    SquadsTeamA.Add(moveIDB);
                    TeamValueA = TeamValueA - this.dicSquadScore[moveIDA].squadScore * this.dicSquadScore[moveIDA].squadsize +
                                this.dicSquadScore[moveIDB].squadScore * this.dicSquadScore[moveIDB].squadsize;
                    AverageTeamA = TeamValueA / TeamSizeA;

                    SquadsTeamB.Remove(moveIDB);
                    SquadsTeamB.Add(moveIDA);
                    TeamValueB = TeamValueB - this.dicSquadScore[moveIDB].squadScore * this.dicSquadScore[moveIDB].squadsize +
                                this.dicSquadScore[moveIDA].squadScore * this.dicSquadScore[moveIDA].squadsize;
                    AverageTeamB = TeamValueB / TeamSizeB;

                    AverageDiff = AverageTeamA - AverageTeamB;

                    this.DebugInfoSkill("SQUAD Adjustment: ^b^2 1." + moveIDA + " ^9<-->^2 2." + moveIDB);
                }

            } while (!adjusted);

            if (squadadjust)
            {
                //PURE
                if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
                {
                    PostBalanceValue("SortValue After SquadAdjustment Sort");
                    PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                    PostBalanceValue("Average Difference After Adjustment: " + AverageDiff);
                }

                this.DebugInfoSkill("SortValue ^bafter^n SQUAD adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                    "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
                this.DebugInfoSkill("Average Difference ^bafter ^nSQUAD adjustment: ^b^2" + AverageDiff);
            }



            int squadscrambledA = 0;
            int squadscrambledB = 0;

            Dictionary<string, int> dicNewSquad = new Dictionary<string, int>();

            foreach (int SquadIDA in SquadsTeamA)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDA].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDA].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDA].teamID + "." + this.dicSquadScore[SquadIDA].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledA++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledA);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledA;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 1;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }

            foreach (int SquadIDB in SquadsTeamB)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDB].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDB].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDB].teamID + "." + this.dicSquadScore[SquadIDB].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledB++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledB);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledB;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }
            
            string DebugSortedList = "";
            strTeam1 = "";
            strTeam2 = "";
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
            }

            DebugSortedList = "\n\nAfter Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugSortedList);


            this.dicSquadList.Clear();
            this.strFinalSquad = "";
            this.intSquadA = 0;
            this.intSquadB = 0;

            this.DebugInfoSkill("Keeping only CLAN-Squads");
            
            // KEEP CLAN SQUADS END
 

        }
        public void KeepNoSquads()
        {
            //KEEP NO SQUADS START
            string DebugScoreList = "";
            string strTeam1 = "";
            string strTeam2 = "";

            int intTeamA = 0;
            int intTeamB = 0;
            int squadIDnew = 0;
            List<string> KeepSquads = new List<string>();
            List<string> squadTags = new List<string>();

            List<int> PlayerTeamA = new List<int>();
            List<int> PlayerTeamB = new List<int>();
            List<int> SquadsTeamA = new List<int>();
            List<int> SquadsTeamB = new List<int>();


            List<int> toremoveKeys = new List<int>();
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }


                if (this.dicPlayerCache.ContainsKey(this.dicPlayerScore[kvp.Key].playerName))
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == 1)
                        intTeamA++;
                    if (this.dicPlayerScore[kvp.Key].teamID == 2)
                        intTeamB++;
                }
                else
                {
                    toremoveKeys.Add(kvp.Key);
                }
            }


            foreach (int removeKey in toremoveKeys)
            {
                this.dicPlayerScore.Remove(removeKey);
            }

            DebugScoreList = "Before Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugScoreList);  




            bool SWITCHplayer = false;

            foreach (KeyValuePair<int, CPlayerScoreInf> kvpCheck in this.dicPlayerScore)
            {
                if (!SWITCHplayer)
                {
                    SWITCHplayer = true;
                    this.dicPlayerScore[kvpCheck.Key].teamID = 100;
                    this.dicPlayerScore[kvpCheck.Key].playerSquad = 0;
                    PlayerTeamA.Add(kvpCheck.Key);
                }
                else if (SWITCHplayer)
                {
                    SWITCHplayer = false;
                    this.dicPlayerScore[kvpCheck.Key].teamID = 200;
                    this.dicPlayerScore[kvpCheck.Key].playerSquad = 0;
                    PlayerTeamB.Add(kvpCheck.Key);
                }
                
            }

            int teamsizedifference = PlayerTeamA.Count - PlayerTeamB.Count;

            double TeamValueA = 0;
            int TeamSizeA = PlayerTeamA.Count;
            double TeamValueB = 0;
            int TeamSizeB = PlayerTeamB.Count;

            foreach (int PlayerIDa in PlayerTeamA)
            {
                TeamValueA = TeamValueA + this.dicPlayerScore[PlayerIDa].playerValue;
            }
            foreach (int PlayerIDb in PlayerTeamB)
            {
                TeamValueB = TeamValueB + this.dicPlayerScore[PlayerIDb].playerValue;
            }

            double AverageTeamA = TeamValueA / TeamSizeA;
            double AverageTeamB = TeamValueB / TeamSizeB;
            double AverageDiff = AverageTeamA - AverageTeamB;

            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue Before KeepNoSquads Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference Before Adjustment: " + AverageDiff);
            }


            this.DebugInfoSkill("SortValue before adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference before adjustment: ^b^2" + AverageDiff);


            bool adjusted = false;
            do
            {
                adjusted = true;
                double tempTeamValueA = 0;
                double tempTeamValueB = 0;
                double tempAverageTeamA = 0;
                double tempAverageTeamB = 0;
                double tempAverageDiff = 0;
                double adjustvalue = AverageDiff;
                int moveIDA = 0;
                int moveIDB = 0;

                foreach (int playerIDa in PlayerTeamA)
                {
                    foreach (int playerIDb in PlayerTeamB)
                    {
                        if (adjustvalue > 0 && this.dicPlayerScore[playerIDa].playerValue > this.dicPlayerScore[playerIDb].playerValue
                            && this.dicPlayerScore[playerIDa].playerSquad == 0 && this.dicPlayerScore[playerIDb].playerSquad == 0)
                        {
                            tempTeamValueA = TeamValueA - this.dicPlayerScore[playerIDa].playerValue + this.dicPlayerScore[playerIDb].playerValue;
                            tempTeamValueB = TeamValueB - this.dicPlayerScore[playerIDb].playerValue + this.dicPlayerScore[playerIDa].playerValue;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = playerIDa;
                                moveIDB = playerIDb;
                                adjusted = false;
                            }

                        }
                        else if (adjustvalue < 0 && this.dicPlayerScore[playerIDa].playerValue < this.dicPlayerScore[playerIDb].playerValue
                            && this.dicPlayerScore[playerIDa].playerSquad == 0 && this.dicPlayerScore[playerIDb].playerSquad == 0)
                        {
                            tempTeamValueA = TeamValueA - this.dicPlayerScore[playerIDa].playerValue + this.dicPlayerScore[playerIDb].playerValue;
                            tempTeamValueB = TeamValueB - this.dicPlayerScore[playerIDb].playerValue + this.dicPlayerScore[playerIDa].playerValue;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = playerIDa;
                                moveIDB = playerIDb;
                                adjusted = false;
                            }

                        }

                    }
                }

                if (!adjusted)
                {
                    PlayerTeamA.Remove(moveIDA);
                    PlayerTeamA.Add(moveIDB);
                    TeamValueA = TeamValueA - this.dicPlayerScore[moveIDA].playerValue + this.dicPlayerScore[moveIDB].playerValue;
                    AverageTeamA = TeamValueA / TeamSizeA;

                    PlayerTeamB.Remove(moveIDB);
                    PlayerTeamB.Add(moveIDA);
                    TeamValueB = TeamValueB - this.dicPlayerScore[moveIDB].playerValue + this.dicPlayerScore[moveIDA].playerValue;
                    AverageTeamB = TeamValueB / TeamSizeB;

                    AverageDiff = AverageTeamA - AverageTeamB;

                    this.DebugInfoSkill("Adjustment: ^b^2" + this.dicPlayerScore[moveIDA].playerName + "/" + this.dicPlayerScore[moveIDA].playerValue + " ^9 <--> ^2" + this.dicPlayerScore[moveIDB].playerName + "/" + this.dicPlayerScore[moveIDB].playerValue);
                }

            } while (!adjusted);
            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue After KeepNoSquads Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference After Adjustment: " + AverageDiff);
            }


            this.DebugInfoSkill("SortValue ^bafter^n adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference ^bafter ^nadjustment: ^b^2" + AverageDiff);

            int count1 = 1;
            foreach (int playerIDa in PlayerTeamA)
            {
                if (this.dicPlayerScore[playerIDa].playerSquad == 0)
                {
                    if (count1 == 1)
                    {
                        squadIDnew++;
                        this.dicPlayerScore[playerIDa].teamID = 100;
                        this.dicPlayerScore[playerIDa].playerSquad = 100 + squadIDnew;

                        CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[playerIDa].teamID, this.dicPlayerScore[playerIDa].playerSquad, 1, this.dicPlayerScore[playerIDa].playerValue, false);
                        this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                        SquadsTeamA.Add(squadIDnew);

                    }
                    else
                    {
                        this.dicPlayerScore[playerIDa].teamID = 100;
                        this.dicPlayerScore[playerIDa].playerSquad = 100 + squadIDnew;
                        this.dicSquadScore[squadIDnew].squadsize++;
                        this.dicSquadScore[squadIDnew].squadScore = this.dicSquadScore[squadIDnew].squadScore + this.dicPlayerScore[playerIDa].playerValue;
                    }

                    count1++;
                    if (count1 == 5)
                        count1 = 1;
                }
            }


            count1 = 1;
            foreach (int playerIDb in PlayerTeamB)
            {
                if (this.dicPlayerScore[playerIDb].playerSquad == 0)
                {
                    if (count1 == 1)
                    {
                        squadIDnew++;
                        this.dicPlayerScore[playerIDb].teamID = 200;
                        this.dicPlayerScore[playerIDb].playerSquad = 200 + squadIDnew;

                        CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[playerIDb].teamID, this.dicPlayerScore[playerIDb].playerSquad, 1, this.dicPlayerScore[playerIDb].playerValue, false);
                        this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                        SquadsTeamB.Add(squadIDnew);

                    }
                    else
                    {
                        this.dicPlayerScore[playerIDb].teamID = 200;
                        this.dicPlayerScore[playerIDb].playerSquad = 200 + squadIDnew;
                        this.dicSquadScore[squadIDnew].squadsize++;
                        this.dicSquadScore[squadIDnew].squadScore = this.dicSquadScore[squadIDnew].squadScore + this.dicPlayerScore[playerIDb].playerValue;
                    }

                    count1++;
                    if (count1 == 5)
                        count1 = 1;
                }
            }
     

            int squadscrambledA = 0;
            int squadscrambledB = 0;
            int n = 0;

            Dictionary<string, int> dicNewSquad = new Dictionary<string, int>();

            foreach (int SquadIDA in SquadsTeamA)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDA].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDA].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDA].teamID + "." + this.dicSquadScore[SquadIDA].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledA++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledA);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledA;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 1;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }

            foreach (int SquadIDB in SquadsTeamB)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDB].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDB].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDB].teamID + "." + this.dicSquadScore[SquadIDB].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledB++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledB);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledB;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }


            n = 0;
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].playerSquad == 0)
                {
                    if (n == 0 && this.dicPlayerScore[kvp.Key].teamID != 2)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    else if (n > 0 && n <= 2 && dicPlayerScore[kvp.Key].teamID != 1)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 1;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    else if (n > 2 && this.dicPlayerScore[kvp.Key].teamID != 2)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    n++;
                    if (n == 5) n = 1;
                }
            }

            string DebugSortedList = "";
            strTeam1 = "";
            strTeam2 = "";
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
            }

            DebugSortedList = "\n\nAfter Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugSortedList);


            this.dicSquadList.Clear();
            this.strFinalSquad = "";
            this.intSquadA = 0;
            this.intSquadB = 0;

            this.DebugInfoSkill("Keeping NO Squads");

            // KEEP NO SQUADS END
        }
        public void KeepAllSquads()
        {

            //KEEP ALL SQUADS START
            string DebugScoreList = "";
            string strTeam1 = "";
            string strTeam2 = "";
            bool squadexists = false;
            int squadIDnew = 0;

            int intTeamA = 0;
            int intTeamB = 0;

            List<int> toremoveKeys = new List<int>();
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }

                if (this.dicPlayerCache.ContainsKey(this.dicPlayerScore[kvp.Key].playerName))
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == 1)
                        intTeamA++;
                    if (this.dicPlayerScore[kvp.Key].teamID == 2)
                        intTeamB++;
                }
                else
                {
                    toremoveKeys.Add(kvp.Key);
                }
            }
            foreach (int removeKey in toremoveKeys)
            {
                this.dicPlayerScore.Remove(removeKey);
            }


            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                
                if (this.dicPlayerScore[kvp.Key].playerSquad != 0)
                {
                    squadexists = false;
                    foreach (KeyValuePair<int, CSquadScoreInf> kvpsquad in this.dicSquadScore)
                    {
                        if (this.dicSquadScore[kvpsquad.Key].teamID == this.dicPlayerScore[kvp.Key].teamID &&
                                    this.dicSquadScore[kvpsquad.Key].squadID == this.dicPlayerScore[kvp.Key].playerSquad)
                        {
                            this.dicSquadScore[kvpsquad.Key].squadScore = this.dicSquadScore[kvpsquad.Key].squadScore + this.dicPlayerScore[kvp.Key].playerValue;
                            this.dicSquadScore[kvpsquad.Key].squadsize++;
                            squadexists = true;
                            break;
                        }
                    }
                    if (!squadexists)
                    {
                        CSquadScoreInf newEntrySquad = new CSquadScoreInf(this.dicPlayerScore[kvp.Key].teamID, this.dicPlayerScore[kvp.Key].playerSquad, 1, this.dicPlayerScore[kvp.Key].playerValue, false);
                        squadIDnew++;
                        this.dicSquadScore.Add(squadIDnew, newEntrySquad);
                    }
                }

            }


            foreach (KeyValuePair<int, CSquadScoreInf> kvpsquad in this.dicSquadScore)
            {
                this.dicSquadScore[kvpsquad.Key].squadScore = this.dicSquadScore[kvpsquad.Key].squadScore / this.dicSquadScore[kvpsquad.Key].squadsize;
            }
            
            bool Sortiert = true;

            do
            {
                Sortiert = true;
                for (int j = 1; j < this.dicSquadScore.Count; j++)
                {
                    if (this.dicSquadScore[j].squadsize < this.dicSquadScore[j + 1].squadsize)
                    {
                        CSquadScoreInf tempsquad = new CSquadScoreInf(this.dicSquadScore[j].teamID, this.dicSquadScore[j].squadID, this.dicSquadScore[j].squadsize, this.dicSquadScore[j].squadScore, false);
                        this.dicSquadScore[j] = this.dicSquadScore[j + 1];
                        this.dicSquadScore[j + 1] = tempsquad;
                        Sortiert = false;
                    }
                    else if (this.dicSquadScore[j].squadsize == this.dicSquadScore[j + 1].squadsize && this.dicSquadScore[j].squadScore < this.dicSquadScore[j + 1].squadScore) 
                    {
                        CSquadScoreInf tempsquad = new CSquadScoreInf(this.dicSquadScore[j].teamID, this.dicSquadScore[j].squadID, this.dicSquadScore[j].squadsize, this.dicSquadScore[j].squadScore, false);
                        this.dicSquadScore[j] = this.dicSquadScore[j + 1];
                        this.dicSquadScore[j + 1] = tempsquad;
                        Sortiert = false;
                    }
                }

            } while (!Sortiert);

            string DebugSquadSorted = "";
            foreach (KeyValuePair<int, CSquadScoreInf> kvpsquad in this.dicSquadScore)
            {
                DebugSquadSorted = DebugSquadSorted + "^0" + this.dicSquadScore[kvpsquad.Key].teamID + "." + this.dicSquadScore[kvpsquad.Key].squadID +
                                    ": ^7" + this.dicSquadScore[kvpsquad.Key].squadsize + "^9*^5" + this.dicSquadScore[kvpsquad.Key].squadScore + "^9 --- ";
            }


            DebugScoreList = "Before Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugScoreList);

            bestSquadTeamID = this.dicSquadScore[1].teamID;
            this.DebugInfoSkill("Squads sorted: " + DebugSquadSorted);

            List<int> SquadsTeamA = new List<int>();
            List<int> SquadsTeamB = new List<int>();
            double TeamValueA = 0;
            int TeamSizeA = 0;
            double TeamValueB = 0;
            int TeamSizeB = 0;

            int n = 0;

            foreach (KeyValuePair<int, CSquadScoreInf> kvpsquad in this.dicSquadScore)
            {
                if (n == 0 && kvpsquad.Key == 1)
                    SquadsTeamA.Add(kvpsquad.Key);
                else if (n <= 2)
                    SquadsTeamB.Add(kvpsquad.Key);
                else if (n > 2)
                    SquadsTeamA.Add(kvpsquad.Key);

                n++;

                if (n == 5)
                    n = 1;
            }

            foreach (int squadID in SquadsTeamA)
            {
                TeamValueA = TeamValueA + this.dicSquadScore[squadID].squadScore * this.dicSquadScore[squadID].squadsize;
                TeamSizeA = TeamSizeA + this.dicSquadScore[squadID].squadsize;
            }

            foreach (int squadID in SquadsTeamB)
            {
                TeamValueB = TeamValueB + this.dicSquadScore[squadID].squadScore * this.dicSquadScore[squadID].squadsize;
                TeamSizeB = TeamSizeB + this.dicSquadScore[squadID].squadsize;
            }


            double AverageTeamA = TeamValueA / TeamSizeA;
            double AverageTeamB = TeamValueB / TeamSizeB;
            double AverageDiff = AverageTeamA - AverageTeamB;
            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue Before KeepAllSquads Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference Before Adjustment: " + AverageDiff);
            }


            this.DebugInfoSkill("SortValue before adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference before adjustment: ^b^2" + AverageDiff);

            bool adjusted = false;
            do
            {
                adjusted = true;
                double tempTeamValueA = 0;
                double tempTeamValueB = 0;
                double tempAverageTeamA = 0;
                double tempAverageTeamB = 0;
                double tempAverageDiff = 0;
                double adjustvalue = AverageDiff;
                int moveIDA = 0;
                int moveIDB = 0;

                foreach (int squadIDA in SquadsTeamA)
                {
                    foreach (int squadIDB in SquadsTeamB)
                    {
                        if (adjustvalue > 0 && this.dicSquadScore[squadIDA].squadScore > this.dicSquadScore[squadIDB].squadScore && this.dicSquadScore[squadIDA].squadsize == this.dicSquadScore[squadIDB].squadsize)
                        {
                            tempTeamValueA = TeamValueA - this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize +
                                this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize;
                            tempTeamValueB = TeamValueB - this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize +
                                this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = squadIDA;
                                moveIDB = squadIDB;
                                adjusted = false;
                            }

                        }
                        else if (adjustvalue < 0 && this.dicSquadScore[squadIDA].squadScore < this.dicSquadScore[squadIDB].squadScore && this.dicSquadScore[squadIDA].squadsize == this.dicSquadScore[squadIDB].squadsize)
                        {
                            tempTeamValueA = TeamValueA - this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize +
                                this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize;
                            tempTeamValueB = TeamValueB - this.dicSquadScore[squadIDB].squadScore * this.dicSquadScore[squadIDB].squadsize +
                                this.dicSquadScore[squadIDA].squadScore * this.dicSquadScore[squadIDA].squadsize;
                            tempAverageTeamA = tempTeamValueA / TeamSizeA;
                            tempAverageTeamB = tempTeamValueB / TeamSizeB;
                            tempAverageDiff = tempAverageTeamA - tempAverageTeamB;

                            if (Math.Abs(adjustvalue) > Math.Abs(tempAverageDiff))
                            {
                                adjustvalue = tempAverageDiff;
                                moveIDA = squadIDA;
                                moveIDB = squadIDB;
                                adjusted = false;
                            }

                        }

                    }
                }



                if (!adjusted)
                {
                    SquadsTeamA.Remove(moveIDA);
                    SquadsTeamA.Add(moveIDB);
                    TeamValueA = TeamValueA - this.dicSquadScore[moveIDA].squadScore * this.dicSquadScore[moveIDA].squadsize +
                                this.dicSquadScore[moveIDB].squadScore * this.dicSquadScore[moveIDB].squadsize;
                    AverageTeamA = TeamValueA / TeamSizeA;

                    SquadsTeamB.Remove(moveIDB);
                    SquadsTeamB.Add(moveIDA);
                    TeamValueB = TeamValueB - this.dicSquadScore[moveIDB].squadScore * this.dicSquadScore[moveIDB].squadsize +
                                this.dicSquadScore[moveIDA].squadScore * this.dicSquadScore[moveIDA].squadsize;
                    AverageTeamB = TeamValueB / TeamSizeB;

                    AverageDiff = AverageTeamA - AverageTeamB;

                    this.DebugInfoSkill("Adjustment: ^b^2 1." + moveIDA + " ^9<-->^2 2." + moveIDB);
                }

            } while (!adjusted);

            //PURE
            if (ynEnableTBValueToChat == enumBoolYesNo.Yes)
            {
                PostBalanceValue("SortValue After KeepAllSquads Sort");
                PostBalanceValue("Team1: " + TeamSizeA + " - " + AverageTeamA + "   Team2: " + TeamSizeB + " - " + AverageTeamB);
                PostBalanceValue("Average Difference After Adjustment: " + AverageDiff);
            }


            this.DebugInfoSkill("SortValue ^bafter^n adjustment: ^bTeam 1: ^7" + TeamSizeA + "^9*^2" + AverageTeamA +
                "^9^n --- ^bTeam 2: ^7" + TeamSizeB + "^9*^2" + AverageTeamB);
            this.DebugInfoSkill("Average Difference ^bafter ^nadjustment: ^b^2" + AverageDiff);




            int squadscrambledA = 0;
            int squadscrambledB = 0;
            n = 0;

            Dictionary<string, int> dicNewSquad = new Dictionary<string, int>();

            foreach (int SquadIDA in SquadsTeamA)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDA].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDA].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDA].teamID + "." + this.dicSquadScore[SquadIDA].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledA++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledA);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledA;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 1;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }

            foreach (int SquadIDB in SquadsTeamB)
            {
                foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
                {
                    if (this.dicPlayerScore[kvp.Key].teamID == this.dicSquadScore[SquadIDB].teamID && this.dicPlayerScore[kvp.Key].playerSquad == this.dicSquadScore[SquadIDB].squadID && !this.dicPlayerScore[kvp.Key].balanced)
                    {
                        string strTeamSquad = this.dicSquadScore[SquadIDB].teamID + "." + this.dicSquadScore[SquadIDB].squadID;

                        if (dicNewSquad.ContainsKey(strTeamSquad))
                        {
                            this.dicPlayerScore[kvp.Key].playerSquad = dicNewSquad[strTeamSquad];
                        }
                        else
                        {
                            squadscrambledB++;
                            dicNewSquad.Add(strTeamSquad, squadscrambledB);
                            this.dicPlayerScore[kvp.Key].playerSquad = squadscrambledB;
                        }

                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                }

            }

            n = 0;
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].playerSquad == 0)
                {
                    if (n == 0 && this.dicPlayerScore[kvp.Key].teamID != 2)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    else if (n > 0 && n <= 2 && dicPlayerScore[kvp.Key].teamID != 1)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 1;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    else if (n > 2 && this.dicPlayerScore[kvp.Key].teamID != 2)
                    {
                        this.dicPlayerScore[kvp.Key].teamID = 2;
                        this.dicPlayerScore[kvp.Key].balanced = true;
                    }
                    n++;
                    if (n == 5) n = 1;
                }
            }

            string DebugSortedList = "";
            strTeam1 = "";
            strTeam2 = "";
            foreach (KeyValuePair<int, CPlayerScoreInf> kvp in this.dicPlayerScore)
            {
                if (this.dicPlayerScore[kvp.Key].teamID == 1)
                {
                    strTeam1 = strTeam1 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
                else if (this.dicPlayerScore[kvp.Key].teamID == 2)
                {
                    strTeam2 = strTeam2 + "^1" + this.dicPlayerScore[kvp.Key].playerSquad + "^9.^0" + "[" + this.dicPlayerScore[kvp.Key].tag + "]^b" + this.dicPlayerScore[kvp.Key].playerName +
                    "^n^9: ^4" + this.dicPlayerScore[kvp.Key].playerValue + "^9 --- ";
                }
            }

            DebugSortedList = "\n\nAfter Scramble:\nTeam 1: " + strTeam1 + "\n\nTeam 2: " + strTeam2;
            this.DebugInfoSkill(DebugSortedList);


            this.dicSquadList.Clear();
            this.strFinalSquad = "";
            this.intSquadA = 0;
            this.intSquadB = 0;

            this.DebugInfoSkill("Keeping ALL Squads");
            // KEEP ALL SQUADS END
 
        }