Skip to content

Commit

Permalink
special funcs -> side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
simlmx committed Aug 17, 2024
1 parent 0d2b4ba commit 998464b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 45 deletions.
3 changes: 2 additions & 1 deletion games/game1-v2.3.0/ui/src/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "./index.css";

import { Trans } from "@lingui/macro";
import classNames from "classnames";
import type { RollGame, RollGameState } from "game1-v2.3.0-game";

import type { UserId } from "@lefun/core";
import {
Expand All @@ -13,6 +12,8 @@ import {
useUsername,
} from "@lefun/ui";

import type { RollGame, RollGameState } from "game1-v2.3.0-game";

// Dice symbol characters
const DICE = ["", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684", "\u2685"];

Expand Down
51 changes: 28 additions & 23 deletions packages/dev-server/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createRoot } from "react-dom/client";
import { createStore as _createStore } from "zustand";

import type { Locale, MatchSettings, UserId, UsersState } from "@lefun/core";
import { Game } from "@lefun/game";
import { Game, MoveSideEffects } from "@lefun/game";
import { setMakeMove, Store, storeContext } from "@lefun/ui";

import {
Expand Down Expand Up @@ -111,6 +111,31 @@ const BoardForPlayer = ({
}
}

const sideEffects: MoveSideEffects = {
delayMove() {
console.warn("delayMove not implemented yet");
return { ts: 0 };
},
endMatch() {
//
},
logPlayerStat() {
//
},
logMatchStat() {
//
},
turns: {
begin() {
console.warn("turns.begin not implemented");
return { expiresAt: 0 };
},
end() {
//
},
},
};

if (executeNow) {
// Optimistic update directly on the `store` of the player making the move.
store.setState((state: MatchState) => {
Expand All @@ -121,28 +146,8 @@ const BoardForPlayer = ({
board,
playerboard,
payload,
delayMove: () => {
console.warn("delayMove not implemented yet");
return { ts: 0 };
},
turns: {
begin() {
console.warn("turns.begin() not implemented yet");
return { expiresAt: 0 };
},
end() {
console.warn("turns.end() not implemented yet");
},
},
endMatch() {
console.warn("endMatch not implemented");
},
logPlayerStat() {
console.warn("logPlayerStat not implemented");
},
logMatchStat() {
console.warn("logMatchStat not implemented");
},
_: sideEffects,
...sideEffects,
});
});
return newState;
Expand Down
48 changes: 30 additions & 18 deletions packages/dev-server/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
User,
UserId,
} from "@lefun/core";
import { Game, GameStateBase, Random } from "@lefun/game";
import { Game, GameStateBase, MoveSideEffects, Random } from "@lefun/game";

type State = {
board: unknown;
Expand Down Expand Up @@ -155,6 +155,31 @@ class Match extends EventTarget {
);
patchesByUserId["spectator"] = [];

const sideEffects: MoveSideEffects = {
delayMove() {
console.warn("delayMove not implemented yet");
return { ts: 0 };
},
endMatch() {
console.warn("endMatch not implemented");
},
logPlayerStat() {
console.warn("logPlayerStat not implemented");
},
logMatchStat() {
console.warn("logMatchStat not implemented");
},
turns: {
begin() {
console.warn("turns.begin not implemented");
return { expiresAt: 0 };
},
end() {
console.warn("turns.end not implemented");
},
},
};

if (executeNow) {
// Also run `executeNow` on the local state.
this.store.setState((state: State) => {
Expand All @@ -170,10 +195,8 @@ class Match extends EventTarget {
userId,
board,
playerboard: playerboards[userId],
delayMove: () => {
console.warn("delayMove not implemented yet");
return { ts: 0 };
},
_: sideEffects,
...sideEffects,
});
},
);
Expand Down Expand Up @@ -211,19 +234,8 @@ class Match extends EventTarget {
gameData,
random,
ts: now,
delayMove: () => {
console.warn("delayMove not implemented yet");
return { ts: 0 };
},
endMatch: () => {
console.warn("todo implement endMatch");
},
itsYourTurn: () => {
console.warn("todo implement itsYourTurn");
},
logStat: () => {
console.warn("todo implement logStats");
},
_: sideEffects,
...sideEffects,
});
},
);
Expand Down
8 changes: 5 additions & 3 deletions packages/game/src/gameDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export type LogMatchStat<MS extends GameStats> = (
value: number,
) => void;

export type SpecialFuncs<
export type MoveSideEffects<
PMT extends MoveTypesBase = MoveTypesBase,
BMT extends MoveTypesBase = MoveTypesBase,
PS extends GameStats = GameStats,
Expand All @@ -130,7 +130,8 @@ type ExecuteNowOptions<
// Assume that the game developer has defined the playerboard if they're using it.
playerboard: GS["PB"];
payload: P;
} & SpecialFuncs<PMT, BMT, PS, MS>;
_: MoveSideEffects<PMT, BMT, PS, MS>;
} & MoveSideEffects<PMT, BMT, PS, MS>;

export type ExecuteNow<
GS extends GameStateBase = GameStateBase,
Expand Down Expand Up @@ -163,7 +164,8 @@ export type ExecuteOptions<
ts: number;
gameData: any;
matchData?: any;
} & SpecialFuncs<PMT, BMT, PS, MS>;
_: MoveSideEffects<PMT, BMT, PS, MS>;
} & MoveSideEffects<PMT, BMT, PS, MS>;

export type Execute<
GS extends GameStateBase = GameStateBase,
Expand Down
2 changes: 2 additions & 0 deletions packages/game/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ export class MatchTester<
board,
playerboard,
payload,
_: specialExecuteFuncs,
...specialExecuteFuncs,
});
}
Expand All @@ -693,6 +694,7 @@ export class MatchTester<
payload,
random,
ts: time,
_: specialExecuteFuncs,
...specialExecuteFuncs,
});
}
Expand Down

0 comments on commit 998464b

Please sign in to comment.