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.
172 lines
4.7 KiB
TypeScript
172 lines
4.7 KiB
TypeScript
"use server";
|
|
|
|
/**
|
|
* Server-action surface for Ordnance Disposal (internal key `bomperator`).
|
|
* All match logic lives in the registry
|
|
* (src/lib/minigames/bomperator/registry.ts). The userId is always taken from
|
|
* the session, never from a client payload.
|
|
*/
|
|
|
|
import config from "@payload-config";
|
|
import { getPayload } from "payload";
|
|
import { isSuperuser } from "@/utils/access-control/hasPermission";
|
|
import type {
|
|
BomperatorMatchSummary,
|
|
BomperatorView,
|
|
} from "@/lib/minigames/bomperator/types";
|
|
import type { BomperatorInput } from "@/lib/minigames/bomperator/engine";
|
|
import {
|
|
createMatch,
|
|
endMatch,
|
|
forceExpireMatch,
|
|
getSnapshotFor,
|
|
joinMatch,
|
|
leaveMatch,
|
|
listMatchSummaries,
|
|
startMatch,
|
|
submitInput,
|
|
} from "@/lib/minigames/bomperator/registry";
|
|
|
|
export type { BomperatorMatchSummary, BomperatorView, BomperatorInput };
|
|
|
|
interface ActionResult<T = undefined> {
|
|
success: boolean;
|
|
error?: string;
|
|
data?: T;
|
|
}
|
|
|
|
async function authenticate() {
|
|
const payloadConfig = await config;
|
|
const payload = await getPayload({ config: payloadConfig });
|
|
const { headers } = await import("next/headers");
|
|
const hdrs = await headers();
|
|
const { user } = await payload.auth({
|
|
headers: hdrs,
|
|
canSetHeaders: false,
|
|
});
|
|
|
|
if (!user) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
return { payload, user };
|
|
}
|
|
|
|
function toError(error: unknown): { success: false; error: string } {
|
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
if (message === "Unauthorized") {
|
|
return { success: false, error: "You must be logged in." };
|
|
}
|
|
return { success: false, error: message };
|
|
}
|
|
|
|
interface UserLike {
|
|
id: number;
|
|
displayName?: string | null;
|
|
username?: string | null;
|
|
}
|
|
|
|
function userName(user: UserLike): string {
|
|
if (typeof user.displayName === "string" && user.displayName) return user.displayName;
|
|
if (typeof user.username === "string" && user.username) return user.username;
|
|
return `Operator ${user.id}`;
|
|
}
|
|
|
|
export async function createBomperatorMatch(
|
|
roundTarget?: number,
|
|
): Promise<ActionResult<{ matchId: string }>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
const matchId = createMatch(Number(user.id), userName(user), roundTarget);
|
|
return { success: true, data: { matchId } };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function listBomperatorMatches(): Promise<ActionResult<BomperatorMatchSummary[]>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
return { success: true, data: listMatchSummaries(Number(user.id)) };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function joinBomperatorMatch(matchId: string): Promise<ActionResult<BomperatorView>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
return { success: true, data: joinMatch(matchId, Number(user.id), userName(user)) };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function fetchBomperatorMatch(
|
|
matchId: string,
|
|
): Promise<ActionResult<BomperatorView>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
return { success: true, data: getSnapshotFor(matchId, Number(user.id)) };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function startBomperatorMatch(matchId: string): Promise<ActionResult<BomperatorView>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
return { success: true, data: startMatch(matchId, Number(user.id)) };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function sendBomperatorInput(
|
|
matchId: string,
|
|
input: BomperatorInput,
|
|
): Promise<ActionResult<undefined>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
submitInput(matchId, Number(user.id), input);
|
|
return { success: true };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function leaveBomperatorMatch(matchId: string): Promise<ActionResult<undefined>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
leaveMatch(matchId, Number(user.id));
|
|
return { success: true };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function endBomperatorMatch(matchId: string): Promise<ActionResult<undefined>> {
|
|
try {
|
|
const { user } = await authenticate();
|
|
endMatch(matchId, Number(user.id));
|
|
return { success: true };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|
|
|
|
export async function forceExpireBomperatorMatch(
|
|
matchId: string,
|
|
): Promise<ActionResult<undefined>> {
|
|
try {
|
|
const { payload, user } = await authenticate();
|
|
const allowed = await isSuperuser(payload, user);
|
|
if (!allowed) {
|
|
return { success: false, error: "Only administrators can force-end matches." };
|
|
}
|
|
forceExpireMatch(matchId);
|
|
return { success: true };
|
|
} catch (error) {
|
|
return toError(error);
|
|
}
|
|
}
|