- relay 응답에서 버리던 textRelays 를 타석 단위 그룹으로 서빙 (한국어 원문 그대로) - 기본 응답은 현재 이닝만 담아, 지난 이닝은 ?inning=N 병렬 조회 후 캐시 (현재·직전 이닝은 매번 재조회 — 이닝 전환 직후 마지막 타석 누락 방지) - 득점 하이라이트는 텍스트 패턴이 아니라 currentGameState 점수 합 변화로 판정 (홈런·적시타뿐 아니라 홈인·폭투/실책 득점·밀어내기까지 정확) - 네이버 type 코드는 kind(pitch/result/run/sub/note)로 변환해 UI 에 노출하지 않음 - 진행 중엔 최근 40타석, 종료 후엔 전 경기 중계(다시보기) - 프론트: 투구 접기/펼치기 토글 · 이닝 구분선 · 득점 강조 MLB 는 소스(feed/live allPlays)가 영문이라 별도 작업 — 이번 커밋은 KBO 만. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
384 lines
14 KiB
TypeScript
384 lines
14 KiB
TypeScript
import { useEffect, useState } from "react";
|
||
import type { Lang } from "@/lib/i18n";
|
||
import { timeOnly } from "@/lib/format";
|
||
import { getLive, type LiveData, type RelayEvent, type RelayGroup } from "@/lib/api";
|
||
import type { Match } from "@/lib/types";
|
||
|
||
// 라이브 필드 뷰 — 수비 배치 다이아몬드 + 현재 타자/투수 + B/S/O + 주자 + 대기타석.
|
||
// phase === "live" 일 때 20초 폴링. (?debugLive=1 로 종료 경기에서도 강제 표시)
|
||
|
||
// 포지션 → 필드 위 좌표(%) — left/top (SVG viewBox 100×70 과 동일 비율)
|
||
const POS_XY: Record<string, [number, number]> = {
|
||
포수: [50, 90],
|
||
"1루수": [71, 55],
|
||
"2루수": [61, 39],
|
||
유격수: [39, 39],
|
||
"3루수": [29, 55],
|
||
좌익수: [20, 26],
|
||
중견수: [50, 14],
|
||
우익수: [80, 26],
|
||
};
|
||
|
||
// 필드 SVG 상 베이스 좌표 (viewBox 0 0 100 70) — 1루·2루·3루
|
||
const BASE_XY: [number, number][] = [
|
||
[63.5, 42.5],
|
||
[50, 29],
|
||
[36.5, 42.5],
|
||
];
|
||
|
||
function NameChip({
|
||
name,
|
||
sub,
|
||
accent = false,
|
||
x,
|
||
y,
|
||
}: {
|
||
name: string;
|
||
sub?: string;
|
||
accent?: boolean;
|
||
x: number;
|
||
y: number;
|
||
}) {
|
||
return (
|
||
<div
|
||
className="absolute -translate-x-1/2 -translate-y-1/2 text-center"
|
||
style={{ left: `${x}%`, top: `${y}%` }}
|
||
>
|
||
<div
|
||
className={`whitespace-nowrap rounded-full px-2.5 py-1 text-[11px] font-bold shadow-[0_1px_3px_rgba(0,0,0,0.25)] ${
|
||
accent ? "bg-[#d94141] text-white" : "bg-white text-[#222]"
|
||
}`}
|
||
>
|
||
{name}
|
||
</div>
|
||
{sub && (
|
||
<div className="mt-0.5 text-[9px] font-bold text-[#245c2a]/80">{sub}</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Dot({ on, color }: { on: boolean; color: string }) {
|
||
return (
|
||
<span
|
||
className="inline-block h-2.5 w-2.5 rounded-full"
|
||
style={{ background: on ? color : "rgba(255,255,255,0.14)" }}
|
||
/>
|
||
);
|
||
}
|
||
|
||
// 구장 SVG — 부채꼴 외야 + 파울라인 + 흙 내야 + 마운드 + 베이스(주자 점등).
|
||
// 경기 전(빈 구장)·경기 중·종료 스냅샷 모두 공용.
|
||
function BallparkSvg({ bases }: { bases?: boolean[] }) {
|
||
return (
|
||
<svg
|
||
viewBox="0 0 100 70"
|
||
preserveAspectRatio="none"
|
||
className="absolute inset-0 h-full w-full"
|
||
>
|
||
<rect width="100" height="70" fill="#4e8a48" />
|
||
<path
|
||
d="M50,62 L8,20 A60,60 0 0 1 92,20 Z"
|
||
fill="#6cb163"
|
||
stroke="rgba(255,255,255,0.85)"
|
||
strokeWidth="0.5"
|
||
/>
|
||
<polygon points="50,20 73,43 50,66 27,43" fill="#cd9f63" />
|
||
<polygon
|
||
points="50,29 63.5,42.5 50,56 36.5,42.5"
|
||
fill="#6cb163"
|
||
stroke="rgba(255,255,255,0.4)"
|
||
strokeWidth="0.4"
|
||
/>
|
||
<circle cx="50" cy="43" r="3.2" fill="#cd9f63" />
|
||
<circle cx="50" cy="60.5" r="4.2" fill="#cd9f63" />
|
||
{BASE_XY.map(([cx, cy], i) => (
|
||
<rect
|
||
key={i}
|
||
x={cx - 1.5}
|
||
y={cy - 1.5}
|
||
width="3"
|
||
height="3"
|
||
rx="0.5"
|
||
transform={`rotate(45 ${cx} ${cy})`}
|
||
fill={bases?.[i] ? "#ffd23e" : "#ffffff"}
|
||
stroke="rgba(0,0,0,0.15)"
|
||
strokeWidth="0.3"
|
||
/>
|
||
))}
|
||
<rect x="48.8" y="59.3" width="2.4" height="2.4" rx="0.4" fill="#ffffff" />
|
||
</svg>
|
||
);
|
||
}
|
||
|
||
// 이벤트 종류별 텍스트 스타일 — 투구는 흐리게, 타석 결과는 강조.
|
||
const RELAY_STYLE: Record<RelayEvent["kind"], string> = {
|
||
pitch: "text-white/35",
|
||
result: "font-extrabold text-white/90",
|
||
run: "text-[#8FD3FF]",
|
||
sub: "text-white/45",
|
||
note: "text-white/40",
|
||
inning: "text-white/60",
|
||
batter: "text-white/60",
|
||
end: "font-bold text-white/70",
|
||
};
|
||
|
||
// 문자중계 — 타석 단위 그룹(최신순). events 가 비면 이닝 구분 헤더.
|
||
// 원문이 한국어(네이버)라 영문 모드에서도 그대로 노출한다.
|
||
function RelayFeed({ groups, lang }: { groups: RelayGroup[]; lang: Lang }) {
|
||
const [showPitches, setShowPitches] = useState(true);
|
||
|
||
return (
|
||
<div className="mt-3 border-t border-white/10 pt-2.5">
|
||
<div className="mb-1.5 flex items-baseline justify-between">
|
||
<span className="text-[10px] font-bold text-white/35">
|
||
{lang === "en" ? "PLAY-BY-PLAY" : "문자중계"}
|
||
</span>
|
||
<div className="flex items-baseline gap-2">
|
||
<button
|
||
onClick={() => setShowPitches((v) => !v)}
|
||
className="text-[10px] font-bold text-white/45 transition active:scale-95"
|
||
>
|
||
{showPitches
|
||
? lang === "en" ? "Hide pitches" : "투구 접기"
|
||
: lang === "en" ? "Show pitches" : "투구 펼치기"}
|
||
</button>
|
||
<span className="text-[9px] text-white/25">NAVER</span>
|
||
</div>
|
||
</div>
|
||
|
||
<ul className="max-h-[260px] space-y-2 overflow-y-auto pr-1 [scrollbar-width:thin] [scrollbar-color:rgba(255,255,255,.22)_transparent] [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-track]:bg-transparent [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-white/20">
|
||
{groups.map((g) => {
|
||
const events = showPitches ? g.events : g.events.filter((e) => e.kind !== "pitch");
|
||
// 이닝 구분 헤더 (내용 없는 그룹)
|
||
if (g.events.length === 0) {
|
||
return (
|
||
<li key={g.no} className="flex items-center gap-2 pt-1">
|
||
<span className="h-px flex-1 bg-white/10" />
|
||
<span className="text-[10px] font-extrabold text-white/40">{g.title}</span>
|
||
<span className="h-px flex-1 bg-white/10" />
|
||
</li>
|
||
);
|
||
}
|
||
if (events.length === 0) return null; // 투구만 있던 타석 — 접기 모드에선 숨김
|
||
return (
|
||
<li key={g.no}>
|
||
{g.title && (
|
||
<div className="flex items-baseline gap-1.5">
|
||
{g.inn != null && (
|
||
<span className="shrink-0 font-mono text-[10px] font-bold text-white/30">
|
||
{g.inn}
|
||
{g.half === "B" ? "말" : "초"}
|
||
</span>
|
||
)}
|
||
<span className="text-[11px] font-extrabold text-white/75">{g.title}</span>
|
||
</div>
|
||
)}
|
||
<ul className="mt-0.5 space-y-px pl-1.5 text-[11px] leading-snug">
|
||
{events.map((e, i) => (
|
||
<li
|
||
key={i}
|
||
className={
|
||
e.score
|
||
? "rounded-lg border border-[#94FBE0]/30 bg-[#94FBE0]/10 px-1.5 py-1 font-extrabold text-[#94FBE0]"
|
||
: RELAY_STYLE[e.kind]
|
||
}
|
||
>
|
||
{e.text}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</li>
|
||
);
|
||
})}
|
||
</ul>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function LiveField({ match, lang = "ko" }: { match: Match; lang?: Lang }) {
|
||
const isLive = match.phase === "live";
|
||
// 경기 중엔 20초 폴링, 그 외(경기 전·종료)는 1회 로드.
|
||
// 경기 전엔 중계 미게시(available=false)라도 빈 구장 + "경기 전"으로 표시.
|
||
const [data, setData] = useState<LiveData | null>(null);
|
||
|
||
const isBaseball = match.league === "kbo" || match.league === "mlb";
|
||
|
||
useEffect(() => {
|
||
if (!isBaseball) return;
|
||
let alive = true;
|
||
const load = () =>
|
||
getLive(match.matchId)
|
||
.then((d) => alive && setData(d))
|
||
.catch(() => {});
|
||
load();
|
||
if (!isLive) return () => { alive = false; };
|
||
const id = setInterval(load, 20_000);
|
||
return () => {
|
||
alive = false;
|
||
clearInterval(id);
|
||
};
|
||
}, [isBaseball, isLive, match.matchId]);
|
||
|
||
// 야구 전용 위젯 — 축구(월드컵)에서는 렌더하지 않음
|
||
if (!isBaseball) return null;
|
||
|
||
// 중계 데이터 없음 (경기 전 또는 시작 직후) — 빈 구장 + 상태 라벨
|
||
if (!data?.available) {
|
||
if (match.phase === "finished") return null; // 종료인데 기록 없음 — 표시할 것 없음
|
||
return (
|
||
<section className="mt-4">
|
||
<div className="rounded-3xl border border-white/10 bg-[#171b21] p-4">
|
||
<div className="flex items-center justify-between">
|
||
<span className="text-[13px] font-extrabold text-white/45">
|
||
{lang === "en" ? "FIELD" : "필드"}
|
||
</span>
|
||
<span className="font-mono text-[13px] font-bold text-white/55">
|
||
{timeOnly(match.kickoffKst)} KST
|
||
</span>
|
||
</div>
|
||
<div className="relative mt-3 aspect-[10/7] overflow-hidden rounded-2xl">
|
||
<BallparkSvg />
|
||
<div className="absolute inset-0 grid place-items-center">
|
||
<span className="rounded-full bg-black/50 px-4 py-1.5 text-[13px] font-extrabold text-white/95">
|
||
{isLive
|
||
? lang === "en"
|
||
? "Starting soon…"
|
||
: "경기 시작 대기 중"
|
||
: lang === "en"
|
||
? "Pre-game · lineups TBA"
|
||
: "경기 전 · 라인업 발표 전"}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
);
|
||
}
|
||
|
||
const half = data.half === "B" ? (lang === "en" ? "Bot" : "말") : lang === "en" ? "Top" : "초";
|
||
const battingTeam = data.half === "B" ? match.teamB : match.teamA;
|
||
const num = (v: unknown) => Number(v ?? 0) || 0;
|
||
|
||
return (
|
||
<section className="mt-4">
|
||
<div className="rounded-3xl border border-white/10 bg-[#171b21] p-4">
|
||
{/* 헤더: 이닝·스코어·B/S/O */}
|
||
<div className="flex items-center justify-between">
|
||
<div className="flex items-center gap-2">
|
||
{isLive ? (
|
||
<>
|
||
<span className="flex h-2 w-2">
|
||
<span className="absolute inline-flex h-2 w-2 animate-ping rounded-full bg-[#FF5B5B] opacity-60" />
|
||
<span className="relative inline-flex h-2 w-2 rounded-full bg-[#FF5B5B]" />
|
||
</span>
|
||
<span className="text-[13px] font-extrabold text-[#FF5B5B]">
|
||
LIVE · {data.inn}
|
||
{lang === "en" ? ` (${half})` : `회${half}`}
|
||
</span>
|
||
</>
|
||
) : (
|
||
<span className="text-[13px] font-extrabold text-white/45">
|
||
{lang === "en" ? "FINAL" : "경기 종료"} · {data.inn}
|
||
{lang === "en" ? ` (${half})` : `회${half}`}
|
||
</span>
|
||
)}
|
||
</div>
|
||
<span className="font-mono text-[18px] font-extrabold tabular-nums">
|
||
{match.teamA.shortName} {num(data.score?.away)}
|
||
<span className="px-1 text-white/40">:</span>
|
||
{num(data.score?.home)} {match.teamB.shortName}
|
||
</span>
|
||
</div>
|
||
|
||
{/* B/S/O + 주자 다이아몬드 */}
|
||
<div className="mt-3 flex items-center justify-between rounded-2xl bg-white/[0.04] px-3 py-2">
|
||
<div className="space-y-1 text-[10px] font-bold text-white/50">
|
||
<div className="flex items-center gap-1.5">
|
||
B{" "}
|
||
{[0, 1, 2].map((i) => (
|
||
<Dot key={i} on={num(data.bso?.b) > i} color="#4ade80" />
|
||
))}
|
||
</div>
|
||
<div className="flex items-center gap-1.5">
|
||
S{" "}
|
||
{[0, 1].map((i) => (
|
||
<Dot key={i} on={num(data.bso?.s) > i} color="#facc15" />
|
||
))}
|
||
</div>
|
||
<div className="flex items-center gap-1.5">
|
||
O{" "}
|
||
{[0, 1].map((i) => (
|
||
<Dot key={i} on={num(data.bso?.o) > i} color="#f87171" />
|
||
))}
|
||
</div>
|
||
</div>
|
||
|
||
<div className="text-right text-[11px] font-semibold text-white/55">
|
||
<div>
|
||
{lang === "en" ? "AB" : "타석"}{" "}
|
||
<span className="font-extrabold text-white/90">{battingTeam.shortName}</span>
|
||
</div>
|
||
{data.vsRecord && <div className="mt-0.5 text-[10px] text-white/35">{data.vsRecord}</div>}
|
||
</div>
|
||
</div>
|
||
|
||
{/* 필드: 구장 SVG + 수비 배치/투수/타자 칩 */}
|
||
<div className="relative mt-3 aspect-[10/7] overflow-hidden rounded-2xl">
|
||
<BallparkSvg bases={data.bases} />
|
||
{data.defense?.map((f) => {
|
||
const xy = POS_XY[f.pos ?? ""];
|
||
if (!xy) return null;
|
||
return <NameChip key={f.pos} name={f.name} sub={f.pos} x={xy[0]} y={xy[1]} />;
|
||
})}
|
||
{data.pitcher && (
|
||
<NameChip
|
||
name={data.pitcher.name}
|
||
sub={`${lang === "en" ? "P" : "투구수"} ${num(data.pitcher.ballCount)}`}
|
||
x={50}
|
||
y={61}
|
||
/>
|
||
)}
|
||
{data.batter && (
|
||
<NameChip
|
||
name={`${data.batter.order ?? ""}. ${data.batter.name}`}
|
||
accent
|
||
x={50}
|
||
y={79}
|
||
/>
|
||
)}
|
||
</div>
|
||
|
||
{/* 대기타석 */}
|
||
{data.batter && data.offenseLineup && (
|
||
<div className="mt-3 flex items-center gap-2 overflow-x-auto pb-1 text-[11px] font-semibold text-white/55 [scrollbar-width:thin] [scrollbar-color:rgba(255,255,255,.22)_transparent] [&::-webkit-scrollbar]:h-1.5 [&::-webkit-scrollbar-track]:bg-transparent [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-white/20">
|
||
<span className="shrink-0 text-white/35">{lang === "en" ? "Next" : "대기타석"}</span>
|
||
{data.offenseLineup
|
||
.filter((b) => {
|
||
const cur = data.batter!.order ?? 0;
|
||
const diff = ((b.order ?? 0) - cur + 9) % 9;
|
||
return diff >= 1 && diff <= 3;
|
||
})
|
||
.sort((a, b) => {
|
||
const cur = data.batter!.order ?? 0;
|
||
return (((a.order ?? 0) - cur + 9) % 9) - (((b.order ?? 0) - cur + 9) % 9);
|
||
})
|
||
.map((b) => (
|
||
<span
|
||
key={b.order}
|
||
className="shrink-0 rounded-full border border-white/10 px-2 py-0.5"
|
||
>
|
||
{b.order}. {b.name}
|
||
{b.sub ? " ↺" : ""}
|
||
</span>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{/* 문자중계 (KBO 만 — MLB 는 소스 미연동) */}
|
||
{(data.relay?.length ?? 0) > 0 && <RelayFeed groups={data.relay!} lang={lang} />}
|
||
</div>
|
||
</section>
|
||
);
|
||
}
|