working e2e

This commit is contained in:
2025-08-10 12:40:02 -04:00
parent 5e8978c550
commit 32c516bf37
5 changed files with 67 additions and 55 deletions

View File

@@ -47,7 +47,10 @@ export const newGame = (players: string[]) => {
} as GameState;
};
export const getKnowledge = (state: GameState, humanId: string) => ({
export const getKnowledge = (
state: GameState,
humanId: string
): vGameState => ({
humanId,
deck: state.deck.map((_) => null),
players: Object.fromEntries(
@@ -58,6 +61,17 @@ export const getKnowledge = (state: GameState, humanId: string) => ({
),
});
const getView = (state: vGameState, humanId: string): PlayerView => ({
humanId,
deckCount: state.deck.length,
myHand: state.players[humanId] as Hand,
playerHandCounts: Object.fromEntries(
Object.entries(state.players)
.filter(([id]) => id != humanId)
.map(([id, hand]) => [id, hand.length])
),
});
export const resolveAction = (
state: GameState,
humanId: string,
@@ -112,21 +126,31 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
)
.group("/:instanceId", (app) =>
app
.get("/", ({ params: { instanceId } }) =>
prisma.instance
.findUnique({
where: {
id: instanceId,
},
})
.then((game) => game?.gameState)
.get(
"/",
({ params: { instanceId }, headers: { human: humanId } }) =>
prisma.instance
.findUnique({
where: {
id: instanceId,
},
})
.then((game) =>
getView(
getKnowledge(
game!.gameState as GameState,
humanId!
),
humanId!
)
)
)
.post(
"/",
({
params: { instanceId },
body: { action },
headers: { Human: humanId },
headers: { human: humanId },
}) =>
prisma.instance
.findUniqueOrThrow({
@@ -134,20 +158,22 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
id: instanceId,
},
})
.then((game) => {
.then(async (game) => {
const newState = resolveAction(
game.gameState as GameState,
humanId!,
action
);
const knownState = getKnowledge(newState, humanId!);
void prisma.instance.update({
await prisma.instance.update({
data: { gameState: newState },
where: {
id: instanceId,
},
});
return knownState;
return getView(
getKnowledge(newState, humanId!),
humanId!
);
}),
{
body: t.Object({