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