// MFL player card: a standalone React component (no dependency besides React 18+). // Copy it into your project and pass the player object you got from the MFL API // (GET /players/{playerId} → player). It draws an SVG card that scales to `width`. import { useId, useState, type CSSProperties } from 'react' export const MFL_CDN_URL = 'https://d13e14gtps4iwl.cloudfront.net' export interface PlayerCardSource { id: number // Set on some players (e.g. Arena-generated ones): their photo lives at this path instead. imagePath?: string | null metadata: { firstName?: string lastName?: string overall?: number positions?: string[] age?: number pace?: number shooting?: number passing?: number dribbling?: number defense?: number physical?: number goalkeeping?: number } } // Same rule as PlayerImage.tsx: 960x960 transparent WebP, head and torso. export function playerImageUrl(player: Pick): string { return player.imagePath ? `${MFL_CDN_URL}/${player.imagePath}` : `${MFL_CDN_URL}/players/v2/${player.id}/photo.webp` } export type Rarity = 'common' | 'limited' | 'uncommon' | 'rare' | 'legendary' | 'ultimate' interface RarityStyle { minOverall: number color: string background: string text: [color: string, offset: number][] separator: string } // Colours from the MFL app. const RARITIES: Record = { ultimate: { minOverall: 95, color: '#00ffe9', background: '#063330', text: [['#d9ece7', 0], ['#83ebe2', 0.51], ['#7dd1ca', 1]], separator: '#f8ed90' }, legendary: { minOverall: 85, color: '#fa53ff', background: '#2c0d2d', text: [['#d6d6d6', 0], ['#b137f1', 0.51], ['#e63df8', 1]], separator: '#bf36f1' }, rare: { minOverall: 75, color: '#0077ff', background: '#09152f', text: [['#97e5ff', 0], ['#1f8eff', 0.51], ['#3992ff', 1]], separator: '#175bd6' }, uncommon: { minOverall: 65, color: '#71ff30', background: '#243b1e', text: [['#bfffd9', 0], ['#38ff7a', 0.51], ['#37cb6f', 1]], separator: '#2eea81' }, limited: { minOverall: 55, color: '#ecd17f', background: '#3a3526', text: [['#d6d6d6', 0], ['#ecd27e', 0.51], ['#cdb66b', 1]], separator: '#fdefa2' }, common: { minOverall: 0, color: '#bebebe', background: '#363636', text: [['#ffffff', 0], ['#cecece', 1]], separator: '#ffffff' }, } // common 0–54, limited 55–64, uncommon 65–74, rare 75–84, legendary 85–94, ultimate 95–100. export function playerRarity(overall: number): Rarity { return (Object.keys(RARITIES) as Rarity[]).find((r) => overall >= RARITIES[r].minOverall) ?? 'common' } const STATS = [ ['pace', 'PAC'], ['shooting', 'SHO'], ['passing', 'PAS'], ['dribbling', 'DRI'], ['defense', 'DEF'], ['physical', 'PHY'], ] as const const VIEW_W = 128.27 const VIEW_H = 214.118 const NAME_MAX_WIDTH = 112 // Average width of an uppercase glyph, in em: names are shrunk, then cut with "…", to fit the card. const GLYPH_EM = 0.66 function fitName(text: string, size: number): { text: string; size: number } { const fits = (s: number) => text.length * GLYPH_EM * s <= NAME_MAX_WIDTH if (fits(size)) return { text, size } const min = size * 0.72 if (fits(min)) return { text, size: NAME_MAX_WIDTH / (text.length * GLYPH_EM) } const max = Math.floor(NAME_MAX_WIDTH / (GLYPH_EM * min)) - 1 return { text: `${text.slice(0, max).trimEnd()}…`, size: min } } export interface PlayerCardProps { player: PlayerCardSource // Width in px; the height follows (ratio ≈ 0.6). width?: number // Draw the official MFL card frame from the CDN. false: a plain shape in the rarity colours. frame?: boolean fontFamily?: string className?: string style?: CSSProperties } export function PlayerCard({ player, width = 200, frame = true, fontFamily = 'Oxanium, system-ui, sans-serif', className, style }: PlayerCardProps) { const uid = useId().replace(/[^a-zA-Z0-9_-]/g, '') const ref = (name: string) => `mfl-card-${uid}-${name}` const [photoFailed, setPhotoFailed] = useState(false) const [frameFailed, setFrameFailed] = useState(false) const m = player.metadata const overall = m.overall ?? 0 const rarity = playerRarity(overall) const rs = RARITIES[rarity] const position = m.positions?.[0] ?? '' const isGk = position === 'GK' const fullName = [m.firstName, m.lastName].filter(Boolean).join(' ') || `Player ${player.id}` const first = fitName((m.firstName ?? '').toUpperCase(), 12.3) const last = fitName((m.lastName ?? '').toUpperCase(), 14.2) const initials = ((m.firstName?.[0] ?? '') + (m.lastName?.[0] ?? '')).toUpperCase() || '?' const stat = (v?: number) => (v === undefined ? '–' : String(v)) const label = `${fullName}, ${m.overall ?? 'unknown'} overall${position ? `, ${position}` : ''}` return ( {label} {rs.text.map(([c, o]) => )} ) } export default PlayerCard