Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0dd047f

Browse files
committed
create Tutorial model
1 parent7bbacd6 commit0dd047f

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

‎src/services/tutorial/index.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import*asGfrom'typings/graphql'
2+
import*asCRfrom'typings'
3+
4+
classTutorial{
5+
publicrepo:G.TutorialRepo
6+
publicconfig:{codingLanguage:G.EnumCodingLanguage,testRunner:G.EnumTestRunner}
7+
privateversion:G.TutorialVersion
8+
privateposition:CR.Position
9+
privateprogress:CR.Progress
10+
11+
constructor(tutorial:G.Tutorial){
12+
this.repo=tutorial.repo
13+
this.config={
14+
codingLanguage:tutorial.codingLanguage,
15+
testRunner:tutorial.testRunner,
16+
}
17+
// TODO: allow specific version, currently defaults to latest
18+
this.version=tutorial.version
19+
// set initial position
20+
this.position={
21+
levelId:this.version.levels[0].id,
22+
stageId:this.version.levels[0].stages[0].id,
23+
stepId:this.version.levels[0].stages[0].steps[0].id,
24+
}
25+
// set empty initial progress
26+
this.progress={
27+
levels:{},
28+
stages:{},
29+
steps:{},
30+
complete:false,
31+
}
32+
}
33+
publicload=()=>{
34+
//
35+
}
36+
publiclevel=(levelId:string):G.Level|null=>{
37+
constlevel:G.Level|undefined=this.version.levels.find((l:G.Level)=>l.id===levelId)
38+
if(!level){
39+
console.warn(`LevelId not found:${levelId}`)
40+
returnnull
41+
}
42+
returnlevel
43+
}
44+
publicstage=(stageId:string):G.Stage|null=>{
45+
constlevel:G.Level|null=this.level(this.position.levelId)
46+
if(!level){
47+
returnnull
48+
}
49+
conststage:G.Stage|undefined=level.stages.find((s:G.Stage)=>s.id===stageId)
50+
if(!stage){
51+
console.warn(`StageId not found:${stageId}`)
52+
returnnull
53+
}
54+
returnstage
55+
}
56+
publicstep=(stepId:string):G.Step|null=>{
57+
conststage:G.Stage|null=this.stage(this.position.stageId)
58+
if(!stage){
59+
returnnull
60+
}
61+
conststep:G.Step|undefined=stage.steps.find((s:G.Step)=>s.id===stepId)
62+
if(!step){
63+
console.warn(`StepId not found:${stepId}`)
64+
returnnull
65+
}
66+
returnstep
67+
}
68+
publicupdateProgress=():{position:CR.Position,progress:CR.Progress}=>{
69+
const{levelId, stageId, stepId}=this.position
70+
this.progress.levels[levelId]=true
71+
this.progress.stages[stageId]=true
72+
this.progress.steps[stepId]=true
73+
return{
74+
position:this.position,
75+
progress:this.progress,
76+
}
77+
}
78+
publicnextPosition=():CR.Position=>{
79+
const{levelId, stageId, stepId}=this.position
80+
// TODO: calculate and return next position
81+
82+
// is next step
83+
conststage:G.Stage|null=this.stage(stageId)
84+
if(!stage){
85+
thrownewError('Stage not found')
86+
}
87+
const{steps}=stage
88+
constindexOfStep=steps.findIndex((s:G.Step):boolean=>s.id===stepId)
89+
if(indexOfStep===-1){
90+
thrownewError('Step not found')
91+
}
92+
if(indexOfStep<steps.length-1){
93+
94+
return{
95+
levelId,
96+
stageId,
97+
stepId:steps[indexOfStep+1].id
98+
}
99+
}
100+
101+
// is next stage
102+
constlevel:G.Level|null=this.level(levelId)
103+
if(!level){
104+
thrownewError('Level not found')
105+
}
106+
const{stages}=level
107+
constindexOfStage=stages.findIndex((s:G.Stage):boolean=>s.id===stageId)
108+
if(indexOfStage===-1){
109+
thrownewError('Stage not found')
110+
}
111+
if(indexOfStage<stages.length-1){
112+
// next stage
113+
constnextStage=stages[indexOfStage+1]
114+
return{
115+
levelId,
116+
stageId:nextStage.id,
117+
stepId:nextStage.steps[0].id
118+
}
119+
}
120+
121+
// is next level
122+
constlevels=this.version.levels
123+
constindexOfLevel=levels.findIndex((l:G.Level):boolean=>l.id===levelId)
124+
if(indexOfLevel===-1){
125+
thrownewError('Level not found')
126+
}
127+
if(indexOfLevel<levels.length-1){
128+
constnextLevel=levels[indexOfLevel+1]
129+
constnextStage=nextLevel.stages[0]
130+
return{
131+
levelId:nextLevel.id,
132+
stageId:nextStage.id,
133+
stepId:nextStage.steps[0].id,
134+
}
135+
}
136+
137+
thrownewError('Could not calculate next position')
138+
}
139+
}
140+
141+
exportdefaultTutorial

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp