Bracket hidden artillery targets across patrol/checkpoint/compound difficulties. Pure rules in src/lib/minigames/call-for-fire.ts (compass bearing observer reports, ADD/DROP + RIGHT/LEFT corrections, scoring, distance-to-target accuracy, injected rng) with a 23-case spec. Wires the game into the minigame ecosystem: submit action validates the run, updates the profile callForFire group, pays out XP and chips, emits minigame:call-for-fire, and records the period ledger entry (accuracy is distance-to-target on a win, null on a loss). Adds the game page with period standings and an all-time board, a command palette entry, the Target icon, and the profiles XP aggregation. The registry row and regenerated payload types land with the minigame registry changes in a separate commit.
157 lines
5.3 KiB
TypeScript
157 lines
5.3 KiB
TypeScript
import { getPayload, Payload } from "payload";
|
|
import config from "@/payload.config";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import type { User } from "@/payload-types";
|
|
import { BUILTIN_MINIGAMES } from "@/lib/minigames/registry";
|
|
import {
|
|
loadMinigameRegistry,
|
|
periodScoringGameKeys,
|
|
visibleRegistryGames,
|
|
} from "@/lib/minigames/loadRegistry";
|
|
import { recordMinigameEntry } from "@/lib/minigames/periods/record";
|
|
import { closeDuePeriods } from "@/lib/minigames/periods/lifecycle";
|
|
|
|
const ANCHOR = "2026-06-01T00:00:00.000Z";
|
|
const RUN = `mgr-${Date.now().toString(36)}`;
|
|
|
|
let payload: Payload;
|
|
let userA: number;
|
|
let originalRules: Record<string, unknown> | null = null;
|
|
|
|
describe("Minigame registry", () => {
|
|
beforeAll(async () => {
|
|
const payloadConfig = await config;
|
|
payload = await getPayload({ config: payloadConfig });
|
|
|
|
await clearMinigameData();
|
|
|
|
originalRules = (await payload.findGlobal({
|
|
slug: "minigame-rules",
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
})) as unknown as Record<string, unknown> | null;
|
|
|
|
await payload.updateGlobal({
|
|
slug: "minigame-rules",
|
|
data: {
|
|
enabled: true,
|
|
anchorDate: ANCHOR,
|
|
periodLengthDays: 14,
|
|
placementLadderTopN: 10,
|
|
precisionMinAttempts: 1,
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
userA = (
|
|
(await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username: `${RUN}-a`,
|
|
discordUsername: `${RUN}-a`,
|
|
displayName: "MG A",
|
|
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
|
|
password: "Test123",
|
|
roles: ["user"],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as User
|
|
).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await clearMinigameData();
|
|
if (!originalRules) return;
|
|
await payload
|
|
.updateGlobal({
|
|
slug: "minigame-rules",
|
|
data: {
|
|
enabled: Boolean(originalRules.enabled),
|
|
anchorDate: originalRules.anchorDate as string,
|
|
periodLengthDays: (originalRules.periodLengthDays as number) ?? 14,
|
|
placementLadderTopN: (originalRules.placementLadderTopN as number) ?? 10,
|
|
precisionMinAttempts: (originalRules.precisionMinAttempts as number) ?? 5,
|
|
},
|
|
overrideAccess: true,
|
|
})
|
|
.catch(() => undefined);
|
|
});
|
|
|
|
it("falls back to the built-in eight when the registry is empty", async () => {
|
|
const games = await loadMinigameRegistry(payload);
|
|
expect(games).toHaveLength(BUILTIN_MINIGAMES.length);
|
|
expect(games.map((game) => game.key)).toEqual(BUILTIN_MINIGAMES.map((game) => game.key));
|
|
});
|
|
|
|
it("orders by registry order and hides retired games from the nav", async () => {
|
|
await createGame({ key: "zeta", label: "Zeta", category: "core", status: "retired", order: 1 });
|
|
await createGame({ key: "alpha", label: "Alpha", category: "cardtable", status: "active", order: 2 });
|
|
|
|
const games = await loadMinigameRegistry(payload);
|
|
expect(games.map((game) => game.key)).toEqual(["zeta", "alpha"]);
|
|
expect(visibleRegistryGames(games).map((game) => game.key)).toEqual(["alpha"]);
|
|
// Neither counts for periods (both default countsForPeriods false).
|
|
expect(periodScoringGameKeys(games)).toEqual([]);
|
|
});
|
|
|
|
it("excludes a non-scoring game from period boards", async () => {
|
|
await clearMinigameData();
|
|
await createGame({ key: "flappy", label: "Flight Simulator", category: "core", status: "active", order: 1, countsForPeriods: true });
|
|
await createGame({ key: "eod", label: "Minefield Clearance", category: "core", status: "active", order: 2, countsForPeriods: false });
|
|
|
|
expect(periodScoringGameKeys(await loadMinigameRegistry(payload))).toEqual(["flappy"]);
|
|
|
|
const when = new Date("2026-06-05T12:00:00.000Z");
|
|
await recordMinigameEntry({ payload, userId: userA, game: "flappy", score: 10, durationMs: 1_000, now: when });
|
|
await recordMinigameEntry({ payload, userId: userA, game: "eod", score: 5, durationMs: 1_000, now: when });
|
|
|
|
const summary = await closeDuePeriods(payload, new Date("2026-06-16T00:00:00.000Z"));
|
|
expect(summary.closed).toHaveLength(1);
|
|
|
|
const achievements = await payload.find({
|
|
collection: "achievements",
|
|
where: { type: { equals: "period-champion" } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
// Only the flappy board scored, so the champion has its 10 ladder points;
|
|
// had the non-scoring EOD board counted, this would be 20.
|
|
expect(achievements.docs[0]?.value).toBe(10);
|
|
});
|
|
|
|
async function createGame(data: {
|
|
key: string;
|
|
label: string;
|
|
category: "core" | "cardtable";
|
|
status: "active" | "retired" | "upcoming";
|
|
order: number;
|
|
countsForPeriods?: boolean;
|
|
}) {
|
|
await payload.create({
|
|
collection: "minigames",
|
|
data: {
|
|
...data,
|
|
route: `/minigames/${data.key}`,
|
|
icon: "Gamepad2",
|
|
scoreLabel: "Score",
|
|
description: "",
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
}
|
|
|
|
async function clearMinigameData() {
|
|
for (const collection of [
|
|
"achievements",
|
|
"minigame-entries",
|
|
"minigame-periods",
|
|
"minigames",
|
|
] as const) {
|
|
await payload.delete({ collection, where: { id: { greater_than: 0 } }, overrideAccess: true });
|
|
}
|
|
}
|
|
});
|