File size: 7,600 Bytes
1790bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c88da70
 
1790bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Typed client for the FastAPI backend.
// Empty => same-origin "/api/*" (proxied by Next.js rewrites to the backend),
// so one public URL/tunnel serves both frontend and API.
export const API = process.env.NEXT_PUBLIC_API_URL || "";

export async function api<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${API}${path}`, { cache: "no-store", ...init });
  if (!res.ok) {
    const txt = await res.text().catch(() => "");
    throw new Error(`${res.status} ${txt}`);
  }
  return res.json() as Promise<T>;
}

export type Team = {
  name: string;
  group_label: string;
  confederation: string | null;
  elo: number;
  fifa_rank: number | null;
  attack: number | null;
  defence: number | null;
  p_group_win: number | null;
  p_advance: number | null;
  p_r16: number | null;
  p_qf: number | null;
  p_sf: number | null;
  p_final: number | null;
  p_win: number | null;
};

export type Fixture = {
  date: string;
  kickoff?: string | null;
  home_team: string;
  away_team: string;
  city: string | null;
  country: string | null;
  neutral: number;
  group_label?: string | null;
  p_home: number | null;
  p_draw: number | null;
  p_away: number | null;
  exp_home: number | null;
  exp_away: number | null;
  over25?: number | null;
  btts?: number | null;
  top_scores: [number, number, number][] | null;
};

export type Prediction = {
  home_team: string;
  away_team: string;
  neutral: boolean;
  p_home: number;
  p_draw: number;
  p_away: number;
  exp_home: number;
  exp_away: number;
  top_scores: [number, number, number][];
  grid: number[][];
  over25: number;
  btts: number;
};

export type Meta = {
  id: number;
  model_name: string;
  xi: number;
  trained_at: string;
  n_matches: number;
  rps: number;
  brier: number;
  logloss: number;
  n_sims: number;
  notes: { T?: number; rps_calibrated?: number };
};

export type SimRow = Team & { team: string };

export const FLAG: Record<string, string> = {
  Argentina: "🇦🇷", Spain: "🇪🇸", Brazil: "🇧🇷", France: "🇫🇷", England: "🏴󠁧󠁢󠁥󠁮󠁧󠁿",
  Portugal: "🇵🇹", Germany: "🇩🇪", Netherlands: "🇳🇱", Belgium: "🇧🇪", Croatia: "🇭🇷",
  Morocco: "🇲🇦", Uruguay: "🇺🇾", Colombia: "🇨🇴", Japan: "🇯🇵", "South Korea": "🇰🇷",
  Mexico: "🇲🇽", "United States": "🇺🇸", Canada: "🇨🇦", Switzerland: "🇨🇭",
  Austria: "🇦🇹", Turkey: "🇹🇷", Norway: "🇳🇴", Sweden: "🇸🇪", Denmark: "🇩🇰",
  Senegal: "🇸🇳", "Ivory Coast": "🇨🇮", Ghana: "🇬🇭", Egypt: "🇪🇬", Algeria: "🇩🇿",
  Tunisia: "🇹🇳", "Cape Verde": "🇨🇻", "South Africa": "🇿🇦", "DR Congo": "🇨🇩",
  Iran: "🇮🇷", "Saudi Arabia": "🇸🇦", Qatar: "🇶🇦", Australia: "🇦🇺", Uzbekistan: "🇺🇿",
  Jordan: "🇯🇴", Iraq: "🇮🇶", Paraguay: "🇵🇾", Ecuador: "🇪🇨", Scotland: "🏴󠁧󠁢󠁳󠁣󠁴󠁿",
  "Czech Republic": "🇨🇿", "Bosnia and Herzegovina": "🇧🇦", Panama: "🇵🇦",
  Haiti: "🇭🇹", "Curaçao": "🇨🇼", "New Zealand": "🇳🇿", Wales: "🏴󠁧󠁢󠁷󠁬󠁳󠁿",
};

export const flag = (t: string) => FLAG[t] || "⚽";

// ISO 3166-1 alpha-2 (flagcdn) — England/Scotland use sub-region codes.
export const ISO: Record<string, string> = {
  Argentina: "ar", Spain: "es", Brazil: "br", France: "fr", England: "gb-eng",
  Portugal: "pt", Germany: "de", Netherlands: "nl", Belgium: "be", Croatia: "hr",
  Morocco: "ma", Uruguay: "uy", Colombia: "co", Japan: "jp", "South Korea": "kr",
  Mexico: "mx", "United States": "us", Canada: "ca", Switzerland: "ch",
  Austria: "at", Turkey: "tr", Norway: "no", Sweden: "se", Senegal: "sn",
  "Ivory Coast": "ci", Ghana: "gh", Egypt: "eg", Algeria: "dz", Tunisia: "tn",
  "Cape Verde": "cv", "South Africa": "za", "DR Congo": "cd", Iran: "ir",
  "Saudi Arabia": "sa", Qatar: "qa", Australia: "au", Uzbekistan: "uz",
  Jordan: "jo", Iraq: "iq", Paraguay: "py", Ecuador: "ec", Scotland: "gb-sct",
  "Czech Republic": "cz", "Bosnia and Herzegovina": "ba", Panama: "pa",
  Haiti: "ht", "Curaçao": "cw", "New Zealand": "nz", Wales: "gb-wls",
};

export const flagUrl = (t: string, w: 20 | 40 | 80 | 160 = 40) =>
  ISO[t] ? `https://flagcdn.com/w${w}/${ISO[t]}.png` : null;

export const CONFED_COLOR: Record<string, string> = {
  UEFA: "#3b82f6",
  CONMEBOL: "#22c55e",
  CAF: "#f59e0b",
  AFC: "#ef4444",
  CONCACAF: "#a855f7",
  OFC: "#06b6d4",
};

export const pct = (x: number | null | undefined, d = 1) =>
  x == null ? "—" : `${(x * 100).toFixed(d)}%`;

// Chinese team names
export const CN: Record<string, string> = {
  Argentina: "阿根廷", Spain: "西班牙", Brazil: "巴西", France: "法国", England: "英格兰",
  Portugal: "葡萄牙", Germany: "德国", Netherlands: "荷兰", Belgium: "比利时", Croatia: "克罗地亚",
  Morocco: "摩洛哥", Uruguay: "乌拉圭", Colombia: "哥伦比亚", Japan: "日本", "South Korea": "韩国",
  Mexico: "墨西哥", "United States": "美国", Canada: "加拿大", Switzerland: "瑞士",
  Austria: "奥地利", Turkey: "土耳其", Norway: "挪威", Sweden: "瑞典", Senegal: "塞内加尔",
  "Ivory Coast": "科特迪瓦", Ghana: "加纳", Egypt: "埃及", Algeria: "阿尔及利亚",
  Tunisia: "突尼斯", "Cape Verde": "佛得角", "South Africa": "南非", "DR Congo": "刚果(金)",
  Iran: "伊朗", "Saudi Arabia": "沙特阿拉伯", Qatar: "卡塔尔", Australia: "澳大利亚",
  Uzbekistan: "乌兹别克斯坦", Jordan: "约旦", Iraq: "伊拉克", Paraguay: "巴拉圭",
  Ecuador: "厄瓜多尔", Scotland: "苏格兰", "Czech Republic": "捷克", Panama: "巴拿马",
  "Bosnia and Herzegovina": "波黑", Haiti: "海地", "Curaçao": "库拉索",
  "New Zealand": "新西兰", Wales: "威尔士", Denmark: "丹麦",
};

/** Get Chinese team name, fallback to English. */
export const cn = (t: string) => CN[t] || t;

/** Chinese confederation names */
export const CONFED_CN: Record<string, string> = {
  UEFA: "欧足联", CONMEBOL: "南美足联", CAF: "非洲足联",
  AFC: "亚足联", CONCACAF: "中北美足联", OFC: "大洋洲足联",
};

// ---- Beijing time (UTC+8) helpers ----
function bjParts(iso: string) {
  const b = new Date(new Date(iso).getTime() + 8 * 3600 * 1000);
  const p = (n: number) => String(n).padStart(2, "0");
  return {
    date: `${b.getUTCFullYear()}-${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`,
    md: `${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`,
    hm: `${p(b.getUTCHours())}:${p(b.getUTCMinutes())}`,
  };
}
/** "06-12 03:00" Beijing time. */
export const bjTime = (iso?: string | null) => (iso ? `${bjParts(iso).md} ${bjParts(iso).hm}` : "");
/** "06-12" Beijing month-day. */
export const bjMD = (iso?: string | null) => (iso ? bjParts(iso).md : "");
/** "03:00" Beijing time. */
export const bjHM = (iso?: string | null) => (iso ? bjParts(iso).hm : "");
/** "2026-06-12" Beijing date. */
export const bjDate = (iso?: string | null) => (iso ? bjParts(iso).date : "");
/** Today's date in Beijing, "YYYY-MM-DD". */
export const bjToday = () => {
  const b = new Date(Date.now() + 8 * 3600 * 1000);
  const p = (n: number) => String(n).padStart(2, "0");
  return `${b.getUTCFullYear()}-${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`;
};
/** Whole days from Beijing-today to a YYYY-MM-DD date. */
export const daysUntil = (ymd: string) => {
  const [y, m, d] = ymd.split("-").map(Number);
  const t = bjToday().split("-").map(Number);
  return Math.round((Date.UTC(y, m - 1, d) - Date.UTC(t[0], t[1] - 1, t[2])) / 86400000);
};