Exemplo n.º 1
0
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("OrphanedPlayerRecords");

            if (task == null)
            {
                _logger.Debug("Can't update player names - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var playerList      = _playerRepository.GetAll();
            var uniquePlayerIds = _playerRepository.GetAllUniquePlayerIds();
            var removePlayerIds = playerList.Select(p => p.Id).Where(playerId => !uniquePlayerIds.Contains(playerId)).ToList();

            _logger.Debug(string.Format("Unique players: {0}, total players in DB: {1}. Players to remove: {2}", uniquePlayerIds.Count, playerList.Count, removePlayerIds.Count));
            if (removePlayerIds.Any())
            {
                _playerRepository.RemoveOrphanPlayers(removePlayerIds);
            }
            else
            {
                _logger.Debug("No orphaned player records to remove!");
            }
        }
Exemplo n.º 2
0
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("EncounterNpcRecords");

            if (task == null)
            {
                _logger.Debug("Can't update EncounterNpcs - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var encounters = _encounterRepository.GetEncountersMissingNpcRecords(10);

            if (!encounters.Any())
            {
                return;
            }

            _logger.Debug(string.Format("EncounterNpc update: Found {0} encounters that need updating!", encounters.Count));

            foreach (var encounter in encounters)
            {
                int encounterId = encounter.Id;
                // Find and add the NPCs for this encounter
                var encounterNpcs = _encounterRepository.GetEncounterNpcsFromEncounterInfo(encounterId);
                if (encounterNpcs.Any())
                {
                    foreach (var encounterNpc in encounterNpcs)
                    {
                        encounterNpc.EncounterId = encounterId;
                        if (string.IsNullOrEmpty(encounterNpc.NpcName))
                        {
                            encounterNpc.NpcName = "UNKNOWN NPC";
                        }
                    }
                    encounterNpcs.ForEach(e => e.EncounterId = encounterId);
                    var addNpcResult = _encounterRepository.AddEncounterNpcs(encounterNpcs);
                    _logger.Debug(addNpcResult.Success
                        ? string.Format("Successfully added {0} EncounterNpc records for {1}", encounterNpcs.Count, encounterId)
                        : string.Format("An error occurred while adding EncounterNpc records for {1}: {0}", addNpcResult.Message, encounterId));
                }
            }
            _logger.Debug("Finished looping through encounters to add EncounterNpc records");
        }
Exemplo n.º 3
0
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("UpdatePlayerNames");

            if (task == null)
            {
                _logger.Debug("Can't update player names - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var playersToUpdate = _playerRepository.PlayersWithShardNamesInPlayerName();

            if (!playersToUpdate.Any())
            {
                _logger.Info("Player name update skipped - no names to change!");

                return;
            }
            List <Player> updatedPlayers = new List <Player>();

            foreach (var player in playersToUpdate)
            {
                var nameSplit = player.Name.Split('@');
                if (nameSplit.Length == 2)
                {
                    player.Name  = nameSplit[0];
                    player.Shard = nameSplit[1];
                    updatedPlayers.Add(player);
                }
            }

            var result = _playerRepository.UpdatePlayerNameAndShard(updatedPlayers);

            _logger.Debug(result.Success
                ? "Player name update succeeded."
                : string.Format("Player name update failed. {0}", result.Message));
        }
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("MarkOldWipesForDeletion");

            if (task == null)
            {
                _logger.Debug("Can't mark old wipes for deletion - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var dateLimit  = DateTime.Today.Subtract(new TimeSpan(90, 0, 0, 0));
            var encounters = _encounterRepository.GetUnsuccessfulEncountersBefore(dateLimit);

            int maxEncounters = 500;

            while (encounters.Any())
            {
                var encounterList = encounters.Count > maxEncounters?encounters.Take(maxEncounters).ToList() : encounters;

                var result = _encounterRepository.MarkEncountersForDeletion(encounterList.Select(e => e.Id).ToList(), "ScheduledTask");
                if (result.Success)
                {
                    _logger.Debug(string.Format("Marked {0} encounters for deletion due to age and failure", encounterList.Count));
                }
                else
                {
                    _logger.Debug(string.Format("Error while marking encounters for deletion: {0}", result.Message));
                    break;
                }

                encounters.RemoveRange(0, encounterList.Count);
            }
        }
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("ZeroDurationEncounters");

            if (task == null)
            {
                _logger.Debug("Can't update zero-duration encounters - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task (ZeroDurationEncounters), so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var encounterIds = _encounterRepository.GetEncounterIdsWithNoDuration();

            foreach (var encId in encounterIds)
            {
                var encSeconds = _encounterRepository.GetTotalSecondsFromDamageDone(encId);
                if (encSeconds > 5)
                {
                    TimeSpan encTs = new TimeSpan(0, 0, 0, encSeconds);
                    _encounterRepository.UpdateDurationForEncounter(encId, encTs);
                }
                else
                {
                    // Remove this encounter from the database entirely
                    _logger.Debug(string.Format("Completely removing encounter {0} as it's not valid or has a 0-second duration that can't be updated.", encId));
                    _encounterRepository.RemoveEncounter("ScheduledTask", encId);
                }
            }
        }
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("EncounterPlayerStatistics");

            if (task == null)
            {
                _logger.Debug("Can't update EncounterPlayerStatistics - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            Stopwatch sw = new Stopwatch();

            // Don't bother saving stats for encounters that aren't valid for rankings.
            // There's no point saving DPS/HPS/APS for encounters that are not successful, either.
            var encList = _encounterRepository.GetEncountersMissingPlayerStatistics(100);

            if (!encList.Any())
            {
                _logger.Debug("Found no encounters that require statistics updates!");
            }
            else
            {
                _logger.Debug(string.Format("Found {0} encounters to save statistics for", encList.Count));
                // Optionally filter our list for a specific bossfight here for testing
                //encList = encList.Where(e => e.BossFightId == 41).ToList();
                //_logger.Debug(string.Format("Updating stats for a filtered set of {0} encounters", encList.Count));

                // Get the list of NPC names to check against when determining single target dps
                var bossFightSingleTargetDetails = _bossFightSingleTargetDetailRepository.GetAll();


                var addPlayerStats = new List <Database.Models.EncounterPlayerStatistics>();

                #region Loop through encounters that we previously identified as needing stats

                for (var i = 1; i <= encList.Count; i++)
                {
                    sw.Reset();
                    sw.Start();
                    var enc = encList[i - 1];

                    if ((int)enc.Duration.TotalSeconds == 0)
                    {
                        _logger.Debug(
                            string.Format(
                                "Skipping stats for encounter {0}/{1} as it has a 0-second duration that needs to be fixed.",
                                i, encList.Count));
                        continue;
                    }

                    //_logger.Debug(string.Format("Calculating stats for encounter {0}/{1}", i, encList.Count));
                    addPlayerStats = new List <Database.Models.EncounterPlayerStatistics>();
                    // DPS/HPS/APS

                    #region Damage

                    foreach (var dmg in _encounterRepository.GetOverviewPlayerDamageDone(enc.Id))
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId = enc.Id,
                                PlayerId    = dmg.PlayerId,
                                Deaths      = 0,
                                APS         = 0,
                                DPS         = dmg.TotalToNpcs / (long)enc.Duration.TotalSeconds,
                                HPS         = 0,
                            });
                        }
                        else
                        {
                            thisPlayer.DPS = dmg.TotalToNpcs / (long)enc.Duration.TotalSeconds;
                        }
                    }

                    #endregion

                    #region Healing

                    foreach (var heal in _encounterRepository.GetOverviewPlayerHealingDone(enc.Id))
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == heal.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId = enc.Id,
                                PlayerId    = heal.PlayerId,
                                Deaths      = 0,
                                APS         = 0,
                                DPS         = 0,
                                HPS         = (heal.TotalToOtherPlayers + heal.TotalToSelf) / (long)enc.Duration.TotalSeconds
                            });
                        }
                        else
                        {
                            thisPlayer.HPS = (heal.TotalToOtherPlayers + heal.TotalToSelf) /
                                             (long)enc.Duration.TotalSeconds;
                        }
                    }

                    #endregion

                    #region Shielding

                    foreach (var shield in _encounterRepository.GetOverviewPlayerShieldingDone(enc.Id))
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == shield.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId = enc.Id,
                                PlayerId    = shield.PlayerId,
                                Deaths      = 0,
                                APS         = (shield.TotalToSelf + shield.TotalToOtherPlayers) / (long)enc.Duration.TotalSeconds,
                                DPS         = 0,
                                HPS         = 0
                            });
                        }
                        else
                        {
                            thisPlayer.APS = (shield.TotalToOtherPlayers + shield.TotalToSelf) /
                                             (long)enc.Duration.TotalSeconds;
                        }
                    }

                    #endregion

                    #region Deaths

                    foreach (var playerDeathCount in _encounterRepository.CountDeathsPerPlayer(enc.Id))
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == playerDeathCount.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId = enc.Id,
                                PlayerId    = playerDeathCount.PlayerId,
                                Deaths      = playerDeathCount.Deaths,
                                APS         = 0,
                                DPS         = 0,
                                HPS         = 0
                            });
                        }
                        else
                        {
                            thisPlayer.Deaths = playerDeathCount.Deaths;
                        }
                    }

                    #endregion

                    // Top Hits

                    #region Damage

                    var topDmg = _encounterRepository.GetTopDamageHits(enc.Id);
                    foreach (var dmg in topDmg)
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId        = enc.Id,
                                PlayerId           = dmg.PlayerId,
                                TopDpsAbilityId    = dmg.TopDpsAbilityId,
                                TopDpsAbilityValue = dmg.TopDpsAbilityValue
                            });
                        }
                        else
                        {
                            thisPlayer.TopDpsAbilityId    = dmg.TopDpsAbilityId;
                            thisPlayer.TopDpsAbilityValue = dmg.TopDpsAbilityValue;
                        }
                    }

                    #endregion

                    #region Healing

                    var topHeal = _encounterRepository.GetTopHealingHits(enc.Id);
                    foreach (var heal in topHeal)
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == heal.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId        = enc.Id,
                                PlayerId           = heal.PlayerId,
                                TopHpsAbilityId    = heal.TopHpsAbilityId,
                                TopHpsAbilityValue = heal.TopHpsAbilityValue
                            });
                        }
                        else
                        {
                            thisPlayer.TopHpsAbilityId    = heal.TopHpsAbilityId;
                            thisPlayer.TopHpsAbilityValue = heal.TopHpsAbilityValue;
                        }
                    }

                    #endregion

                    #region Shielding

                    var topShield = _encounterRepository.GetTopShieldHits(enc.Id);
                    foreach (var shield in topShield)
                    {
                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == shield.PlayerId);
                        if (thisPlayer == null)
                        {
                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                            {
                                EncounterId        = enc.Id,
                                PlayerId           = shield.PlayerId,
                                TopApsAbilityId    = shield.TopApsAbilityId,
                                TopApsAbilityValue = shield.TopApsAbilityValue
                            });
                        }
                        else
                        {
                            thisPlayer.TopApsAbilityId    = shield.TopApsAbilityId;
                            thisPlayer.TopApsAbilityValue = shield.TopApsAbilityValue;
                        }
                    }

                    #endregion

                    #region Single target DPS

                    var bossFight = bossFightSingleTargetDetails.FirstOrDefault(b => b.BossFightId == enc.BossFightId);
                    if (bossFight != null)
                    {
                        if (bossFight.TargetName.Contains(","))
                        {
                            var npcsToCheck = bossFight.TargetName.Split(',');
                            foreach (var npcName in npcsToCheck)
                            {
                                var dmgRecords = _encounterRepository.GetPlayerSingleTargetDamageDone(enc.Id, npcName);
                                if (dmgRecords.Any())
                                {
                                    foreach (var dmg in dmgRecords)
                                    {
                                        if (dmg.PlayerId == 0)
                                        {
                                            continue;
                                        }

                                        var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId);
                                        if (thisPlayer == null)
                                        {
                                            addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                                            {
                                                EncounterId     = enc.Id,
                                                PlayerId        = dmg.PlayerId,
                                                SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds
                                            });
                                        }
                                        else
                                        {
                                            thisPlayer.SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds;
                                        }
                                    }
                                    // If we've found one name that matches records, then don't bother checking the other names (might save a few queries)
                                    break;
                                }
                            }
                        }
                        else
                        {
                            foreach (
                                var dmg in
                                _encounterRepository.GetPlayerSingleTargetDamageDone(enc.Id, bossFight.TargetName))
                            {
                                if (dmg.PlayerId == 0)
                                {
                                    continue;
                                }

                                var thisPlayer = addPlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId);
                                if (thisPlayer == null)
                                {
                                    addPlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                                    {
                                        EncounterId     = enc.Id,
                                        PlayerId        = dmg.PlayerId,
                                        SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds
                                    });
                                }
                                else
                                {
                                    thisPlayer.SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds;
                                }
                            }
                        }
                    }

                    #endregion

                    sw.Stop();
                    var generateTime = sw.Elapsed.ToString();
                    sw.Reset();
                    sw.Start();
                    // Finished generating statistics, save this encounter's stats now
                    var result = _encounterRepository.SaveEncounterPlayerStatistics(addPlayerStats);
                    sw.Stop();
                    _logger.Debug(
                        string.Format(
                            "Finished saving stats for encounter {0}. Stats generated in {1} and saved in {2}.", enc.Id,
                            generateTime, sw.Elapsed));
                    //addPlayerStats.AddRange(addPlayerStats);
                }

                #endregion
            }
            // Now that we have added stats that didn't exist before, check if there are any single target stats that haven't been calculated at all yet
            var stEncList = _encounterRepository.GetEncountersMissingSingleTargetDpsStatistics(100);
            if (!stEncList.Any())
            {
                _logger.Debug("Found no encounters that require ST DPS updates!");
            }
            else
            {
                _logger.Debug(string.Format("Found {0} encounters to save ST DPS statistics for", stEncList.Count));

                var updatePlayerStats            = new List <Database.Models.EncounterPlayerStatistics>();
                var bossFightSingleTargetDetails = _bossFightSingleTargetDetailRepository.GetAll();

                for (var i = 1; i <= stEncList.Count; i++)
                {
                    sw.Reset();
                    sw.Start();
                    var enc = stEncList[i - 1];

                    if ((int)enc.Duration.TotalSeconds == 0)
                    {
                        _logger.Debug(string.Format("Skipping ST DPS stats for encounter {0}/{1} as it has a 0-second duration that needs to be fixed.", i, stEncList.Count));
                        continue;
                    }

                    var bossFight = bossFightSingleTargetDetails.FirstOrDefault(b => b.BossFightId == enc.BossFightId);
                    if (bossFight == null)
                    {
                        _logger.Debug(string.Format("Skipping ST DPS stats for encounter {0}/{1} as a matching bossfight couldn't be found.", i, stEncList.Count));
                        continue;
                    }

                    _logger.Debug(string.Format("Calculating ST DPS stats for encounter {0}/{1}", i, stEncList.Count));
                    //updatePlayerStats = new List<EncounterPlayerStatistics>();

                    #region Damage
                    // Handle comma-separated names here!

                    if (bossFight.TargetName.Contains(","))
                    {
                        var npcsToCheck = bossFight.TargetName.Split(',');
                        foreach (var npcName in npcsToCheck)
                        {
                            var dmgRecords = _encounterRepository.GetPlayerSingleTargetDamageDone(enc.Id, npcName);
                            if (dmgRecords.Any())
                            {
                                foreach (var dmg in dmgRecords)
                                {
                                    if (dmg.PlayerId == 0)
                                    {
                                        continue;
                                    }

                                    var thisPlayer = updatePlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId && p.EncounterId == enc.Id);
                                    if (thisPlayer == null)
                                    {
                                        updatePlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                                        {
                                            EncounterId     = enc.Id,
                                            PlayerId        = dmg.PlayerId,
                                            SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds
                                        });
                                    }
                                }
                                // If we've found one name that matches records, then don't bother checking the other names (might save a few queries)
                                break;
                            }
                        }
                    }
                    else
                    {
                        foreach (var dmg in _encounterRepository.GetPlayerSingleTargetDamageDone(enc.Id, bossFight.TargetName))
                        {
                            if (dmg.PlayerId == 0)
                            {
                                continue;
                            }

                            var thisPlayer = updatePlayerStats.FirstOrDefault(p => p.PlayerId == dmg.PlayerId && p.EncounterId == enc.Id);
                            if (thisPlayer == null)
                            {
                                updatePlayerStats.Add(new Database.Models.EncounterPlayerStatistics()
                                {
                                    EncounterId     = enc.Id,
                                    PlayerId        = dmg.PlayerId,
                                    SingleTargetDps = dmg.Damage / (long)enc.Duration.TotalSeconds
                                });
                            }
                        }
                    }

                    #endregion
                    sw.Stop();
                    var generateTime = sw.Elapsed.ToString();
                    _logger.Debug(string.Format("Finished calculating {2} ST DPS stats for encounter {0}. Stats generated in {1}.", enc.Id, generateTime, updatePlayerStats.Count));
                    //sw.Reset();
                    //sw.Start();
                    //// Finished generating statistics, save this encounter's stats now
                    //var result = _encounterRepository.UpdateEncounterSingleTargetDpsStatistics(updatePlayerStats);
                    //sw.Stop();
                    //_logger.Debug(string.Format("Finished updating ST DPS stats for encounter {0}. Stats generated in {1} and updated in {2}.", enc.Id, generateTime, sw.Elapsed));
                    //addPlayerStats.AddRange(addPlayerStats);
                }
                var result = _encounterRepository.UpdateEncounterSingleTargetDpsStatistics(updatePlayerStats);
                _logger.Debug(string.Format("Finished updating {0} ST DPS stats for {1} encounters.", updatePlayerStats.Count, stEncList.Count));
            }
        }
Exemplo n.º 7
0
        public void Execute(IJobExecutionContext context)
        {
            var task = _taskRepository.Get("EncounterPlayerRoleRecords");

            if (task == null)
            {
                _logger.Debug("Can't update EncounterPlayerRoleRecords - no matching task definition exists in the database.");
                return;
            }

            // Check if enough time has passed for us to run this task again
            if (task.LastRun.AddMinutes(task.ScheduleMinutes) > DateTime.Now)
            {
                _logger.Debug("Not enough time has passed for this scheduled task, so it won't be executed now");
                return;
            }

            // Update the task lastrun time first, so if it takes a minute to run, we don't run it on another server at the same time
            _taskRepository.UpdateTask(task.Id, DateTime.Now);

            var encounters = _encounterRepository.GetEncountersMissingPlayerRecords(100);

            if (!encounters.Any())
            {
                return;
            }

            _logger.Debug(string.Format("EncounterPlayerRole update: Found {0} encounters that need updating!", encounters.Count));

            var encounterPlayerRolesToAdd = new List <EncounterPlayerRole>();

            foreach (var id in encounters)
            {
                var rolesFromRecords = _encounterRepository.GetPlayerRoles(id);
                if (rolesFromRecords.Any())
                {
                    var playerRoleList = rolesFromRecords.Select(role => new EncounterPlayerRole()
                    {
                        Class       = role.Class,
                        EncounterId = id,
                        PlayerId    = role.Id,
                        Role        = role.Role,
                        Name        = role.Name
                    }).ToList();
                    encounterPlayerRolesToAdd.AddRange(playerRoleList);
                }
                else
                {
                    // Didn't get any roles. Why? No records in each of the tables?
                    var encRecordCount = _encounterRepository.CountBasicRecordsForEncounter(id);
                    if (encRecordCount.DamageCount == 0 &&
                        encRecordCount.HealingCount == 0 &&
                        encRecordCount.ShieldCount == 0)
                    {
                        // Encounter is empty. Remove it
                        _logger.Debug(string.Format("Marking {0} for deletion as it has no basic records.", id));
                        _encounterRepository.MarkEncountersForDeletion(new List <int>()
                        {
                            id
                        }, "scheduledTask");
                    }
                    else
                    {
                        // Records exist, but we couldn't determine roles, so make sure that the damage records
                        // cover the correct duration of the encounter. If the encounter was a wipe, remove it.
                        var thisEncounter = _encounterRepository.Get(id);
                        if (thisEncounter != null && !thisEncounter.SuccessfulKill)
                        {
                            _logger.Debug(string.Format("Marking {0} for deletion as it was a wipe with no available role detection.", id));
                            _encounterRepository.MarkEncountersForDeletion(new List <int>()
                            {
                                id
                            }, "scheduledTask");
                        }
                    }
                }
            }

            if (!encounterPlayerRolesToAdd.Any())
            {
                _logger.Debug("Didn't find any records to add from these encounters, stopping now!");
                return;
            }

            var result = _encounterRepository.AddPlayerEncounterRoles(encounterPlayerRolesToAdd);

            _logger.Debug(result.Success
                ? string.Format("Successfully added {0} EncounterPlayerRole records",
                                encounterPlayerRolesToAdd.Count)
                : string.Format("An error occurred while adding EncounterPlayerRole records: {0}", result.Message));
        }