// TODO: Return the player for the specified player number, or null if not located. // The playerNumber parameter must be > 0. If it is not then return a null result. // Note // Team.Players has the players for the team. // Player.PlayerNumber is the field to be compared against public Player PlayerByPlayerNumber(int playerNumber) { var selectedPlayer = TeamReferenceData .Select(tfd => tfd.Players.Where(p => p.PlayerNumber == playerNumber).FirstOrDefault()) .Where(tfd => tfd != null).FirstOrDefault(); return(selectedPlayer); }
// TODO: For each team return their win % as well as their players win %, sorted by the team 'win %' highest to lowest. // If a teamId is specified then return data for only that team i.e. result list will only contain a single entry // otherwise if the teamId=0 return item data for each team in TeamReferenceData supplied in the constructor. // If a team is specified and cannot be located then return a empty list (list must be empty and not null). // NB! If any player on the team has played 100 or more matches then IStatsWeighting must be invoked with the required parameters // ONLY make this call if one or more of the player matches is >= 100. // The result must be stored in the PlayerWeighting field inside the TeamValue result class. // If all the players within the team has played less than 100 matches each then PlayerWeighting must be set to 0. // Note // Team Win % is Team.Victories over Team.Matches // Player Win % is Player.Wins over Player.Matches i.e. the sum of all players win / matches on the team. public IEnumerable <TeamValue> TeamWinPercentage(int teamId = 0) { var teamValueList = TeamReferenceData .Select(tfd => new { teamModel = tfd, teamWinsPercantage = percentageCalculator(tfd.Victories, tfd.Matches) }) .ToList() .Select(twp => new { team = twp, playerData = twp.teamModel.Players .Select(p => new { player = p, playerWinPercantage = percentageCalculator(p.Wins, p.Matches), PlayerWeighting = (p.Matches >= 100) ? StatsWeighting.Apply(percentageCalculator(p.Wins, p.Matches), p.Matches) : 0 }) }).ToList() .Select(twapwp => new TeamValue { Id = twapwp.team.teamModel.Id, Name = twapwp.team.teamModel.Name, TeamWinsPercentage = twapwp.team.teamWinsPercantage, PlayerWinPercentage = twapwp.playerData.Select(pd => pd.playerWinPercantage).Sum(), PlayerWeighting = (twapwp.playerData.Select(pd => pd.PlayerWeighting)).FirstOrDefault() }) .OrderByDescending(tv => tv.TeamWinsPercentage) .ToList(); if (teamId > 0) { if (teamValueList.Exists(tv => tv.Id == teamId)) { return(teamValueList.Where(tv => tv.Id == teamId).ToList()); } else { return(new List <TeamValue>()); } } else { return(teamValueList); } }
// TODO: Return the driver for the specified car number, or null if not located. // The carNo parameter must be > 0. If it is not then return a null result. // Note // Team.Drivers has the drivers for the team. // Driver.CarNumber contains the car number public Driver DriverByCarNo(int carNo) { if (carNo <= 0) { return(null); } var driver = TeamReferenceData.ToList().SelectMany(team => team.Drivers.Where(racer => racer.CarNumber == carNo)) .FirstOrDefault(racer => racer.CarNumber == carNo); if (driver == null) { return(null); } else { return(driver); } }
// TODO: For each team return their win % as well as their drivers win %, sorted by the team 'win %' highest to lowest. // If a teamId is specified then return data for only that team i.e. method result list will only a single entry // otherwise if the teamId=0 return item data for each team in TeamReferenceData supplied in the constructor // If a team is specified and cannot be located then return a empty list. // NB! If a driver has done 100 or more races then IF1StatsWeighting must be invoked with the required parameters // ONLY make this call if the drivers races >= 100. // The result must be stored in the DriverWeighting field of team value. // If the driver has done less than 100 races the DriverWeighting must be set to 0. // Note // Team Win % is Team.Victories over Team.Races // Driver Win % is Driver.Wins over Driver.Races // Only calculate win % for drivers. Exclude test drivers public IEnumerable <TeamValue> TeamWinPercentage(int teamId = 0) { List <TeamValue> results = new List <TeamValue>(); if (teamId != 0) { var team = TeamReferenceData.FirstOrDefault(t => t.Id == teamId); if (team != null) { return(CalcTeamStats(team)); } return(new List <TeamValue>()); } foreach (var team in TeamReferenceData) { var resultsForTeam = CalcTeamStats(team); results.AddRange(resultsForTeam); } return(results.OrderByDescending(t => t.TeamWinsPercentage)); }
// TODO: For each team return their win % as well as their drivers win %, sorted by the team 'win %' highest to lowest. // If a teamId is specified then return data for only that team i.e. method result list will only a single entry // otherwise if the teamId=0 return item data for each team in TeamReferenceData supplied in the constructor // If a team is specified and cannot be located then return a empty list. // NB! If a teams drivers has done a total of 100 or more races (sum of all driver races) then // IF1StatsWeighting must be invoked with the required parameters. // winPercentage is the teams drivers win % (summed result of each driver) // numberOfRaces is the teams drivers races (sum of all driver races) // ONLY make this call if the drivers total races >= 100. // The result must be stored in the DriverWeighting field of team value. // If the drivers have done less than 100 races the DriverWeighting must be set to 0. // Note // Team Win % is Team.Victories over Team.Races // Driver Win % is Driver.Wins over Driver.Races // Only calculate win % for drivers. Exclude test drivers public IEnumerable <TeamValue> TeamWinPercentage(int teamId = 0) { IEnumerable <TeamValue> teamValueCollection = new List <TeamValue>(); if (teamId != 0) { // Get team for teamId Team team = TeamReferenceData.FirstOrDefault(t => t.Id == teamId); if (team == null) { return(teamValueCollection); } // I assume the client want these default values set TeamValue teamValue = new TeamValue(); teamValue.Id = team.Id; teamValue.Name = team.Name; // Team teamWinsPercentage teamValue.TeamWinsPercentage = (((double)team.Victories) / team.Races) * 100; // Team Drivers IEnumerable <Driver> teamDrivers = team.Drivers.Where(driver => driver.GetType().Name != "TestDriver"); // Team Drivers Wins double teamsDriversWins = teamDrivers.Sum(driverWins => driverWins.Wins); // Team Drivers Races double teamsDriversRaces = teamDrivers.Sum(driverWins => driverWins.Races); // Team DriverWeighting if (teamsDriversRaces >= 100) { teamValue.DriverWeighting = StatsWeighting.Apply(teamValue.TeamWinsPercentage, (int)teamsDriversRaces);; } else { teamValue.DriverWeighting = 0; } // Team DriverWinPercentage teamValue.DriverWinPercentage = ((double)teamsDriversWins / teamsDriversRaces) * 100; ((List <TeamValue>)teamValueCollection).Add(teamValue); return(teamValueCollection); } else { int maxTeams = TeamReferenceData.Count(); IEnumerator <Team> teamIterator = TeamReferenceData.GetEnumerator(); TeamValue teamValue; while (teamIterator.MoveNext()) { teamValue = new TeamValue(); teamValue.Id = teamIterator.Current.Id; teamValue.Name = teamIterator.Current.Name; teamValue.TeamWinsPercentage = (((double)teamIterator.Current.Victories) / teamIterator.Current.Races) * 100; IEnumerable <Driver> currentTeamDrivers = teamIterator.Current.Drivers.Where(driver => driver.GetType().Name != "TestDriver"); double teamsDriversWins = currentTeamDrivers.Sum(driverWins => driverWins.Wins); double teamsDriversRaces = currentTeamDrivers.Sum(driverWins => driverWins.Races); if (teamsDriversRaces >= 100) { teamValue.DriverWeighting = StatsWeighting.Apply(teamValue.TeamWinsPercentage, (int)teamsDriversRaces);; } else { teamValue.DriverWeighting = 0; } teamValue.DriverWinPercentage = (((double)teamsDriversWins) / teamsDriversRaces) * 100; ((List <TeamValue>)teamValueCollection).Add(teamValue); } ((List <TeamValue>)teamValueCollection).Sort(new TeamValueComparer()); return(teamValueCollection); } }