/// <summary> /// Calculates the top 5(ish) placements for this week /// </summary> /// <returns></returns> private string CalculateTop() { this.logger.Log("Calculating top positions..."); StringBuilder result = new StringBuilder(); long topScore = this.weeklyResults.Max(t => t.Points); long topGrossScore = this.weeklyResults.Max(t => t.ScoreBeforeHits); long top3Score = this.weeklyResults.Take(3).Last().Points; IEnumerable <string> winnerNames = this.weeklyResults // This weeks winner(s) .Where(x => x.Points == topScore) // Find all teams with the top score .Select(x => x.Name); // Get their name(s) IEnumerable <string> topNamesBeforeTransferCost = this.weeklyResults // Used for Dan Davies rule .OrderByDescending(x => x.ScoreBeforeHits) .Where(x => x.ScoreBeforeHits == topGrossScore) .Select(x => x.Name); IEnumerable <string> topNames = this.weeklyResults // Top 3ish teams for this week .Where(x => x.Points >= top3Score) // Find anyone that had top 3 score or better .Where(x => x.Points < topScore) // Exlude the first one .Select(x => $"{x.Name} ({x.Points} pts)"); // Prepare text bool daviesRuleInEffect = !(topNamesBeforeTransferCost.All(x => winnerNames.Contains(x)) && winnerNames.All(x => topNamesBeforeTransferCost.Contains(x))); long winnerHitCost = this.weeklyResults.First().HitsTakenCost; if (daviesRuleInEffect) { result.Append($"{TextUtilities.NaturalParse(topNamesBeforeTransferCost)} {TextUtilities.WasWere(topNamesBeforeTransferCost.Count(), true)} in line to win the week until the ever so controversial Dan Davies rule was applied. But once the dust settled the "); } else { result.Append("The "); } result.Append($"winner{TextUtilities.Pluralize(winnerNames.Count())} for gameweek #{this.currentEventId} {TextUtilities.WasWere(winnerNames.Count())} {TextUtilities.NaturalParse(winnerNames)} with {topScore} points"); if (winnerHitCost > 0 && winnerNames.Count() == 1) { result.Append($" despite taking a -{winnerHitCost} point hit! "); } else { result.Append("! "); } result.AppendLine($"Rounding up the top {topNames.Count() + winnerNames.Count()} for the week {TextUtilities.WasWere(winnerNames.Count())} {TextUtilities.NaturalParse(topNames)}."); return(result.ToString()); }
/// <summary> /// Calculates the captaincy choices for the week. /// </summary> /// <returns></returns> private async Task <string> CalculateCaptains() { this.logger.Log("Calculating captains..."); StringBuilder result = new StringBuilder(); var groupedCaptains = this.fplPicks .Select(p => p.Value.Picks.Find(y => y.IsCaptain.Value).Element) .GroupBy(id => id.Value) .OrderByDescending(g => g.Count()); var groupedViceCaptains = this.fplPicks .Select(p => p.Value.Picks.Find(y => y.IsViceCaptain.Value).Element) .GroupBy(id => id.Value) .OrderByDescending(g => g.Count()); foreach (var p in groupedCaptains.AsParallel()) { if (!this.fplPlayerSummaries.ContainsKey(p.Key)) { FplPlayerSummary summary = await string.Format(this.PlayerSummaryUrI, p.Key).GetJsonAsync <FplPlayerSummary>().ConfigureAwait(false); this.fplPlayerSummaries.Add(p.Key, summary); } } foreach (var p in groupedViceCaptains.AsParallel()) { if (!this.fplPlayerSummaries.ContainsKey(p.Key)) { FplPlayerSummary summary = await string.Format(this.PlayerSummaryUrI, p.Key).GetJsonAsync <FplPlayerSummary>().ConfigureAwait(false); this.fplPlayerSummaries.Add(p.Key, summary); } } List <CaptainChoice> captains = new List <CaptainChoice>(); foreach (var pick in this.fplPicks) { long teamId = pick.Key; var cptPick = pick.Value.Picks.Find(p => p.IsCaptain.Value); var vicePick = pick.Value.Picks.Find(p => p.IsViceCaptain.Value); var cptPlayer = this.fplPlayers[cptPick.Element.Value]; var vicePlayer = this.fplPlayers[vicePick.Element.Value]; CaptainChoice cc = new CaptainChoice() { CaptainMultiplier = cptPick.Multiplier.Value, CaptainId = cptPlayer.Id.Value, CaptainEventPoints = this.fplPlayerSummaries[cptPlayer.Id.Value].History.FindAll(y => y.Round == this.currentEventId).Sum(x => x.TotalPoints.Value), TeamEntryId = teamId, CaptainPlayed = this.fplPlayerSummaries[cptPlayer.Id.Value].History.FindAll(y => y.Round == this.currentEventId).Sum(x => x.Minutes.Value) > 0, ViceMultiplier = vicePick.Multiplier.Value, ViceEventPoints = this.fplPlayerSummaries[vicePlayer.Id.Value].History.FindAll(y => y.Round == this.currentEventId).Sum(x => x.TotalPoints.Value), ViceId = vicePlayer.Id.Value, VicePlayed = this.fplPlayerSummaries[vicePlayer.Id.Value].History.FindAll(y => y.Round == this.currentEventId).Sum(x => x.Minutes.Value) > 0 }; captains.Add(cc); } var groupedScores = captains .Where(c => !c.BothBlanked) .GroupBy(c => c.EventScoreMultiplied) .OrderByDescending(g => g.Key); int noBest = groupedScores.First().Count(); int noWorst = groupedScores.Last().Count(); long bestScore = groupedScores.First().Key; long worstScore = groupedScores.Last().Key; List <long> bestPlayerIds = new List <long>(); List <long> worstPlayerIds = new List <long>(); List <long> bestTeamIds = new List <long>(); List <long> worstTeamIds = new List <long>(); foreach (var p in groupedScores.First()) { if (!bestPlayerIds.Contains(p.ActivePlayerId)) { bestPlayerIds.Add(p.ActivePlayerId); } if (!bestTeamIds.Contains(p.TeamEntryId)) { bestTeamIds.Add(p.TeamEntryId); } } foreach (var p in groupedScores.Last()) { if (!worstPlayerIds.Contains(p.ActivePlayerId)) { worstPlayerIds.Add(p.ActivePlayerId); } if (!worstTeamIds.Contains(p.TeamEntryId)) { worstTeamIds.Add(p.TeamEntryId); } } result.Append($"When it came to captaincy choice{TextUtilities.Pluralize(noBest)} {TextUtilities.NaturalParse(bestTeamIds.Select(i => $"{this.fplTeam[i].Entry.Name}").ToList())} did the best this week with {bestScore} point{TextUtilities.Pluralize(noBest)} from {TextUtilities.NaturalParse(bestPlayerIds.Select(i => $"{this.fplPlayers[i].FirstName} {this.fplPlayers[i].SecondName}").ToList())}. "); result.AppendLine($"On the other end of the spectrum were {TextUtilities.NaturalParse(worstTeamIds.Select(i => $"{this.fplTeam[i].Entry.Name}").ToList())} who had picked {TextUtilities.NaturalParse(worstPlayerIds.Select(i => $"{this.fplPlayers[i].FirstName} {this.fplPlayers[i].SecondName}").ToList())} for a total of {worstScore} point{TextUtilities.Pluralize((int)worstScore)}. You receive the armband of shame for this week. "); return(result.ToString()); }