Ejemplo n.º 1
0
        public KillmailModel ExtractKillmail(string input)
        {
            var   extractedKillmail = new KillmailModel();
            Match killmailMatch     = Regex.Match(input, pattern);

            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;
                }
            }

            return(extractedKillmail);
        }
Ejemplo n.º 2
0
        private async Task<Killmail> InsertParsedKillmailAsync(IDbConnection connection, KillmailModel 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.killmail_raw_id;
            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, killmailToInsert.victim_guild_id);
            killmailToInsert.attacker_id = await GetOrInsertCharacter(connection, parsedKillmailModel.attackerName, killmailToInsert.attacker_guild_id);

            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
            });

            // 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)
                        VALUES (@victim_id, @victim_guild_id, @attacker_id, @attacker_guild_id, @zone_id, @killed_at, @killmail_raw_id)
                        RETURNING id;
                        ";

            await connection.ExecuteAsync(killmailInsertSql, dynamicParams);
            return killmailToInsert;
        }
Ejemplo n.º 3
0
        private async Task ProcessMessage(IMessage message)
        {
            KillMailParser killmailParser = new KillMailParser();
            var rawKillMailId = 0;

            // Check if killmail exists
            using(var connection = DatabaseConnection.CreateConnection(DbConnectionString)) {
                var selectRawKillmailSql = @"SELECT *
                                            FROM killmail_raw 
                                            WHERE discord_message_id = @messageId";

                try {
                    var messageIdSigned = Convert.ToInt64(message.Id);
                    var affectedRows = await connection.QueryAsync(selectRawKillmailSql, new { messageId = messageIdSigned });
                    
                    if (affectedRows.Count() > 0) {
                        return;
                    }
                }

                catch (Exception ex) {
                    Console.WriteLine(ex);
                }
            }

            // Process message if nothing found
            KillmailModel parsedKillmail = null;
            Killmail insertedKillmail = null;

            using(var connection = DatabaseConnection.CreateConnection(DbConnectionString)) {

                connection.Open();

                using (var killmailTransaction = connection.BeginTransaction()) {
                    try
                    {
                        rawKillMailId = await InsertRawKillmailAsync(connection, message);

                        // Parse raw killmail
                        parsedKillmail = killmailParser.ExtractKillmail(message.Content);
                        parsedKillmail.killmail_raw_id = rawKillMailId;

                        insertedKillmail = await InsertParsedKillmailAsync(connection, parsedKillmail);

                        killmailTransaction.Commit();                        
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        killmailTransaction.Rollback();
                    }
                }
            }

            // Get level and class for each char
            var scraper = new Scraper();
            var victim = new CharacterModel{
                name = parsedKillmail.victimName,
                isAttacker = false
            };
            var attacker = new CharacterModel{
                name = parsedKillmail.attackerName,
                isAttacker = true
            };

            victim.classLevel = await scraper.ScrapeCharInfo(victim.name);
            attacker.classLevel = await scraper.ScrapeCharInfo(attacker.name); 

            using(var connection = DatabaseConnection.CreateConnection(DbConnectionString)) {
                if (!string.IsNullOrEmpty(victim.classLevel))
                {
                    await InsertClassLevel(connection, victim, insertedKillmail, message);
                }
                if (!string.IsNullOrEmpty(attacker.classLevel))
                {
                    await InsertClassLevel(connection, attacker, insertedKillmail, message);  
                } 
            }
        
        }