Exemplo n.º 1
0
 private static SeasonPlayer ConvertDataTableToSeasonPlayer(DataRow row)
 {
     var player = new SeasonPlayer();
     player.PeopleID = Convert.ToInt32(row["PeopleID"]);
     player.PlayerID = Convert.ToInt32(row["PlayerID"]);
     player.DraftID = row["PlayerID"].ToString();
     //player.LastName = row["LastName"].ToString();
     //player.FirstName = row["FirstName"].ToString();
     player.Name = row["Name"].ToString();
     player.BirthDate = Convert.ToDateTime(row["BirthDate"].ToString());
     return player;
 }
Exemplo n.º 2
0
        private void SeedTestData()
        {
            var options = new DbContextOptionsBuilder <RankingsTableDbContext>();

            options.UseSqlServer(this.ConnStr);
            using (var db = new RankingsTableDbContext(options.Options))
            {
                if (!db.Players.Any(p => p.Name == "Ben"))
                {
                    var season = new Season {
                        Id = Guid.NewGuid(), Number = 1
                    };

                    var player1 = new Player {
                        Id = Guid.NewGuid(), Name = "Ben"
                    };
                    var player2 = new Player {
                        Id = Guid.NewGuid(), Name = "Foo"
                    };

                    var seasonPlayer1 = new SeasonPlayer {
                        Id = Guid.NewGuid(), Season = season, Player = player1
                    };
                    var seasonPlayer2 = new SeasonPlayer {
                        Id = Guid.NewGuid(), Season = season, Player = player2
                    };
                    player1.SeasonPlayers.Add(seasonPlayer1);
                    player2.SeasonPlayers.Add(seasonPlayer2);

                    var fixture = new Fixture
                    {
                        Id         = Guid.NewGuid(),
                        Season     = season,
                        HomePlayer = seasonPlayer1,
                        HomeGoals  = 1,
                        AwayPlayer = seasonPlayer2,
                        AwayGoals  = 3
                    };
                    season.Fixtures.Add(fixture);
                    seasonPlayer1.HomeFixtures.Add(fixture);
                    seasonPlayer2.AwayFixtures.Add(fixture);

                    db.Seasons.Add(season);
                    db.Players.Add(player1);
                    db.Players.Add(player2);

                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Refreshes or adds a cache for a player
        /// </summary>
        /// <param name="osuUserId">Osu User Id</param>
        /// <param name="c">DB Context</param>
        /// <param name="guildId">Discord Guild Id</param>
        /// <param name="getOverallRatingAction">Function to calculate the overall rating</param>
        public static void ForceRefreshPlayerCache(long osuUserId, DBContext c, long guildId, Func <SeasonPlayer, DBContext, double> getOverallRatingAction)
        {
            if (c == null)
            {
                throw new ArgumentNullException(nameof(c));
            }
            else if (getOverallRatingAction == null)
            {
                throw new ArgumentNullException(nameof(getOverallRatingAction));
            }

            SeasonPlayerCardCache cardCache = ClearPlayerCache(osuUserId, c, guildId);

            SeasonPlayer player = c.SeasonPlayer.FirstOrDefault(p => p.OsuUserId == osuUserId && p.DiscordGuildId == guildId);

            if (player == null)
            {
                return;
            }

            List <SeasonResult> results = c.SeasonResult.Where(sr => sr.DiscordGuildId == guildId).ToList();

            if (results.Count == 0)
            {
                c.SeasonPlayerCardCache.Remove(cardCache);
                c.SaveChanges();
                return;
            }

            List <long> resultIds = results.Select(r => r.Id).ToList();

            List <SeasonScore> scores = c.SeasonScore.Where(s => s.SeasonPlayerId == player.Id).ToList();

            scores = scores.Where(s => resultIds.Contains(s.SeasonResultId)).ToList();

            cardCache.DiscordGuildId = guildId;

            foreach (SeasonScore score in scores)
            {
                cardCache.AverageAccuracy    += score.Accuracy;
                cardCache.AverageCombo       += score.MaxCombo;
                cardCache.AverageMisses      += score.CountMiss;
                cardCache.AverageScore       += score.Score;
                cardCache.AveragePerformance += score.GeneralPerformanceScore;

                if (score.HighestGeneralPerformanceScore)
                {
                    cardCache.MatchMvps++;
                }
            }

            if (cardCache.AverageAccuracy > 0)
            {
                cardCache.AverageAccuracy /= scores.Count;
            }

            if (cardCache.AverageCombo > 0)
            {
                cardCache.AverageCombo /= scores.Count;
            }

            if (cardCache.AverageMisses > 0)
            {
                cardCache.AverageMisses /= scores.Count;
            }

            if (cardCache.AverageScore > 0)
            {
                cardCache.AverageScore /= scores.Count;
            }

            if (cardCache.AveragePerformance > 0)
            {
                cardCache.AveragePerformance /= scores.Count;
                cardCache.OverallRating       = getOverallRatingAction(player, c);
            }

            cardCache.LastUpdated = DateTime.UtcNow;
            cardCache.TeamName    = player.TeamName;

            cardCache.Username = player.LastOsuUsername ?? "not found";

            c.SeasonPlayerCardCache.Update(cardCache);
            c.SaveChanges();
        }