| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace EHonda.KicktippAi.Core; |
| | | 5 | | |
| | | 6 | | public sealed record MatchContextDocumentSelection( |
| | | 7 | | IReadOnlyList<string> RequiredDocumentNames, |
| | | 8 | | IReadOnlyList<string> OptionalDocumentNames); |
| | | 9 | | |
| | | 10 | | public static class MatchContextDocumentCatalog |
| | | 11 | | { |
| | 1 | 12 | | private sealed record MatchContextDocumentPolicy( |
| | 1 | 13 | | bool IncludeCommunityRules, |
| | 1 | 14 | | bool IncludeRecentHistory, |
| | 1 | 15 | | bool IncludeFifaRankings, |
| | 1 | 16 | | bool IncludeLineups, |
| | 1 | 17 | | bool IncludeHomeAwayHistory, |
| | 1 | 18 | | bool IncludeHeadToHead, |
| | 1 | 19 | | bool IncludeTransfers); |
| | | 20 | | |
| | 1 | 21 | | private static readonly MatchContextDocumentPolicy BundesligaPolicy = new( |
| | 1 | 22 | | IncludeCommunityRules: true, |
| | 1 | 23 | | IncludeRecentHistory: true, |
| | 1 | 24 | | IncludeFifaRankings: false, |
| | 1 | 25 | | IncludeLineups: false, |
| | 1 | 26 | | IncludeHomeAwayHistory: true, |
| | 1 | 27 | | IncludeHeadToHead: true, |
| | 1 | 28 | | IncludeTransfers: true); |
| | | 29 | | |
| | 1 | 30 | | private static readonly MatchContextDocumentPolicy WorldCup2026Policy = new( |
| | 1 | 31 | | IncludeCommunityRules: true, |
| | 1 | 32 | | IncludeRecentHistory: true, |
| | 1 | 33 | | IncludeFifaRankings: true, |
| | 1 | 34 | | IncludeLineups: true, |
| | 1 | 35 | | IncludeHomeAwayHistory: false, |
| | 1 | 36 | | IncludeHeadToHead: false, |
| | 1 | 37 | | IncludeTransfers: false); |
| | | 38 | | |
| | 1 | 39 | | private static readonly IReadOnlyDictionary<string, MatchContextDocumentPolicy> CommunityPolicies = |
| | 1 | 40 | | new Dictionary<string, MatchContextDocumentPolicy>(StringComparer.OrdinalIgnoreCase) |
| | 1 | 41 | | { |
| | 1 | 42 | | ["ehonda-dev-wm26"] = WorldCup2026Policy |
| | 1 | 43 | | }; |
| | | 44 | | |
| | 1 | 45 | | private static readonly IReadOnlyDictionary<string, string> TeamAbbreviations = |
| | 1 | 46 | | new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| | 1 | 47 | | { |
| | 1 | 48 | | { "1. FC Heidenheim 1846", "fch" }, |
| | 1 | 49 | | { "1. FC Köln", "fck" }, |
| | 1 | 50 | | { "1. FC Union Berlin", "fcu" }, |
| | 1 | 51 | | { "1899 Hoffenheim", "tsg" }, |
| | 1 | 52 | | { "Bayer 04 Leverkusen", "b04" }, |
| | 1 | 53 | | { "Bor. Mönchengladbach", "bmg" }, |
| | 1 | 54 | | { "Borussia Dortmund", "bvb" }, |
| | 1 | 55 | | { "Eintracht Frankfurt", "sge" }, |
| | 1 | 56 | | { "FC Augsburg", "fca" }, |
| | 1 | 57 | | { "FC Bayern München", "fcb" }, |
| | 1 | 58 | | { "FC St. Pauli", "fcs" }, |
| | 1 | 59 | | { "FSV Mainz 05", "m05" }, |
| | 1 | 60 | | { "Hamburger SV", "hsv" }, |
| | 1 | 61 | | { "RB Leipzig", "rbl" }, |
| | 1 | 62 | | { "SC Freiburg", "scf" }, |
| | 1 | 63 | | { "VfB Stuttgart", "vfb" }, |
| | 1 | 64 | | { "VfL Wolfsburg", "wob" }, |
| | 1 | 65 | | { "Werder Bremen", "svw" } |
| | 1 | 66 | | }; |
| | | 67 | | |
| | | 68 | | public static MatchContextDocumentSelection ForMatch( |
| | | 69 | | string homeTeam, |
| | | 70 | | string awayTeam, |
| | | 71 | | string communityContext, |
| | | 72 | | string? competition = null) |
| | | 73 | | { |
| | 1 | 74 | | ArgumentException.ThrowIfNullOrWhiteSpace(homeTeam); |
| | 1 | 75 | | ArgumentException.ThrowIfNullOrWhiteSpace(awayTeam); |
| | 1 | 76 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | | 77 | | |
| | 1 | 78 | | var homeAbbreviation = GetTeamAbbreviation(homeTeam); |
| | 1 | 79 | | var awayAbbreviation = GetTeamAbbreviation(awayTeam); |
| | 1 | 80 | | var standingsDocumentName = GetStandingsDocumentName(competition); |
| | 1 | 81 | | var policy = ResolvePolicy(communityContext, competition); |
| | | 82 | | |
| | 1 | 83 | | var requiredDocuments = new List<string> { standingsDocumentName }; |
| | 1 | 84 | | if (policy.IncludeCommunityRules) |
| | | 85 | | { |
| | 1 | 86 | | requiredDocuments.Add($"community-rules-{communityContext}.md"); |
| | | 87 | | } |
| | | 88 | | |
| | 1 | 89 | | if (policy.IncludeRecentHistory) |
| | | 90 | | { |
| | 1 | 91 | | requiredDocuments.Add($"recent-history-{homeAbbreviation}.csv"); |
| | 1 | 92 | | requiredDocuments.Add($"recent-history-{awayAbbreviation}.csv"); |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | if (policy.IncludeFifaRankings) |
| | | 96 | | { |
| | 1 | 97 | | requiredDocuments.Add(GetFifaRankingDocumentName(homeTeam)); |
| | 1 | 98 | | requiredDocuments.Add(GetFifaRankingDocumentName(awayTeam)); |
| | | 99 | | } |
| | | 100 | | |
| | 1 | 101 | | if (policy.IncludeLineups) |
| | | 102 | | { |
| | 1 | 103 | | requiredDocuments.Add(GetLineupDocumentName(homeTeam)); |
| | 1 | 104 | | requiredDocuments.Add(GetLineupDocumentName(awayTeam)); |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | if (policy.IncludeHomeAwayHistory) |
| | | 108 | | { |
| | 1 | 109 | | requiredDocuments.Add($"home-history-{homeAbbreviation}.csv"); |
| | 1 | 110 | | requiredDocuments.Add($"away-history-{awayAbbreviation}.csv"); |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | if (policy.IncludeHeadToHead) |
| | | 114 | | { |
| | 1 | 115 | | requiredDocuments.Add($"head-to-head-{homeAbbreviation}-vs-{awayAbbreviation}.csv"); |
| | | 116 | | } |
| | | 117 | | |
| | 1 | 118 | | var optionalDocuments = policy.IncludeTransfers |
| | 1 | 119 | | ? new List<string> |
| | 1 | 120 | | { |
| | 1 | 121 | | $"{homeAbbreviation}-transfers.csv", |
| | 1 | 122 | | $"{awayAbbreviation}-transfers.csv" |
| | 1 | 123 | | } |
| | 1 | 124 | | : []; |
| | | 125 | | |
| | 1 | 126 | | return new MatchContextDocumentSelection(requiredDocuments, optionalDocuments); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public static MatchContextDocumentSelection ForCommunity( |
| | | 130 | | string communityContext, |
| | | 131 | | string? competition = null) |
| | | 132 | | { |
| | 1 | 133 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | | 134 | | |
| | 1 | 135 | | var standingsDocumentName = GetStandingsDocumentName(competition); |
| | 1 | 136 | | var policy = ResolvePolicy(communityContext, competition); |
| | 1 | 137 | | var requiredDocuments = new List<string> { standingsDocumentName }; |
| | | 138 | | |
| | 1 | 139 | | if (policy.IncludeCommunityRules) |
| | | 140 | | { |
| | 1 | 141 | | requiredDocuments.Add($"community-rules-{communityContext}.md"); |
| | | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | return new MatchContextDocumentSelection(requiredDocuments, []); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public static string GetStandingsDocumentName(string? competition = null) |
| | | 148 | | { |
| | 1 | 149 | | return string.Equals(competition, CompetitionIds.FifaWorldCup2026, StringComparison.OrdinalIgnoreCase) |
| | 1 | 150 | | ? "fifa-world-cup-2026-standings.csv" |
| | 1 | 151 | | : "bundesliga-standings.csv"; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public static string GetStandingsDocumentBaseName(string? competition = null) |
| | | 155 | | { |
| | 1 | 156 | | var documentName = GetStandingsDocumentName(competition); |
| | 1 | 157 | | return documentName.EndsWith(".csv", StringComparison.OrdinalIgnoreCase) |
| | 1 | 158 | | ? documentName[..^4] |
| | 1 | 159 | | : documentName; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public static string GetFifaRankingDocumentName(string teamName) |
| | | 163 | | { |
| | 1 | 164 | | ArgumentException.ThrowIfNullOrWhiteSpace(teamName); |
| | | 165 | | |
| | 1 | 166 | | return $"fifa-ranking-{GetTeamAbbreviation(teamName)}.csv"; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public static string GetLineupDocumentName(string teamName) |
| | | 170 | | { |
| | 1 | 171 | | ArgumentException.ThrowIfNullOrWhiteSpace(teamName); |
| | | 172 | | |
| | 1 | 173 | | return $"lineup-{GetTeamAbbreviation(teamName)}.csv"; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public static string GetTeamAbbreviation(string teamName) |
| | | 177 | | { |
| | 1 | 178 | | ArgumentException.ThrowIfNullOrWhiteSpace(teamName); |
| | | 179 | | |
| | 1 | 180 | | if (TeamAbbreviations.TryGetValue(teamName, out var abbreviation)) |
| | | 181 | | { |
| | 1 | 182 | | return abbreviation; |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | return SlugifyTeamName(teamName); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private static MatchContextDocumentPolicy ResolvePolicy(string communityContext, string? competition) |
| | | 189 | | { |
| | 1 | 190 | | if (CommunityPolicies.TryGetValue(communityContext, out var communityPolicy)) |
| | | 191 | | { |
| | 1 | 192 | | return communityPolicy; |
| | | 193 | | } |
| | | 194 | | |
| | 1 | 195 | | return string.Equals(competition, CompetitionIds.FifaWorldCup2026, StringComparison.OrdinalIgnoreCase) |
| | 1 | 196 | | ? WorldCup2026Policy |
| | 1 | 197 | | : BundesligaPolicy; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | private static string SlugifyTeamName(string teamName) |
| | | 201 | | { |
| | 1 | 202 | | var normalized = teamName.Normalize(NormalizationForm.FormD); |
| | 1 | 203 | | var builder = new StringBuilder(); |
| | | 204 | | |
| | 1 | 205 | | foreach (var character in normalized) |
| | | 206 | | { |
| | 1 | 207 | | var category = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(character); |
| | 1 | 208 | | if (category == System.Globalization.UnicodeCategory.NonSpacingMark) |
| | | 209 | | { |
| | | 210 | | continue; |
| | | 211 | | } |
| | | 212 | | |
| | 1 | 213 | | if (char.IsLetterOrDigit(character)) |
| | | 214 | | { |
| | 1 | 215 | | builder.Append(char.ToLowerInvariant(character)); |
| | | 216 | | } |
| | 1 | 217 | | else if (builder.Length > 0 && builder[^1] != '-') |
| | | 218 | | { |
| | 1 | 219 | | builder.Append('-'); |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | 1 | 223 | | var slug = Regex.Replace(builder.ToString().Trim('-'), "-{2,}", "-"); |
| | 1 | 224 | | return string.IsNullOrWhiteSpace(slug) ? "unknown" : slug; |
| | | 225 | | } |
| | | 226 | | } |