public IActionResult Put(MatchReportDTO matchReportDTO) { var result = _matchService.UpdateScoreReport(matchReportDTO); return(Ok(_mapper.ToDTO(result))); }
public Match UpdateScoreReport(MatchReportDTO matchReportDto) { //Find the corresponding match var match = _matchRepository.FindById(matchReportDto.matchId); var oldMatch = Match.Clone(match); var draw = _drawRepository.FindById(match.DrawId); //Format the string and check for errors var p1Points = new List <int>(); var p2Points = new List <int>(); var gameScores = matchReportDto.score.Split(" "); int validGameCount = 0; for (int i = 0; i < gameScores.Length; i++) { try { var pointStrings = gameScores[i].Split("/"); var p1 = Int32.Parse(pointStrings[0]); var p2 = Int32.Parse(pointStrings[1]); p1Points.Add(p1); p2Points.Add(p2); validGameCount++; } catch (Exception e) { //When formatting goes wrong - break the for loop - this might be when we encounter "__/__" break; } } //Check that number of games is as expected var maxGames = draw.Games; var minGames = (1 + maxGames) / 2; if (validGameCount > maxGames) //Too many games { throw new TournamentSoftwareException( $"{validGameCount} valid games in score-string {1}, but a maximum of {maxGames} allowed."); } //Too few games if (validGameCount < minGames) { throw new TournamentSoftwareException( $"{validGameCount} valid games in score-string {matchReportDto.score}," + $" but at least {minGames} were required in draw with up to {draw.Games} games."); } //Check that points correspond to expected values for (int i = 0; i < validGameCount; i++) { var overMax = p1Points[i] > draw.Points || p2Points[i] > draw.Points; if (draw.TieBreaks) //If tie breaks are allowed //There can be too many points, but only if there is excactly 2 points between scores { if (overMax && Math.Abs(p1Points[i] - p2Points[i]) != 2) { throw new TournamentSoftwareException( $"Score in score-string {matchReportDto.score} does not follow tie break rules." + $"A max of {draw.Points} is allowed, unless it is a tie break."); } } else //If tie breaks are not allowed - no score must be greater than max { if (overMax) { throw new TournamentSoftwareException( $"Too high point score in score-string {matchReportDto.score}." + $"A max of {draw.Points} is allowed, since tie breaks are disallowed."); } } } //Find the winner int p1Games = 0, p2Games = 0, p1PointsTotal = 0, p2PointsTotal = 0; for (int i = 0; i < validGameCount; i++) { p1PointsTotal += p1Points[i]; p2PointsTotal += p2Points[i]; if (p1Points[i] > p2Points[i]) { p1Games++; } else if (p1Points[i] < p2Points[i]) { p2Games++; } else { throw new TournamentSoftwareException( $"Game with equal score in score-string {matchReportDto.score}"); } } if (p1Games > p2Games) { match.P1Won = true; } else if (p1Games < p2Games) { match.P1Won = false; } else { //Winner is decided by who won the most points match.P1Won = p1PointsTotal > p2PointsTotal; } //Set variables match.P1PointsArray = p1Points.ToArray(); match.P2PointsArray = p2Points.ToArray(); match.P1Games = p1Games; match.P2Games = p2Games; match.Status = Status.FINISHED; //Update the match using existing Update method and return return(Update(match, oldMatch)); }