Skip to content

Instantly share code, notes, and snippets.

@SawyerHood
Last active July 18, 2020 04:08
Show Gist options
  • Save SawyerHood/c9a93834e5b6859158aa4e28333324ec to your computer and use it in GitHub Desktop.
Save SawyerHood/c9a93834e5b6859158aa4e28333324ec to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const gameMachine = Machine(
{
id: "game",
context: {
images: {},
players: {},
},
initial: "start",
states: {
start: {
on: {
JOIN: "waitingRoom",
HOST: "waitingRoom",
},
},
waitingRoom: {
on: {
START_GAME: "showAnimal",
USER_JOIN: {actions: 'userJoin'}
},
},
showAnimal: {
on: {
START_DRAWING: "drawing",
},
},
drawing: {
on: {
TIMER_END: "voting",
},
},
waitingForImages: {
on: {
UPLOAD_DRAWING: {
actions: "uploadDrawing",
},
"": {
target: "voting",
cond: "imagesSubmitted",
},
},
},
voting: {
on: {
TIMER_END: "results",
VOTING_COMPLETE: "results",
},
},
results: {
on: {
RESTART: "waitingRoom",
},
},
},
},
{
actions: {
uploadDrawing: assign({
images: (context, event) => ({
...context.images,
[event.userID]: image,
}),
}),
userJoin: assign({
users: (context, event) => ({
...context.users,
[event.userID]: true,
})
})
},
guards: {
imagesSubmitted: (context) => Object.keys(context.images).length === Object.keys(context.players).length,
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment