| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text; |
| | | 3 | | using ContextProviders.Kicktipp.Csv; |
| | | 4 | | using EHonda.KicktippAi.Core; |
| | | 5 | | using KicktippIntegration; |
| | | 6 | | using Microsoft.Extensions.FileProviders; |
| | | 7 | | |
| | | 8 | | namespace ContextProviders.Kicktipp; |
| | | 9 | | |
| | | 10 | | public class KicktippContextProvider : IKicktippContextProvider |
| | | 11 | | { |
| | | 12 | | private readonly IKicktippClient _kicktippClient; |
| | | 13 | | private readonly IFileProvider _communityRulesFileProvider; |
| | | 14 | | private readonly string _community; |
| | | 15 | | private readonly string _communityContext; |
| | | 16 | | private readonly Lazy<Task<IReadOnlyDictionary<string, List<MatchResult>>>> _teamHistoryLazy; |
| | | 17 | | private readonly Lazy<Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory |
| | | 18 | | private readonly Lazy<Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>>> _detailedHeadToHeadHistoryLazy; |
| | | 19 | | |
| | 1 | 20 | | public KicktippContextProvider( |
| | 1 | 21 | | IKicktippClient kicktippClient, |
| | 1 | 22 | | IFileProvider communityRulesFileProvider, |
| | 1 | 23 | | string community, |
| | 1 | 24 | | string? communityContext = null) |
| | | 25 | | { |
| | 1 | 26 | | _kicktippClient = kicktippClient ?? throw new ArgumentNullException(nameof(kicktippClient)); |
| | 1 | 27 | | _communityRulesFileProvider = communityRulesFileProvider ?? throw new ArgumentNullException(nameof(communityRule |
| | 1 | 28 | | _community = community ?? throw new ArgumentNullException(nameof(community)); |
| | 1 | 29 | | _communityContext = communityContext ?? community; |
| | 1 | 30 | | _teamHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, List<MatchResult>>>>(LoadTeamHistoryAsync); |
| | 1 | 31 | | _homeAwayHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResul |
| | 1 | 32 | | _detailedHeadToHeadHistoryLazy = new Lazy<Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>>>(LoadDetaile |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | private async Task<IReadOnlyDictionary<string, List<MatchResult>>> LoadTeamHistoryAsync() |
| | | 36 | | { |
| | 1 | 37 | | var matchesWithHistory = await _kicktippClient.GetMatchesWithHistoryAsync(_community); |
| | 1 | 38 | | var teamHistory = new Dictionary<string, List<MatchResult>>(); |
| | | 39 | | |
| | 1 | 40 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 41 | | { |
| | 1 | 42 | | teamHistory[matchWithHistory.Match.HomeTeam] = matchWithHistory.HomeTeamHistory; |
| | 1 | 43 | | teamHistory[matchWithHistory.Match.AwayTeam] = matchWithHistory.AwayTeamHistory; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | return teamHistory; |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task<IReadOnlyDictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory)>> Load |
| | | 50 | | { |
| | 1 | 51 | | var matchesWithHistory = await _kicktippClient.GetMatchesWithHistoryAsync(_community); |
| | 1 | 52 | | var homeAwayHistory = new Dictionary<string, (List<MatchResult> homeHistory, List<MatchResult> awayHistory)>(); |
| | | 53 | | |
| | 1 | 54 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 55 | | { |
| | 1 | 56 | | var homeTeam = matchWithHistory.Match.HomeTeam; |
| | 1 | 57 | | var awayTeam = matchWithHistory.Match.AwayTeam; |
| | | 58 | | |
| | 1 | 59 | | var (homeTeamHistory, awayTeamHistory) = await _kicktippClient.GetHomeAwayHistoryAsync(_community, homeTeam, |
| | 1 | 60 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | 1 | 61 | | homeAwayHistory[cacheKey] = (homeTeamHistory, awayTeamHistory); |
| | 1 | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | return homeAwayHistory; |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | private async Task<IReadOnlyDictionary<string, List<HeadToHeadResult>>> LoadDetailedHeadToHeadHistoryAsync() |
| | | 68 | | { |
| | 1 | 69 | | var matchesWithHistory = await _kicktippClient.GetMatchesWithHistoryAsync(_community); |
| | 1 | 70 | | var detailedHeadToHeadHistory = new Dictionary<string, List<HeadToHeadResult>>(); |
| | | 71 | | |
| | 1 | 72 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 73 | | { |
| | 1 | 74 | | var homeTeam = matchWithHistory.Match.HomeTeam; |
| | 1 | 75 | | var awayTeam = matchWithHistory.Match.AwayTeam; |
| | | 76 | | |
| | 1 | 77 | | var history = await _kicktippClient.GetHeadToHeadDetailedHistoryAsync(_community, homeTeam, awayTeam); |
| | 1 | 78 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | 1 | 79 | | detailedHeadToHeadHistory[cacheKey] = history; |
| | 1 | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | return detailedHeadToHeadHistory; |
| | 1 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async IAsyncEnumerable<DocumentContext> GetContextAsync([EnumeratorCancellation] CancellationToken cancellati |
| | | 86 | | { |
| | | 87 | | // Provide current Bundesliga standings |
| | 1 | 88 | | yield return await CurrentBundesligaStandings(); |
| | | 89 | | |
| | | 90 | | // Provide community scoring rules |
| | 1 | 91 | | yield return await CommunityScoringRules(); |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets context for the two teams in a match. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="homeTeam">The home team name.</param> |
| | | 98 | | /// <param name="awayTeam">The away team name.</param> |
| | | 99 | | /// <returns>An enumerable of context documents for both teams.</returns> |
| | | 100 | | public async IAsyncEnumerable<DocumentContext> GetMatchContextAsync(string homeTeam, string awayTeam, [EnumeratorCan |
| | | 101 | | { |
| | | 102 | | // Provide current Bundesliga standings |
| | 1 | 103 | | yield return await CurrentBundesligaStandings(); |
| | | 104 | | |
| | | 105 | | // Provide community scoring rules |
| | 1 | 106 | | yield return await CommunityScoringRules(); |
| | | 107 | | |
| | | 108 | | // Provide recent history for both teams (Position 1 - already implemented) |
| | 1 | 109 | | yield return await RecentHistory(homeTeam); |
| | 1 | 110 | | yield return await RecentHistory(awayTeam); |
| | | 111 | | |
| | | 112 | | // Provide home/away specific history for both teams (Position 2) |
| | 1 | 113 | | yield return await HomeHistory(homeTeam, awayTeam); |
| | 1 | 114 | | yield return await AwayHistory(homeTeam, awayTeam); |
| | | 115 | | |
| | | 116 | | // Provide head-to-head history between the teams (Position 3) |
| | 1 | 117 | | yield return await HeadToHeadHistory(homeTeam, awayTeam); |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Gets context for bonus questions. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <returns>An enumerable of context documents relevant for bonus questions.</returns> |
| | | 124 | | public async IAsyncEnumerable<DocumentContext> GetBonusQuestionContextAsync([EnumeratorCancellation] CancellationTok |
| | | 125 | | { |
| | | 126 | | // Provide current Bundesliga standings |
| | 1 | 127 | | yield return await CurrentBundesligaStandings(); |
| | | 128 | | |
| | | 129 | | // Provide community scoring rules |
| | 1 | 130 | | yield return await CommunityScoringRules(); |
| | | 131 | | |
| | | 132 | | // For bonus questions, we could add historical season data, transfer information, etc. |
| | | 133 | | // For now, we'll use the standings as the primary context |
| | 1 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets the current Bundesliga standings as context. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <returns>A document context containing the current standings.</returns> |
| | | 140 | | public async Task<DocumentContext> CurrentBundesligaStandings() |
| | | 141 | | { |
| | 1 | 142 | | var standings = await _kicktippClient.GetStandingsAsync(_community); |
| | | 143 | | |
| | 1 | 144 | | return standings.ToCsvDocumentContext<TeamStanding, TeamStandingCsvMap>("bundesliga-standings"); |
| | 1 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Gets recent match history for a specific team. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <param name="teamName">The name of the team to get history for.</param> |
| | | 151 | | /// <returns>A document context containing the team's recent match history.</returns> |
| | | 152 | | public async Task<DocumentContext> RecentHistory(string teamName) |
| | | 153 | | { |
| | 1 | 154 | | var teamHistory = await _teamHistoryLazy.Value; |
| | 1 | 155 | | var matchResults = teamHistory.TryGetValue(teamName, out var results) ? results : new List<MatchResult>(); |
| | | 156 | | |
| | 1 | 157 | | var teamAbbreviation = GetTeamAbbreviation(teamName); |
| | | 158 | | |
| | 1 | 159 | | return matchResults.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"recent-history-{teamAbbreviation}"); |
| | 1 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets home/away specific history for both teams in a match. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="homeTeam">The home team name.</param> |
| | | 166 | | /// <param name="awayTeam">The away team name.</param> |
| | | 167 | | /// <returns>A document context containing home team's home history and away team's away history.</returns> |
| | | 168 | | public async Task<DocumentContext> HomeHistory(string homeTeam, string awayTeam) |
| | | 169 | | { |
| | 1 | 170 | | var homeAwayHistory = await _homeAwayHistoryLazy.Value; |
| | 1 | 171 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 172 | | |
| | 1 | 173 | | var (homeTeamHistory, _) = homeAwayHistory.TryGetValue(cacheKey, out var history) |
| | 1 | 174 | | ? history |
| | 1 | 175 | | : (new List<MatchResult>(), new List<MatchResult>()); |
| | | 176 | | |
| | 1 | 177 | | var homeAbbreviation = GetTeamAbbreviation(homeTeam); |
| | | 178 | | |
| | 1 | 179 | | return homeTeamHistory.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"home-history-{homeAbbreviation}"); |
| | 1 | 180 | | } |
| | | 181 | | |
| | | 182 | | public async Task<DocumentContext> AwayHistory(string homeTeam, string awayTeam) |
| | | 183 | | { |
| | 1 | 184 | | var homeAwayHistory = await _homeAwayHistoryLazy.Value; |
| | 1 | 185 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 186 | | |
| | 1 | 187 | | var (_, awayTeamHistory) = homeAwayHistory.TryGetValue(cacheKey, out var history) |
| | 1 | 188 | | ? history |
| | 1 | 189 | | : (new List<MatchResult>(), new List<MatchResult>()); |
| | | 190 | | |
| | 1 | 191 | | var awayAbbreviation = GetTeamAbbreviation(awayTeam); |
| | | 192 | | |
| | 1 | 193 | | return awayTeamHistory.ToCsvDocumentContext<MatchResult, MatchResultCsvMap>($"away-history-{awayAbbreviation}"); |
| | 1 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Gets head-to-head history between two teams. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="homeTeam">The home team name.</param> |
| | | 200 | | /// <param name="awayTeam">The away team name.</param> |
| | | 201 | | /// <returns>A document context containing head-to-head match history.</returns> |
| | | 202 | | public async Task<DocumentContext> HeadToHeadHistory(string homeTeam, string awayTeam) |
| | | 203 | | { |
| | 1 | 204 | | var detailedHeadToHeadHistory = await _detailedHeadToHeadHistoryLazy.Value; |
| | 1 | 205 | | var cacheKey = $"{homeTeam}|{awayTeam}"; |
| | | 206 | | |
| | 1 | 207 | | var history = detailedHeadToHeadHistory.TryGetValue(cacheKey, out var cachedHistory) |
| | 1 | 208 | | ? cachedHistory |
| | 1 | 209 | | : new List<HeadToHeadResult>(); |
| | | 210 | | |
| | 1 | 211 | | var homeAbbreviation = GetTeamAbbreviation(homeTeam); |
| | 1 | 212 | | var awayAbbreviation = GetTeamAbbreviation(awayTeam); |
| | | 213 | | |
| | 1 | 214 | | return history.ToCsvDocumentContext<HeadToHeadResult, HeadToHeadResultCsvMap>( |
| | 1 | 215 | | $"head-to-head-{homeAbbreviation}-vs-{awayAbbreviation}"); |
| | 1 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Gets the community scoring rules as context. |
| | | 220 | | /// </summary> |
| | | 221 | | /// <returns>A document context containing the scoring rules.</returns> |
| | | 222 | | /// <exception cref="FileNotFoundException">Thrown when the community-specific scoring rules file is not found.</exc |
| | | 223 | | public async Task<DocumentContext> CommunityScoringRules() |
| | | 224 | | { |
| | 1 | 225 | | var filePath = $"{_communityContext}.md"; |
| | 1 | 226 | | var fileInfo = _communityRulesFileProvider.GetFileInfo(filePath); |
| | | 227 | | |
| | 1 | 228 | | if (!fileInfo.Exists) |
| | | 229 | | { |
| | 1 | 230 | | throw new FileNotFoundException( |
| | 1 | 231 | | $"Community scoring rules file not found: {filePath}. Expected file for community context '{_communityCo |
| | | 232 | | } |
| | | 233 | | |
| | 1 | 234 | | var content = await ReadFileContentAsync(fileInfo); |
| | 1 | 235 | | return new DocumentContext( |
| | 1 | 236 | | Name: $"community-rules-{_communityContext}.md", |
| | 1 | 237 | | Content: content); |
| | 1 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Reads the content from a file info asynchronously. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <param name="fileInfo">The file info to read from.</param> |
| | | 244 | | /// <returns>The file content as a string.</returns> |
| | | 245 | | private static async Task<string> ReadFileContentAsync(IFileInfo fileInfo) |
| | | 246 | | { |
| | 1 | 247 | | await using var stream = fileInfo.CreateReadStream(); |
| | 1 | 248 | | using var reader = new StreamReader(stream); |
| | 1 | 249 | | return await reader.ReadToEndAsync(); |
| | 1 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Gets a team abbreviation for file naming. |
| | | 254 | | /// </summary> |
| | | 255 | | private string GetTeamAbbreviation(string teamName) |
| | | 256 | | { |
| | | 257 | | // Current season team abbreviations (2025-26 Bundesliga participants) |
| | 1 | 258 | | var abbreviations = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| | 1 | 259 | | { |
| | 1 | 260 | | { "1. FC Heidenheim 1846", "fch" }, |
| | 1 | 261 | | { "1. FC Köln", "fck" }, |
| | 1 | 262 | | { "1. FC Union Berlin", "fcu" }, |
| | 1 | 263 | | { "1899 Hoffenheim", "tsg" }, |
| | 1 | 264 | | { "Bayer 04 Leverkusen", "b04" }, |
| | 1 | 265 | | { "Bor. Mönchengladbach", "bmg" }, |
| | 1 | 266 | | { "Borussia Dortmund", "bvb" }, |
| | 1 | 267 | | { "Eintracht Frankfurt", "sge" }, |
| | 1 | 268 | | { "FC Augsburg", "fca" }, |
| | 1 | 269 | | { "FC Bayern München", "fcb" }, |
| | 1 | 270 | | { "FC St. Pauli", "fcs" }, |
| | 1 | 271 | | { "FSV Mainz 05", "m05" }, |
| | 1 | 272 | | { "Hamburger SV", "hsv" }, |
| | 1 | 273 | | { "RB Leipzig", "rbl" }, |
| | 1 | 274 | | { "SC Freiburg", "scf" }, |
| | 1 | 275 | | { "VfB Stuttgart", "vfb" }, |
| | 1 | 276 | | { "VfL Wolfsburg", "wob" }, |
| | 1 | 277 | | { "Werder Bremen", "svw" } |
| | 1 | 278 | | }; |
| | | 279 | | |
| | 1 | 280 | | if (abbreviations.TryGetValue(teamName, out var abbreviation)) |
| | | 281 | | { |
| | 1 | 282 | | return abbreviation; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | // Fallback: create abbreviation from team name |
| | 1 | 286 | | var words = teamName.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | 1 | 287 | | var abbr = new StringBuilder(); |
| | | 288 | | |
| | 1 | 289 | | foreach (var word in words.Take(3)) // Take up to 3 words |
| | | 290 | | { |
| | 1 | 291 | | if (word.Length > 0 && char.IsLetter(word[0])) |
| | | 292 | | { |
| | 1 | 293 | | abbr.Append(char.ToLowerInvariant(word[0])); |
| | | 294 | | } |
| | | 295 | | } |
| | | 296 | | |
| | 1 | 297 | | return abbr.Length > 0 ? abbr.ToString() : "unknown"; |
| | | 298 | | } |
| | | 299 | | } |