diff --git a/tests/unit/entityLayer.test.js b/tests/unit/entityLayer.test.js new file mode 100644 index 0000000..3e22dc4 --- /dev/null +++ b/tests/unit/entityLayer.test.js @@ -0,0 +1,74 @@ +// DS0 (Design v2) — THE ENTITY LAYER. Teams resolve to real logos + real +// colors; the accent rule keeps team color VISIBLE on the near-black terminal. + +const { resolveTeam, teamLogoUrl, accentColor, luminance } = require('../../web/src/lib/teamMeta'); + +describe('resolveTeam — abbr / full name / nickname / alias', () => { + test('canonical statsapi abbr', () => { + expect(resolveTeam('NYY', 'mlb').name).toBe('New York Yankees'); + expect(resolveTeam('MIL', 'mlb').name).toBe('Milwaukee Brewers'); + }); + test('full name and nickname', () => { + expect(resolveTeam('Milwaukee Brewers', 'mlb').abbr).toBe('MIL'); + expect(resolveTeam('Pirates', 'mlb').abbr).toBe('PIT'); + expect(resolveTeam('Red Sox', 'mlb').abbr).toBe('BOS'); + expect(resolveTeam('White Sox', 'mlb').abbr).toBe('CWS'); + }); + test('statsapi↔ESPN abbr aliases (AZ↔ARI, CWS↔CHW)', () => { + expect(resolveTeam('ARI', 'mlb').abbr).toBe('AZ'); + expect(resolveTeam('CHW', 'mlb').abbr).toBe('CWS'); + }); + test('unknown team → null (never a fake)', () => { + expect(resolveTeam('ZZZ', 'mlb')).toBeNull(); + expect(resolveTeam('', 'mlb')).toBeNull(); + }); + test('resolves across leagues', () => { + expect(resolveTeam('LAL', 'nba').name).toBe('Los Angeles Lakers'); + expect(resolveTeam('NY', 'wnba').name).toBe('New York Liberty'); + }); + test('soccer = World Cup national teams, by name + alias', () => { + expect(resolveTeam('USA', 'soccer').name).toBe('United States'); + expect(resolveTeam('United States', 'soccer').abbr).toBe('USA'); + expect(resolveTeam('Brazil', 'soccer').abbr).toBe('BRA'); + expect(resolveTeam('South Korea', 'soccer').abbr).toBe('KOR'); + expect(resolveTeam('Ivory Coast', 'soccer').abbr).toBe('CIV'); // alias + }); +}); + +describe('teamLogoUrl — real ESPN CDN paths', () => { + test('MLB logo url uses the ESPN logo abbr, not the app abbr', () => { + expect(teamLogoUrl('AZ', 'mlb')).toBe('https://a.espncdn.com/i/teamlogos/mlb/500/ari.png'); + expect(teamLogoUrl('CWS', 'mlb')).toBe('https://a.espncdn.com/i/teamlogos/mlb/500/chw.png'); + expect(teamLogoUrl('NYY', 'mlb')).toBe('https://a.espncdn.com/i/teamlogos/mlb/500/nyy.png'); + }); + test('per-league path', () => { + expect(teamLogoUrl('LAL', 'nba')).toContain('/teamlogos/nba/500/lal.png'); + expect(teamLogoUrl('NY', 'wnba')).toContain('/teamlogos/wnba/500/ny.png'); + }); + test('soccer national teams use the flag CDN (countries/ + numeric edge)', () => { + expect(teamLogoUrl('USA', 'soccer')).toBe('https://a.espncdn.com/i/teamlogos/countries/500/usa.png'); + expect(teamLogoUrl('South Korea', 'soccer')).toContain('/countries/500/kors.png'); + expect(teamLogoUrl('Curaçao', 'soccer')).toBe('https://a.espncdn.com/i/teamlogos/soccer/500/11678.png'); + }); + test('unknown → null', () => { + expect(teamLogoUrl('ZZZ', 'mlb')).toBeNull(); + expect(teamLogoUrl('Narnia', 'soccer')).toBeNull(); + }); +}); + +describe('accentColor — VISIBLE on #06060B (the near-black bug guard)', () => { + test('picks the more luminous of primary/secondary', () => { + // White Sox: primary silver (#c4ced4) vs black — must pick silver. + expect(accentColor('CWS', 'mlb').toLowerCase()).toBe('#c4ced4'); + // Pirates: black primary + gold secondary — must pick gold. + expect(accentColor('PIT', 'mlb').toLowerCase()).toBe('#fdb827'); + }); + test('both-dark team never returns an invisible accent', () => { + const acc = accentColor('SF', 'mlb'); // black + orange → orange, visible + expect(luminance(acc)).toBeGreaterThan(40); + }); + test('luminance ranks black low, white high', () => { + expect(luminance('#000000')).toBeLessThan(10); + expect(luminance('#ffffff')).toBeGreaterThan(240); + }); +}); diff --git a/web/src/app/team/[abbr]/TeamHub.tsx b/web/src/app/team/[abbr]/TeamHub.tsx index aad8585..9b247ac 100644 --- a/web/src/app/team/[abbr]/TeamHub.tsx +++ b/web/src/app/team/[abbr]/TeamHub.tsx @@ -5,6 +5,7 @@ import { useRouter } from 'next/navigation'; import SportBadge from '@/components/vyndr/SportBadge'; import GradeBadge from '@/components/vyndr/GradeBadge'; import ArchetypeBadge from '@/components/vyndr/ArchetypeBadge'; +import TeamLogo from '@/components/vyndr/TeamLogo'; import ModelRecord from '@/components/vyndr/ModelRecord'; import { playerHref } from '@/lib/playerHref'; import { useParlay, legKey } from '@/contexts/ParlayContext'; @@ -105,8 +106,9 @@ export default function TeamHub({ abbr, sport }: { abbr: string; sport: string }
← Back to Slate - {/* Header */} + {/* Header — DS0: the team's real crest leads the hub. */}
+

{data.team.name}

{data.team.abbr} diff --git a/web/src/components/StreaksPanel.tsx b/web/src/components/StreaksPanel.tsx index 90307d9..d00a2e2 100644 --- a/web/src/components/StreaksPanel.tsx +++ b/web/src/components/StreaksPanel.tsx @@ -1,7 +1,7 @@ 'use client'; import { useEffect, useState } from 'react'; -import { getHeadshotUrl, PLAYER_SILHOUETTE } from '@/lib/playerHeadshot'; +import PlayerAvatar from '@/components/vyndr/PlayerAvatar'; import { type Tier } from '@/lib/tierGate'; /** @@ -73,14 +73,9 @@ export default function StreaksPanel({ sport, tier = 'free', stat = 'all', limit
{visible.map((s) => (
- {s.player} { (e.target as HTMLImageElement).src = PLAYER_SILHOUETTE; }} - /> + {/* DS0 — real headshot / team-colored monogram (kills the gray + silhouette). The STREAKS row is a P0 billboard. */} +
{s.player}{s.team ? ` · ${s.team}` : ''} @@ -118,9 +113,6 @@ const rowStyle: React.CSSProperties = { padding: '8px 10px', borderRadius: 10, background: 'var(--surface, #12121A)', border: '1px solid var(--border, #2A2A36)', }; -const avatarStyle: React.CSSProperties = { - borderRadius: '50%', objectFit: 'cover', background: '#1A1A24', flex: '0 0 auto', -}; const playerName: React.CSSProperties = { fontSize: 14, fontWeight: 700, color: 'var(--text-primary, #F0F0F4)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', diff --git a/web/src/components/vyndr/BookWordmark.tsx b/web/src/components/vyndr/BookWordmark.tsx new file mode 100644 index 0000000..04628b2 --- /dev/null +++ b/web/src/components/vyndr/BookWordmark.tsx @@ -0,0 +1,35 @@ +import { bookInfo } from '@/lib/books'; + +/** + * BookWordmark (DS0) — a sportsbook renders as its brand: the real name in + * the book's brand color, properly cased (DraftKings, FanDuel), NEVER a bare + * lowercase "draftkings" string (DESIGN-SPEC Part 2). For inline contexts + * where the BookChip tile is too heavy. `best` gives it the signal ring. + */ +export default function BookWordmark({ + book, + best = false, + size = 12, +}: { + book: string; + best?: boolean; + size?: number; +}) { + const b = bookInfo(book); + return ( + + {best && } + {b.name} + + ); +} diff --git a/web/src/components/vyndr/GameCard.tsx b/web/src/components/vyndr/GameCard.tsx index 6f46e2e..c80c053 100644 --- a/web/src/components/vyndr/GameCard.tsx +++ b/web/src/components/vyndr/GameCard.tsx @@ -6,6 +6,8 @@ import SectionHead from '@/components/vyndr/SectionHead'; import GradeBadge from '@/components/vyndr/GradeBadge'; import ArchetypeBadge from '@/components/vyndr/ArchetypeBadge'; import StatStrip, { type StatCell, type StripProp, type StripArchetype } from '@/components/vyndr/StatStrip'; +import TeamLogo from '@/components/vyndr/TeamLogo'; +import { accentColor } from '@/lib/teamMeta'; import { playerHref } from '@/lib/playerHref'; import { isPreferredBook } from '@/lib/books'; import { useParlay, legKey } from '@/contexts/ParlayContext'; @@ -100,19 +102,21 @@ function collapseStrips(strips: PlayerStrip[]) { return { sorted, visible, totalReads, truncated: visible.length < sorted.length }; } -/** Clickable team abbreviation → /team/:abbr (Session 51). Stops propagation so - * it doesn't trigger the card's open-game handler; green underline on hover. */ +/** Clickable team → /team/:abbr (Session 51). DS0: renders the real LOGO + + * abbr (the entity layer), not a bare string. Stops propagation so it + * doesn't trigger the card's open-game handler. */ function TeamLink({ abbr, sport }: { abbr: string; sport: string }) { if (!abbr) return ; return ( e.stopPropagation()} - style={{ color: '#fff', textDecoration: 'none', borderBottom: '1px solid transparent' }} + style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: '#fff', textDecoration: 'none', borderBottom: '1px solid transparent' }} onMouseEnter={(e) => { e.currentTarget.style.color = 'var(--g-a)'; e.currentTarget.style.borderBottomColor = 'var(--g-a)'; }} onMouseLeave={(e) => { e.currentTarget.style.color = '#fff'; e.currentTarget.style.borderBottomColor = 'transparent'; }} title={`${abbr} team hub`} > + {abbr} ); @@ -206,8 +210,11 @@ export default function GameCard({ game: g, onAddParlay, onOpen, preferredBooks }, isLegActive: (p: StripProp) => hasLeg(legKey(toLeg(ps, p))), }); + // DS0 — a team-colored accent edge (home team) so the card reads as THIS + // matchup, not a generic panel. Visible-color rule keeps it off near-black. + const homeAccent = accentColor(g.home.abbr, g.sport) || 'var(--border)'; return ( -
+
{/* HEADER */}
onOpen && onOpen(g.id)} title="Open game detail" style={{ display: 'flex', alignItems: 'center', gap: 11, minWidth: 0, cursor: onOpen ? 'pointer' : 'default' }}> diff --git a/web/src/components/vyndr/PlayerAvatar.tsx b/web/src/components/vyndr/PlayerAvatar.tsx new file mode 100644 index 0000000..e074cc0 --- /dev/null +++ b/web/src/components/vyndr/PlayerAvatar.tsx @@ -0,0 +1,76 @@ +'use client'; + +import { useState } from 'react'; +import { getHeadshotUrl, type HeadshotSport } from '@/lib/playerHeadshot'; +import { accentColor } from '@/lib/teamMeta'; + +/** + * PlayerAvatar (DS0) — real headshot; fallback = TEAM-COLORED MONOGRAM, never + * the gray silhouette (DESIGN-SPEC Part 2). A player always renders with + * identity. When the CDN 404s (roster gaps) we draw the player's initials on + * a disc tinted with their team color — branded, legible, never a gray blob. + */ +function initials(name: string): string { + const parts = String(name || '').trim().split(/\s+/).filter(Boolean); + if (parts.length === 0) return '?'; + if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase(); + return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase(); +} + +export default function PlayerAvatar({ + name, + sport = 'mlb', + playerId, + team, + size = 36, +}: { + name: string; + sport?: HeadshotSport; + playerId?: string | number | null; + team?: string | null; + size?: number; +}) { + const [broken, setBroken] = useState(false); + const url = playerId != null ? getHeadshotUrl({ sport, playerId }) : null; + const accent = (team && accentColor(team, String(sport))) || '#4A9EFF'; + const showImg = url && url !== '/images/player-silhouette.svg' && !broken; + + const Monogram = ( + + {initials(name)} + + ); + + if (!showImg) return Monogram; + + return ( + + {/* eslint-disable-next-line @next/next/no-img-element */} + {name} setBroken(true)} + style={{ width: size, height: size, objectFit: 'cover', objectPosition: 'top center' }} + /> + + ); +} diff --git a/web/src/components/vyndr/StatStrip.tsx b/web/src/components/vyndr/StatStrip.tsx index 9bda785..b0bd6bd 100644 --- a/web/src/components/vyndr/StatStrip.tsx +++ b/web/src/components/vyndr/StatStrip.tsx @@ -1,6 +1,7 @@ import ArchetypeBadge from '@/components/vyndr/ArchetypeBadge'; import GradeBadge from '@/components/vyndr/GradeBadge'; import Sparkline from '@/components/vyndr/Sparkline'; +import PlayerAvatar from '@/components/vyndr/PlayerAvatar'; import { nextRunLabelET } from '@/lib/pipelineSchedule'; // A1 S3 — BOOK IT is a real per-book deep link now (organic until the // affiliate config flips a book on). rel MUST stay BOOK_LINK_REL. @@ -375,6 +376,9 @@ export default function StatStrip({ return (
+ {/* DS0 — player identity block: real headshot / team-colored monogram + (never the gray silhouette). Team drives the accent color. */} + {player} {team} {archetype && } diff --git a/web/src/components/vyndr/TeamLogo.tsx b/web/src/components/vyndr/TeamLogo.tsx new file mode 100644 index 0000000..7fe370f --- /dev/null +++ b/web/src/components/vyndr/TeamLogo.tsx @@ -0,0 +1,64 @@ +'use client'; + +import { useState } from 'react'; +import { teamLogoUrl, accentColor, resolveTeam } from '@/lib/teamMeta'; + +/** + * TeamLogo (DS0) — a team renders as ITSELF: real ESPN-CDN logo, with a + * team-colored monogram fallback (NEVER a gray box or bare abbr). The whole + * entity layer's job is that "MIL @ PIT" stops being flat text. + * + * On a 404 / unknown team we draw the abbr on a disc in the team's accent + * color — still branded, still legible, never a fallback that reads broken. + */ +export default function TeamLogo({ + team, + sport = 'mlb', + size = 28, + title, +}: { + team: string; + sport?: string; + size?: number; + title?: string; +}) { + const [broken, setBroken] = useState(false); + const url = teamLogoUrl(team, sport); + const meta = resolveTeam(team, sport); + const accent = accentColor(team, sport) || 'var(--text-1)'; + const label = meta?.abbr || String(team || '').toUpperCase().slice(0, 3); + + if (!url || broken) { + // Team-colored monogram disc — branded fallback, never gray. + return ( + + {label.slice(0, 3)} + + ); + } + + return ( + // eslint-disable-next-line @next/next/no-img-element + {meta?.name setBroken(true)} + style={{ width: size, height: size, objectFit: 'contain', flexShrink: 0, display: 'block' }} + /> + ); +} diff --git a/web/src/components/vyndr/index.ts b/web/src/components/vyndr/index.ts index 7cab8dd..487276f 100644 --- a/web/src/components/vyndr/index.ts +++ b/web/src/components/vyndr/index.ts @@ -25,6 +25,11 @@ export { default as StatStrip } from './StatStrip'; export type { StatCell, StripProp, StripArchetype } from './StatStrip'; export { default as BookChip } from './BookChip'; +/* DS0 (Design v2) — the Entity Layer: teams/players/books as themselves. */ +export { default as TeamLogo } from './TeamLogo'; +export { default as PlayerAvatar } from './PlayerAvatar'; +export { default as BookWordmark } from './BookWordmark'; + /* S6 (A1 board) — global search + row-grammar micro-marks */ export { default as SearchModal } from './SearchModal'; export { DotStrip, LineSparkline } from './StatStrip'; diff --git a/web/src/lib/teamMeta.js b/web/src/lib/teamMeta.js new file mode 100644 index 0000000..bc230ed --- /dev/null +++ b/web/src/lib/teamMeta.js @@ -0,0 +1,244 @@ +/* ============================================================ + DS0 (Design v2) — THE ENTITY LAYER: team logos + real colors. + + Teams render as THEMSELVES (DESIGN-SPEC Part 2 / Part 0 law #4): real + logo + team-colored accent, never a bare "MIL @ PIT" string. + + Colors + abbrs are stable public facts (like teams.js's names), so a + static registry is honest and zero-latency — no runtime fetch, no paid + dependency. Colors sourced once from ESPN's team API; logos from ESPN's + free CDN (verified 200 image/png). CommonJS so Jest requires it and the + .tsx components import it (allowJs). + + Each entry: { name, espn (logo-CDN abbr), primary, secondary }. Keyed by + the app/statsapi abbr; resolveTeam() also matches full names + nicknames. + The ESPN "primary" color is often near-black (dark-theme invisible), so + accentColor() picks the more VISIBLE of the two against #06060B. + ============================================================ */ + +// abbr → meta. MLB abbrs match statsapi; where ESPN's logo abbr differs +// (AZ→ari, CWS→chw) the `espn` field carries the CDN spelling. +const MLB = { + AZ: { name: 'Arizona Diamondbacks', espn: 'ari', primary: '#aa182c', secondary: '#e3d4ad' }, + ATH: { name: 'Athletics', espn: 'oak', primary: '#003831', secondary: '#efb21e' }, + ATL: { name: 'Atlanta Braves', espn: 'atl', primary: '#ba0c2f', secondary: '#13274f' }, + BAL: { name: 'Baltimore Orioles', espn: 'bal', primary: '#df4601', secondary: '#000000' }, + BOS: { name: 'Boston Red Sox', espn: 'bos', primary: '#bd3039', secondary: '#0d2b56' }, + CHC: { name: 'Chicago Cubs', espn: 'chc', primary: '#0e3386', secondary: '#cc3433' }, + CWS: { name: 'Chicago White Sox', espn: 'chw', primary: '#c4ced4', secondary: '#000000' }, + CIN: { name: 'Cincinnati Reds', espn: 'cin', primary: '#c6011f', secondary: '#000000' }, + CLE: { name: 'Cleveland Guardians', espn: 'cle', primary: '#e31937', secondary: '#002b5c' }, + COL: { name: 'Colorado Rockies', espn: 'col', primary: '#8e6cae', secondary: '#c4ced4' }, + DET: { name: 'Detroit Tigers', espn: 'det', primary: '#0a2240', secondary: '#ff4713' }, + HOU: { name: 'Houston Astros', espn: 'hou', primary: '#eb6e1f', secondary: '#002d62' }, + KC: { name: 'Kansas City Royals', espn: 'kc', primary: '#7ab2dd', secondary: '#004687' }, + LAA: { name: 'Los Angeles Angels', espn: 'laa', primary: '#ba0021', secondary: '#c4ced4' }, + LAD: { name: 'Los Angeles Dodgers', espn: 'lad', primary: '#005a9c', secondary: '#ef3e42' }, + MIA: { name: 'Miami Marlins', espn: 'mia', primary: '#00a3e0', secondary: '#ef3340' }, + MIL: { name: 'Milwaukee Brewers', espn: 'mil', primary: '#ffc72c', secondary: '#13294b' }, + MIN: { name: 'Minnesota Twins', espn: 'min', primary: '#e20e32', secondary: '#002b5c' }, + NYM: { name: 'New York Mets', espn: 'nym', primary: '#ff5910', secondary: '#002d72' }, + NYY: { name: 'New York Yankees', espn: 'nyy', primary: '#c4ced4', secondary: '#132448' }, + PHI: { name: 'Philadelphia Phillies',espn: 'phi', primary: '#e81828', secondary: '#284898' }, + PIT: { name: 'Pittsburgh Pirates', espn: 'pit', primary: '#fdb827', secondary: '#000000' }, + SD: { name: 'San Diego Padres', espn: 'sd', primary: '#ffc425', secondary: '#2f241d' }, + SF: { name: 'San Francisco Giants', espn: 'sf', primary: '#fd5a1e', secondary: '#000000' }, + SEA: { name: 'Seattle Mariners', espn: 'sea', primary: '#0c8f8f', secondary: '#0c2c56' }, + STL: { name: 'St. Louis Cardinals', espn: 'stl', primary: '#be0a14', secondary: '#0a2252' }, + TB: { name: 'Tampa Bay Rays', espn: 'tb', primary: '#8fbce6', secondary: '#092c5c' }, + TEX: { name: 'Texas Rangers', espn: 'tex', primary: '#c0111f', secondary: '#003278' }, + TOR: { name: 'Toronto Blue Jays', espn: 'tor', primary: '#1d78cf', secondary: '#134a8e' }, + WSH: { name: 'Washington Nationals', espn: 'wsh', primary: '#ab0003', secondary: '#11225b' }, +}; + +const NBA = { + ATL: { name: 'Atlanta Hawks', espn: 'atl', primary: '#e03a3e', secondary: '#fdb927' }, + BOS: { name: 'Boston Celtics', espn: 'bos', primary: '#008348', secondary: '#ffffff' }, + BKN: { name: 'Brooklyn Nets', espn: 'bkn', primary: '#c4ced4', secondary: '#000000' }, + CHA: { name: 'Charlotte Hornets', espn: 'cha', primary: '#00a3af', secondary: '#1d1160' }, + CHI: { name: 'Chicago Bulls', espn: 'chi', primary: '#ce1141', secondary: '#000000' }, + CLE: { name: 'Cleveland Cavaliers', espn: 'cle', primary: '#bc945c', secondary: '#860038' }, + DAL: { name: 'Dallas Mavericks', espn: 'dal', primary: '#0064b1', secondary: '#bbc4ca' }, + DEN: { name: 'Denver Nuggets', espn: 'den', primary: '#fec524', secondary: '#0e2240' }, + DET: { name: 'Detroit Pistons', espn: 'det', primary: '#1d428a', secondary: '#c8102e' }, + GSW: { name: 'Golden State Warriors', espn: 'gs', primary: '#fdb927', secondary: '#1d428a' }, + HOU: { name: 'Houston Rockets', espn: 'hou', primary: '#ce1141', secondary: '#c4ced4' }, + IND: { name: 'Indiana Pacers', espn: 'ind', primary: '#ffd520', secondary: '#0c2340' }, + LAC: { name: 'LA Clippers', espn: 'lac', primary: '#c8102e', secondary: '#1d428a' }, + LAL: { name: 'Los Angeles Lakers', espn: 'lal', primary: '#fdb927', secondary: '#552583' }, + MEM: { name: 'Memphis Grizzlies', espn: 'mem', primary: '#5d76a9', secondary: '#12173f' }, + MIA: { name: 'Miami Heat', espn: 'mia', primary: '#98002e', secondary: '#f9a01b' }, + MIL: { name: 'Milwaukee Bucks', espn: 'mil', primary: '#00471b', secondary: '#eee1c6' }, + MIN: { name: 'Minnesota Timberwolves', espn: 'min', primary: '#79bc43', secondary: '#266092' }, + NOP: { name: 'New Orleans Pelicans', espn: 'no', primary: '#b4975a', secondary: '#0a2240' }, + NYK: { name: 'New York Knicks', espn: 'ny', primary: '#f58426', secondary: '#1d428a' }, + OKC: { name: 'Oklahoma City Thunder', espn: 'okc', primary: '#007ac1', secondary: '#ef3b24' }, + ORL: { name: 'Orlando Magic', espn: 'orl', primary: '#0077c0', secondary: '#c4ced4' }, + PHI: { name: 'Philadelphia 76ers', espn: 'phi', primary: '#1d428a', secondary: '#e01234' }, + PHX: { name: 'Phoenix Suns', espn: 'phx', primary: '#e56020', secondary: '#1d1160' }, + POR: { name: 'Portland Trail Blazers', espn: 'por', primary: '#e03a3e', secondary: '#c4ced4' }, + SAC: { name: 'Sacramento Kings', espn: 'sac', primary: '#5a2d81', secondary: '#63727a' }, + SAS: { name: 'San Antonio Spurs', espn: 'sa', primary: '#c4ced4', secondary: '#000000' }, + TOR: { name: 'Toronto Raptors', espn: 'tor', primary: '#ce1141', secondary: '#000000' }, + UTA: { name: 'Utah Jazz', espn: 'utah', primary: '#79a3dc', secondary: '#4e008e' }, + WAS: { name: 'Washington Wizards', espn: 'wsh', primary: '#e31837', secondary: '#002b5c' }, +}; + +const WNBA = { + ATL: { name: 'Atlanta Dream', espn: 'atl', primary: '#e31837', secondary: '#5091cc' }, + CHI: { name: 'Chicago Sky', espn: 'chi', primary: '#5091cd', secondary: '#ffd520' }, + CON: { name: 'Connecticut Sun', espn: 'conn', primary: '#f05023', secondary: '#0a2240' }, + DAL: { name: 'Dallas Wings', espn: 'dal', primary: '#c4d600', secondary: '#002b5c' }, + GSV: { name: 'Golden State Valkyries', espn: 'gs', primary: '#b38fcf', secondary: '#000000' }, + IND: { name: 'Indiana Fever', espn: 'ind', primary: '#e03a3e', secondary: '#002d62' }, + LV: { name: 'Las Vegas Aces', espn: 'lv', primary: '#a7a8aa', secondary: '#000000' }, + LA: { name: 'Los Angeles Sparks', espn: 'la', primary: '#fdb927', secondary: '#552583' }, + MIN: { name: 'Minnesota Lynx', espn: 'min', primary: '#79bc43', secondary: '#266092' }, + NY: { name: 'New York Liberty', espn: 'ny', primary: '#86cebc', secondary: '#000000' }, + PHX: { name: 'Phoenix Mercury', espn: 'phx', primary: '#fa4b0a', secondary: '#3c286e' }, + SEA: { name: 'Seattle Storm', espn: 'sea', primary: '#2c5235', secondary: '#fee11a' }, + WAS: { name: 'Washington Mystics', espn: 'wsh', primary: '#e03a3e', secondary: '#002b5c' }, +}; + +// Soccer = World Cup national teams (fifa.world). Keyed by the ESPN country +// code (uppercased); the flag CDN is a different path (countries/, not +// teamlogos/soccer). Colors + codes from ESPN's fifa.world API. National +// teams ARE their flag — that's the crest. +const SOCCER = { + ALG: { name: 'Algeria', espn: 'alg', primary: '#4f9a44', secondary: '#ffffff' }, + ARG: { name: 'Argentina', espn: 'arg', primary: '#74acdf', secondary: '#173e69' }, + AUS: { name: 'Australia', espn: 'aus', primary: '#ffcd00', secondary: '#00843d' }, + AUT: { name: 'Austria', espn: 'aut', primary: '#d72b2c', secondary: '#000000' }, + BEL: { name: 'Belgium', espn: 'bel', primary: '#e30613', secondary: '#6ecff6' }, + BIH: { name: 'Bosnia-Herzegovina', espn: 'bih', primary: '#112855', secondary: '#ffce00' }, + BRA: { name: 'Brazil', espn: 'bra', primary: '#fee000', secondary: '#193375' }, + CAN: { name: 'Canada', espn: 'can', primary: '#ed2224', secondary: '#ffffff' }, + CPV: { name: 'Cape Verde', espn: 'cpv', primary: '#0537e4', secondary: '#ef3340' }, + COL: { name: 'Colombia', espn: 'col', primary: '#fbd632', secondary: '#21418c' }, + RDC: { name: 'Congo DR', espn: 'rdc', primary: '#418fde', secondary: '#c60000' }, + CRO: { name: 'Croatia', espn: 'cro', primary: '#ff0000', secondary: '#0c2fff' }, + CUW: { name: 'Curaçao', espn: '11678', primary: '#0537e4', secondary: '#ffce00' }, + CZE: { name: 'Czechia', espn: 'cze', primary: '#d7141a', secondary: '#11457e' }, + ECU: { name: 'Ecuador', espn: 'ecu', primary: '#ffdd00', secondary: '#034ea2' }, + EGY: { name: 'Egypt', espn: 'egy', primary: '#d20300', secondary: '#ffffff' }, + ENG: { name: 'England', espn: 'eng', primary: '#ea1f29', secondary: '#ffffff' }, + FRA: { name: 'France', espn: 'fra', primary: '#3a6dd8', secondary: '#ef4135' }, + GER: { name: 'Germany', espn: 'ger', primary: '#ffce00', secondary: '#dd0000' }, + GHA: { name: 'Ghana', espn: 'gha', primary: '#fbd632', secondary: '#006b3f' }, + HAI: { name: 'Haiti', espn: 'hai', primary: '#0033a0', secondary: '#d21034' }, + IRN: { name: 'Iran', espn: 'irn', primary: '#da0000', secondary: '#239f40' }, + IRQ: { name: 'Iraq', espn: 'irq', primary: '#0a4d2e', secondary: '#ce1126' }, + CIV: { name: 'Ivory Coast', espn: 'civ', primary: '#ff8200', secondary: '#009e60' }, + JPN: { name: 'Japan', espn: 'jpn', primary: '#3a6dd8', secondary: '#bc002d' }, + JOR: { name: 'Jordan', espn: 'jor', primary: '#e70000', secondary: '#007a3d' }, + MEX: { name: 'Mexico', espn: 'mex', primary: '#00843d', secondary: '#ce1126' }, + MAR: { name: 'Morocco', espn: 'mar', primary: '#df2027', secondary: '#006233' }, + NED: { name: 'Netherlands', espn: 'ned', primary: '#fb5d00', secondary: '#21468b' }, + NZL: { name: 'New Zealand', espn: 'nzl', primary: '#c8c8c8', secondary: '#000000' }, + NOR: { name: 'Norway', espn: 'nor', primary: '#c8102e', secondary: '#00205b' }, + PAN: { name: 'Panama', espn: 'pan', primary: '#d21034', secondary: '#005293' }, + PAR: { name: 'Paraguay', espn: 'par', primary: '#ea2300', secondary: '#21418c' }, + POR: { name: 'Portugal', espn: 'por', primary: '#da291c', secondary: '#0d6938' }, + QAT: { name: 'Qatar', espn: 'qat', primary: '#8a1538', secondary: '#ffffff' }, + KSA: { name: 'Saudi Arabia', espn: 'ksa', primary: '#0f7a3d', secondary: '#ffffff' }, + SCO: { name: 'Scotland', espn: 'sco', primary: '#4b6cb7', secondary: '#1a2d69' }, + SEN: { name: 'Senegal', espn: 'sen', primary: '#00853f', secondary: '#fdef42' }, + RSA: { name: 'South Africa', espn: 'rsa', primary: '#ffb81c', secondary: '#0c562e' }, + KOR: { name: 'South Korea', espn: 'kors', primary: '#ce2028', secondary: '#003478' }, + ESP: { name: 'Spain', espn: 'esp', primary: '#c60b1e', secondary: '#ffc400' }, + SWE: { name: 'Sweden', espn: 'swe', primary: '#fecb00', secondary: '#006aa7' }, + SUI: { name: 'Switzerland', espn: 'sui', primary: '#ff0000', secondary: '#ffffff' }, + TUN: { name: 'Tunisia', espn: 'tun', primary: '#d20300', secondary: '#ffffff' }, + TUR: { name: 'Türkiye', espn: 'tur', primary: '#ef3340', secondary: '#ffffff' }, + USA: { name: 'United States', espn: 'usa', primary: '#3a6dd8', secondary: '#d42339' }, + URU: { name: 'Uruguay', espn: 'uru', primary: '#55b5e5', secondary: '#00205b' }, + UZB: { name: 'Uzbekistan', espn: 'uzb', primary: '#0072ce', secondary: '#1eb53a' }, +}; + +const LEAGUES = { mlb: MLB, nba: NBA, wnba: WNBA, soccer: SOCCER }; + +// Common naming variants the odds feeds use → the canonical soccer key. +const SOCCER_ALIAS = { + usa: 'USA', unitedstates: 'USA', usmnt: 'USA', + southkorea: 'KOR', koreademocraticpeoplesrepublic: 'KOR', kore : 'KOR', korearepublic: 'KOR', + ivorycoast: 'CIV', cotedivoire: 'CIV', + turkey: 'TUR', turkiye: 'TUR', + czechrepublic: 'CZE', czechia: 'CZE', + bosnia: 'BIH', bosniaandherzegovina: 'BIH', + congodr: 'RDC', drcongo: 'RDC', + curacao: 'CUW', +}; + +// Full-name + nickname → abbr, built once per league for resolveTeam(). +const NAME_INDEX = {}; +for (const [sp, table] of Object.entries(LEAGUES)) { + NAME_INDEX[sp] = {}; + for (const [abbr, meta] of Object.entries(table)) { + const norm = (s) => String(s).toLowerCase().replace(/[^a-z0-9]/g, ''); + NAME_INDEX[sp][norm(meta.name)] = abbr; + // nickname = last word ("Yankees", "Red Sox" → "redsox") + const parts = meta.name.split(/\s+/); + NAME_INDEX[sp][norm(parts[parts.length - 1])] = abbr; + // two-word nickname ("Red Sox", "Blue Jays", "White Sox") + if (parts.length >= 2) NAME_INDEX[sp][norm(parts.slice(-2).join(''))] = abbr; + } +} + +const normKey = (s) => String(s || '').toLowerCase().replace(/[^a-z0-9]/g, ''); + +/** Resolve any team key (abbr, full name, nickname) + sport → meta or null. */ +function resolveTeam(key, sport) { + if (!key) return null; + const sp = String(sport || 'mlb').toLowerCase(); + const table = LEAGUES[sp]; + if (!table) return null; + const up = String(key).toUpperCase().trim(); + if (table[up]) return { abbr: up, sport: sp, ...table[up] }; + // statsapi/ESPN alias fallbacks for the two MLB mismatches + const ALIAS = { ARI: 'AZ', CHW: 'CWS', OAK: 'ATH', SFG: 'SF', TBR: 'TB', WSN: 'WSH', KCR: 'KC', SDP: 'SD', GS: 'GSW', NO: 'NOP', NYK: 'NYK', UTAH: 'UTA', PHO: 'PHX' }; + if (ALIAS[up] && table[ALIAS[up]]) return { abbr: ALIAS[up], sport: sp, ...table[ALIAS[up]] }; + if (sp === 'soccer' && SOCCER_ALIAS[normKey(key)]) { + const a = SOCCER_ALIAS[normKey(key)]; + return { abbr: a, sport: sp, ...table[a] }; + } + const byName = NAME_INDEX[sp][normKey(key)]; + if (byName) return { abbr: byName, sport: sp, ...table[byName] }; + return null; +} + +/** ESPN free logo CDN url for a team, or null when unresolved. Soccer national + * teams use the flag CDN (countries/ for a 3-letter code, soccer/ for the rare + * numeric team id); the ball sports use teamlogos/{league}/. */ +function teamLogoUrl(key, sport) { + const m = resolveTeam(key, sport); + if (!m) return null; + if (m.sport === 'soccer') { + return /^\d+$/.test(m.espn) + ? `https://a.espncdn.com/i/teamlogos/soccer/500/${m.espn}.png` + : `https://a.espncdn.com/i/teamlogos/countries/500/${m.espn}.png`; + } + const path = m.sport === 'wnba' ? 'wnba' : 'nba'; + return `https://a.espncdn.com/i/teamlogos/${m.sport === 'mlb' ? 'mlb' : path}/500/${m.espn}.png`; +} + +// Perceived luminance of a #rrggbb (0–255). Used to keep team accents +// VISIBLE on the near-black terminal bg — a #000000 primary is useless. +function luminance(hex) { + const h = String(hex || '').replace('#', ''); + if (h.length !== 6) return 0; + const r = parseInt(h.slice(0, 2), 16), g = parseInt(h.slice(2, 4), 16), b = parseInt(h.slice(4, 6), 16); + return 0.2126 * r + 0.7152 * g + 0.0722 * b; +} + +/** The team color to use for an accent/monogram against the dark theme: + * the more visible of primary/secondary. Falls back to a neutral when + * both are near-black (never returns an invisible accent). */ +function accentColor(key, sport) { + const m = resolveTeam(key, sport); + if (!m) return null; + const lp = luminance(m.primary), ls = luminance(m.secondary); + const best = lp >= ls ? m.primary : m.secondary; + return luminance(best) < 40 ? '#8A8A9A' : best; // both dark → neutral chrome +} + +module.exports = { resolveTeam, teamLogoUrl, accentColor, luminance, LEAGUES, __tables: { MLB, NBA, WNBA } };