Created
February 18, 2021 12:36
-
-
Save seeden/81edc2510c03e50ea19b88fa795a1aff to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const questionStates = { | |
initial: 'ready', | |
context: { | |
userAnswer: undefined, | |
oponentAnswer: undefined, | |
}, | |
states: { | |
ready: { | |
on: { | |
ANSWER: 'wait_for_oponent_answer', | |
OPONENT_ANSWER: 'wait_for_user_answer', | |
}, | |
}, | |
wait_for_user_answer: { | |
on: { | |
ANSWER: 'results', | |
}, | |
}, | |
wait_for_oponent_answer: { | |
on: { | |
OPONENT_ANSWER: 'results', | |
}, | |
}, | |
results: { | |
exit: 'resetQuestion', | |
}, | |
}, | |
actions: { | |
resetQuestion: () => ({ | |
userAnswer: undefined, | |
oponentAnswer: undefined, | |
}), | |
}, | |
}; | |
const quizStates = { | |
initial: 'quiz', | |
context: { | |
answers: [], | |
currentQuestion: 0, | |
currentQuestionDisplay: 1, | |
questions: [], | |
totalCorrectAnswers: 0, | |
}, | |
states: { | |
quiz: { | |
on: { | |
NEXT_QUESTION: { | |
actions: ['nextQuestion'] | |
}, | |
}, | |
...questionStates, | |
}, | |
results: { | |
exit: 'resetQuiz', | |
}, | |
}, | |
actions: { | |
resetQuiz: () => ({ | |
currentQuestion: 0, | |
currentQuestionDisplay: 1, | |
questions: [], | |
totalCorrectAnswers: 0, | |
}), | |
nextQuestion: (context) => ({ | |
...context, | |
currentQuestion: context.currentQuestion + 1, | |
}), | |
}, | |
}; | |
const fetchMachine = Machine({ | |
id: 'duel', | |
initial: 'welcome', | |
states: { | |
welcome: { | |
on: { | |
SEARCH: 'searching_oponent', | |
}, | |
}, | |
searching_oponent: { | |
on: { | |
OPONENT_FIND: 'quiz', | |
}, | |
}, | |
quiz: { | |
...quizStates, | |
}, | |
results: { | |
on: { | |
PLAY_AGAIN: 'welcome', | |
}, | |
exit: 'resetGame', | |
}, | |
failure: { | |
on: { | |
RETRY: 'quiz', | |
START_OVER: 'welcome', | |
}, | |
}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment