Exemplo n.º 1
0
        public static PlayerResource Create(Player player)
        {
            var playerResource = new PlayerResource(player.Id)
             {
            Name = player.Name,
            Age = player.Age
             };

             return playerResource;
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        private Player GeneratePlayer(Line line, Position position, AgeRange ageRange, int startNumber)
        {
            var player = new Player();

             player.Name = GetPlayerName();

             // A position is needed to randomly pick a player profile for a specific position.
             // The position can be passed into this method as an argument.
             // Also a line can be provided, then a position within that line will be randomly determined.
             // If no line and position is provided a position will be picked randomly.
             if (position == null)
             {
            // If no Line is provided, pick one randomly.
            if (line == null)
            {
               line = _listRandomizer.GetItem(_lines);
            }

            // Get random position for this line.
            var positions = _positions.Where(x => x.Line.Equals(line));
            position = _listRandomizer.GetItem(positions);
             }

             // A Position for getting a random player profile is determined now, so get the player profile.
             var playerProfile = GetPlayerProfile(position);

             // The profile name and percentage is stored for testing purposes only.
             player.PlayerProfile = playerProfile.Name;

             // Age.
             if (ageRange == null)
             {
            ageRange = new AgeRange(16, 36);
             }
             var age = GetPersonAge(ageRange);
             player.Age = age;

             // Randomly calculate skill scores.
             player.SkillScores = startNumber > 0
                                 ? _profileScoreCalculator.Calculate(startNumber, playerProfile, age)
                                 : _profileScoreCalculator.Calculate(playerProfile, age);

             foreach (var playerSkillScore in player.SkillScores)
             {
            playerSkillScore.Player = player;
             }

             // Now the skill scores for the player are determined, determine the real position for the player.
             // This position can be different from the position that either was passed into this method or picked randomly.
             var determinedPosition = _positionDeterminator.Determine(player.SkillScores);
             player.PreferredPosition = determinedPosition;

             player.Rating = PlayerRater.GetRating(player);

             return player;
        }
Exemplo n.º 4
0
        public void SubstitutePlayers(Player player1, Player player2)
        {
            // Update the team order of both players.
             int oldPlayer1TeamOrder = player1.TeamOrder;
             int oldPlayer2TeamOrder = player2.TeamOrder;
             player1.TeamOrder = oldPlayer2TeamOrder;
             player2.TeamOrder = oldPlayer1TeamOrder;

             using (var repository = new RepositoryFactory().CreateUnitOfWorkRepository())
             {
            repository.RegisterUpdate(player1);
            repository.RegisterUpdate(player2);
            repository.Save();
             }

             var team = player1.Team;
             new TeamService(Game).UpdateRating(team);
        }