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. var skillScores = startNumber > 0 ? _profileScoreCalculator.Calculate(startNumber, playerProfile, age) : _profileScoreCalculator.Calculate(playerProfile, age); using (var playerSkillRepo = new RepositoryFactory().CreatePlayerSkillRepository()) { player.SkillGoalkeeping = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetGoalkeeping().Id).Score; player.SkillDefending = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetDefending().Id).Score; player.SkillPassing = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetPassing().Id).Score; player.SkillSpeed = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetSpeed().Id).Score; player.SkillTechnique = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetTechnique().Id).Score; player.SkillShooting = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetShooting().Id).Score; player.SkillHeading = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetHeading().Id).Score; player.SkillTactics = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetTactics().Id).Score; player.SkillFitness = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetFitness().Id).Score; player.SkillForm = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetForm().Id).Score; player.SkillConfidence = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetConfidence().Id).Score; player.SkillPace = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetPace().Id).Score; player.SkillIntelligence = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetIntelligence().Id).Score; player.SkillMentality = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetMentality().Id).Score; player.SkillStrength = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetStrength().Id).Score; player.SkillStamina = skillScores.Single(x => x.PlayerSkill.Id == playerSkillRepo.GetStamina().Id).Score; } // 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(skillScores); player.PreferredPosition = determinedPosition; DetermineRating(player); return(player); }
public Position Determine(List <PlayerSkillScore> skillScores) { List <Position> positions; positions = _positionRepository.GetAll().ToList(); var possiblePositions = new List <Position>(); // Sort skill scores. skillScores = skillScores.OrderByDescending(x => x.Score).ThenBy(x => x.PlayerSkill.Name).ToList(); // Create a list of positions per skill. foreach (var skillScore in skillScores) { // Create an empty anonymous list. var positionsWithPrimarySkill = Enumerable.Empty <object>() .Select(r => new { Position = new Position(), Index = 0 }) // prototype of anonymous type .ToList(); // Add all positions to the anonymous list that have the skill as primary skill // and also the index of the primary skill. foreach (var position in positions) { int index = position.PrimarySkills.IndexOf(skillScore.PlayerSkill); if (index != -1) { positionsWithPrimarySkill.Add(new { Position = position, Index = index }); } } if (positionsWithPrimarySkill.Any()) { // Only get the positions with the lowest index value. int minIndex = positionsWithPrimarySkill.Min(x => x.Index); possiblePositions = positionsWithPrimarySkill.Where(x => x.Index == minIndex).Select(x => x.Position).ToList(); // Quit searching if one remaining position is left. if (possiblePositions.Count == 1) { break; } // For the next skill only the remaining positions will be queried. positions = possiblePositions; } } if (!possiblePositions.Any()) { throw new ApplicationException("No position was determined, this shouldn't be possible?"); } Position determinedPosition = possiblePositions.Count == 1 ? possiblePositions.First() : _listRandomizer.GetItem(possiblePositions); // Dirty Hack: Do not use the sweeper position. If a player has been identified as a sweeper he will become a centre back. bool playerIsSweeper = determinedPosition.Equals(_positionRepository.GetSweeper()); if (playerIsSweeper) { determinedPosition = _positionRepository.GetCentreBack(); } return(determinedPosition); }