/// <summary> /// Assings roles for players /// </summary> void AssignPlayerRoles() { Random rnd = new Random(); List <MRKToSPlayer> unassigned = new List <MRKToSPlayer>(); foreach (MRKToSPlayer player in Players) { unassigned.Add(player); } foreach (MRKToSRoleID role in Settings.Gamemode.RoleList) { Dictionary <MRKToSPlayer, RoleAssignmentContext> ctx = new Dictionary <MRKToSPlayer, RoleAssignmentContext>(); int totalLots = 1; int[] playerMap = new int[unassigned.Count]; int idx = 0; foreach (MRKToSPlayer player in unassigned) { MRKToSScroll scroll = player.GetScroll(role); int playerLots = 10 * (scroll.Count > 0 ? Settings.ScrollMultiplier : 1); playerMap[idx++] = totalLots; totalLots += playerLots; ctx[player] = new RoleAssignmentContext { Lots = playerLots, Scroll = scroll }; } int num = rnd.Next(totalLots) + 1; int playerIdxForRole = -1; for (int i = unassigned.Count - 1; i > -1; i--) { if (num > playerMap[i]) { playerIdxForRole = i; break; } } MRKToSPlayer __player = unassigned[playerIdxForRole]; if (ctx[__player].Scroll.Count > 0) { ctx[__player].Scroll.Count--; } __player.Role = (MRKToSRole)Activator.CreateInstance(ms_RoleAssignmentMap[role], __player); unassigned.Remove(__player); } MatchListener.OnMatchRolesAssigned(); }
public MRKToSScroll GetScroll(MRKToSRoleID role) { if (m_Scrolls.ContainsKey(role)) { return(m_Scrolls[role]); } MRKToSScroll scroll = new MRKToSScroll(role, 0); m_Scrolls[role] = scroll; return(scroll); }