Skip to content

Commit

Permalink
addPeriod, reducers etc to test demo game
Browse files Browse the repository at this point in the history
  • Loading branch information
jajakob committed Jun 5, 2024
1 parent b27e000 commit cc7334b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
60 changes: 51 additions & 9 deletions apps/demo-game/__tests__/gameMachine.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<PrismaClient> = {
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,
Expand Down Expand Up @@ -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(
Expand All @@ -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<PeriodFacts>({ gameId, facts }, ctx, {
schema: PeriodFactsSchema,
reducers,
})
})
})

Expand Down
1 change: 1 addition & 0 deletions apps/demo-game/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 2 additions & 9 deletions apps/demo-game/src/reducers/PeriodReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion apps/demo-game/src/reducers/SegmentResultReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions packages/platform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export type Output<OutputType, NotificationType, EventType> = {
type: OutputType
extras?: any
result: any
notifications: Notification<NotificationType>[]
events: Event<EventType>[]
notifications?: Notification<NotificationType>[]
events?: Event<EventType>[]
actions?: any[]
isDirty: boolean
}
Expand All @@ -56,7 +56,7 @@ interface Reducer<
ActionTypes: Record<string, string>
}

interface Reducers<PrismaType> {
export interface Reducers<PrismaType> {
Actions: Reducer<any, any, any, any, any, any, PrismaType>
Period: Reducer<any, any, any, any, any, any, PrismaType>
PeriodResult: Reducer<any, any, any, any, any, any, PrismaType>
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc7334b

Please sign in to comment.