Skip to content

Instantly share code, notes, and snippets.

@MarceloAlves
Last active May 20, 2020 02:52
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 MarceloAlves/48c341f9fe9842907f8475ab3b46afdc to your computer and use it in GitHub Desktop.
Save MarceloAlves/48c341f9fe9842907f8475ab3b46afdc 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 fetchMachine = Machine({
id: 'game',
initial: 'idle',
context: {
selectedMarble: {},
currentPlayer: { id: 1},
stagedMarbles: [{ id: 1, playerId: 1}],
rollResult: 2,
},
states: {
idle: {
on: {
SELECT: [
{ cond: 'canMoveOutOfStaging', actions: 'selectMarble', target: 'awaitingSelection.inStaging'}
]
}
},
awaitingSelection: {
states: {
inStaging: {
on: {
MOVE: ''
}
}
}
}
}
}, {
guards: {
canMoveOutOfStaging: (ctx, evt) => {
console.log('here', ctx, evt)
const isCorrectRoll = ctx.rollResult === 1 || ctx.rollResult === 6
const isStagedMarble = evt.selectedMarble && ctx.stagedMarbles.find(marble => marble.id === evt.selectedMarble.id)
return isCorrectRoll
}
},
actions: {
selectMarble: assign({
selectedMarble: (ctx, evt) => evt.selectedMarble
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment