1+ import * as CR from 'typings'
2+ import * as G from 'typings/graphql'
3+
4+ // position
5+ class Position {
6+ private value : CR . Position
7+ constructor ( ) {
8+ this . value = {
9+ levelId : '' ,
10+ stageId : '' ,
11+ stepId : '' ,
12+ }
13+ }
14+ public get = ( ) => {
15+ return this . value
16+ }
17+ public set = ( value : CR . Position ) => {
18+ this . value = value
19+ }
20+ // calculate the current position based on the saved progress
21+ public setPositionFromProgress = ( tutorial : G . Tutorial , progress : CR . Progress ) : CR . Position => {
22+
23+ // tutorial already completed
24+ // TODO: handle start again?
25+ if ( progress . complete ) {
26+ return this . value
27+ }
28+
29+ const { levels} = tutorial . version
30+
31+ const lastLevelIndex : number | undefined = levels . findIndex ( ( l : G . Level ) => progress . levels [ l . id ] )
32+ // TODO: consider all levels complete as progress.complete
33+ if ( lastLevelIndex < 0 && lastLevelIndex < levels . length ) {
34+ throw new Error ( 'Error setting progress level' )
35+ }
36+ const currentLevel : G . Level = levels [ lastLevelIndex + 1 ]
37+
38+ const { stages} = currentLevel
39+
40+ const lastStageIndex : number | undefined = stages . findIndex ( ( s : G . Stage ) => progress . stages [ s . id ] )
41+ if ( lastStageIndex < 0 && lastStageIndex < stages . length ) {
42+ throw new Error ( 'Error setting progress stage' )
43+ }
44+ const currentStage : G . Stage = stages [ lastStageIndex + 1 ]
45+
46+ const { steps} = currentStage
47+
48+ const lastStepIndex : number | undefined = steps . findIndex ( ( s : G . Step ) => progress . steps [ s . id ] )
49+ if ( lastStepIndex < 0 && lastStepIndex < steps . length ) {
50+ throw new Error ( 'Error setting progress step' )
51+ }
52+ const currentStep : G . Step = steps [ lastStepIndex + 1 ]
53+
54+ this . value = {
55+ levelId : currentLevel . id ,
56+ stageId : currentStage . id ,
57+ stepId : currentStep . id ,
58+ }
59+ return this . value
60+ }
61+ }
62+
63+ export default Position
0 commit comments