Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion games/game1-v2.7.0/src/game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("happy path", () => {
match.makeMove(p0, "roll");
match.makeMove(p1, "roll");
match.makeMove(p2, "roll");
expect(() => match.makeMove(p1, "roll")).toThrowError("not your turn");
expect(() => match.makeMove(p1, "roll")).toThrow("not your turn");
match.makeMove(p0, "roll");
match.makeMove(p1, "roll");

Expand Down
4 changes: 2 additions & 2 deletions packages/dev-server/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ class Backend extends EventTarget {
}

// Also cancel move expiry timeouts for users whose turn has ended.
for (const userId of Object.keys(result.endTurn)) {
for (const userId of result.endTurn.keys()) {
this._removeDelayedMoveForUser(userId);
}

for (const userId of Object.keys(result.beginTurn)) {
for (const userId of result.beginTurn.keys()) {
this._removeDelayedMoveForUser(userId);
}

Expand Down
24 changes: 12 additions & 12 deletions packages/game/src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export function executePlayerMove<GS extends GameStateBase>(

// For expiration moves, if the player's turn was not explicitely begun,
// it means their turn should end.
if (isExpiration && sideEffectResults.beginTurn[userId] === undefined) {
sideEffectResults.endTurn[userId] = null;
if (isExpiration && !sideEffectResults.beginTurn.has(userId)) {
sideEffectResults.endTurn.set(userId, null);
}

const allPatches: Patch[] = [];
Expand Down Expand Up @@ -282,8 +282,8 @@ export function executePlayerMove<GS extends GameStateBase>(
};
}

type BeginTurn = Record<UserId, { expiresAt?: number }>;
type EndTurn = Record<UserId, null>;
export type BeginTurn = Map<UserId, { expiresAt?: number }>;
export type EndTurn = Map<UserId, null>;

type SideEffectResults = {
matchHasEnded: boolean;
Expand Down Expand Up @@ -314,8 +314,8 @@ function defineMoveSideEffects<GS extends GameStateBase>({
}): { sideEffectResults: SideEffectResults; moveSideEffects: MoveSideEffects } {
const sideEffectResults: SideEffectResults = {
matchHasEnded: false,
beginTurn: {},
endTurn: {},
beginTurn: new Map(),
endTurn: new Map(),
delayedMoves: [],
stats: [],
};
Expand All @@ -325,8 +325,8 @@ function defineMoveSideEffects<GS extends GameStateBase>({
};

const endTurnForUser = (userId: UserId) => {
delete sideEffectResults.beginTurn[userId];
sideEffectResults.endTurn[userId] = null;
sideEffectResults.beginTurn.delete(userId);
sideEffectResults.endTurn.set(userId, null);

// Note that this is not very efficient because we need to loop through all
// delayed moves, and we call this for all users.
Expand Down Expand Up @@ -376,8 +376,8 @@ function defineMoveSideEffects<GS extends GameStateBase>({
}

for (const userId of userIds) {
sideEffectResults.beginTurn[userId] = { expiresAt };
delete sideEffectResults.endTurn[userId];
sideEffectResults.beginTurn.set(userId, { expiresAt });
sideEffectResults.endTurn.delete(userId);
}

return { expiresAt };
Expand Down Expand Up @@ -553,7 +553,7 @@ export function updateMetaWithTurnInfo({
(draft: { meta: Meta }) => {
const { meta } = draft;

for (const [userId, { expiresAt }] of Object.entries(beginTurn)) {
for (const [userId, { expiresAt }] of beginTurn.entries()) {
metaBeginTurn({
meta,
beginsAt: now,
Expand All @@ -562,7 +562,7 @@ export function updateMetaWithTurnInfo({
});
}

for (const userId of Object.keys(endTurn)) {
for (const userId of endTurn.keys()) {
metaEndTurn({
meta,
userId,
Expand Down
4 changes: 2 additions & 2 deletions packages/game/src/testing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
match.fastForward(500);

expect(match.board.me).toBe(2);
expect(match.board.bot).toBe(2);

Check failure on line 118 in packages/game/src/testing.test.ts

View workflow job for this annotation

GitHub Actions / test

src/testing.test.ts > turns > playerMoveOnExpire

AssertionError: expected 3 to be 2 // Object.is equality - Expected + Received - 2 + 3 ❯ src/testing.test.ts:118:29

// Auto move triggered 1s after the last move.
match.fastForward(500);
Expand Down Expand Up @@ -167,7 +167,7 @@
expect(match.hasTurn(userId)).toBe(false);
match.makeMove(userId, "go", {});
expect(match.board.expiresAt).toEqual(1000);
expect(match.hasTurn(userId)).toBe(true);

Check failure on line 170 in packages/game/src/testing.test.ts

View workflow job for this annotation

GitHub Actions / test

src/testing.test.ts > turns > expiresAt

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/testing.test.ts:170:35
});

test("double begin turn: only the latest counts", () => {
Expand Down Expand Up @@ -207,7 +207,7 @@
match.makeMove(userId, "begin", { onExpire: "b" });
match.makeMove(userId, "begin", { onExpire: "c" });
match.fastForward(20);
expect(match.board.x).toBe("ac");

Check failure on line 210 in packages/game/src/testing.test.ts

View workflow job for this annotation

GitHub Actions / test

src/testing.test.ts > turns > double begin turn: only the latest counts

AssertionError: expected 'abc' to be 'ac' // Object.is equality Expected: "ac" Received: "abc" ❯ src/testing.test.ts:210:27
});

// In a given `execute*` function, `turns.begin(userId, {...}); turns.end(userId)` has no effect.
Expand Down Expand Up @@ -415,7 +415,7 @@

match.makeMove(userId, "move");

expect(match.hasTurn(userId)).toBe(true);

Check failure on line 418 in packages/game/src/testing.test.ts

View workflow job for this annotation

GitHub Actions / test

src/testing.test.ts > turns > turn ends on expire

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/testing.test.ts:418:35

match.fastForward(9);

Expand Down Expand Up @@ -606,7 +606,7 @@

expect(() => {
match.makeMove(userId, "incrementWithError");
}).toThrowError("error");
}).toThrow("error");

expect(match.board.x).toBe(0);
});
Expand Down Expand Up @@ -637,7 +637,7 @@

expect(() => {
match.makeMove(userId, "go", null, { canFail: false });
}).toThrowError("error");
}).toThrow("error");

expect(match.board.x).toBe(0);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/game/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
import { IfAnyNull } from "@lefun/core";

import {
BeginTurn,
DelayedMove,
EndTurn,
executeBoardMove,
executePlayerMove,
Stat,
Expand Down Expand Up @@ -391,8 +393,8 @@ export class MatchTester<GS extends GameStateBase, G extends Game<GS>> {
stats,
}: {
matchHasEnded: boolean;
beginTurn: Record<UserId, { expiresAt?: number }>;
endTurn: Record<UserId, null>;
beginTurn: BeginTurn;
endTurn: EndTurn;
delayedMoves: DelayedMove[];
stats: Stat[];
}) {
Expand Down
Loading