public async Task <IActionResult> Faction(string id) { FactionResultViewModel viewModel = new FactionResultViewModel { Factions = await _db.Factions.OrderBy(f => f.Name).ToListAsync() }; if (!await _db.Factions.AnyAsync(f => f.Name == id)) { viewModel.Faction = viewModel.Factions.FirstOrDefault().Name; viewModel.StatusMessage = "Select a faction to view its results."; return(View(viewModel)); } Faction faction = await _db.Factions.FirstAsync(f => f.Name == id); List <Theme> factionThemes = await _db.Themes.Where(t => t.FactionId == faction.Id).OrderBy(t => t.Name).ToListAsync(); List <Caster> factionCasters = await _db.Casters.Where(c => c.FactionId == faction.Id).OrderBy(c => c.Name).ToListAsync(); List <BattleReport> factionBattleReports = await _db.BattleReports.Where(br => br.ConfirmedByOpponent && (br.WinningFaction == faction.Name || br.LosingFaction == faction.Name)).OrderByDescending(br => br.DatePlayed).ToListAsync(); FactionResult factionResult = CreateFactionResult(faction, factionThemes, factionCasters, factionBattleReports); viewModel.Faction = faction.Name; viewModel.StatusMessage = string.Empty; viewModel.FactionResult = factionResult; viewModel.Themes = FactionEntityResult(StaticDetails.ThemeType, faction.Name, factionBattleReports); viewModel.Casters = FactionEntityResult(StaticDetails.CasterType, faction.Name, factionBattleReports); viewModel.GameSizes = FactionEntityResult(StaticDetails.GameSizeType, faction.Name, factionBattleReports); viewModel.Scenarios = FactionEntityResult(StaticDetails.ScenarioType, faction.Name, factionBattleReports); viewModel.EndConditions = FactionEntityResult(StaticDetails.EndConditionType, faction.Name, factionBattleReports); viewModel.VersusFactions = FactionEntityResult(StaticDetails.VersusFactionType, faction.Name, factionBattleReports); viewModel.VersusThemes = FactionEntityResult(StaticDetails.VersusThemeType, faction.Name, factionBattleReports); viewModel.VersusCasters = FactionEntityResult(StaticDetails.VersusCasterType, faction.Name, factionBattleReports); return(View(viewModel)); }
private FactionResult CreateFactionResult(Faction faction, List <Theme> factionThemes, List <Caster> factionCasters, List <BattleReport> factionBattleReports) { FactionResult factionResult = new FactionResult() { Name = faction.Name, NumberOfGamesPlayed = faction.NumberOfGamesPlayed, NumberOfGamesLost = faction.NumberOfGamesLost, NumberOfGamesWon = faction.NumberOfGamesWon, Winrate = faction.Winrate, NumberOfMirrorMatches = factionBattleReports.Where(br => br.WinningFaction == br.LosingFaction).Count() }; if (factionBattleReports.Count == 0) { factionResult.MostPlayedTheme = StaticDetails.NoRecords; factionResult.BestPerformingTheme = StaticDetails.NoRecords; factionResult.MostPlayedCaster = StaticDetails.NoRecords; factionResult.BestPerformingCaster = StaticDetails.NoRecords; } else { Theme mostPlayedTheme = factionThemes.OrderByDescending(t => t.NumberOfGamesPlayed).FirstOrDefault(); factionResult.MostPlayedTheme = mostPlayedTheme.Name; factionResult.GamesWithMostPlayedTheme = mostPlayedTheme.NumberOfGamesPlayed; Theme bestPerformingTheme = factionThemes.OrderByDescending(t => t.Winrate).FirstOrDefault(); factionResult.BestPerformingTheme = bestPerformingTheme.Name; factionResult.WinrateBestPerformingTheme = bestPerformingTheme.Winrate; Caster mostPlayedCaster = factionCasters.OrderByDescending(c => c.NumberOfGamesPlayed).FirstOrDefault(); factionResult.MostPlayedCaster = mostPlayedCaster.Name; factionResult.GamesWithMostPlayedCaster = mostPlayedCaster.NumberOfGamesPlayed; Caster bestPerformingCaster = factionCasters.OrderByDescending(c => c.Winrate).FirstOrDefault(); factionResult.BestPerformingCaster = bestPerformingCaster.Name; factionResult.WinrateBestPerformingCaster = bestPerformingCaster.Winrate; } return(factionResult); }