/// <summary> /// Combines and matches existing records with the updated records. /// </summary> /// <param name="updatedRecords">The calculated records with new wins, losses, and ties.</param> /// <param name="existingRecords">The existing records that will need to be updated.</param> /// <returns>List{UserRecord}. The collection of user records that should be updated or created.</returns> public static List<UserRecord> CombineExistingRecordsWithUpdatedRecords(IList<UserRecord> updatedRecords, IList<UserRecord> existingRecords) { var records = new List<UserRecord>(); // Iterate through the updated records and checks if the user has a matching record that matches the record type foreach (var record in updatedRecords) { UserRecord recordToUpdate = null; if (record.RecordType == RecordType.Lifetime) { recordToUpdate = existingRecords.FirstOrDefault(existingRecord => MatchingLifetimeCondition(existingRecord, record)); } else if (record.RecordType == RecordType.Season) { recordToUpdate = existingRecords.FirstOrDefault(existingRecord => MatchingSeasonCondition(existingRecord, record)); } else if (record.RecordType == RecordType.SeasonType) { recordToUpdate = existingRecords.FirstOrDefault(existingRecord => MatchingSeasonTypeCondition(existingRecord, record)); } else if (record.RecordType == RecordType.Week) { recordToUpdate = existingRecords.FirstOrDefault(existingRecord => MatchingWeekCondition(existingRecord, record)); } // Create a new record since the user does not have an existing record for the record type if (recordToUpdate == null) { recordToUpdate = new UserRecord { Losses = record.Losses, RecordType = record.RecordType, Season = record.Season, SeasonType = record.SeasonType, Ties = record.Ties, UserId = record.UserId, Week = record.Week, Wins = record.Wins }; } // Update the existing record type's wins, losses, and ties. else { recordToUpdate.Losses = record.Losses; recordToUpdate.Ties = record.Ties; recordToUpdate.Wins = record.Wins; } records.Add(recordToUpdate); } return records; }
private static UserRecord MapUserRecord(MySqlDataReader dr) { var record = new UserRecord(); record.Id = dr.GetInt64("id"); record.LastUpdated = dr.GetDateTime("lastUpdated"); record.Losses = dr.GetInt32("losses"); record.RecordType = (RecordType)dr.GetInt32("type"); record.Ties = dr.GetInt32("ties"); record.UserId = dr.GetInt64("userId"); record.UserName = dr.GetString("userName"); record.Wins = dr.GetInt32("wins"); if (!dr.IsDBNull(dr.GetOrdinal("season"))) { record.Season = dr.GetInt32("season"); } if (!dr.IsDBNull(dr.GetOrdinal("seasonType"))) { record.SeasonType = (SeasonType) dr.GetInt32("seasonType"); } if (!dr.IsDBNull(dr.GetOrdinal("week"))) { record.Week = dr.GetInt32("week"); } return record; }
private static UserRecord CreateRecord(int id, int userId, RecordType recordType, int wins, int losses, int ties, int? season = null, SeasonType? seasonType = null, int? week = null) { var record = new UserRecord { Id = id, Losses = losses, RecordType = recordType, Season = 2013, Ties = ties, UserId = userId, Wins = wins }; if (recordType == RecordType.Season) { record.Season = season; } else if (recordType == RecordType.SeasonType) { record.Season = season; record.SeasonType = seasonType; } else if (recordType == RecordType.Week) { record.Season = season; record.SeasonType = seasonType; record.Week = week; } return record; }
private static bool MatchingWeekCondition(UserRecord existingRecord, UserRecord record) { return existingRecord.UserId == record.UserId && existingRecord.RecordType == record.RecordType && existingRecord.SeasonType == record.SeasonType && existingRecord.Season == record.Season && existingRecord.Week == record.Week; }
private static bool MatchingLifetimeCondition(UserRecord existingRecord, UserRecord record) { return existingRecord.UserId == record.UserId && existingRecord.RecordType == record.RecordType && existingRecord.SeasonType == record.SeasonType; }
/// <summary> /// Iterates through all the games, options, and picks for the specified season. Then creates new /// records for all users who have made picks and calculates the wins, losses, and ties. /// </summary> /// <param name="season">The season to calculate records for.</param> /// <returns>List{UserRecord}.</returns> public List<UserRecord> CalculateRecordsForSeason(int season) { // Get all the options for the season and the users who picked them var gameOptionPicks = _recordsRepository.GetGameOptionPicksForSeason(season); // Extract the unique user Ids from the picks var userIds = gameOptionPicks.SelectMany(gameOptionPick => gameOptionPick.Options.SelectMany(option => option.UserPicks).Select(pick => pick.UserId)).Distinct(); // Create a dictionary with they key being the user id and the value initalized to records for their season and lifetime var userRecords = userIds.ToDictionary(userId => userId, userId => new List<UserRecord> { new UserRecord { UserId = userId, Season = season, RecordType = RecordType.Season }, new UserRecord { UserId = userId, RecordType = RecordType.Lifetime } }); // Iterate through all the games foreach (var game in gameOptionPicks) { // Iterate through all the options for the game foreach (var optionPick in game.Options) { // Iterate through all the users who selected the option foreach (var pick in optionPick.UserPicks) { var outcome = optionPick.Outcome; // Temporary container that holds the records we want to update for the user associated with the pick we are iterating over var records = new List<UserRecord>(); // See if the user has a weekly record for game's season, season type, and week var weeklyRecord = userRecords[pick.UserId].FirstOrDefault(record => record.RecordType == RecordType.Week && record.Week == game.Week && record.SeasonType == game.SeasonType && record.Season == game.Season); // Create a new weekly record if the user does not have a record for the season, season type, and week if (weeklyRecord == null) { weeklyRecord = new UserRecord { RecordType = RecordType.Week, Season = season, SeasonType = game.SeasonType, UserId = pick.UserId, Week = game.Week }; // Add the new weekly records to the dictionary of user Ids to records userRecords[pick.UserId].Add(weeklyRecord); } // See if the user has a season type record for the game's season and season type var seasonTypeRecord = userRecords[pick.UserId].FirstOrDefault(record => record.RecordType == RecordType.SeasonType && record.SeasonType == game.SeasonType && record.Season == game.Season); // Create a new season type record if the user does not have a record for the season and season type if (seasonTypeRecord == null) { seasonTypeRecord = new UserRecord { RecordType = RecordType.SeasonType, Season = season, SeasonType = game.SeasonType, UserId = pick.UserId, }; // Add the new season type record to the dictionary of user Ids to records userRecords[pick.UserId].Add(seasonTypeRecord); } // Add all the user records to the temporary container of records for the user records.AddRange(userRecords[pick.UserId].Where(record => record.RecordType == RecordType.Season || record.RecordType == RecordType.Lifetime)); records.Add(weeklyRecord); records.Add(seasonTypeRecord); // Update all the records for the user with either a win, loss, or tie SetRecord(outcome, records); } } } return userRecords.Values.SelectMany(records => records).ToList(); }