1
0
Fork 0
polaris-task-force/tests/int/call-for-fire-versus-registry.int.spec.ts
Z8MB1E 005ff0e5df feat(minigames): add Call for Fire: Versus multiplayer artillery duel
Head-to-head artillery duel in a new "multiplayer" registry category
(out of periods). Each operator hides a fire-base fleet on a 10x10 grid,
then takes turns calling fire: the first mission is a grid designation,
every later shot adjusts from that operator's last impact using the
single-player range/deflection controls. First to reduce every enemy
emplacement wins; a cap decides on emplacements then cells.

Pure engine in src/lib/minigames/call-for-fire-versus (engine, views,
types, constants) is client-safe and unit-tested. The server-only
registry mirrors the blackjack table pattern (globalThis-anchored Map,
lobby, per-turn timeout, expiry sweep, single-instance only); its views
are built per viewer so the opponent's fleet layout never leaves the
server. The client polls the match snapshot (the in-memory SSE bus is
unreliable across dev module copies). Rewards on completion update the
callForFireVersus profile group, pay XP and chips, and emit
minigame:call-for-fire-versus.

Wires the game into the registry (new multiplayer category + enum
migration), the sidebar grouping, the command palette, and the profile
XP aggregation. The registry row seed is split into its own migration
because Postgres forbids using a freshly added enum value in the same
transaction.

Verified: 21 new tests (engine + registry) plus the wider minigame suite
pass; tsc clean; a live two-player award run confirmed XP, chips, and
the event.
2026-10-11 21:55:39 -04:00

148 lines
5.3 KiB
TypeScript

import { afterAll, beforeEach, describe, expect, it } from "vitest";
import type { FobPlacement } from "@/lib/minigames/call-for-fire-versus/constants";
import {
clearMatchesForTests,
createMatch,
fireShotFor,
getSnapshotFor,
joinMatch,
leaveMatch,
listMatchSummaries,
placeFleetFor,
} from "@/lib/minigames/call-for-fire-versus/registry";
import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/minigames/call-for-fire-versus/stats";
function fleetAt(x: number, y: number): FobPlacement[] {
return [
{ assetId: "command-post", origin: { x, y }, orientation: "h" },
{ assetId: "ammo-depot", origin: { x, y: y + 1 }, orientation: "h" },
{ assetId: "radar-site", origin: { x, y: y + 2 }, orientation: "h" },
{ assetId: "fob", origin: { x, y: y + 3 }, orientation: "h" },
];
}
beforeEach(() => {
clearMatchesForTests();
});
afterAll(() => {
clearMatchesForTests();
});
describe("call for fire versus registry: lobby", () => {
it("seats the host and reports a waiting match", () => {
const matchId = createMatch(9001, "Host");
const summary = listMatchSummaries(9001).find((row) => row.matchId === matchId);
expect(summary?.phase).toBe("waiting");
expect(summary?.seatedCount).toBe(1);
expect(summary?.joined).toBe(true);
// A different viewer is not joined.
expect(listMatchSummaries(9002).find((row) => row.matchId === matchId)?.joined).toBe(false);
});
it("seats a guest and rejects a third player", () => {
const matchId = createMatch(9101, "Host");
const view = joinMatch(matchId, 9102, "Guest");
expect(view.yourIndex).toBe(1);
expect(view.you.opponentName).toBe("Host");
expect(() => joinMatch(matchId, 9103, "Third")).toThrow("already full");
});
it("stops a user seating at two matches at once", () => {
const first = createMatch(9201, "Host");
createMatch(9202, "Other");
expect(() => joinMatch(first, 9202, "Other")).toThrow("already in another match");
});
it("rejects snapshots for non-members and unknown matches", () => {
const matchId = createMatch(9301, "Host");
expect(() => getSnapshotFor(matchId, 9302)).toThrow("You are not in this match.");
expect(() => getSnapshotFor("does-not-exist", 9301)).toThrow("Match not found.");
});
});
describe("call for fire versus registry: battle", () => {
it("allows pre-placement while waiting, but does not start the battle", () => {
const matchId = createMatch(9401, "Host");
const view = placeFleetFor(matchId, 9401, fleetAt(0, 0));
expect(view.phase).toBe("placing");
expect(view.you.ready).toBe(true);
expect(view.turnUserId).toBeNull();
});
it("starts the battle after both fleets, host firing first", () => {
const matchId = createMatch(9501, "Host");
joinMatch(matchId, 9502, "Guest");
placeFleetFor(matchId, 9501, fleetAt(4, 4));
const view = placeFleetFor(matchId, 9502, fleetAt(0, 0));
expect(view.phase).toBe("battle");
expect(view.turnUserId).toBe(9501);
expect(view.turnDeadline).not.toBeNull();
});
it("resolves a shot and passes the turn", () => {
const matchId = createMatch(9601, "Host");
joinMatch(matchId, 9602, "Guest");
placeFleetFor(matchId, 9601, fleetAt(4, 4));
placeFleetFor(matchId, 9602, fleetAt(0, 0));
const afterShot = fireShotFor(matchId, 9601, { kind: "grid", grid: { x: 0, y: 0 } });
expect(afterShot.you.shots).toHaveLength(1);
expect(afterShot.you.shots[0].outcome).toBe("hit");
expect(afterShot.turnUserId).toBe(9602);
// The defender sees the incoming round on their own board.
const defender = getSnapshotFor(matchId, 9602);
expect(defender.you.incoming[0].grid).toEqual({ x: 0, y: 0 });
});
it("awards the win to the opponent when a player leaves mid-battle", () => {
const matchId = createMatch(9701, "Host");
joinMatch(matchId, 9702, "Guest");
placeFleetFor(matchId, 9701, fleetAt(4, 4));
placeFleetFor(matchId, 9702, fleetAt(0, 0));
leaveMatch(matchId, 9701);
const guest = getSnapshotFor(matchId, 9702);
expect(guest.phase).toBe("complete");
expect(guest.result?.winnerUserId).toBe(9702);
expect(guest.result?.reason).toBe("resigned");
// The leaver is no longer a member.
expect(() => getSnapshotFor(matchId, 9701)).toThrow("You are not in this match.");
});
it("disposes a match once the last player leaves", () => {
const matchId = createMatch(9801, "Host");
leaveMatch(matchId, 9801);
expect(() => getSnapshotFor(matchId, 9801)).toThrow("Match not found.");
});
});
describe("call for fire versus: stat math", () => {
it("reads defaults and accumulates a finished match", () => {
const current = readVersusStats({});
expect(current).toEqual({ gamesPlayed: 0, wins: 0, losses: 0, bestScore: 0, xpEarned: 0 });
const win = computeVersusStatsDelta(current, {
userId: 1,
won: true,
fobsDestroyed: 4,
cellsHit: 8,
});
expect(win.gamesPlayed).toBe(1);
expect(win.wins).toBe(1);
expect(win.losses).toBe(0);
expect(win.bestScore).toBe(408);
expect(win.xpEarned).toBe(xpForVersus(true, 4));
const loss = computeVersusStatsDelta(win, {
userId: 1,
won: false,
fobsDestroyed: 1,
cellsHit: 3,
});
expect(loss.wins).toBe(1);
expect(loss.losses).toBe(1);
expect(loss.bestScore).toBe(408);
});
});