Skip to content

Setup #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add machine loadTutorial
  • Loading branch information
ShMcK committed Jun 2, 2019
commit 6993147a0c124a894979c835635f060ef17b1181
44 changes: 42 additions & 2 deletions src/state/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { send } from "xstate";
import { assign, send } from 'xstate'
import * as CR from 'typings'
import * as storage from '../../services/storage'
import * as git from '../../services/git'

let initialTutorial: CR.Tutorial | undefined
let initialProgress: CR.Progress = {
levels: {},
stages: {},
steps: {},
complete: false,
}

export default {
start: async () => {
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
Expand All @@ -10,7 +19,38 @@ export default {
git.gitVersion(),
git.gitCheckRemoteExists(),
])
initialTutorial = tutorial
initialProgress = progress
const canContinue = !!(tutorial && progress && hasGit && hasGitRemote)
send(canContinue ? 'CONTINUE' : 'NEW')
}
},
loadTutorial: assign({
// load initial data, progress & position
data(): CR.TutorialData {
if (!initialTutorial) {
throw new Error('No Tutorial loaded')
}
return initialTutorial.data

},
progress(): CR.Progress { return initialProgress },
position() {
if (!initialTutorial) {
throw new Error('No Tutorial loaded')
}
const { data } = initialTutorial

const levelId = data.summary.levelList[0]
const stageId = data.levels[levelId].stageList[0]
const stepId = data.stages[stageId].stepList[0]

const position = {
levelId,
stageId,
stepId,
}

return position
}
}),
}
204 changes: 114 additions & 90 deletions src/state/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,120 +15,144 @@ export const tutorialMachine = Machine<
{
id: 'tutorial',
context: initialContext,
initial: 'initial',
initial: 'Start',
states: {
initial: {
onEntry: 'start',
on: {
CONTINUE: 'continue',
NEW: 'new',
},
},
new: {
on: {
TUTORIAL_START: 'loading',
},
},
continue: {
on: {
TUTORIAL_START: 'loading',
},
},
loading: {
on: {
TUTORIAL_LOADED: [
{
target: 'summary',
cond: 'hasNoProgress',
},
{
target: 'level',
cond: 'hasNoLevelProgress',
},
{
target: 'stage',
},
],
},
},
summary: {
on: {
NEXT: 'level',
},
},
level: {
onEntry: ['loadLevel'],
on: {
NEXT: 'stage',
BACK: 'summary',
},
},
stage: {
onEntry: ['loadStage'],
initial: 'stageNormal',
Start: {
states: {
stageNormal: {
Initial: {
onEntry: 'start',
on: {
TEST_RUN: 'testRunning',
STEP_SOLUTION_LOAD: {
actions: ['callSolution'],
},
CONTINUE: 'ContinueTutorial',
NEW: 'NewTutorial',
},
},
testRunning: {
on: {
TEST_SUCCESS: [
{
target: 'complete',
cond: 'tasksComplete',
NewTutorial: {
initial: 'SelectTutorial',
states: {
SelectTutorial: {
on: {
TUTORIAL_START: 'InitializeTutorial',
},
{
target: 'testPass',
},
],
TEST_FAILURE: 'testFail',
},
},
InitializeTutorial: {
on: {
TUTORIAL_LOADED: 'Tutorial'
}
},
}

},
ContinueTutorial: {
onEntry: 'loadTutorial',
on: {
TUTORIAL_START: {
target: 'Tutorial',
}
}
},
testPass: {
onEntry: ['stepComplete'],
}
},
Tutorial: {
initial: 'Initialize',
states: {
Initialize: {
on: {
NEXT: [
TUTORIAL_LOADED: [
{
target: 'Summary',
cond: 'hasNoProgress',
},
{
target: 'stageNormal',
cond: 'hasNextStep',
target: 'Level',
cond: 'hasNoLevelProgress',
},
{
target: 'stageComplete',
target: 'Stage',
},
],
},
},
testFail: {

Summary: {
on: {
RETURN: 'stageNormal',
NEXT: 'Level',
},
},
stageComplete: {
Level: {
onEntry: ['loadLevel'],
on: {
NEXT: [
{
target: 'stage',
cond: 'hasNextStage',
NEXT: 'Stage',
BACK: 'Summary',
},
},
Stage: {
onEntry: ['loadStage'],
initial: 'StageNormal',
states: {
StageNormal: {
on: {
TEST_RUN: 'TestRunning',
STEP_SOLUTION_LOAD: {
actions: ['callSolution'],
},
},
{
target: 'level',
cond: 'hasNextLevel',
},
TestRunning: {
on: {
TEST_SUCCESS: [
{
target: 'StageComplete',
cond: 'tasksComplete',
},
{
target: 'TestPass',
},
],
TEST_FAILURE: 'TestFail',
},
{
target: 'complete',
},
TestPass: {
onEntry: ['stepComplete'],
on: {
NEXT: [
{
target: 'StageNormal',
cond: 'hasNextStep',
},
{
target: 'StageComplete',
},
],
},
],
},
TestFail: {
on: {
RETURN: 'StageNormal',
},
},
StageComplete: {
on: {
NEXT: [
{
target: 'Stage',
cond: 'hasNextStage',
},
{
target: 'Level',
cond: 'hasNextLevel',
},
{
target: 'EndTutorial',
},
],
},
},
},
},
},
},
complete: {},
},
EndTutorial: {},
}
}
}
},
{
actions,
Expand Down
39 changes: 26 additions & 13 deletions src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,34 @@ export interface MachineEvent {

export interface MachineStateSchema {
states: {
initial: {}
new: {}
continue: {}
loading: {}
summary: {}
level: {}
stage: {
Start: {
states: {
stageNormal: {}
testRunning: {}
testPass: {}
testFail: {}
stageComplete: {}
Initial: {}
NewTutorial: {
states: {
SelectTutorial: {}
InitializeTutorial: {}
}
}
ContinueTutorial: {}
}
}
Tutorial: {
states: {
Initialize: {}
Summary: {}
Level: {}
Stage: {
states: {
StageNormal: {}
TestRunning: {}
TestPass: {}
TestFail: {}
StageComplete: {}
}
}
EndTutorial: {}
}
}
complete: {}
}
}