Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
simlmx committed Jun 25, 2024
1 parent f9370b4 commit 4c24f36
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 284 deletions.
61 changes: 42 additions & 19 deletions game-template/game/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { UserId } from "@lefun/core";
import {
makeGameDef,
GameDef,
GameMoves,
PlayerMove,
GameState,
PlayerMoveDef,
BoardMoveDefs,
} from "@lefun/game";

type Player = {
Expand All @@ -17,36 +19,56 @@ export type Board = {

type EmptyObject = Record<string, never>;

type GS = {
B: Board;
PB: EmptyObject;
SB: EmptyObject;
};
type GS = GameState<Board>;

type MoveWithArgPayload = { someArg: number };
type BoardMoveTypes = {
_initMove: EmptyObject;
someBoardMove: EmptyObject;
someBoardMoveWithArgs: { someArg: number };
};

const moveWithArg = {
execute({ board, userId, payload }) {
//
execute({ board, userId, payload, delayMove }) {
// execute content
},
} satisfies PlayerMove<GS, MoveWithArgPayload>;
} satisfies PlayerMoveDef<GS, BoardMoveTypes, { someArg: string }>;

const roll = {
executeNow({ board, userId }) {
board.players[userId].isRolling = true;
},
execute({ board, userId, random, playerboards }) {
execute({ board, userId, random, playerboards, delayMove }) {
board.players[userId].diceValue = random.d6();
board.players[userId].isRolling = false;
delayMove({ name: "someBoardMove" }, 100);
delayMove({ name: "someBoardMoveWithArgs", payload: { someArg: 3 } }, 100);
},
} satisfies PlayerMove<GS, EmptyObject>;
} satisfies PlayerMoveDef<GS, BoardMoveTypes>;

const moves = {
const playerMoves = {
moveWithArg,
roll,
}
};

type GM = typeof moves;
type PM = typeof playerMoves;

const boardMoves = {
_initMove: {
execute({ board }) {
//
},
},
someBoardMove: {
execute({ board }) {
//
},
},
someBoardMoveWithArgs: {
execute({ board, payload }) {
//
},
},
} satisfies BoardMoveDefs<GS, BoardMoveTypes>;

const game = {
initialBoards: ({ players }) => ({
Expand All @@ -57,9 +79,10 @@ const game = {
),
},
}),
moves,
playerMoves,
boardMoves,
minPlayers: 1,
maxPlayers: 10,
} satisfies GameDef<GS, GM>;
} satisfies GameDef<GS, PM, typeof boardMoves, BoardMoveTypes>;

export { GS, GM, game };
export { GS, PM, game };
6 changes: 3 additions & 3 deletions game-template/ui/src/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
makeUseMakeMove,
} from "@lefun/ui";

import { GS, GM } from "roll-game";
import { GS, PM } from "roll-game";

import { Trans } from "@lingui/macro";

Expand All @@ -19,7 +19,7 @@ const DICE = ["", "\u2680", "\u2681", "\u2682", "\u2683", "\u2684", "\u2685"];

const useSelector = makeUseSelector<GS>();
const useSelectorShallow = makeUseSelectorShallow<GS>();
const useMakeMove = makeUseMakeMove<GS, GM>();
const useMakeMove = makeUseMakeMove<GS, PM>();

function Player({ userId }: { userId: UserId }) {
const itsMe = useSelector((state) => state.userId === userId);
Expand Down Expand Up @@ -67,7 +67,7 @@ function Board() {
</button>
<button
onClick={() =>
makeMove({ name: "moveWithArg", payload: { someArg: 3 } })
makeMove({ name: "moveWithArg", payload: { someArg: "123" } })
}
>
Go
Expand Down
36 changes: 20 additions & 16 deletions packages/dev-server/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createRoot } from "react-dom/client";
import { createStore as _createStore, useStore as _useStore } from "zustand";

import type { Locale, MatchSettings, UserId, UsersState } from "@lefun/core";
import { GameDef, GameMoves, GameState } from "@lefun/game";
import { GameDef, GameStateBase } from "@lefun/game";
import { setMakeMove, Store, storeContext } from "@lefun/ui";

import { loadMatch, Match, saveMatch } from "./match";
Expand All @@ -35,7 +35,7 @@ type MatchState<B, PB> = {
users: UsersState;
};

const BoardForPlayer = <GS extends GameState>({
const BoardForPlayer = <GS extends GameStateBase>({
board,
userId,
messages,
Expand Down Expand Up @@ -196,10 +196,11 @@ function useSetDimensionCssVariablesOnResize(ref: RefObject<HTMLElement>) {
return { height, width };
}

function MatchStateView<GS extends GameState>({
function MatchStateView({
matchRef,
}: {
matchRef: RefObject<Match<GS>>;
// FIXME
matchRef: RefObject<Match<any, any, any>>;
}) {
const state = _useStore(matchRef.current?.store as any, (state) =>
deepCopy(state),
Expand Down Expand Up @@ -251,11 +252,12 @@ function ButtonRow({ children }: { children: ReactNode }) {
return <div className="flex space-x-1">{children}</div>;
}

function Settings<GS extends GameState>({
function Settings({
matchRef,
resetMatch,
}: {
matchRef: RefObject<Match<GS>>;
// FIXME
matchRef: RefObject<Match<any, any, any>>;
resetMatch: ({
locale,
numPlayers,
Expand Down Expand Up @@ -372,17 +374,17 @@ function Settings<GS extends GameState>({
</Button>
</ButtonRow>
</div>
<MatchStateView<GS> matchRef={matchRef} />
<MatchStateView matchRef={matchRef} />
</div>
);
}

function Main<GS extends GameState, GM extends GameMoves<GS>>({
function Main({
gameDef,
matchSettings,
matchData,
}: {
gameDef: GameDef<GS, GM>;
gameDef: GameDef<any, any, any>;
matchSettings: MatchSettings;
matchData?: any;
}) {
Expand All @@ -393,7 +395,7 @@ function Main<GS extends GameState, GM extends GameMoves<GS>>({

const [loading, setLoading] = useState(true);

const matchRef = useRef<Match<GS> | null>(null);
const matchRef = useRef<Match<any, any, any> | null>(null);

const resetMatch = useCallback(
({
Expand All @@ -407,10 +409,11 @@ function Main<GS extends GameState, GM extends GameMoves<GS>>({
}) => {
const userIds = getUserIds(numPlayers);

let match: Match<GS> | null = null;
// FIXME
let match: Match<any, any, any> | null = null;

if (tryToLoad) {
match = loadMatch<GS>(gameDef);
match = loadMatch(gameDef);
}

const players = Object.fromEntries(
Expand All @@ -434,7 +437,8 @@ function Main<GS extends GameState, GM extends GameMoves<GS>>({
userIds.map((userId, i) => [userId, { color: i.toString() }]),
);

match = new Match<GS>({
// FIXME
match = new Match<any, any, any>({
gameDef,
matchSettings,
matchPlayersSettings,
Expand Down Expand Up @@ -505,7 +509,7 @@ function Main<GS extends GameState, GM extends GameMoves<GS>>({
})}
</div>
{matchRef && (
<Settings<GS>
<Settings
matchRef={matchRef}
resetMatch={({
numPlayers,
Expand All @@ -522,7 +526,7 @@ function Main<GS extends GameState, GM extends GameMoves<GS>>({

type AllMessages = Record<string, Record<string, string>>;

async function render<GS extends GameState>({
async function render({
gameDef,
board,
matchSettings = {},
Expand All @@ -531,7 +535,7 @@ async function render<GS extends GameState>({
messages = { en: {} },
}: {
// FIXME
gameDef: GameDef<GS, any>;
gameDef: GameDef<any, any, any>;
board: () => Promise<ReactNode>;
matchSettings?: MatchSettings;
matchData?: any;
Expand Down
33 changes: 23 additions & 10 deletions packages/dev-server/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@ import { Draft, Patch, produceWithPatches } from "immer";
import { createStore, StoreApi } from "zustand";

import {
// Move,
Locale,
MatchPlayersSettings,
MatchSettings,
User,
UserId,
} from "@lefun/core";
import { GameDef, GameMove, GameState, Random } from "@lefun/game";
import {
GameDef,
GameStateBase,
PlayerMoveDefs,
PlayerMoveName,
PlayerMoveWithOptionalPayload,
Random,
} from "@lefun/game";

type EmptyObject = Record<string, never>;

type State<GS extends GameState> = {
type State<GS extends GameStateBase> = {
board: GS["B"];
playerboards: Record<UserId, GS["PB"] | EmptyObject>;
secretboard: GS["SB"] | EmptyObject;
};

class Match<GS extends GameState> extends EventTarget {
class Match<
GS extends GameStateBase,
PM extends PlayerMoveDefs<GS, BMT>,
BMT,
> extends EventTarget {
userIds: UserId[];
random: Random;
// FIXME
Expand Down Expand Up @@ -135,7 +145,10 @@ class Match<GS extends GameState> extends EventTarget {
}
}

makeMove(userId: UserId, move: GameMove<GS, any>) {
makeMove<K extends PlayerMoveName<GS, PM>>(
userId: UserId,
move: PlayerMoveWithOptionalPayload<GS, PM, K>,
) {
const now = new Date().getTime();

// Here the `store` is the store for the player making the move, since
Expand All @@ -149,7 +162,7 @@ class Match<GS extends GameState> extends EventTarget {
}

const { name, payload } = move;
const { executeNow, execute } = this.gameDef.moves[name];
const { executeNow, execute } = this.gameDef.playerMoves[name];

const patchesByUserId: Record<UserId, Patch[]> = Object.fromEntries(
this.userIds.map((userId) => [userId, []]),
Expand Down Expand Up @@ -267,17 +280,17 @@ function separatePatchesByUser(
}

/* Save match to localStorage */
function saveMatch<GS extends GameState>(match: Match<GS>) {
function saveMatch(match: Match<any, any, any>) {
const state = match.store.getState();
const userIds = match.userIds;
localStorage.setItem("match", JSON.stringify({ state, userIds }));
}

/* Load match from localStorage */
function loadMatch<GS extends GameState>(
function loadMatch(
// FIXME
gameDef: GameDef<GS, any>,
): Match<GS> | null {
gameDef: GameDef<any, any>,
): Match<any, any, any> | null {
const data = localStorage.getItem("match");
if (!data) {
return null;
Expand Down
Loading

0 comments on commit 4c24f36

Please sign in to comment.