public void CreateCalculatedRecord() { var calculatedRecord = new CalculatedRecord(); var dataItemItems = new List<DataItem>(); var data = _context.Records.OrderByDescending(r => r.DateCreated).First(); var items = _context.Items.ToList(); var champions = APChampions.Champions; foreach (var key in APItems.Items.Keys) { var iditem = APItems.Items[key]; // Usage by champion (Top 5 champions that used this item the most) var listMostUsedPreChange = MostUsedChampions(data, true, iditem, champions); var listMostUsedPostChange = MostUsedChampions(data, false, iditem, champions); var playersThatUsedAPChampionsPreChange = _matches.Where(m => m.Pre_Change == true).SelectMany(ma => ma.Players).Where(p => champions.Contains(p.ChampionUsed.ChampionId)).ToList(); var playersThatUsedAPChampionsPostChange = _matches.Where(m => m.Pre_Change == false).SelectMany(ma => ma.Players).Where(p => champions.Contains(p.ChampionUsed.ChampionId)).ToList(); var playersAPChampionsUsedItemPreChange = playersThatUsedAPChampionsPreChange.Where(p => p.ItemsBought.Contains(iditem.ToString())).ToList(); var playersAPChampionsUsedItemPostChange = playersThatUsedAPChampionsPostChange.Where(p => p.ItemsBought.Contains(iditem.ToString())).ToList(); // KDA var KDAAvgPreChange = CalculateKDA(playersAPChampionsUsedItemPreChange); var KDAAvgPostChange = CalculateKDA(playersAPChampionsUsedItemPostChange); // Total of Multikills var multikillsPreChange = CalculateMultiKills(playersAPChampionsUsedItemPreChange); var multikillsPostChange = CalculateMultiKills(playersAPChampionsUsedItemPostChange); // Winrate var winratePreChange = CalculateWinRate(playersAPChampionsUsedItemPreChange); var winratePostChange = CalculateWinRate(playersAPChampionsUsedItemPostChange); // Champions who dealt the most magic damage (Top 5) var listMostDamagePreChange = MostDamageChampions(playersAPChampionsUsedItemPreChange, iditem, champions); var listMostDamagePostChange = MostDamageChampions(playersAPChampionsUsedItemPostChange, iditem, champions); var playersThatUsedAPChampionsPreChangeCount = playersThatUsedAPChampionsPreChange.Count; var playersThatUsedAPChampionsPostChangeCount = playersThatUsedAPChampionsPostChange.Count; var itemRecords = data.RecordsByChampions .ChampionsRecords .Where(r => champions.Contains(r.ChampionId)) .Select(c => c.ItemsRecord) .SelectMany(ir => ir.Items) .Where(i => i.ItemID == iditem) .ToList(); // Usage per rank var listUsePerRankPrePatch = new List<DataPerRank>(); var listUsePerRankPostPatch = new List<DataPerRank>(); foreach (Rank rank in Enum.GetValues(typeof(Rank))) { var playersInRankPreChangeCount = playersThatUsedAPChampionsPreChange.Where(p => p.Rank == rank).ToList().Count; var playersInRankPostChangeCount = playersThatUsedAPChampionsPostChange.Where(p => p.Rank == rank).ToList().Count; var playersThatBoughtThisItemInRankPreChangeCount = playersThatUsedAPChampionsPreChange .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString()) && p.Rank == rank) .ToList() .Count; var playersThatBoughtThisItemInRankPostChangeCount = playersThatUsedAPChampionsPostChange .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString()) && p.Rank == rank) .ToList() .Count; DataPerRank dataPrePatch = new DataPerRank() { Rank = rank.ToString(), Data = playersInRankPreChangeCount != 0 ? Convert.ToInt32((playersThatBoughtThisItemInRankPreChangeCount / (float)playersInRankPreChangeCount) * 100f) : 0 }; DataPerRank dataPostPatch = new DataPerRank() { Rank = rank.ToString(), Data = playersInRankPostChangeCount != 0 ? Convert.ToInt32((playersThatBoughtThisItemInRankPostChangeCount / (float)playersInRankPostChangeCount) * 100f) : 0 }; listUsePerRankPrePatch.Add(dataPrePatch); listUsePerRankPostPatch.Add(dataPostPatch); } // Usage per region var listUsePerRegionPrePatch = new List<DataPerRegion>(); var listUsePerRegionPostPatch = new List<DataPerRegion>(); foreach (Region region in Enum.GetValues(typeof(Region))) { var playersInRegionPreChangeCount = playersThatUsedAPChampionsPreChange.Where(p => p.Region == region).ToList().Count; var playersInRegionPostChangeCount = playersThatUsedAPChampionsPostChange.Where(p => p.Region == region).ToList().Count; var playersThatBoughtThisItemInRegionPreChangeCount = playersThatUsedAPChampionsPreChange .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString()) && p.Region == region) .ToList() .Count; var playersThatBoughtThisItemInRegionPostChangeCount = playersThatUsedAPChampionsPostChange .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString()) && p.Region == region) .ToList() .Count; DataPerRegion dataPrePatch = new DataPerRegion() { Region = region.ToString(), Data = playersInRegionPreChangeCount != 0 ? Convert.ToInt32((playersThatBoughtThisItemInRegionPreChangeCount / (float)playersInRegionPreChangeCount) * 100f) : 0 }; DataPerRegion dataPostPatch = new DataPerRegion() { Region = region.ToString(), Data = playersInRegionPostChangeCount != 0 ? Convert.ToInt32((playersThatBoughtThisItemInRegionPostChangeCount / (float)playersInRegionPostChangeCount) * 100f) : 0 }; listUsePerRegionPrePatch.Add(dataPrePatch); listUsePerRegionPostPatch.Add(dataPostPatch); } // Total usage var item = new ItemRecord() { PreChangeRecord = itemRecords.Select(i => i.PreChangeRecord).Sum(), PostChangeRecord = itemRecords.Select(i => i.PostChangeRecord).Sum() }; int percentagePrePatch = Convert.ToInt32((item.PreChangeRecord / (float)playersThatUsedAPChampionsPreChangeCount) * 100f); int percentagePostPatch = Convert.ToInt32((item.PostChangeRecord / (float)playersThatUsedAPChampionsPostChangeCount) * 100f); string itemName = APItems.ItemsNames[iditem]; dataItemItems.Add(new DataItem { ItemId = iditem, ItemName = itemName, PrePatch = percentagePrePatch, PostPatch = percentagePostPatch, MostUsedChampionsPrePatch = listMostUsedPreChange, MostUsedChampionsPostPatch = listMostUsedPostChange, DataPerRankPrePatch = listUsePerRankPrePatch, DataPerRankPostPatch = listUsePerRankPostPatch, DataPerRegionPrePatch = listUsePerRegionPrePatch, DataPerRegionPostPatch = listUsePerRegionPostPatch, KDAAvgPrePatch = KDAAvgPreChange, KDAAvgPostPatch = KDAAvgPostChange, MultiKillsPrePatch = multikillsPreChange, MultiKillsPostPatch = multikillsPostChange, WinRatePrePatch = winratePreChange, WinRatePostPatch = winratePostChange, ChampionsWithMoreMagicDamagePrePatch = listMostDamagePreChange, ChampionsWithMoreMagicDamagePostPatch = listMostDamagePostChange }); } calculatedRecord.Items = dataItemItems; _context.CalculatedRecord.Add(calculatedRecord); _context.SaveChanges(); }
public void CreateRecord() { try { Record record = new Record(); record.Matches = _matches.Count; record.Players = _matches.Select(m => m.Players).ToList().Count; var matchesData = _matches.ToList(); var matchesPreChange = matchesData.Where(m => m.Pre_Change == true).ToList(); var matchesPostChange = matchesData.Where(m => m.Pre_Change == false).ToList(); Record_Buy_Percentage recordBuy = new Record_Buy_Percentage(); AllItemsRecord itemsRecord = new AllItemsRecord(); var listOfItemRecords = new List<ItemRecord>(); foreach (var key in APItems.Items.Keys) { var item = new ItemRecord() { ItemID = APItems.Items[key], PreChangeRecord = matchesPreChange.SelectMany(m => m.Players) .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString())) .ToList() .Count, PostChangeRecord = matchesPostChange.SelectMany(m => m.Players) .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString())) .ToList() .Count }; listOfItemRecords.Add(item); } itemsRecord.Items = listOfItemRecords; recordBuy.ItemsRecord = itemsRecord; record.RecordsByBuyPercentage = recordBuy; Record_Champions recordChampions = new Record_Champions(); var listOfChampionRecords = new List<ChampionRecord>(); var championIds = _context.Champions.Select(c => c.ChampionId).ToList(); foreach (int championId in championIds) { var championRecord = new ChampionRecord(); championRecord.ChampionId = championId; var allitemsChampionRecord = new AllItemsRecord(); var listRecords = new List<ItemRecord>(); foreach (var key in APItems.Items.Keys) { var item = new ItemRecord() { ItemID = APItems.Items[key], PreChangeRecord = matchesPreChange.SelectMany(m => m.Players) .Where(p => p.ChampionUsed.ChampionId == championId && p.ItemsBought.Contains(APItems.Items[key].ToString())) .ToList() .Count, PostChangeRecord = matchesPostChange.SelectMany(m => m.Players) .Where(p => p.ChampionUsed.ChampionId == championId && p.ItemsBought.Contains(APItems.Items[key].ToString())) .ToList() .Count }; listRecords.Add(item); } allitemsChampionRecord.Items = listRecords; championRecord.ItemsRecord = allitemsChampionRecord; listOfChampionRecords.Add(championRecord); } recordChampions.ChampionsRecords = listOfChampionRecords; record.RecordsByChampions = recordChampions; _context.Records.Add(record); _context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }