public static FormationResource Create(Formation formation) { var formationResource = new FormationResource(formation.Id) { Name = formation.Name }; return formationResource; }
public List<Player> GenerateStartingLineup(List<Player> players, Formation formation) { // Sort the players on rating and then on name. players.Sort(delegate (Player p1, Player p2) { int ratingDiff = p1.Rating.CompareTo(p2.Rating); if (ratingDiff != 0) return ratingDiff; return string.Compare(p1.Name, p2.Name, StringComparison.OrdinalIgnoreCase); }); var newPlayers = new Player[players.Count]; int teamOrder = 0; // Split the players into three lists: the goalkeepers, the best 10 and the rest. Position goalkeeperPosition; using (var positionRepository = new RepositoryFactory().CreatePositionRepository()) { goalkeeperPosition = positionRepository.GetGoalkeeper(); } var goalkeepers = players.Where(player => player.PreferredPosition.Equals(goalkeeperPosition)).ToList(); var bestFieldPlayers = players.Where(player => !player.PreferredPosition.Equals(goalkeeperPosition)).Take(10).ToList(); var otherFieldPlayers = players.Where(player => !player.PreferredPosition.Equals(goalkeeperPosition)).Skip(10).ToList(); // Pick the best goalkeeper. var bestGoalkeeper = goalkeepers.First(); goalkeepers.Remove(bestGoalkeeper); bestGoalkeeper.CurrentPosition = goalkeeperPosition; bestGoalkeeper.TeamOrder = teamOrder; newPlayers[teamOrder] = bestGoalkeeper; teamOrder++; // Put the 10 best field players on their preferred position if possible. while (teamOrder < 11) { var positionWeNeed = formation.Positions[teamOrder]; var playerForPos = bestFieldPlayers.FirstOrDefault(player => player.PreferredPosition.Equals(positionWeNeed)); if (playerForPos != null) { playerForPos.CurrentPosition = positionWeNeed; playerForPos.TeamOrder = teamOrder; newPlayers[teamOrder] = playerForPos; bestFieldPlayers.Remove(playerForPos); } teamOrder++; } // Check whether the starting eleven has positions without a player and pick players for these positions. for (int i = 0; i < 11; i++) { if (newPlayers[i] == null) { Player playerForPos = null; var positionWeNeed = formation.Positions[i]; // 1) Get a player from the best 10 players (if any left) that has an alternative for this position. var alternativePositions = GetAlternativePositions(positionWeNeed); foreach (var alternativePosition in alternativePositions) { playerForPos = bestFieldPlayers.FirstOrDefault(player => player.PreferredPosition.Equals(alternativePosition)); if (playerForPos != null) { playerForPos.CurrentPosition = positionWeNeed; playerForPos.TeamOrder = i; newPlayers[i] = playerForPos; bestFieldPlayers.Remove(playerForPos); break; } } // 2) If not found, get one of the remaining players that has this position if (playerForPos == null) { playerForPos = otherFieldPlayers.FirstOrDefault(player => player.PreferredPosition.Equals(positionWeNeed)); if (playerForPos != null) { playerForPos.CurrentPosition = positionWeNeed; playerForPos.TeamOrder = i; newPlayers[i] = playerForPos; otherFieldPlayers.Remove(playerForPos); } } // 3) If not found, get one of the remaining players that has an alternative for this position. if (playerForPos == null) { foreach (var alternativePosition in alternativePositions) { playerForPos = otherFieldPlayers.FirstOrDefault(player => player.PreferredPosition.Equals(alternativePosition)); if (playerForPos != null) { playerForPos.CurrentPosition = positionWeNeed; playerForPos.TeamOrder = i; newPlayers[i] = playerForPos; otherFieldPlayers.Remove(playerForPos); break; } } } } } // If the starting eleven still have vacant positions we have a real problem... for (int i = 0; i < 11; i++) { if (newPlayers[i] == null) { throw new Exception("Houston, we have a problem: The starting line up algorithm is not working"); } } // Assign the team order to all players that are not in the starting eleven. foreach (var bestFieldPlayer in bestFieldPlayers) { bestFieldPlayer.TeamOrder = teamOrder; newPlayers[teamOrder] = bestFieldPlayer; teamOrder++; } foreach (var goalkeeper in goalkeepers) { goalkeeper.TeamOrder = teamOrder; newPlayers[teamOrder] = goalkeeper; teamOrder++; } foreach (var otherFieldPlayer in otherFieldPlayers) { otherFieldPlayer.TeamOrder = teamOrder; newPlayers[teamOrder] = otherFieldPlayer; teamOrder++; } return newPlayers.ToList(); }