"use client"; import { motion } from "framer-motion"; import { ChevronLeft, Coins, Lock, Trophy } from "lucide-react"; import { ScreenHeader, ScreenShell } from "@/components/online/ScreenHeader"; import { CoinsPill } from "@/components/online/CoinsPill"; import { MATCH_LEAGUES } from "@/lib/online/gamification"; import { useOnlineStore } from "@/lib/online-store"; import { useSessionStore } from "@/lib/session-store"; import { useUIStore } from "@/lib/ui-store"; import { useGameStore, hasActiveMatch } from "@/lib/game-store"; import { pushNotification } from "@/lib/notification-store"; import { useI18n } from "@/lib/i18n"; import { cn } from "@/lib/cn"; /** Block starting a 2nd game while one is running — resume it instead. */ function guardActiveMatch(): boolean { if (!hasActiveMatch()) return false; useGameStore.getState().resume(); useUIStore.getState().goGame("online"); pushNotification({ kind: "system", titleFa: "بازی در جریان", titleEn: "Game in progress", bodyFa: "ابتدا بازی فعلی را تمام کنید یا تسلیم شوید.", bodyEn: "Finish or forfeit your current game first.", icon: "🎮", }); return true; } export function OnlineLobbyScreen() { const { t, locale } = useI18n(); const startMatchmaking = useOnlineStore((s) => s.startMatchmaking); const go = useUIStore((s) => s.go); const profile = useSessionStore((s) => s.profile); const coins = profile?.coins ?? 0; const level = profile?.level ?? 1; // The cheapest league you can enter is highlighted as the default pick. const featuredId = MATCH_LEAGUES.find((l) => level >= l.minLevel)?.id; // Each league is its own play button — tap to queue a ranked match at its stake. const playLeague = async (l: (typeof MATCH_LEAGUES)[number]) => { if (guardActiveMatch()) return; if (level < l.minLevel) return; if (coins < l.entry) { go("buycoins"); return; } // Navigate FIRST so the button never hangs on the SignalR handshake; the // matchmaking screen shows "searching" while the connection establishes. go("matchmaking"); void startMatchmaking({ ranked: true, stake: l.entry }); }; return ( } />
{t("lobby.chooseLeague")}
{MATCH_LEAGUES.map((l) => { const locked = level < l.minLevel; const gold = !locked && l.id === featuredId; const poor = !locked && coins < l.entry; return ( playLeague(l)} className={cn( "w-full rounded-3xl p-4 flex items-center gap-3 text-start transition", gold ? "btn-gold" : "press-3d panel hover:border-gold-500/40", locked && "opacity-50 cursor-not-allowed" )} > {l.icon} {locale === "fa" ? l.nameFa : l.nameEn} {locale === "fa" ? l.descFa : l.descEn} {poor && ( {t("lobby.needCoins")} )} {locked ? ( {t("lobby.lvl")} {l.minLevel} ) : ( {l.entry.toLocaleString()} )} ); })}
); }