The L now pivots on its elbow (corner) cell instead of being re-laid-out in its bounding box, so rotating keeps that one cell in place instead of shifting the whole piece. Orientation 0 is the L look with the elbow at the bottom-left and arms up and to the right. Cells are the anchor plus two arm vectors rotated in quarter-turns, so the anchor is always occupied. The editor recovers a stored emplacement's (anchor, rotation) by matching its cells against every candidate, and the engine test asserts the anchor cell is present in all four orientations.
148 lines
5.3 KiB
TypeScript
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: 0 },
|
|
{ assetId: "ammo-depot", origin: { x, y: y + 3 }, orientation: 0 },
|
|
{ assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 },
|
|
{ assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 },
|
|
];
|
|
}
|
|
|
|
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);
|
|
});
|
|
});
|