Exemple #1
0
        private async Task ProcessMessage(IMessage message)
        {
            if (message.CreatedAt < seasonStart)
            {
                return;
            }

            try
            {
                ParsedKillMail parsedKillmail = null;

                parsedKillmail = await DeathRecapParser.ParseKillmail(message.Content);

                if (parsedKillmail != null)
                {
                    var existingRawKillMail = await dbService.GetRawKillMailAsync(message.Id);

                    if (existingRawKillMail != null)
                    {
                        await dbService.InsertMissingKillMailInvolvedAsync(message, parsedKillmail);
                    }
                    else
                    {
                        var insertedKillmail = await dbService.InsertRawAndParsedKillMailAsync(message, parsedKillmail);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemple #2
0
        public ParsedKillMail ExtractKillmail(string input)
        {
            var   extractedKillmail = new ParsedKillMail();
            Match killmailMatch     = Regex.Match(input, pattern);

            if (killmailMatch.Success)
            {
                foreach (Group group in killmailMatch.Groups)
                {
                    switch (group.Name)
                    {
                    case "datetime":
                        extractedKillmail.KilledAt = group.Value;
                        break;

                    case "victimName":
                        extractedKillmail.VictimName = group.Value;
                        break;

                    case "victimGuild":
                        extractedKillmail.VictimGuild = group.Value;
                        break;

                    case "attackerName":
                        extractedKillmail.AttackerName = group.Value;
                        break;

                    case "attackerGuild":
                        extractedKillmail.AttackerGuild = group.Value;
                        break;

                    case "zone":
                        extractedKillmail.Zone = group.Value;
                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                return(null);
            }

            return(extractedKillmail);
        }
Exemple #3
0
        public async Task InsertMissingKillMailInvolvedAsync(IMessage discordMessage, ParsedKillMail parsedKillMail)
        {
            using (var connection = DatabaseConnection.CreateConnection(DbConnectionString))
            {
                connection.Open();
                using (var killmailTransaction = connection.BeginTransaction()) {
                    try
                    {
                        var killmail = await GetKillmailAsync(discordMessage.Id);

                        if (killmail != null && parsedKillMail.Involved != null)
                        {
                            foreach (var involved in parsedKillMail.Involved)
                            {
                                await InsertParsedKillMailInvolved(connection, killmail, involved);
                            }
                        }
                        killmailTransaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        killmailTransaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemple #4
0
        public async Task <Killmail> InsertRawAndParsedKillMailAsync(IMessage discordMessage, ParsedKillMail parsedKillMail)
        {
            using (var connection = DatabaseConnection.CreateConnection(DbConnectionString)) {
                connection.Open();
                using (var killmailTransaction = connection.BeginTransaction()) {
                    try
                    {
                        var rawKillMail = await InsertRawKillmailAsync(connection, discordMessage);

                        parsedKillMail.KillMailRawId = rawKillMail.id;

                        var insertedKillmail = await InsertParsedKillmailAsync(connection, parsedKillMail);

                        foreach (var involved in parsedKillMail.Involved)
                        {
                            insertedKillmail.Involved.Add(await InsertParsedKillMailInvolved(connection, insertedKillmail, involved));
                        }

                        killmailTransaction.Commit();

                        return(insertedKillmail);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        killmailTransaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemple #5
0
        private async Task <Killmail> InsertParsedKillmailAsync(IDbConnection connection, ParsedKillMail parsedKillmailModel)
        {
            var killmailToInsert = new Killmail();

            CultureInfo USCultureInfo = new CultureInfo("en-US");
            var         timezone      = "America/Chicago";

            // Server changed from chicago time to UTC time instead so this is not being used for now
            //var killedAtLocalTime = DateTime.SpecifyKind(DateTime.Parse(parsedKillmailModel.killedAt, USCultureInfo), DateTimeKind.Unspecified);
            //killedAtLocalTime.InZone(timezone);

            var killedAtLocalTime = DateTime.SpecifyKind(DateTime.Parse(parsedKillmailModel.KilledAt, USCultureInfo), DateTimeKind.Utc);

            killmailToInsert.killed_at       = killedAtLocalTime;
            killmailToInsert.killmail_raw_id = parsedKillmailModel.KillMailRawId;
            killmailToInsert.victim_guild_id = await GetOrInsertGuild(connection, parsedKillmailModel.VictimGuild);

            killmailToInsert.attacker_guild_id = await GetOrInsertGuild(connection, parsedKillmailModel.AttackerGuild);

            killmailToInsert.zone_id = await GetOrInsertZone(connection, parsedKillmailModel.Zone);

            killmailToInsert.victim_id = await GetOrInsertCharacter(connection, parsedKillmailModel.VictimName, parsedKillmailModel.VictimIsNpc, killmailToInsert.victim_guild_id, parsedKillmailModel.VictimLevel, parsedKillmailModel.VictimClass);

            killmailToInsert.attacker_id = await GetOrInsertCharacter(connection, parsedKillmailModel.AttackerName, parsedKillmailModel.AttackerIsNpc, killmailToInsert.attacker_guild_id, parsedKillmailModel.AttackerLevel, parsedKillmailModel.AttackerClass);

            killmailToInsert.victim_level   = parsedKillmailModel.VictimLevel;
            killmailToInsert.attacker_level = parsedKillmailModel.AttackerLevel;

            var dynamicParams = new DynamicParameters();

            dynamicParams.AddDynamicParams(new {
                killed_at         = killmailToInsert.killed_at,
                killmail_raw_id   = killmailToInsert.killmail_raw_id,
                victim_guild_id   = killmailToInsert.victim_guild_id,
                attacker_guild_id = killmailToInsert.attacker_guild_id,
                zone_id           = killmailToInsert.zone_id,
                victim_id         = killmailToInsert.victim_id,
                attacker_id       = killmailToInsert.attacker_id,
                victim_level      = killmailToInsert.victim_level,
                attacker_level    = killmailToInsert.attacker_level,
                season            = Season
            });
            dynamicParams.Add("@KillMailId", direction: ParameterDirection.Output);

            // Finally, insert killmail
            var killmailInsertSql =
                @"INSERT INTO killmail (victim_id, victim_guild_id, attacker_id, attacker_guild_id, zone_id, killed_at, killmail_raw_id, victim_level, attacker_level, season)
            VALUES (@victim_id, @victim_guild_id, @attacker_id, @attacker_guild_id, @zone_id, @killed_at, @killmail_raw_id, @victim_level, @attacker_level, @season)
            RETURNING id;
            ";

            await connection.ExecuteAsync(killmailInsertSql, dynamicParams);

            killmailToInsert.id = dynamicParams.Get <int>("KillMailId");
            return(killmailToInsert);
        }
        public static async Task <ParsedKillMail> ParseKillmail(string input)
        {
            // Remove special formatting
            var lines = input.Split(new [] { '\r', '\n' });

            if (lines.Any() && lines.FirstOrDefault().Contains("```"))
            {
                input = string.Join("\n", lines.Skip(1));
            }
            input = input.Replace("**", "");

            var   extractedKillmail = new ParsedKillMail();
            Match killmailMatch     = Regex.Match(input, Pattern);

            if (killmailMatch.Success)
            {
                foreach (Group group in killmailMatch.Groups.Where(x => x.Success))
                {
                    switch (group.Name)
                    {
                    case "datetime":
                        extractedKillmail.KilledAt = group.Value.Trim();
                        break;

                    case "victimName":
                        extractedKillmail.VictimName = group.Value.Trim();
                        break;

                    case "attackerName":
                        extractedKillmail.AttackerName = group.Value.Trim();
                        break;

                    case "zone":
                        extractedKillmail.Zone = ZoneMapper.fullZoneName(group.Value.Trim());
                        break;

                    case "killingBlow":
                        if (int.TryParse(group.Value.Trim(), out var killingBlow))
                        {
                            extractedKillmail.KillingBlow = killingBlow;
                        }
                        break;

                    case "overDamage":
                        if (int.TryParse(group.Value.Trim(), out var overDamage))
                        {
                            extractedKillmail.OverDamage = overDamage;
                        }
                        break;

                    case "involvedText":
                        await ParseInvolved(extractedKillmail, group.Value);

                        break;

                    default:
                        break;
                    }
                }


                // Get level and class and guild for each char
                var victimScraper   = new CharBrowserScraper(extractedKillmail.VictimName);
                var attackerScraper = new CharBrowserScraper(extractedKillmail.AttackerName);
                await victimScraper.Fetch();

                await attackerScraper.Fetch();

                extractedKillmail.AttackerGuild = attackerScraper.Guild;
                extractedKillmail.AttackerLevel = attackerScraper.Level;
                extractedKillmail.AttackerClass = attackerScraper.Class;
                extractedKillmail.AttackerIsNpc = attackerScraper.IsNpc;
                extractedKillmail.VictimGuild   = victimScraper.Guild;
                extractedKillmail.VictimLevel   = victimScraper.Level;
                extractedKillmail.VictimClass   = victimScraper.Class;
                extractedKillmail.VictimIsNpc   = victimScraper.IsNpc;
            }
            else
            {
                return(null);
            }

            return(extractedKillmail);
        }
        private static async Task ParseInvolved(ParsedKillMail killMail, string involvedText)
        {
            var involvedMatches = Regex.Matches(involvedText, InvolvedPattern);

            foreach (Match involvedMatch in involvedMatches)
            {
                if (involvedMatch.Success)
                {
                    var parsedInvolved = new ParsedKillMailInvolved();
                    foreach (Group group in involvedMatch.Groups.Where(x => x.Success))
                    {
                        switch (group.Name)
                        {
                        case "attacker":
                            parsedInvolved.AttackerName = group.Value.Trim();
                            break;

                        case "contribution":
                            var contributionText = group.Value.Trim();
                            var meleeDamageMatch = Regex.Match(contributionText, MeleeDamagePattern)?.Groups.FirstOrDefault(x => x.Name == "meleeDamage");
                            var meleeHitMatch    = Regex.Match(contributionText, MeleeDamagePattern)?.Groups.FirstOrDefault(x => x.Name == "meleeHit");
                            var spellDamageMatch = Regex.Match(contributionText, SpellDamagePattern)?.Groups.FirstOrDefault(x => x.Name == "spellDamage");
                            var spellHitMatch    = Regex.Match(contributionText, SpellDamagePattern)?.Groups.FirstOrDefault(x => x.Name == "spellHit");
                            var dispelMatch      = Regex.Match(contributionText, DispelPattern)?.Groups.FirstOrDefault(x => x.Name == "dispelSlots");

                            if (meleeDamageMatch != null && int.TryParse(meleeDamageMatch.Value.Trim(), out var meleeDamage))
                            {
                                parsedInvolved.MeleeDamage = meleeDamage;
                            }
                            if (meleeHitMatch != null && int.TryParse(meleeHitMatch.Value.Trim(), out var meleeHit))
                            {
                                parsedInvolved.MeleeHits = meleeHit;
                            }
                            if (spellDamageMatch != null && int.TryParse(spellDamageMatch.Value.Trim(), out var spellDamage))
                            {
                                parsedInvolved.SpellDamage = spellDamage;
                            }
                            if (spellHitMatch != null && int.TryParse(spellHitMatch.Value.Trim(), out var spellHit))
                            {
                                parsedInvolved.SpellHits = spellHit;
                            }
                            if (dispelMatch != null && int.TryParse(dispelMatch.Value.Trim(), out var dispelSlots))
                            {
                                parsedInvolved.DispelSlots = dispelSlots;
                            }
                            break;

                        default:
                            break;
                        }
                    }

                    // Get level and class and guild for each char
                    var attackerScraper = new CharBrowserScraper(parsedInvolved.AttackerName);
                    await attackerScraper.Fetch();

                    parsedInvolved.AttackerGuild = attackerScraper.Guild;
                    parsedInvolved.AttackerLevel = attackerScraper.Level;
                    parsedInvolved.AttackerClass = attackerScraper.Class;
                    parsedInvolved.AttackerIsNpc = attackerScraper.IsNpc;

                    killMail.Involved.Add(parsedInvolved);
                }
            }
        }