| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using ContextProviders.Kicktipp.Csv; |
| | | 3 | | using EHonda.KicktippAi.Core; |
| | | 4 | | using KicktippIntegration; |
| | | 5 | | using Microsoft.Extensions.FileProviders; |
| | | 6 | | |
| | | 7 | | namespace ContextProviders.Kicktipp; |
| | | 8 | | |
| | | 9 | | public class KicktippContextProvider : IKicktippContextProvider |
| | | 10 | | { |
| | | 11 | | private readonly IKicktippClient _kicktippClient; |
| | | 12 | | private readonly IFileProvider _communityRulesFileProvider; |
| | | 13 | | private readonly string _community; |
| | | 14 | | private readonly string _communityContext; |
| | | 15 | | private readonly string _competition; |
| | | 16 | | private readonly int? _matchday; |
| | | 17 | | private readonly Lazy<Task<IReadOnlyDictionary<string, List<MatchResult>>>> _teamHistoryLazy; |
| | | 18 | | private readonly Lazy<Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory |
| | | 19 | | private readonly Lazy<Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>>> _detailedHeadToHeadHistoryLazy; |
| | | 20 | | |
| | 1 | 21 | | public KicktippContextProvider( |
| | 1 | 22 | | IKicktippClient kicktippClient, |
| | 1 | 23 | | IFileProvider communityRulesFileProvider, |
| | 1 | 24 | | string community, |
| | 1 | 25 | | string? communityContext = null, |
| | 1 | 26 | | string? competition = null, |
| | 1 | 27 | | int? matchday = null) |
| | | 28 | | { |
| | 1 | 29 | | _kicktippClient = kicktippClient ?? throw new ArgumentNullException(nameof(kicktippClient)); |
| | 1 | 30 | | _communityRulesFileProvider = communityRulesFileProvider ?? throw new ArgumentNullException(nameof(communityRule |
| | 1 | 31 | | _community = community ?? throw new ArgumentNullException(nameof(community)); |
| | 1 | 32 | | _communityContext = communityContext ?? community; |
| | 1 | 33 | | _competition = competition ?? CompetitionIds.Bundesliga2025_26; |
| | 1 | 34 | | _matchday = matchday; |
| | 1 | 35 | | _teamHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, List<MatchResult>>>>(LoadTeamHistoryAsync); |
| | 1 | 36 | | _homeAwayHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResul |
| | 1 | 37 | | _detailedHeadToHeadHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>>>(LoadDetaile |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | private async Task<IReadOnlyDictionary<string, List<MatchResult>>> LoadTeamHistoryAsync() |
| | | 41 | | { |
| | 1 | 42 | | var matchesWithHistory = await GetMatchesWithHistoryAsync(); |
| | 1 | 43 | | var teamHistory = new Dictionary<string, List<MatchResult>>(); |
| | | 44 | | |
| | 1 | 45 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 46 | | { |
| | 1 | 47 | | teamHistory[matchWithHistory.Match.HomeTeam] = matchWithHistory.HomeTeamHistory; |
| | 1 | 48 | | teamHistory[matchWithHistory.Match.AwayTeam] = matchWithHistory.AwayTeamHistory; |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | return teamHistory; |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | private async Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory)>> Load |
| | | 55 | | { |
| | 1 | 56 | | var matchesWithHistory = await GetMatchesWithHistoryAsync(); |
| | 1 | 57 | | var homeAwayHistory = new Dictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory)>(); |
| | | 58 | | |
| | 1 | 59 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 60 | | { |
| | 1 | 61 | | var homeTeam = matchWithHistory.Match.HomeTeam; |
| | 1 | 62 | | var awayTeam = matchWithHistory.Match.AwayTeam; |
| | | 63 | | |
| | 1 | 64 | | var (homeTeamHistory, awayTeamHistory) = await _kicktippClient.GetHomeAwayHistoryAsync(_community, homeTeam, |
| | 1 | 65 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | 1 | 66 | | homeAwayHistory[cacheKey] = (homeTeamHistory, awayTeamHistory); |
| | 1 | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | return homeAwayHistory; |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | private async Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>> LoadDetailedHeadToHeadHistoryAsync() |
| | | 73 | | { |
| | 1 | 74 | | var matchesWithHistory = await GetMatchesWithHistoryAsync(); |
| | 1 | 75 | | var detailedHeadToHeadHistory = new Dictionary<string, List<HeadToHeadResult>>(); |
| | | 76 | | |
| | 1 | 77 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 78 | | { |
| | 1 | 79 | | var homeTeam = matchWithHistory.Match.HomeTeam; |
| | 1 | 80 | | var awayTeam = matchWithHistory.Match.AwayTeam; |
| | | 81 | | |
| | 1 | 82 | | var history = await _kicktippClient.GetHeadToHeadDetailedHistoryAsync(_community, homeTeam, awayTeam); |
| | 1 | 83 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | 1 | 84 | | detailedHeadToHeadHistory[cacheKey] = history; |
| | 1 | 85 | | } |
| | | 86 | | |
| | 1 | 87 | | return detailedHeadToHeadHistory; |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | private Task<List<MatchWithHistory>> GetMatchesWithHistoryAsync() |
| | | 91 | | { |
| | 1 | 92 | | return _matchday.HasValue |
| | 1 | 93 | | ? _kicktippClient.GetMatchesWithHistoryAsync(_community, _matchday.Value) |
| | 1 | 94 | | : _kicktippClient.GetMatchesWithHistoryAsync(_community); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public async IAsyncEnumerable<DocumentContext> GetContextAsync([EnumeratorCancellation] CancellationToken cancellati |
| | | 98 | | { |
| | 1 | 99 | | var selection = MatchContextDocumentCatalog.ForCommunity(_communityContext, _competition); |
| | 1 | 100 | | foreach (var documentName in selection.RequiredDocumentNames) |
| | | 101 | | { |
| | 1 | 102 | | if (documentName == MatchContextDocumentCatalog.GetStandingsDocumentName(_competition)) |
| | | 103 | | { |
| | 1 | 104 | | yield return await CurrentStandings(); |
| | | 105 | | } |
| | 1 | 106 | | else if (documentName == $"community-rules-{_communityContext}.md") |
| | | 107 | | { |
| | 1 | 108 | | yield return await CommunityScoringRules(); |
| | | 109 | | } |
| | | 110 | | } |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets context for the two teams in a match. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="homeTeam">The home team name.</param> |
| | | 117 | | /// <param name="awayTeam">The away team name.</param> |
| | | 118 | | /// <returns>An enumerable of context documents for both teams.</returns> |
| | | 119 | | public async IAsyncEnumerable<DocumentContext> GetMatchContextAsync(string homeTeam, string awayTeam, [EnumeratorCan |
| | | 120 | | { |
| | 1 | 121 | | var selection = MatchContextDocumentCatalog.ForMatch(homeTeam, awayTeam, _communityContext, _competition); |
| | 1 | 122 | | var homeAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(homeTeam); |
| | 1 | 123 | | var awayAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(awayTeam); |
| | | 124 | | |
| | 1 | 125 | | foreach (var documentName in selection.RequiredDocumentNames) |
| | | 126 | | { |
| | 1 | 127 | | if (documentName == MatchContextDocumentCatalog.GetStandingsDocumentName(_competition)) |
| | | 128 | | { |
| | 1 | 129 | | yield return await CurrentStandings(); |
| | | 130 | | } |
| | 1 | 131 | | else if (documentName == $"community-rules-{_communityContext}.md") |
| | | 132 | | { |
| | 1 | 133 | | yield return await CommunityScoringRules(); |
| | | 134 | | } |
| | 1 | 135 | | else if (documentName == $"recent-history-{homeAbbreviation}.csv") |
| | | 136 | | { |
| | 1 | 137 | | yield return await RecentHistory(homeTeam); |
| | | 138 | | } |
| | 1 | 139 | | else if (documentName == $"recent-history-{awayAbbreviation}.csv") |
| | | 140 | | { |
| | 1 | 141 | | yield return await RecentHistory(awayTeam); |
| | | 142 | | } |
| | 1 | 143 | | else if (documentName == $"home-history-{homeAbbreviation}.csv") |
| | | 144 | | { |
| | 1 | 145 | | yield return await HomeHistory(homeTeam, awayTeam); |
| | | 146 | | } |
| | 1 | 147 | | else if (documentName == $"away-history-{awayAbbreviation}.csv") |
| | | 148 | | { |
| | 1 | 149 | | yield return await AwayHistory(homeTeam, awayTeam); |
| | | 150 | | } |
| | 1 | 151 | | else if (documentName == $"head-to-head-{homeAbbreviation}-vs-{awayAbbreviation}.csv") |
| | | 152 | | { |
| | 1 | 153 | | yield return await HeadToHeadHistory(homeTeam, awayTeam); |
| | | 154 | | } |
| | | 155 | | } |
| | 1 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Gets context for bonus questions. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <returns>An enumerable of context documents relevant for bonus questions.</returns> |
| | | 162 | | public async IAsyncEnumerable<DocumentContext> GetBonusQuestionContextAsync([EnumeratorCancellation] CancellationTok |
| | | 163 | | { |
| | 1 | 164 | | var selection = MatchContextDocumentCatalog.ForCommunity(_communityContext, _competition); |
| | 1 | 165 | | foreach (var documentName in selection.RequiredDocumentNames) |
| | | 166 | | { |
| | 1 | 167 | | if (documentName == MatchContextDocumentCatalog.GetStandingsDocumentName(_competition)) |
| | | 168 | | { |
| | 1 | 169 | | yield return await CurrentStandings(); |
| | | 170 | | } |
| | 1 | 171 | | else if (documentName == $"community-rules-{_communityContext}.md") |
| | | 172 | | { |
| | 1 | 173 | | yield return await CommunityScoringRules(); |
| | | 174 | | } |
| | | 175 | | } |
| | 1 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Gets the current competition standings as context. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <returns>A document context containing the current standings.</returns> |
| | | 182 | | public async Task<DocumentContext> CurrentStandings() |
| | | 183 | | { |
| | 1 | 184 | | var standings = await _kicktippClient.GetStandingsAsync(_community); |
| | | 185 | | |
| | 1 | 186 | | return standings.ToCsvDocumentContext<TeamStanding, TeamStandingCsvMap>( |
| | 1 | 187 | | MatchContextDocumentCatalog.GetStandingsDocumentBaseName(_competition)); |
| | 1 | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets the current Bundesliga standings as context. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <returns>A document context containing the current standings.</returns> |
| | | 194 | | public Task<DocumentContext> CurrentBundesligaStandings() |
| | | 195 | | { |
| | 1 | 196 | | return CurrentStandings(); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Gets recent match history for a specific team. |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="teamName">The name of the team to get history for.</param> |
| | | 203 | | /// <returns>A document context containing the team's recent match history.</returns> |
| | | 204 | | public async Task<DocumentContext> RecentHistory(string teamName) |
| | | 205 | | { |
| | 1 | 206 | | var teamHistory = await _teamHistoryLazy.Value; |
| | 1 | 207 | | var matchResults = teamHistory.TryGetValue(teamName, out var results) ? results : new List<MatchResult>(); |
| | | 208 | | |
| | 1 | 209 | | var teamAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(teamName); |
| | | 210 | | |
| | 1 | 211 | | return matchResults.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"recent-history-{teamAbbreviation}"); |
| | 1 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets home/away specific history for both teams in a match. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="homeTeam">The home team name.</param> |
| | | 218 | | /// <param name="awayTeam">The away team name.</param> |
| | | 219 | | /// <returns>A document context containing home team's home history and away team's away history.</returns> |
| | | 220 | | public async Task<DocumentContext> HomeHistory(string homeTeam, string awayTeam) |
| | | 221 | | { |
| | 1 | 222 | | var homeAwayHistory = await _homeAwayHistoryLazy.Value; |
| | 1 | 223 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 224 | | |
| | 1 | 225 | | var (homeTeamHistory, _) = homeAwayHistory.TryGetValue(cacheKey, out var history) |
| | 1 | 226 | | ? history |
| | 1 | 227 | | : (new List<MatchResult>(), new List<MatchResult>()); |
| | | 228 | | |
| | 1 | 229 | | var homeAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(homeTeam); |
| | | 230 | | |
| | 1 | 231 | | return homeTeamHistory.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"home-history-{homeAbbreviation}"); |
| | 1 | 232 | | } |
| | | 233 | | |
| | | 234 | | public async Task<DocumentContext> AwayHistory(string homeTeam, string awayTeam) |
| | | 235 | | { |
| | 1 | 236 | | var homeAwayHistory = await _homeAwayHistoryLazy.Value; |
| | 1 | 237 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 238 | | |
| | 1 | 239 | | var (_, awayTeamHistory) = homeAwayHistory.TryGetValue(cacheKey, out var history) |
| | 1 | 240 | | ? history |
| | 1 | 241 | | : (new List<MatchResult>(), new List<MatchResult>()); |
| | | 242 | | |
| | 1 | 243 | | var awayAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(awayTeam); |
| | | 244 | | |
| | 1 | 245 | | return awayTeamHistory.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"away-history-{awayAbbreviation}"); |
| | 1 | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Gets head-to-head history between two teams. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <param name="homeTeam">The home team name.</param> |
| | | 252 | | /// <param name="awayTeam">The away team name.</param> |
| | | 253 | | /// <returns>A document context containing head-to-head match history.</returns> |
| | | 254 | | public async Task<DocumentContext> HeadToHeadHistory(string homeTeam, string awayTeam) |
| | | 255 | | { |
| | 1 | 256 | | var detailedHeadToHeadHistory = await _detailedHeadToHeadHistoryLazy.Value; |
| | 1 | 257 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 258 | | |
| | 1 | 259 | | var history = detailedHeadToHeadHistory.TryGetValue(cacheKey, out var cachedHistory) |
| | 1 | 260 | | ? cachedHistory |
| | 1 | 261 | | : new List<HeadToHeadResult>(); |
| | | 262 | | |
| | 1 | 263 | | var homeAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(homeTeam); |
| | 1 | 264 | | var awayAbbreviation = MatchContextDocumentCatalog.GetTeamAbbreviation(awayTeam); |
| | | 265 | | |
| | 1 | 266 | | return history.ToCsvDocumentContext<HeadToHeadResult, HeadToHeadResultCsvMap>( |
| | 1 | 267 | | $"head-to-head-{homeAbbreviation}-vs-{awayAbbreviation}"); |
| | 1 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Gets the community scoring rules as context. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <returns>A document context containing the scoring rules.</returns> |
| | | 274 | | /// <exception cref="FileNotFoundException">Thrown when the community-specific scoring rules file is not found.</exc |
| | | 275 | | public async Task<DocumentContext> CommunityScoringRules() |
| | | 276 | | { |
| | 1 | 277 | | var filePath = $"{_communityContext}.md"; |
| | 1 | 278 | | var fileInfo = _communityRulesFileProvider.GetFileInfo(filePath); |
| | | 279 | | |
| | 1 | 280 | | if (!fileInfo.Exists) |
| | | 281 | | { |
| | 1 | 282 | | throw new FileNotFoundException( |
| | 1 | 283 | | $"Community scoring rules file not found: {filePath}. Expected file for community context '{_communityCo |
| | | 284 | | } |
| | | 285 | | |
| | 1 | 286 | | var content = await ReadFileContentAsync(fileInfo); |
| | 1 | 287 | | return new DocumentContext( |
| | 1 | 288 | | Name: $"community-rules-{_communityContext}.md", |
| | 1 | 289 | | Content: content); |
| | 1 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Reads the content from a file info asynchronously. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="fileInfo">The file info to read from.</param> |
| | | 296 | | /// <returns>The file content as a string.</returns> |
| | | 297 | | private static async Task<string> ReadFileContentAsync(IFileInfo fileInfo) |
| | | 298 | | { |
| | 1 | 299 | | await using var stream = fileInfo.CreateReadStream(); |
| | 1 | 300 | | using var reader = new StreamReader(stream); |
| | 1 | 301 | | return await reader.ReadToEndAsync(); |
| | 1 | 302 | | } |
| | | 303 | | } |