Skip to content

Commit cc7334b

Browse files
committed
addPeriod, reducers etc to test demo game
1 parent b27e000 commit cc7334b

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed

apps/demo-game/__tests__/gameMachine.test.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
import { GameService, UserRole } from '@gbl-uzh/platform'
1+
import { GameService, Reducers, UserRole } from '@gbl-uzh/platform'
22
import { PrismaClient } from '@prisma/client'
3+
import { PeriodFacts, PeriodFactsSchema } from '../src/graphql/types'
4+
import * as reds from '../src/reducers'
5+
6+
// TODO(Jakob):
7+
// - Check where we get the facts from to addPeriod
8+
// - flag for clearing the prisma db and do from scratch -> only over cli?
9+
10+
// - Why do we always provide all reducers for every function in GameServie?
11+
12+
// - Add state machine in the platform code and add it here
13+
// - Use the functions in util and GameService and alter them iteratively
14+
// in order to use the state machine -> also check how to use the actor
315

416
describe('Testing Demo Game', () => {
517
const nameGame = 'TestDemoGame'
@@ -10,6 +22,28 @@ describe('Testing Demo Game', () => {
1022
const userSub = '716f7632-ed33-4701-a281-0f27bd4f6e82'
1123
const roleAssigner = (ix: number): UserRole => UserRole.PLAYER
1224
const prisma = new PrismaClient()
25+
const reducers: Reducers<PrismaClient> = {
26+
Actions: {
27+
apply: reds.Actions.apply,
28+
ActionTypes: reds.Actions.ActionTypes,
29+
},
30+
Period: {
31+
apply: reds.Period.apply,
32+
ActionTypes: reds.Period.ActionTypes,
33+
},
34+
PeriodResult: {
35+
apply: reds.PeriodResult.apply,
36+
ActionTypes: reds.PeriodResult.ActionTypes,
37+
},
38+
Segment: {
39+
apply: reds.Segment.apply,
40+
ActionTypes: reds.Segment.ActionTypes,
41+
},
42+
SegmentResult: {
43+
apply: reds.SegmentResult.apply,
44+
ActionTypes: reds.SegmentResult.ActionTypes,
45+
},
46+
}
1347

1448
let ctx: GameService.Context = {
1549
prisma: prisma,
@@ -58,14 +92,6 @@ describe('Testing Demo Game', () => {
5892
})
5993
}
6094

61-
// TODO(Jakob):
62-
// - flag for clearing the prisma db and do from scratch
63-
// - what is the return of GameService.createGame and what is it used for?
64-
65-
// - Add state machine in the platform code and add it here
66-
// - Use the functions in util and GameService and alter them iteratively
67-
// in order to use the state machine -> also check how to use the actor
68-
6995
it('creates a new Game', async () => {
7096
if (createNewGame) {
7197
game = await GameService.createGame(
@@ -77,6 +103,22 @@ describe('Testing Demo Game', () => {
77103
} else {
78104
game = await findGame(gameId)
79105
}
106+
107+
let facts = {
108+
rollsPerSegment: 1,
109+
scenario: {
110+
bankReturn: 1,
111+
seed: 1,
112+
trendStocks: 1,
113+
trendBonds: 1,
114+
gapStocks: 1,
115+
gapBonds: 1,
116+
},
117+
}
118+
GameService.addGamePeriod<PeriodFacts>({ gameId, facts }, ctx, {
119+
schema: PeriodFactsSchema,
120+
reducers,
121+
})
80122
})
81123
})
82124

apps/demo-game/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@tsconfig/recommended": "1.0.2",
4949
"@types/jest": "^29.5.4",
5050
"@types/node": "^18.11.9",
51+
"@types/ramda": "^0.28.25",
5152
"@types/react": "^18.0.25",
5253
"@types/react-dom": "^18.0.0",
5354
"autoprefixer": "10.4.15",

apps/demo-game/src/reducers/PeriodReducer.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,19 @@ type Actions =
2929
>
3030

3131
export function apply(state: any, action: Actions) {
32-
const output = {
32+
let newState = {
3333
type: action.type,
3434
result: state,
3535
isDirty: true,
3636
}
3737

38-
let newState
3938
switch (action.type) {
40-
// TODO(Jakob):
41-
// Is it ok if we add here events and notifications as well?
42-
// -> it would be more consistent
4339
case ActionTypes.PERIOD_INITIALIZE:
44-
newState = output
4540
break
4641
case ActionTypes.PERIOD_CONSOLIDATE:
4742
newState = {
48-
...output,
43+
...newState,
4944
isDirty: false,
50-
events: [],
51-
notification: [],
5245
}
5346
break
5447

apps/demo-game/src/reducers/SegmentResultReducer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export function apply(state: State, action: Actions) {
5757
isDirty: true,
5858
result: state,
5959
}
60-
let newState
60+
61+
let newState: {
62+
type: ActionTypes
63+
result: any
64+
events: any[]
65+
notification: any[]
66+
isDirty: boolean
67+
} = output
68+
6169
switch (action.type) {
6270
case ActionTypes.SEGMENT_RESULTS_INITIALIZE:
6371
case ActionTypes.SEGMENT_RESULTS_START:

packages/platform/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export type Output<OutputType, NotificationType, EventType> = {
2828
type: OutputType
2929
extras?: any
3030
result: any
31-
notifications: Notification<NotificationType>[]
32-
events: Event<EventType>[]
31+
notifications?: Notification<NotificationType>[]
32+
events?: Event<EventType>[]
3333
actions?: any[]
3434
isDirty: boolean
3535
}
@@ -56,7 +56,7 @@ interface Reducer<
5656
ActionTypes: Record<string, string>
5757
}
5858

59-
interface Reducers<PrismaType> {
59+
export interface Reducers<PrismaType> {
6060
Actions: Reducer<any, any, any, any, any, any, PrismaType>
6161
Period: Reducer<any, any, any, any, any, any, PrismaType>
6262
PeriodResult: Reducer<any, any, any, any, any, any, PrismaType>

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)