private Tuple <uint, uint> DebugRankingForPlayer(uint id, uint playerId, string date) { if (!DateTime.TryParse(date, out DateTime realDateEnd)) { throw new ArgumentException(Messages.InvalidInputDateException, nameof(date)); } var rankingVersion = RankingVersionPivot.Get(id); if (rankingVersion == null) { throw new ArgumentException(Messages.RankingRulesetNotFoundException, nameof(id)); } var player = PlayerPivot.Get(playerId); if (player == null) { return(null); } // Ensures monday. while (realDateEnd.DayOfWeek != DayOfWeek.Monday) { realDateEnd = realDateEnd.AddDays(1); } SqlMapper.LoadMatches((uint)(realDateEnd.Year - 1)); SqlMapper.LoadMatches((uint)realDateEnd.Year); return(rankingVersion.DebugRankingForPlayer(player, realDateEnd)); }
private void GenerateRanking(uint id) { var rankingVersion = RankingVersionPivot.Get(id); if (rankingVersion == null) { throw new ArgumentException(Messages.RankingRulesetNotFoundException, nameof(id)); } // Gets the latest monday with a computed ranking. var startDate = MySqlTools.ExecuteScalar(GlobalAppConfig.GetConnectionString(), "SELECT MAX(date) FROM ranking WHERE version_id = @version", RankingVersionPivot.OPEN_ERA_BEGIN, new MySqlParameter("@version", MySqlDbType.UInt32) { Value = id }); // Monday one day after the latest tournament played (always a sunday). var dateStop = (EditionPivot.GetLatestEditionDateEnding() ?? startDate).AddDays(1); // Loads matches from the previous year. SqlMapper.LoadMatches((uint)startDate.Year - 1); using (var sqlConnection = new MySqlConnection(GlobalAppConfig.GetConnectionString())) { sqlConnection.Open(); using (var sqlCommand = sqlConnection.CreateCommand()) { sqlCommand.CommandText = MySqlTools.GetSqlInsertStatement("ranking", new List <string> { "player_id", "date", "points", "ranking", "version_id", "editions" }); sqlCommand.Parameters.Add("@player_id", MySqlDbType.UInt32); sqlCommand.Parameters.Add("@date", MySqlDbType.DateTime); sqlCommand.Parameters.Add("@points", MySqlDbType.UInt32); sqlCommand.Parameters.Add("@ranking", MySqlDbType.UInt32); sqlCommand.Parameters.Add("@version_id", MySqlDbType.UInt32); sqlCommand.Parameters.Add("@editions", MySqlDbType.UInt32); sqlCommand.Prepare(); // Static. sqlCommand.Parameters["@version_id"].Value = id; // Puts in cache the triplet player/edition/points, no need to recompute each week. var cachePlayerEditionPoints = new Dictionary <KeyValuePair <PlayerPivot, EditionPivot>, uint>(); // For each week until latest date. startDate = startDate.AddDays(7); while (startDate <= dateStop) { // Loads matches from the current year (do nothing if already done). SqlMapper.LoadMatches((uint)startDate.Year); var playersRankedThisWeek = rankingVersion.ComputePointsForPlayersInvolvedAtDate(startDate, cachePlayerEditionPoints); // Static for each player. sqlCommand.Parameters["@date"].Value = startDate; // Inserts each player. int rank = 1; foreach (var player in playersRankedThisWeek.Keys) { sqlCommand.Parameters["@player_id"].Value = player.Id; sqlCommand.Parameters["@points"].Value = playersRankedThisWeek[player].Item1; sqlCommand.Parameters["@editions"].Value = playersRankedThisWeek[player].Item2; sqlCommand.Parameters["@ranking"].Value = rank; sqlCommand.ExecuteNonQuery(); rank++; } startDate = startDate.AddDays(7); } } } }