/// <exception cref="MatchResultScoresSameException"></exception> /// <exception cref="MatchResultZeroTeamScoreException"></exception> public void SaveMatchResult(Fixture fixtureToUpdate, List <PlayerFixture> homePlayerStats, List <PlayerFixture> awayPlayerStats, List <bool> homeHasPlayed, List <bool> awayHasPlayed, int homeMvpPlayerId, int awayMvpPlayerId) { ValidateMatchResultFixture(fixtureToUpdate); ValidatePlayerFixtures(homePlayerStats, homeHasPlayed, fixtureToUpdate.HomeTeamScore, fixtureToUpdate.HasPlayerStats.YesNoToBool(), homeMvpPlayerId); ValidatePlayerFixtures(awayPlayerStats, awayHasPlayed, fixtureToUpdate.AwayTeamScore, fixtureToUpdate.HasPlayerStats.YesNoToBool(), awayMvpPlayerId); // Assume that if the fixture is cancelled and the result has been added, the game // is no longer cancelled if (fixtureToUpdate.IsCancelled == "Y" /*&& model.HomeTeamScore != null*/) { fixtureToUpdate.IsCancelled = "N"; } // Set the fixture as played fixtureToUpdate.IsPlayed = "Y"; // TODO Add tickbox for played? if (fixtureToUpdate.ResultAddedDate == null) { fixtureToUpdate.ResultAddedDate = DateTime.Now; } // Set MVP if (fixtureToUpdate.HasPlayerStats.YesNoToBool()) { SetFixtureMvp(homePlayerStats, homeMvpPlayerId); SetFixtureMvp(awayPlayerStats, awayMvpPlayerId); } using (TransactionScope scope = new TransactionScope()) { competitionRepository.UpdateFixture(fixtureToUpdate); // Delete existing records that have been marked as not played statsService.DeleteExistingPlayerFixturesThatDidNotPlay(homePlayerStats, homeHasPlayed); statsService.DeleteExistingPlayerFixturesThatDidNotPlay(awayPlayerStats, awayHasPlayed); // Remove players that did not player from the list so we only update those who played RemovePlayersThatDidNotPlay(homePlayerStats, homeHasPlayed); RemovePlayersThatDidNotPlay(awayPlayerStats, awayHasPlayed); // Save player fixture records. Pass whether each player has played so players that haven't played are not // updated. If this wasn't passed in the PlayerFixture records would be delete by the method call above then // inserted again by the call below // The logic is a bit odd here. All players, played or unplayed must be passed to the update // season/league/career methods so they are always update (e.g. if a PlayerFixture record is saved then marked // unplayed it is deleted, but the player record must still be passed to the update stats method to recalculate // the stats statsService.InsertOrUpdatePlayerFixturesThatHavePlayed(homePlayerStats, homeHasPlayed); statsService.InsertOrUpdatePlayerFixturesThatHavePlayed(awayPlayerStats, awayHasPlayed); //statsService.Commit(); // Update player league stats statsService.UpdatePlayerLeagueStats(homePlayerStats, fixtureToUpdate.HomeTeamLeague.League); statsService.UpdatePlayerLeagueStats(awayPlayerStats, fixtureToUpdate.HomeTeamLeague.League); // Update player season stats Season currentSeason = GetCurrentSeason(); statsService.UpdatePlayerSeasonStats(homePlayerStats, currentSeason); statsService.UpdatePlayerSeasonStats(awayPlayerStats, currentSeason); // Update player career stats statsService.UpdatePlayerCareerStats(homePlayerStats); statsService.UpdatePlayerCareerStats(awayPlayerStats); // Update team stats UpdateTeamLeagueStats(fixtureToUpdate.HomeTeamLeague.Id); UpdateTeamLeagueStats(fixtureToUpdate.AwayTeamLeague.Id); Commit(); //teamLeagueRepository.SaveOrUpdate(teamLeagueRepository.UpdateTeamLeagueStats(fixtureToUpdate.HomeTeamLeague.Id)); //teamLeagueRepository.DbContext.CommitChanges(); //teamLeagueRepository.SaveOrUpdate(teamLeagueRepository.UpdateTeamLeagueStats(fixtureToUpdate.AwayTeamLeague.Id)); scope.Complete(); } }