Skip to content

Instantly share code, notes, and snippets.

@Madumo
Last active March 22, 2020 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madumo/5bd21bd0c3bd8dd03890172a948e1e51 to your computer and use it in GitHub Desktop.
Save Madumo/5bd21bd0c3bd8dd03890172a948e1e51 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 addPlayer = assign({
playerCount: (context, event) => context.playerCount + 1,
});
const removePlayer = assign({
playerCount: (context, event) => context.playerCount - 1,
});
const game = Machine(
{
id: 'game',
initial: 'waitingForPlayers',
context: {
playerCount: 0,
},
states: {
waitingForPlayers: {
on: {
'': {target: 'ready', cond: 'hasMinimumPlayers'},
CONNECT:{
target: 'waitingForPlayers',
actions: 'addPlayer',
}
,
DISCONNECT: {
target: 'waitingForPlayers',
actions: 'removePlayer',
},
},
},
ready: {
on: {
'': [
{target: 'waitingForPlayers', cond: 'requireMorePlayers'}
],
CONNECT: {
target: 'ready',
actions: 'addPlayer',
},
DISCONNECT: {
target: 'ready',
actions: 'removePlayer',
},
START: 'started',
},
},
started: {
on: {
END: 'ended'
}
},
ended: {}
},
},
{
actions: {addPlayer, removePlayer},
guards: {
hasMinimumPlayers: (context, event) => {
return context.playerCount >= 3
},
requireMorePlayers: (context, event) => {
return context.playerCount < 3
},
hasPlaceLeft: (context, event) => {
return context.playerCount < 6;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment