1
0
Fork 0
polaris-task-force/src/components/frontend/minigames/bomperator/BomperatorLobby.tsx
Z8MB1E bfb5cea3c1 feat(minigames): add Ordnance Disposal (bomperator) multiplayer drill
Add a 2-8 player free-for-all Bomberman-style charge-clearing drill at
/minigames/bomperator, category multiplayer. Pure engine plus a single-instance
in-memory match registry, mirroring the call-for-fire-versus split but upgraded
from turn-based to a fixed ~10 Hz tick loop.

Engine (src/lib/minigames/bomperator): seeded RNG via state.rngState/gridSeed;
grid is 15x13 with border, even/even pillars and seeded crates; classic pickups
bomb/range/speed/kick. Movement is grid-locked with sub-cell interpolation,
charges fuse about 2.5s, cross-flames stop at walls, crates are destroyed and
charges chain-detonate and kick-push. Round ends at one alive; sudden death
spawns random explosions after 150s with a draw guard.

Registry (registry.ts): one setInterval per live match started on startMatch and
cleared on complete/dissolve; globalThis-anchored map; holds seats across a
reconnect window then eliminates; below two active seats it dissolves. On a
manual end, broadcasts a bomperator-terminated event so remaining players see a
toast, then awards rewards via stats.ts (minigame:bomperator, profile group
bomperator: matchesPlayed/matchWins/roundsWon/kills/xpEarned).

Clients POST rate-limited inputs and poll fetchBomperatorMatch; the SSE bus pushes
snapshots with a guarded slow-poll fallback. Registry row ships as upcoming so the
page renders the coming-soon shell until an admin flips it active. Adds the
migration for the stats group, the minigame:bomperator event type, XP
aggregation on the profile, generated types, and real-time event wiring.
2026-10-16 00:32:52 -04:00

180 lines
6.6 KiB
TypeScript

"use client";
import { useCallback, useEffect, useState } from "react";
import { Eye, Plus, RefreshCw, Bomb } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { ROUND_TARGET_OPTIONS } from "@/lib/minigames/bomperator/constants";
import type { BomperatorMatchSummary, BomperatorView } from "@/lib/minigames/bomperator/types";
import {
createBomperatorMatch,
fetchBomperatorMatch,
joinBomperatorMatch,
listBomperatorMatches,
} from "@/app/(frontend)/minigames/bomperator/actions";
const PHASE_LABELS: Record<BomperatorMatchSummary["phase"], string> = {
waiting: "Waiting for technicians",
lobby: "In the waiting room",
countdown: "Starting",
battle: "In progress",
roundEnd: "Round over",
complete: "Finished",
};
export function BomperatorLobby({
onOpenMatch,
currentUserId,
}: {
onOpenMatch: (match: BomperatorView) => void;
currentUserId: number | string;
}) {
const [matches, setMatches] = useState<BomperatorMatchSummary[] | null>(null);
const [roundTarget, setRoundTarget] = useState<number>(3);
const [error, setError] = useState<string | null>(null);
const [busyId, setBusyId] = useState<string | null>(null);
const refresh = useCallback(() => {
void listBomperatorMatches()
.then((result) => {
if (result.success && result.data) setMatches(result.data);
else setError(result.error ?? "Could not load matches.");
})
.catch(() => setError("Could not load matches."));
}, []);
useEffect(() => {
refresh();
const id = window.setInterval(refresh, 4000);
return () => window.clearInterval(id);
}, [refresh]);
const create = useCallback(async () => {
setBusyId("new");
setError(null);
const created = await createBomperatorMatch(roundTarget);
if (!created.success || !created.data) {
setBusyId(null);
setError(created.error ?? "Could not create a match.");
return;
}
const snapshot = await fetchBomperatorMatch(created.data.matchId);
setBusyId(null);
if (snapshot.success && snapshot.data) onOpenMatch(snapshot.data);
else refresh();
}, [onOpenMatch, refresh, roundTarget]);
const enter = useCallback(
async (matchId: string, spectate: boolean) => {
setBusyId(matchId);
setError(null);
const result = spectate ? await fetchBomperatorMatch(matchId) : await joinBomperatorMatch(matchId);
setBusyId(null);
if (result.success && result.data) onOpenMatch(result.data);
else {
setError(result.error ?? "Could not enter the match.");
refresh();
}
},
[onOpenMatch, refresh],
);
return (
<Card>
<CardHeader className="border-b p-4">
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<CardTitle className="flex items-center gap-2 text-sm">
<Bomb className="size-4 text-muted-foreground" />
Ordnance Disposal drills
</CardTitle>
<p className="text-sm text-muted-foreground">
Open a depot and share the link, or join one below. Last technician standing wins the round.
</p>
</div>
<div className="flex flex-wrap items-center gap-2">
<div className="flex items-center gap-1" role="group" aria-label="Rounds to win">
<span className="mr-1 text-xs text-muted-foreground">Best of</span>
{ROUND_TARGET_OPTIONS.map((option) => (
<Button
key={option}
type="button"
size="sm"
variant={roundTarget === option ? "default" : "outline"}
className="h-8 w-8 p-0"
onClick={() => setRoundTarget(option)}
disabled={busyId !== null}
>
{option}
</Button>
))}
</div>
<Button variant="outline" size="sm" onClick={refresh} disabled={busyId !== null}>
<RefreshCw /> Refresh
</Button>
<Button size="sm" onClick={create} disabled={busyId !== null}>
<Plus /> New drill
</Button>
</div>
</div>
</CardHeader>
<CardContent className="p-0">
{error && (
<p className="px-4 pt-4 text-sm text-destructive" role="alert">
{error}
</p>
)}
{matches === null ? (
<p className="px-4 py-6 text-sm text-muted-foreground">Loading matches…</p>
) : matches.length === 0 ? (
<p className="px-4 py-6 text-sm text-muted-foreground">
No live drills. Start one and send the link to your squad.
</p>
) : (
<ul className="divide-y divide-border">
{matches.map((match) => {
const open = match.phase === "waiting" || match.phase === "lobby";
const canJoin = !match.joined && open && match.seatedCount < match.maxSeats;
return (
<li
key={match.matchId}
className="flex flex-wrap items-center justify-between gap-3 px-4 py-3"
>
<div className="flex min-w-0 flex-col">
<span className="truncate text-sm font-medium">
{match.name ?? "Ordnance Disposal"}
</span>
<span className="text-xs text-muted-foreground">
{match.hostName} · {match.seatedCount}/{match.maxSeats} · first to{" "}
{match.roundTarget} · {PHASE_LABELS[match.phase]}
</span>
</div>
<div className="flex items-center gap-2">
{!open && !match.joined && (
<Button
size="sm"
variant="ghost"
disabled={busyId !== null}
onClick={() => enter(match.matchId, true)}
>
<Eye /> Watch
</Button>
)}
<Button
size="sm"
variant={match.joined || canJoin ? "default" : "outline"}
disabled={busyId !== null || (!match.joined && !canJoin)}
onClick={() => enter(match.matchId, false)}
>
{match.joined ? "Rejoin" : canJoin ? "Join" : "Full"}
</Button>
</div>
</li>
);
})}
</ul>
)}
</CardContent>
</Card>
);
}