-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- split turn machine into its own file - start turn machine when starting round, then infinitely cycle until win conditions are met; set a question each time we enter the turn state and pass it down to the turn machine as input/context - we're invoking the turn machine from the round machine rather than instantiating a Turn model, like we do with the Round machine. This means we're currently not getting the same kind of logging for the turn machine as other machines - we might want to revisit this later - the turn machine currently isn't triggering the client to render the question or accept answers - the round machine doesn't currently type check - it has an action that needs moving to the turn machine - we might need to work out how to connect up with the work in `emit-timer-value-from-machine` which changes how the countdown works Co-authored-by: Rich James <[email protected]>
- Loading branch information
Showing
3 changed files
with
77 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { assign, createMachine } from "xstate"; | ||
import type { Answer, Question } from "../@types/entities"; | ||
|
||
const context = { | ||
answers: [] as Answer[], | ||
selectedQuestion: {} as Question | undefined, | ||
}; | ||
|
||
type Context = typeof context; | ||
|
||
type Event = { | ||
type: "playerSubmitsAnswer"; | ||
answer: Answer; | ||
}; | ||
|
||
const turnMachine = createMachine( | ||
{ | ||
context: context, | ||
id: "turn", | ||
initial: "turnStart", | ||
types: { | ||
context: {} as Context, | ||
events: {} as Event, | ||
typegen: {} as import("./round.typegen").Typegen0, | ||
}, | ||
states: { | ||
turnStart: { | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer", target: "countdown" }, | ||
}, | ||
}, | ||
countdown: { | ||
on: { | ||
playerSubmitsAnswer: { actions: "addAnswer" }, | ||
}, | ||
after: { | ||
[15000]: { target: "finished" }, | ||
}, | ||
}, | ||
finished: { | ||
type: "final", | ||
// pass answers back to round machine | ||
}, | ||
}, | ||
}, | ||
{ | ||
actions: { | ||
addAnswer: assign({ | ||
answers: (args) => [...args.context.answers, args.event.answer], | ||
}), | ||
}, | ||
}, | ||
); | ||
|
||
export { context, turnMachine }; |
This file contains 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