From cc7334b2a9f1032e34c4a98da31c831521265b7c Mon Sep 17 00:00:00 2001 From: jajakob Date: Wed, 5 Jun 2024 15:51:48 +0200 Subject: [PATCH] addPeriod, reducers etc to test demo game --- apps/demo-game/__tests__/gameMachine.test.ts | 60 ++++++++++++++++--- apps/demo-game/package.json | 1 + apps/demo-game/src/reducers/PeriodReducer.ts | 11 +--- .../src/reducers/SegmentResultReducer.ts | 10 +++- packages/platform/src/types.ts | 6 +- pnpm-lock.yaml | 3 + 6 files changed, 69 insertions(+), 22 deletions(-) diff --git a/apps/demo-game/__tests__/gameMachine.test.ts b/apps/demo-game/__tests__/gameMachine.test.ts index 42eabf2c..afa3221e 100644 --- a/apps/demo-game/__tests__/gameMachine.test.ts +++ b/apps/demo-game/__tests__/gameMachine.test.ts @@ -1,5 +1,17 @@ -import { GameService, UserRole } from '@gbl-uzh/platform' +import { GameService, Reducers, UserRole } from '@gbl-uzh/platform' import { PrismaClient } from '@prisma/client' +import { PeriodFacts, PeriodFactsSchema } from '../src/graphql/types' +import * as reds from '../src/reducers' + +// TODO(Jakob): +// - Check where we get the facts from to addPeriod +// - flag for clearing the prisma db and do from scratch -> only over cli? + +// - Why do we always provide all reducers for every function in GameServie? + +// - Add state machine in the platform code and add it here +// - Use the functions in util and GameService and alter them iteratively +// in order to use the state machine -> also check how to use the actor describe('Testing Demo Game', () => { const nameGame = 'TestDemoGame' @@ -10,6 +22,28 @@ describe('Testing Demo Game', () => { const userSub = '716f7632-ed33-4701-a281-0f27bd4f6e82' const roleAssigner = (ix: number): UserRole => UserRole.PLAYER const prisma = new PrismaClient() + const reducers: Reducers = { + Actions: { + apply: reds.Actions.apply, + ActionTypes: reds.Actions.ActionTypes, + }, + Period: { + apply: reds.Period.apply, + ActionTypes: reds.Period.ActionTypes, + }, + PeriodResult: { + apply: reds.PeriodResult.apply, + ActionTypes: reds.PeriodResult.ActionTypes, + }, + Segment: { + apply: reds.Segment.apply, + ActionTypes: reds.Segment.ActionTypes, + }, + SegmentResult: { + apply: reds.SegmentResult.apply, + ActionTypes: reds.SegmentResult.ActionTypes, + }, + } let ctx: GameService.Context = { prisma: prisma, @@ -58,14 +92,6 @@ describe('Testing Demo Game', () => { }) } - // TODO(Jakob): - // - flag for clearing the prisma db and do from scratch - // - what is the return of GameService.createGame and what is it used for? - - // - Add state machine in the platform code and add it here - // - Use the functions in util and GameService and alter them iteratively - // in order to use the state machine -> also check how to use the actor - it('creates a new Game', async () => { if (createNewGame) { game = await GameService.createGame( @@ -77,6 +103,22 @@ describe('Testing Demo Game', () => { } else { game = await findGame(gameId) } + + let facts = { + rollsPerSegment: 1, + scenario: { + bankReturn: 1, + seed: 1, + trendStocks: 1, + trendBonds: 1, + gapStocks: 1, + gapBonds: 1, + }, + } + GameService.addGamePeriod({ gameId, facts }, ctx, { + schema: PeriodFactsSchema, + reducers, + }) }) }) diff --git a/apps/demo-game/package.json b/apps/demo-game/package.json index 250c7f8e..5866e304 100644 --- a/apps/demo-game/package.json +++ b/apps/demo-game/package.json @@ -48,6 +48,7 @@ "@tsconfig/recommended": "1.0.2", "@types/jest": "^29.5.4", "@types/node": "^18.11.9", + "@types/ramda": "^0.28.25", "@types/react": "^18.0.25", "@types/react-dom": "^18.0.0", "autoprefixer": "10.4.15", diff --git a/apps/demo-game/src/reducers/PeriodReducer.ts b/apps/demo-game/src/reducers/PeriodReducer.ts index 08524287..ddc4bdf7 100644 --- a/apps/demo-game/src/reducers/PeriodReducer.ts +++ b/apps/demo-game/src/reducers/PeriodReducer.ts @@ -29,26 +29,19 @@ type Actions = > export function apply(state: any, action: Actions) { - const output = { + let newState = { type: action.type, result: state, isDirty: true, } - let newState switch (action.type) { - // TODO(Jakob): - // Is it ok if we add here events and notifications as well? - // -> it would be more consistent case ActionTypes.PERIOD_INITIALIZE: - newState = output break case ActionTypes.PERIOD_CONSOLIDATE: newState = { - ...output, + ...newState, isDirty: false, - events: [], - notification: [], } break diff --git a/apps/demo-game/src/reducers/SegmentResultReducer.ts b/apps/demo-game/src/reducers/SegmentResultReducer.ts index e56e2cd1..3daffcf0 100644 --- a/apps/demo-game/src/reducers/SegmentResultReducer.ts +++ b/apps/demo-game/src/reducers/SegmentResultReducer.ts @@ -57,7 +57,15 @@ export function apply(state: State, action: Actions) { isDirty: true, result: state, } - let newState + + let newState: { + type: ActionTypes + result: any + events: any[] + notification: any[] + isDirty: boolean + } = output + switch (action.type) { case ActionTypes.SEGMENT_RESULTS_INITIALIZE: case ActionTypes.SEGMENT_RESULTS_START: diff --git a/packages/platform/src/types.ts b/packages/platform/src/types.ts index 729c3e2b..ab134255 100644 --- a/packages/platform/src/types.ts +++ b/packages/platform/src/types.ts @@ -28,8 +28,8 @@ export type Output = { type: OutputType extras?: any result: any - notifications: Notification[] - events: Event[] + notifications?: Notification[] + events?: Event[] actions?: any[] isDirty: boolean } @@ -56,7 +56,7 @@ interface Reducer< ActionTypes: Record } -interface Reducers { +export interface Reducers { Actions: Reducer Period: Reducer PeriodResult: Reducer diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 57c6b0d6..e589ba2d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -155,6 +155,9 @@ importers: '@types/node': specifier: ^18.11.9 version: 18.17.14 + '@types/ramda': + specifier: ^0.28.25 + version: 0.28.25 '@types/react': specifier: ^18.0.25 version: 18.2.21