Last active
July 18, 2020 04:08
-
-
Save SawyerHood/c9a93834e5b6859158aa4e28333324ec 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
// 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