Skip to content

Commit

Permalink
Big fefactor the way we define moves
Browse files Browse the repository at this point in the history
Also fix some of the linting.
  • Loading branch information
simlmx committed Jul 18, 2024
1 parent ec05c86 commit b28e8a8
Show file tree
Hide file tree
Showing 31 changed files with 612 additions and 435 deletions.
25 changes: 12 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ name: Build, Check Format and Test

on:
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'
- run: make init
- run: make build
- run: make check-format
- run: make test
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- run: make init
- run: make build
- run: make check-format
- run: make test
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
packages/*/dist
pnpm-lock.yaml
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ watch:

.PHONY: format
format:
pnpm prettier packages --write
pnpm lerna run format
pnpm prettier . --write

.PHONY: check-format
check-format:
pnpm prettier packages --check
pnpm lerna run check-format
pnpm prettier . --check

.PHONY: bump-version
bump-version:
Expand Down
4 changes: 1 addition & 3 deletions game-template/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Game Template

This is a very minimalist game example.
This is a very minimalist game example.

See [Dudo][dudo] for a more complex example.


## Run the game locally

pnpm install
cd ui
pnpm dev


[dudo]: https://github.com/lefun-fun/dudo
5 changes: 3 additions & 2 deletions game-template/game/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"devDependencies": {
"@lefun/core": "workspace:*",
"@lefun/game": "workspace:*",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.1",
"rollup-plugin-typescript2": "^0.36.0",
"tslib": "^2.6.3",
"typescript": "^5.5.3"
"typescript": "^5.5.3",
"vitest": "^1.6.0"
},
"peerDependencies": {
"@lefun/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion game-template/game/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import typescript from "@rollup/plugin-typescript";
import typescript from "rollup-plugin-typescript2";

export default {
input: "src/index.ts",
Expand Down
30 changes: 30 additions & 0 deletions game-template/game/src/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expectTypeOf, test } from "vitest";

import { MatchTester, PlayerMove } from "@lefun/game";

import { game, RollGame, RollGameState as GS } from ".";

test("inside PlayerMove", () => {
expectTypeOf(game).toEqualTypeOf<RollGame>();

const move: PlayerMove<GS, { x: number }> = {
executeNow() {
//
},
};

expectTypeOf(move.executeNow).parameter(0).toMatchTypeOf<{
board: GS["B"];
playerboard: GS["PB"];
payload: { x: number };
}>();

expectTypeOf(move.executeNow).parameter(0).toMatchTypeOf<{
board: { count: number };
}>();
});

test("match tester", () => {
const match = new MatchTester<GS, typeof game>({ game, numPlayers: 2 });
expectTypeOf(match.board.count).toEqualTypeOf<number>();
});
17 changes: 17 additions & 0 deletions game-template/game/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test } from "vitest";

import { MatchTester as _MatchTester } from "@lefun/game";

import { game, RollGame as G, RollGameState as GS } from ".";

class MatchTester extends _MatchTester<GS, G> {}

test("sanity check", () => {
const match = new MatchTester({ game, numPlayers: 2 });
const { players } = match.board;

const userId = Object.keys(players)[0];

match.makeMove(userId, "roll");
match.makeMove(userId, "moveWithArg", { someArg: "123" });
});
64 changes: 49 additions & 15 deletions game-template/game/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserId } from "@lefun/core";
import { createMove, GameDef, Moves } from "@lefun/game";
import { BoardMove, Game, GameState, INIT_MOVE, PlayerMove } from "@lefun/game";

type Player = {
isRolling: boolean;
Expand All @@ -11,21 +11,54 @@ export type Board = {
players: Record<UserId, Player>;
};

const [ROLL, roll] = createMove("roll");
export type RollGameState = GameState<Board>;

const moves: Moves<Board> = {
[ROLL]: {
executeNow({ board, userId }) {
board.players[userId].isRolling = true;
},
execute({ board, userId, random }) {
board.players[userId].diceValue = random.d6();
board.players[userId].isRolling = false;
},
type BMT = {
someBoardMove: never;
someBoardMoveWithArgs: { someArg: number };
};

const moveWithArg: PlayerMove<RollGameState, { someArg: string }, BMT> = {
execute() {
//
},
};

const roll: PlayerMove<RollGameState, never, BMT> = {
executeNow({ board, userId }) {
board.players[userId].isRolling = true;
},
execute({ board, userId, random, delayMove }) {
board.players[userId].diceValue = random.d6();
board.players[userId].isRolling = false;
delayMove("someBoardMove", 100);
delayMove("someBoardMoveWithArgs", { someArg: 3 }, 100);
},
};

const initMove: BoardMove<RollGameState, never, BMT> = {
execute() {
//
},
};

const someBoardMove: BoardMove<RollGameState, never, BMT> = {
execute() {
//
},
};

const game: GameDef<Board> = {
const someBoardMoveWithArgs: BoardMove<
RollGameState,
BMT["someBoardMoveWithArgs"],
BMT
> = {
execute() {
//
},
};

export const game = {
initialBoards: ({ players }) => ({
board: {
count: 0,
Expand All @@ -34,9 +67,10 @@ const game: GameDef<Board> = {
),
},
}),
moves,
playerMoves: { roll, moveWithArg },
boardMoves: { [INIT_MOVE]: initMove, someBoardMove, someBoardMoveWithArgs },
minPlayers: 1,
maxPlayers: 10,
};
} satisfies Game<RollGameState, BMT>;

export { game, roll };
export type RollGame = typeof game;
2 changes: 1 addition & 1 deletion game-template/game/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "esnext",
"lib": ["dom", "esnext"],
"sourceMap": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"declaration": true,
"allowSyntheticDefaultImports": true,
"allowJs": true,
Expand Down
9 changes: 9 additions & 0 deletions game-template/game/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
typecheck: {
enabled: true,
},
},
});
2 changes: 1 addition & 1 deletion game-template/ui/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
["^node:"],
["^@?\\w"],
["^@lefun/"],
["^roll-game$"],
["roll-game"],
["^"],
["^\\."]
]
Expand Down
2 changes: 1 addition & 1 deletion game-template/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
Expand All @@ -38,6 +37,7 @@
"rollup": "^4.18.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.5.3",
"vite": "^5.3.4"
},
Expand Down
2 changes: 1 addition & 1 deletion game-template/ui/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import copy from "rollup-plugin-copy";
import postcss from "rollup-plugin-postcss";
import typescript from "rollup-plugin-typescript2";

export default {
input: "src/index.ts",
Expand Down
28 changes: 18 additions & 10 deletions game-template/ui/src/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import classNames from "classnames";

import type { UserId } from "@lefun/core";
import {
makeUseMakeMove,
makeUseSelector,
makeUseSelectorShallow,
useDispatch,
useIsPlayer,
useUsername,
} from "@lefun/ui";

import { Board as _Board, roll } from "roll-game";

type B = _Board;
import type { RollGame, RollGameState } from "roll-game";

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

const useSelector = makeUseSelector<B>();
const useSelectorShallow = makeUseSelectorShallow<B>();
const useSelector = makeUseSelector<RollGameState>();
const useSelectorShallow = makeUseSelectorShallow<RollGameState>();
const useMakeMove = makeUseMakeMove<RollGame>();

function Player({ userId }: { userId: UserId }) {
const itsMe = useSelector((state) => state.userId === userId);
Expand Down Expand Up @@ -50,7 +49,7 @@ function Die({ userId }: { userId: UserId }) {
}

function Board() {
const dispatch = useDispatch();
const makeMove = useMakeMove();
const players = useSelectorShallow((state) =>
Object.keys(state.board.players),
);
Expand All @@ -66,9 +65,18 @@ function Board() {
))}
</div>
{isPlayer && (
<button onClick={() => dispatch(roll())}>
<Trans>Roll</Trans>
</button>
<>
<button onClick={() => makeMove("roll")}>
<Trans>Roll</Trans>
</button>
<button
onClick={() => {
makeMove("moveWithArg", { someArg: "123" });
}}
>
Go
</button>
</>
)}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion game-template/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Board from "./Board";

export { Board }
export { Board };
6 changes: 3 additions & 3 deletions game-template/ui/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { render } from "@lefun/dev-server";

import { Board, game } from "roll-game";
import { game } from "roll-game";

// @ts-expect-error abc
import { messages as en } from "./locales/en/messages";
// @ts-expect-error abc
import { messages as fr } from "./locales/fr/messages";

render<Board>({
render({
board: async () => {
const { default: Board } = await import("./Board");
// @ts-expect-error the import is there even if TS does not see it!
await import("./index.css");
return <Board />;
},
gameDef: game,
game,
messages: { en, fr },
});
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "1.5.0",
"npmClient": "pnpm"
}
}
8 changes: 0 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ export * from "./types";

import { UserId } from "./types";

/*
* This is the type returned by move functions created by the game developer.
*/
export type Move<P = unknown> = {
name: string;
payload: P;
};

export type AkaType = "similar" | "aka" | "inspired" | "original";

export type User = {
Expand Down
Loading

0 comments on commit b28e8a8

Please sign in to comment.