public static string GetTopThreeGameweekEntries(ClassicLeague league, Gameweek gameweek) { var topThree = league.Standings.Entries .GroupBy(e => e.EventTotal) .OrderByDescending(g => g.Key) .Take(3) .ToArray(); if (!topThree.Any()) { return(null); } var sb = new StringBuilder(); sb.Append("Top three this gameweek was:\n"); for (var i = 0; i < topThree.Length; i++) { var group = topThree[i]; foreach (var entry in group) { sb.Append($"{Formatter.RankEmoji(i)} {entry.GetEntryLink(gameweek.Id)} - {entry.EventTotal}\n"); } } return(sb.ToString()); }
public async Task Handle(PublishGameweekFinishedToGuild message, IMessageHandlerContext context) { var sub = await _repo.GetGuildSubscription(message.GuildId, message.ChannelId); if (sub != null && message.LeagueId.HasValue && sub.Subscriptions.ContainsSubscriptionFor(EventSubscription.Standings)) { var settings = await _settingsClient.GetGlobalSettings(); var gameweeks = settings.Gameweeks; var gw = gameweeks.SingleOrDefault(g => g.Id == message.GameweekId); ClassicLeague league = await _leagueClient.GetClassicLeague(message.LeagueId.Value, tolerate404 : true); if (league != null) { var messages = new List <RichMesssage>(); var intro = Formatter.FormatGameweekFinished(gw, league); var standings = Formatter.GetStandings(league, gw, includeExternalLinks: false); var topThree = Formatter.GetTopThreeGameweekEntries(league, gw, includeExternalLinks: false); var worst = Formatter.GetWorstGameweekEntry(league, gw, includeExternalLinks: false); messages.AddRange(new RichMesssage[] { new ("ℹ️ Gameweek finished!", intro), new ("ℹ️ Standings", standings), new ("ℹ️ Top 3", topThree), new ("ℹ️ Lantern beige", worst) });
public static string GetWorstGameweekEntry(ClassicLeague league, Gameweek gameweek, bool includeExternalLinks = true) { var worst = league.Standings.Entries.OrderBy(e => e.EventTotal).FirstOrDefault(); string entryOrEntryLink = includeExternalLinks? worst.GetEntryLink(gameweek.Id) : worst?.EntryName; return(worst == null ? null : $"💩 {entryOrEntryLink} only got {worst.EventTotal} points. Wow."); }
public static string GetStandings(ClassicLeague league, Gameweek gameweek, bool includeExternalLinks = true) { var sb = new StringBuilder(); var sortedByRank = league.Standings.Entries.OrderBy(x => x.Rank); var numPlayers = league.Standings.Entries.Count; if (gameweek == null) { sb.Append("No current gameweek!"); return(sb.ToString()); } sb.Append($"⭐️ *Here's the current standings after {gameweek.Name}* ⭐ \n\n"); foreach (var player in sortedByRank) { var arrow = GetRankChangeEmoji(player, numPlayers, gameweek.Id); string entryOrLink = includeExternalLinks ? player.GetEntryLink(gameweek.Id) : player.EntryName; sb.Append($"\n{player.Rank}. {entryOrLink} - {player.Total} {arrow}"); } return(sb.ToString()); }
public static string GetStandings(ClassicLeague league, ICollection <Gameweek> gameweeks) { var sb = new StringBuilder(); var sortedByRank = league.Standings.Entries.OrderBy(x => x.Rank); var numPlayers = league.Standings.Entries.Count; var currentGw = gameweeks.SingleOrDefault(x => x.IsCurrent)?.Id; sb.Append($":star: *Results after GW {currentGw}* :star: \n\n"); foreach (var player in sortedByRank) { var arrow = GetRankChangeEmoji(player, numPlayers); sb.Append($"{player.Rank}. {player.GetEntryLink(currentGw)} - {player.Total} {arrow} \n"); } return(sb.ToString()); }
public async Task Handle(PublishStandingsToSlackWorkspace message, IMessageHandlerContext context) { var settings = await _settingsClient.GetGlobalSettings(); var gameweeks = settings.Gameweeks; var gw = gameweeks.SingleOrDefault(g => g.Id == message.GameweekId); ClassicLeague league = null; try { league = await _leagueClient.GetClassicLeague(message.LeagueId); var intro = Formatter.FormatGameweekFinished(gw, league); var standings = Formatter.GetStandings(league, gw); var topThree = Formatter.GetTopThreeGameweekEntries(league, gw); var worst = Formatter.GetWorstGameweekEntry(league, gw); await _publisher.PublishToWorkspace(message.WorkspaceId, message.Channel, intro, standings, topThree, worst); } catch (HttpRequestException e) when(e.StatusCode == HttpStatusCode.NotFound) { await _publisher.PublishToWorkspace(message.WorkspaceId, message.Channel, $"League standings are now generally ready, but I could not seem to find a classic league with id `{message.LeagueId}`. Are you sure it's a valid classic league id?"); } }
public void FormatGameweekFinished_ShoudReturnCorrectMessage( int globalAverage, int entryScore1, int entryScore2, int entryScore3, string expectedMessage) { // Arrange var gameweek = new Gameweek { Name = "Gameweek 1", AverageScore = globalAverage }; var league = new ClassicLeague { Standings = new ClassicLeagueStandings { Entries = new[] { new ClassicLeagueEntry { EventTotal = entryScore1 }, new ClassicLeagueEntry { EventTotal = entryScore2 }, new ClassicLeagueEntry { EventTotal = entryScore3 } } } }; // Act var message = Formatter.FormatGameweekFinished(gameweek, league); // Assert Assert.Equal(expectedMessage, message); }
public static string GetStandings(ClassicLeague league, Gameweek gameweek) { var sb = new StringBuilder(); var sortedByRank = league.Standings.Entries.OrderBy(x => x.Rank); var numPlayers = league.Standings.Entries.Count; if (gameweek == null) { sb.Append("No current gameweek!"); return(sb.ToString()); } sb.Append($":star: *Here's the current standings after {gameweek.Name}* :star: \n\n"); foreach (var player in sortedByRank) { var arrow = GetRankChangeEmoji(player, numPlayers); sb.Append($"{player.Rank}. {player.GetEntryLink(gameweek.Id)} - {player.Total} {arrow} \n"); } return(sb.ToString()); }
public static string GetWorstGameweekEntry(ClassicLeague league, Gameweek gameweek) { var worst = league.Standings.Entries.OrderBy(e => e.EventTotal).FirstOrDefault(); return(worst == null ? null : $":poop: {worst.GetEntryLink(gameweek.Id)} only got {worst.EventTotal} points. Wow."); }
public async Task Handle(ProcessGameweekStartedForSlackWorkspace message, IMessageHandlerContext context) { var newGameweek = message.GameweekId; var team = await _teamRepo.GetTeam(message.WorkspaceId); if (team.HasRegisteredFor(EventSubscription.Captains) || team.HasRegisteredFor(EventSubscription.Transfers)) { await _publisher.PublishToWorkspace(team.TeamId, team.FplBotSlackChannel, $"Gameweek {message.GameweekId}!"); } var messages = new List <string>(); var leagueExists = false; ClassicLeague league = null; if (team.FplbotLeagueId.HasValue) { league = await _leagueClient.GetClassicLeague(team.FplbotLeagueId.Value, tolerate404 : true); } leagueExists = league != null; if (leagueExists && team.HasRegisteredFor(EventSubscription.Captains)) { var captainPicks = await _captainsByGameweek.GetEntryCaptainPicks(newGameweek, team.FplbotLeagueId.Value); if (league.Standings.Entries.Count < MemberCountForLargeLeague) { messages.Add(_captainsByGameweek.GetCaptainsByGameWeek(newGameweek, captainPicks)); messages.Add(_captainsByGameweek.GetCaptainsChartByGameWeek(newGameweek, captainPicks)); } else { messages.Add(_captainsByGameweek.GetCaptainsStatsByGameWeek(captainPicks)); } } else if (team.FplbotLeagueId.HasValue && !leagueExists && team.HasRegisteredFor(EventSubscription.Captains)) { messages.Add($"⚠️ You're subscribing to captains notifications, but following a league ({team.FplbotLeagueId.Value}) that does not exist. Update to a valid classic league, or unsubscribe to captains to avoid this message in the future."); } else { _logger.LogInformation("Team {team} hasn't subscribed for gw start captains, so bypassing it", team.TeamId); } if (leagueExists && team.HasRegisteredFor(EventSubscription.Transfers)) { try { if (league.Standings.Entries.Count < MemberCountForLargeLeague) { messages.Add(await _transfersByGameweek.GetTransfersByGameweekTexts(newGameweek, team.FplbotLeagueId.Value)); } else { var externalLink = $"See https://www.fplbot.app/leagues/{team.FplbotLeagueId.Value} for all transfers"; messages.Add(externalLink); } } catch (HttpRequestException hre) when(hre.StatusCode == HttpStatusCode.TooManyRequests) // fallback { var externalLink = $"See https://www.fplbot.app/leagues/{team.FplbotLeagueId.Value} for all transfers"; messages.Add(externalLink); } } else if (team.FplbotLeagueId.HasValue && !leagueExists && team.HasRegisteredFor(EventSubscription.Transfers)) { messages.Add($"⚠️ You're subscribing to transfers notifications, but following a league ({team.FplbotLeagueId.Value}) that does not exist. Update to a valid classic league, or unsubscribe to transfers to avoid this message in the future."); } else { _logger.LogInformation("Team {team} hasn't subscribed for gw start transfers, so bypassing it", team.TeamId); } await _publisher.PublishToWorkspace(team.TeamId, team.FplBotSlackChannel, messages.ToArray()); }
public void GetTopThreeGameweekEntriesTests_ShoudReturnCorrectMessage() { // Arrange var gameweek = new Gameweek { Name = "1", Id = 1 }; var league = new ClassicLeague { Standings = new ClassicLeagueStandings { Entries = new[] { new ClassicLeagueEntry { EventTotal = 50, Total = 1337, Entry = 1, EntryName = "K" }, new ClassicLeagueEntry { EventTotal = 90, Total = 500, Entry = 2, EntryName = "L" }, new ClassicLeagueEntry { EventTotal = 90, Total = 500, Entry = 3, EntryName = "La" }, new ClassicLeagueEntry { EventTotal = 10, Total = 42, Entry = 4, EntryName = "M" }, new ClassicLeagueEntry { EventTotal = 10, Total = 43, Entry = 5, EntryName = "J" }, new ClassicLeagueEntry { EventTotal = 5, Total = 43, Entry = 6, EntryName = "X" } } } }; // Act var message = Formatter.GetTopThreeGameweekEntries(league, gameweek); // Assert Assert.Equal("Top three this gameweek was:\n" + ":first_place_medal: <https://fantasy.premierleague.com/entry/2/event/1|L> - 90\n" + ":first_place_medal: <https://fantasy.premierleague.com/entry/3/event/1|La> - 90\n" + ":second_place_medal: <https://fantasy.premierleague.com/entry/1/event/1|K> - 50\n" + ":third_place_medal: <https://fantasy.premierleague.com/entry/4/event/1|M> - 10\n" + ":third_place_medal: <https://fantasy.premierleague.com/entry/5/event/1|J> - 10\n", message); }
public async Task Handle(ProcessGameweekStartedForGuildChannel message, IMessageHandlerContext context) { var newGameweek = message.GameweekId; var team = await _repo.GetGuildSubscription(message.GuildId, message.ChannelId); var messages = new List <RichMesssage>(); if (team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Captains) || team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Transfers)) { messages.Add(new RichMesssage($"Gameweek {message.GameweekId}!", "")); } ClassicLeague league = null; if (team.LeagueId.HasValue) { league = await _leagueClient.GetClassicLeague(team.LeagueId.Value, tolerate404 : true); } var leagueExists = league != null; if (leagueExists && team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Captains)) { var captainPicks = await _captainsByGameweek.GetEntryCaptainPicks(newGameweek, team.LeagueId.Value); if (league.Standings.Entries.Count < MemberCountForLargeLeague) { string captainsByGameWeek = _captainsByGameweek.GetCaptainsByGameWeek(newGameweek, captainPicks, includeExternalLinks: false); messages.Add(new RichMesssage("Captains:", captainsByGameWeek)); string captainsChartByGameWeek = _captainsByGameweek.GetCaptainsChartByGameWeek(newGameweek, captainPicks); messages.Add(new RichMesssage("Chart", captainsChartByGameWeek)); } else { string captainsByGameWeek = _captainsByGameweek.GetCaptainsStatsByGameWeek(captainPicks, includeHeader: false); messages.Add(new RichMesssage("Captain stats:", captainsByGameWeek)); } } else if (team.LeagueId.HasValue && !leagueExists && team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Captains)) { messages.Add(new RichMesssage("⚠️Warning!", $"️ You're subscribing to captains notifications, but following a league ({team.LeagueId.Value}) that does not exist. Update to a valid classic league, or unsubscribe to captains to avoid this message in the future.")); } else { _logger.LogInformation("Team {team} hasn't subscribed for gw start captains, so bypassing it", team.GuildId); } if (leagueExists && team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Transfers)) { if (league.Standings.Entries.Count < MemberCountForLargeLeague) { var transfersByGameweekTexts = await _transfersByGameweek.GetTransferMessages(newGameweek, team.LeagueId.Value, includeExternalLinks : false); if (transfersByGameweekTexts.GetTotalCharCount() > 2000) { var array = transfersByGameweekTexts.Messages.ToList(); int halfway = array.Count() / 2; var firstArray = array.Take(halfway); var secondArray = array.Skip(halfway); messages.Add(new RichMesssage("Transfers", string.Join("", firstArray.Select(c => c.Message)))); messages.Add(new RichMesssage("Transfers (#2)", string.Join("", secondArray.Select(c => c.Message)))); } else { messages.Add(new RichMesssage("Transfers", string.Join("", transfersByGameweekTexts.Messages.Select(m => m.Message)))); } } else { var externalLink = $"See https://www.fplbot.app/leagues/{team.LeagueId.Value} for full details"; messages.Add(new RichMesssage("Captains/Transfers/Chips", externalLink)); } } else if (team.LeagueId.HasValue && !leagueExists && team.Subscriptions.ContainsSubscriptionFor(EventSubscription.Transfers)) { messages.Add(new RichMesssage("⚠️Warning!", $"⚠️ You're subscribing to transfers notifications, but following a league ({team.LeagueId.Value}) that does not exist. Update to a valid classic league, or unsubscribe to transfers to avoid this message in the future.")); } else { _logger.LogInformation("Team {team} hasn't subscribed for gw start transfers, so bypassing it", team.GuildId); } var i = 0; foreach (var richMessage in messages) { i = i + 2; var sendOptions = new SendOptions(); sendOptions.DelayDeliveryWith(TimeSpan.FromSeconds(i)); sendOptions.RouteToThisEndpoint(); await context.Send(new PublishRichToGuildChannel(team.GuildId, team.ChannelId, richMessage.Title, richMessage.Description), sendOptions); } }
public void GetWorstGameweekEntryTests_ShoudReturnCorrectMessage() { // Arrange var gameweek = new Gameweek { Name = "1", Id = 1 }; var league = new ClassicLeague { Standings = new ClassicLeagueStandings { Entries = new[] { new ClassicLeagueEntry { EventTotal = 50, Total = 1337, Entry = 1, EntryName = "K" }, new ClassicLeagueEntry { EventTotal = 90, Total = 500, Entry = 2, EntryName = "L" }, new ClassicLeagueEntry { EventTotal = 90, Total = 500, Entry = 3, EntryName = "La" }, new ClassicLeagueEntry { EventTotal = 10, Total = 42, Entry = 4, EntryName = "M" }, new ClassicLeagueEntry { EventTotal = 10, Total = 43, Entry = 5, EntryName = "J" }, new ClassicLeagueEntry { EventTotal = 5, Total = 43, Entry = 6, EntryName = "X" } } } }; // Act var message = Formatter.GetWorstGameweekEntry(league, gameweek); // Assert Assert.Equal("💩 <https://fantasy.premierleague.com/entry/6/event/1|X> only got 5 points. Wow.", message); }