Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 1x 1x 1x 1x 1x 1x 1x 1x 163x 163x 163x 34x 1x 1x 52x 52x 52x 52x 52x 1x 43x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 1x 1x 14x 14x 14x 14x 8x 8x 22x 22x 22x 22x 22x 22x 22x 22x 22x 8x 8x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 46x 46x 46x 17x 17x 15x 17x 27x 27x 15x 15x 15x 15x 15x 17x 17x 2x 2x 15x 15x 15x 57x 57x 57x 57x 57x 57x 57x 30x 30x 27x 27x 27x 57x 27x 3x 3x 3x 3x 27x 57x 57x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 15x 15x 1x 1x 11x 11x 25x 25x 9x 11x 11x 11x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 8x 11x 2x 2x 6x 9x 6x 11x 4x 4x 4x 4x 4x 4x 2x 2x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x | /**
* The Team's Path share card (SB-1918): one team's road through a knockout
* tournament — its group, then each knockout round to the final. Built from
* the team's own matches only, because for most tournaments (the GA Cup) MT
* holds the matches of the teams it follows, not the whole bracket.
* Pure functions; the share modal fetches, the card draws.
*/
import { festKickoff } from './festCard';
import { fieldOf, opponentInitials, teamResult } from './festSchedule';
import { BRACKET_ROUND_ORDER, roundLabel } from './tournamentRounds';
const sideOf = (m, teamId) =>
m?.home_team?.id === teamId
? 'home'
: m?.away_team?.id === teamId
? 'away'
: null;
const kickoffTime = m => {
const t = Date.parse(m?.scheduled_kickoff || '');
if (!Number.isNaN(t)) return t;
const d = Date.parse(`${m?.match_date || ''}T12:00:00Z`);
return Number.isNaN(d) ? Infinity : d;
};
const isGroupGame = m =>
m?.tournament_round === 'group_stage' ||
(!m?.tournament_round && !!m?.tournament_group);
/** Short round names for the ladder's left column. */
const RUNG_LABELS = {
round_of_32: 'R32',
round_of_16: 'R16',
quarterfinal: 'QF',
semifinal: 'SF',
third_place: '3RD',
final: 'FINAL',
};
/** How far a team got, for the banner. */
const REACHED = {
round_of_32: 'ROUND OF 32',
round_of_16: 'ROUND OF 16',
quarterfinal: 'QUARTERFINALS',
semifinal: 'SEMIFINALS',
final: 'FINAL',
};
const opponentOf = (m, teamId) => {
const home = sideOf(m, teamId) === 'home';
const team = home ? m.away_team : m.home_team;
const club = home ? m.away_team_club : m.home_team_club;
return {
name: team?.name || 'TBD',
logoUrl: club?.logo_url || null,
initials: opponentInitials(team?.name),
vs: home ? 'VS' : '@',
};
};
/** The group block: its name, the W-D-L record, each game. Null if none. */
export function groupBlock(matches, teamId) {
const games = (matches || [])
.filter(m => sideOf(m, teamId) && isGroupGame(m))
.sort((a, b) => kickoffTime(a) - kickoffTime(b));
if (!games.length) return null;
const record = { w: 0, d: 0, l: 0 };
const rows = games.map(m => {
const result = teamResult(m, teamId);
if (result && !result.live) record[result.letter.toLowerCase()] += 1;
const opp = opponentOf(m, teamId);
return {
id: m.id,
vs: opp.vs,
opponent: opp,
result: result && !result.live ? result : null,
};
});
return {
name: games.find(m => m.tournament_group)?.tournament_group || 'Group',
record: `${record.w}-${record.d}-${record.l}`,
games: rows,
};
}
/**
* One rung per bracket round, from the team's first knockout round to the
* final. A third-place match, when the team played one, sits before the
* final. Each rung: `state` is 'won' | 'lost' | 'live' | 'next' |
* 'upcoming' | 'future' (not reached yet, still possible) | 'dead' (after
* the team went out).
*/
export function ladder(matches, teamId, tournamentLocation = null) {
const ko = (matches || []).filter(
m =>
sideOf(m, teamId) &&
(BRACKET_ROUND_ORDER.includes(m.tournament_round) ||
m.tournament_round === 'third_place')
);
if (!ko.length) return [];
const byRound = new Map();
for (const m of ko.sort((a, b) => kickoffTime(a) - kickoffTime(b))) {
if (!byRound.has(m.tournament_round)) byRound.set(m.tournament_round, m);
}
const first = Math.min(
...ko
.map(m => BRACKET_ROUND_ORDER.indexOf(m.tournament_round))
.filter(i => i >= 0)
);
const keys = BRACKET_ROUND_ORDER.slice(Number.isFinite(first) ? first : 0);
if (byRound.has('third_place')) {
keys.splice(keys.indexOf('final'), 0, 'third_place');
}
let out = false;
let nextMarked = false;
return keys.map(key => {
const m = byRound.get(key);
const base = {
key,
label: RUNG_LABELS[key],
longLabel: roundLabel(key),
};
if (!m) {
return { ...base, state: out ? 'dead' : 'future', opponent: null };
}
const k = festKickoff({ ...m, tournament_location: tournamentLocation });
const result = teamResult(m, teamId);
let state;
if (result?.live) state = 'live';
else if (result) state = result.letter === 'L' ? 'lost' : 'won';
else if (!nextMarked) {
state = 'next';
nextMarked = true;
} else state = 'upcoming';
// A third-place game is played after going out; the final after it is dead.
if (state === 'lost' && key !== 'third_place') out = true;
if (key === 'third_place') out = true;
return {
...base,
state,
matchId: m.id,
opponent: opponentOf(m, teamId),
result,
day: k.day,
date: k.date,
time: k.time,
field: fieldOf(m.location),
};
});
}
/** The knockout bracket's name ("Championship Playoff"), if it has one. */
export function bracketName(matches, teamId) {
const m = (matches || []).find(
x =>
sideOf(x, teamId) &&
BRACKET_ROUND_ORDER.includes(x.tournament_round) &&
x.tournament_group
);
return m?.tournament_group || null;
}
/**
* Where the run stands: { outcome, banner }. outcome is 'champion' |
* 'runner_up' | 'third' | 'out' | 'alive' | 'group'.
*/
export function pathOutcome(rungs, group) {
const played = rungs.filter(r => r.state === 'won' || r.state === 'lost');
const final = rungs.find(r => r.key === 'final');
const third = rungs.find(r => r.key === 'third_place');
if (final?.state === 'won')
return { outcome: 'champion', banner: 'CHAMPIONS' };
if (final?.state === 'lost')
return { outcome: 'runner_up', banner: 'RUNNERS-UP' };
if (third?.state === 'won')
return { outcome: 'third', banner: 'THIRD PLACE' };
const lost = played.find(r => r.state === 'lost' && r.key !== 'third_place');
if (lost) {
return { outcome: 'out', banner: `OUT IN THE ${REACHED[lost.key]}` };
}
const ahead = rungs.find(r =>
['next', 'live', 'upcoming', 'future'].includes(r.state)
);
if (ahead && rungs.length) {
const reached = REACHED[ahead.key] || roundLabel(ahead.key)?.toUpperCase();
return {
outcome: 'alive',
banner: ahead.key === 'final' ? 'INTO THE FINAL' : `INTO THE ${reached}`,
};
}
return {
outcome: 'group',
banner: group ? `GROUP STAGE · ${group.record}` : 'GROUP STAGE',
};
}
const EYEBROWS = {
champion: 'CHAMPIONS',
runner_up: 'RUNNERS-UP',
third: 'THIRD PLACE',
out: 'KNOCKED OUT',
alive: 'STILL ALIVE',
group: 'GROUP STAGE',
};
/** Everything the card draws, from the team's matches in the tournament. */
export function teamPath(matches, teamId, tournamentLocation = null) {
const group = groupBlock(matches, teamId);
const rungs = ladder(matches, teamId, tournamentLocation);
const { outcome, banner } = pathOutcome(rungs, group);
return {
group,
rungs,
bracket: bracketName(matches, teamId),
outcome,
banner,
eyebrow: EYEBROWS[outcome],
};
}
|