private CasterResult CreateCasterResult(Caster caster, List <BattleReport> battleReports)
        {
            List <BattleReport> casterBattleReports = battleReports.Where(br => br.WinningCaster == caster.Name || br.LosingCaster == caster.Name).OrderByDescending(br => br.DatePlayed).ToList();
            CasterResult        casterResult        = new CasterResult()
            {
                Name = caster.Name,
                NumberOfGamesPlayed   = caster.NumberOfGamesPlayed,
                NumberOfGamesLost     = caster.NumberOfGamesLost,
                NumberOfGamesWon      = caster.NumberOfGamesWon,
                Winrate               = caster.Winrate,
                NumberOfMirrorMatches = casterBattleReports.Where(br => br.WinningCaster == br.LosingCaster).Count()
            };

            if (casterBattleReports.Count == 0)
            {
                casterResult.MostPlayedTheme            = StaticDetails.NoRecords;
                casterResult.BestPerformingTheme        = StaticDetails.NoRecords;
                casterResult.MostPlayedScenario         = StaticDetails.NoRecords;
                casterResult.BestPerformingScenario     = StaticDetails.NoRecords;
                casterResult.MostPlayedEndCondition     = StaticDetails.NoRecords;
                casterResult.BestPerformingEndCondition = StaticDetails.NoRecords;
            }
            else
            {
                EntityResult themes        = CasterEntityResult(StaticDetails.ThemeType, caster.Name, battleReports);
                EntityResult scenarios     = CasterEntityResult(StaticDetails.ScenarioType, caster.Name, casterBattleReports);
                EntityResult endConditions = CasterEntityResult(StaticDetails.EndConditionType, caster.Name, casterBattleReports);
                casterResult.MostPlayedTheme                   = themes.Results.OrderByDescending(t => t.NumberOfGamesPlayed).FirstOrDefault().Name;
                casterResult.GamesWithMostPlayedTheme          = themes.Results.OrderByDescending(t => t.NumberOfGamesPlayed).FirstOrDefault().NumberOfGamesPlayed;
                casterResult.BestPerformingTheme               = themes.Results.OrderByDescending(t => t.Winrate).FirstOrDefault().Name;
                casterResult.WinrateBestPerformingTheme        = themes.Results.OrderByDescending(t => t.Winrate).FirstOrDefault().Winrate;
                casterResult.MostPlayedScenario                = scenarios.Results.OrderByDescending(s => s.NumberOfGamesPlayed).FirstOrDefault().Name;
                casterResult.GamesMostPlayedScenario           = scenarios.Results.OrderByDescending(s => s.NumberOfGamesPlayed).FirstOrDefault().NumberOfGamesPlayed;
                casterResult.BestPerformingScenario            = scenarios.Results.OrderByDescending(s => s.Winrate).FirstOrDefault().Name;
                casterResult.WinrateBestPerformingScenario     = scenarios.Results.OrderByDescending(s => s.Winrate).FirstOrDefault().Winrate;
                casterResult.MostPlayedEndCondition            = endConditions.Results.OrderByDescending(ec => ec.NumberOfGamesPlayed).FirstOrDefault().Name;
                casterResult.GamesMostPlayedEndCondition       = endConditions.Results.OrderByDescending(ec => ec.NumberOfGamesPlayed).FirstOrDefault().NumberOfGamesPlayed;
                casterResult.BestPerformingEndCondition        = endConditions.Results.OrderByDescending(ec => ec.Winrate).FirstOrDefault().Name;
                casterResult.WinrateBestPerformingEndCondition = endConditions.Results.OrderByDescending(ec => ec.Winrate).FirstOrDefault().Winrate;
            }
            return(casterResult);
        }
        public async Task <IActionResult> Caster(string id)
        {
            CasterResultViewModel viewModel = new CasterResultViewModel
            {
                Factions = await _db.Factions.OrderBy(f => f.Name).ToListAsync(),
                Casters  = await _db.Casters.OrderBy(c => c.Name).ToListAsync()
            };

            if (!viewModel.Casters.Any(c => c.Name == id))
            {
                viewModel.Faction = viewModel.Factions.FirstOrDefault().Name;
                Faction faction = viewModel.Factions.First(f => f.Name == viewModel.Faction);
                viewModel.Casters       = viewModel.Casters.Where(c => c.FactionId == faction.Id).ToList();
                viewModel.Caster        = viewModel.Casters.FirstOrDefault().Name;
                viewModel.StatusMessage = "Select a caster to view its results.";
                return(View(viewModel));
            }

            Caster              caster              = viewModel.Casters.First(c => c.Name == id);
            Faction             castersFaction      = viewModel.Factions.First(f => f.Id == caster.FactionId);
            List <BattleReport> casterBattleReports = await _db.BattleReports.Where(br => br.ConfirmedByOpponent && (br.WinningCaster == caster.Name || br.LosingCaster == caster.Name)).OrderByDescending(br => br.DatePlayed).ToListAsync();

            CasterResult casterResult = CreateCasterResult(caster, casterBattleReports);

            viewModel.Faction        = castersFaction.Name;
            viewModel.Caster         = caster.Name;
            viewModel.Casters        = viewModel.Casters.Where(c => c.FactionId == castersFaction.Id).ToList();
            viewModel.StatusMessage  = string.Empty;
            viewModel.CasterResult   = casterResult;
            viewModel.Themes         = CasterEntityResult(StaticDetails.ThemeType, caster.Name, casterBattleReports);
            viewModel.GameSizes      = CasterEntityResult(StaticDetails.GameSizeType, caster.Name, casterBattleReports);
            viewModel.Scenarios      = CasterEntityResult(StaticDetails.ScenarioType, caster.Name, casterBattleReports);
            viewModel.EndConditions  = CasterEntityResult(StaticDetails.EndConditionType, caster.Name, casterBattleReports);
            viewModel.VersusFactions = CasterEntityResult(StaticDetails.VersusFactionType, caster.Name, casterBattleReports);
            viewModel.VersusThemes   = CasterEntityResult(StaticDetails.VersusThemeType, caster.Name, casterBattleReports);
            viewModel.VersusCasters  = CasterEntityResult(StaticDetails.VersusCasterType, caster.Name, casterBattleReports);

            return(View(viewModel));
        }