private async Task Init() { var context = new DataContext(); if (context.Matches.Count() > 0) { return; } var matches = await apiClient.GetLastestMatches(); var lastest = matches.OrderByDescending(_ => _.match_seq_num).FirstOrDefault(); var date = DateTimeOffset.FromUnixTimeSeconds(lastest.start_time).UtcDateTime; var match = new Data.Models.Match() { match_id = lastest.match_id, match_number = lastest.match_seq_num, date = date, valid = false }; context.Matches.Add(match); await context.SaveChangesAsync(); }
public async Task Processing() { var client = new MetaClient(); var heroesTest = client.GetHeroes(); var heroes = client.GetHeroes().Select(_ => new { Key = _.Id, Abilities = _.Abilities.Select(__ => __.Id).ToList() }).ToDictionary(_ => _.Key, _ => _.Abilities); var abilities = client.GetAbilities().Select(_ => _.Id).ToList(); var ultimates = client.GetUltimates().Select(_ => _.Id).ToList(); var talents = client.GetTalents().Select(_ => _.Id).ToList(); while (true) { var context = new DataContext(); using (var transation = context.Database.BeginTransaction()) { try { Daedalus.GetMatchDetails.Match match; if (!this.qProcessing.TryDequeue(out match)) { continue; } var count = context.Matches.Where(_ => _.match_number == match.match_seq_num).Count(); if (count > 0) { continue; } var date = DateTimeOffset.FromUnixTimeSeconds(match.start_time).UtcDateTime; var duration = DateTimeOffset.FromUnixTimeSeconds(match.duration).TimeOfDay.TotalMinutes; var match_summary = new Data.Models.Match() { match_id = match.match_id, match_number = match.match_seq_num, league_id = match.leagueid, duration = duration, day_of_week = (int)date.DayOfWeek, hour_of_day = date.Hour, date = date, cluster = match.cluster, region = client.ConvertClusterToRegion(match.cluster), victory_radiant = match.radiant_win ? 1 : 0, victory_dire = match.radiant_win ? 0 : 1, valid = true, }; var time_limit = 10; if (duration < time_limit) { // this.logger.Warning($" Error: match {match.match_id} is invalid as its durration of {duration:0.00} is lower then the time limit of {time_limit}"); match_summary.valid = false; } context.Matches.Add(match_summary); await context.SaveChangesAsync(); var players_leave = 0; foreach (var player in match.players) { var team = player.player_slot < 6 ? 0 : 1; var order = this.ConvertPlayerSlotToDraftOrder(player.player_slot); var victory = team == 0 ? match.radiant_win : !match.radiant_win; var hero_id = player.hero_id; if (player.leaver_status > 1) { players_leave++; } if (players_leave > 1) { match_summary.valid = false; } /* * if (player.leaver_status > 1) * { * // https://wiki.teamfortress.com/wiki/WebAPI/GetMatchDetails * var status = player.leaver_status == 2 ? "DISCONNECTED" : player.leaver_status == 3 ? "ABANDONED" : player.leaver_status == 4 ? "AFK" : "Unknown"; * // this.logger.Warning($" Warning: match {match.match_id} ({duration:0.00}) is invalid as player {order} has a leaver status of {player.leaver_status} ({status}) "); * match_summary.valid = false; * } */ var heroes_abilities = new List <int>(); heroes.TryGetValue(hero_id, out heroes_abilities); var match_player = new Data.Models.Player() { // Match match_ref = match_summary.id, victory = victory ? 1 : 0, // Player team = team, draft_order = order, player_slot = player.player_slot, account_id = player.account_id, persona = player.persona, // Hero hero_id = hero_id, kills = player.kills, deaths = player.deaths, assists = player.assists, last_hits = player.last_hits, denies = player.denies, gold = player.gold, level = player.level, gold_per_min = player.gold_per_min, xp_per_min = player.xp_per_min, gold_spent = player.gold_spent, hero_damage = player.hero_damage, tower_damage = player.tower_damage, hero_healing = player.hero_healing, }; context.Players.Add(match_player); await context.SaveChangesAsync(); if (player.ability_upgrades == null) { throw new Exception($" Error: match {match.match_id} is invalid as player {order} has no abilities."); } var query = player.ability_upgrades.Select(_ => _).ToList(); var collection = query.Select(_ => _.ability).Distinct().ToList(); foreach (var ability_id in collection) { var id = ConvertAbilityID(ability_id); var match_skill = new Data.Models.Skill() { // Match match_ref = match_summary.id, // Player player_ref = match_player.id, // Ability ability_id = id, is_skill = abilities.Contains(id) ? 1 : 0, is_ulimate = ultimates.Contains(id) ? 1 : 0, is_taltent = talents.Contains(id) ? 1 : 0, is_hero_same = heroes_abilities.Contains(id) ? 1 : 0, level = query.Where(_ => _.ability == id || _.ability == ability_id).Max(_ => _.level), }; context.Skills.Add(match_skill); } await context.SaveChangesAsync(); } var match_delta = DateTime.Now - match_summary.date.ToLocalTime().AddMinutes(match_summary.duration); // this.logger.Info($"Processed: match {match_summary.match_id} which ended {match_delta.Humanize(3)} mins ago."); transation.Commit(); } catch (Exception ex) { transation.Rollback(); // this.logger.Error(ex); } } } }