Last active
March 22, 2020 23:03
-
-
Save Madumo/5bd21bd0c3bd8dd03890172a948e1e51 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 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