/// <summary> /// Adds a new Match, marked as played, and updates the rankings of each player based on who won, and their current rankings /// </summary> /// <param name="team1">Team 1's ID</param> /// <param name="team2">Team 2's ID</param> /// <param name="matchSets">The sets played</param> /// <returns>A Match entity expressing the results of the match</returns> public async Task <Match> Played(int team1, int team2, IList <MatchSet> matchSets) { var match = await AddMatch(DateTime.Now, team1, team2, matchSets); //match = _gameOnContext.Matches // .Include(x => x.WinnerPlayer) // .Include(x => x.LoserPlayer) // .Include(x => x.PlayerOne) // .Include(x => x.PlayerTwo) // .First(x => x.Id == match.Id); // Get current leader var leader = await _teamService.GetTopRankingTeam(match.TeamOne.IsDouble); // Calculate new rankings _ratingHelper.UpdateMatchPlayersRanks(match); await _gameOnContext.SaveChangesAsync(); await SaveRankHistory(match.TeamOne); await SaveRankHistory(match.TeamTwo); // timeline the match _timelineService.AddMessage($"{match.WinnerTeam.Name} beat {match.LoserTeam.Name} ({match.FinishScore})"); // check if leader has changed, if so add to timeline var newLeader = await _teamService.GetTopRankingTeam(match.TeamOne.IsDouble); if (leader != newLeader) { _timelineService.AddMessage($"{match.WinnerTeam.Name} has replaced {leader.Name} as the new top ranking team with a rating of {match.WinnerTeam.CurrentRank}!"); } return(match); }