Exemple #1
0
        public bool DeleteGame(int id)
        {
            try
            {
                // get the game
                var game = GameHandler.GetGame(id);

                if (game == null)
                {
                    throw new ArgumentException("No such game");
                }

                // delete the game (incl game scores and player ratings)
                GameHandler.DeleteGame(game);

                // delete ratings from later games
                RatingHandler.DeleteRatingsAfter(game.PlayerRatings[0].Id, deleteDefaultRatings: false);

                // reset current ratings in PlayerSeasons
                var lastPlayerRatings    = RatingHandler.GetLatestRatingsPerPlayerSeason();
                var updatedPlayerSeasons = lastPlayerRatings
                                           .Where(pr => !pr.PlayerSeason.Season.HasEnded(game.Created)) // current and future seasons
                                           .Select(pr =>
                {
                    var playerSeason           = pr.PlayerSeason;
                    playerSeason.Season        = null;
                    playerSeason.Rating        = pr.Rating;
                    playerSeason.RatingChange  = pr.RatingChange;
                    playerSeason.Wins          = pr.Wins;
                    playerSeason.Losses        = pr.Losses;
                    playerSeason.CurrentStreak = pr.CurrentStreak;
                    return(playerSeason);
                })
                                           .ToList();

                PlayerHandler.UpdatePlayerSeasons(updatedPlayerSeasons.Where(ps => ps.GamesPlayed > 0));
                PlayerHandler.DeletePlayerSeasons(updatedPlayerSeasons.Where(ps => ps.GamesPlayed == 0));

                // get all games after the deleted game
                var games = GameHandler.GetGamesAfter(game.Id, SortOrder.Ascending);

                // recalculate ratings
                games.ForEach(g => Ratings.CalculateNewRatings(g));

                // delete players with no games
                var players = PlayerHandler.GetAllPlayers()
                              .Where(p => p.Seasons.Count == 0);
                PlayerHandler.DeletePlayers(players);

                return(true);
            }
            catch (Exception /*ex*/)
            {
                return(false);
            }
        }