PRoConEvents.MULTIbalancer.AssignGroups C# (CSharp) Method

AssignGroups() private method

private AssignGroups ( ) : void
return void
        private void AssignGroups()
        {
            int grandTotal = 0;
            List<int> availableTeamIds = new List<int>(new int[4]{1, 2, 3, 4});

            try {
            // Insure that dispersal groups have been assigned
            List<PlayerModel> all = new List<PlayerModel>();
            all.AddRange(fTeam1);
            all.AddRange(fTeam2);
            all.AddRange(fTeam3);
            all.AddRange(fTeam4);
            foreach (PlayerModel p in all) {
            if (IsInDispersalList(p, true)) {
                if (DebugLevel >= 6) ConsoleDebug("AssignGroups assigned ^b" + p.FullName + "^n to Group " + p.DispersalGroup);
            }
            }

            // Clear
            for (int groupId = 1; groupId <= 4; ++groupId) {
            fGroupAssignments[groupId] = 0;
            }

            // Compute distribution of groups
            int[,] count = new int[5,5]{ // group,team
            {0,0,0,0,0},
            {0,0,0,0,0},
            {0,0,0,0,0},
            {0,0,0,0,0},
            {0,0,0,0,0}
            };

            foreach (PlayerModel p in all) {
            if (p.DispersalGroup == 0) continue;
            ++count[p.DispersalGroup,p.Team];
            ++grandTotal;
            }

            if (grandTotal == 0) {
            ConsoleDebug("AssignGroups: No players or no groups, defaulting to 1,2,3,4");
            for (int i = 1; i <= 4; ++i) {
                fGroupAssignments[i] = i;
            }
            return;
            }

            // Assign team to group that has the most players in that team
            for (int groupId = 1; groupId <= 4; ++groupId) {
            // Find the max team count for this group
            int most = 0;
            int num = 0;
            foreach (int teamId in availableTeamIds) {
                if (count[groupId,teamId] > num) {
                    most = teamId;
                    num = count[groupId,teamId];
                }
            }
            if (most != 0) {
                if (!availableTeamIds.Contains(most)) {
                    throw new Exception("team " + most + " already allocated!");
                }
                fGroupAssignments[groupId] = most;
                availableTeamIds.Remove(most);
            }
            }

            // Assign unallocated teams
            for (int groupId = 1; groupId <= 4; ++groupId) {
            if (fGroupAssignments[groupId] == 0) {
                if (availableTeamIds.Count == 0) {
                    throw new Exception("Ran out of team IDs!");
                }
                int ti = availableTeamIds[0];
                fGroupAssignments[groupId] = ti;
                availableTeamIds.Remove(ti);
            }
            }

            // Sanity check
            availableTeamIds.Clear();
            for (int groupId = 1; groupId <= 4; ++groupId) {
            if (availableTeamIds.Contains(fGroupAssignments[groupId])) {
                throw new Exception("Duplicate assignment!");
            } else {
                availableTeamIds.Add(fGroupAssignments[groupId]);
            }
            }
            } catch (Exception e) {
            ConsoleException(e);
            ConsoleDebug("AssignGroups: Defaulting to 1,2,3,4");
            for (int i = 1; i <= 4; ++i) {
            fGroupAssignments[i] = i;
            }
            } finally {
            if (DebugLevel >= 6) {
            String msg = "Group assignments: ";
            for (int i = 1; i <= 4; ++i) {
                msg = msg + fGroupAssignments[i];
                if (i < 4) msg = msg + "/";
            }
            ConsoleWrite(msg, 6);
            }
            }
        }
MULTIbalancer