Skip to content

Instantly share code, notes, and snippets.

@chrishyle
Created March 20, 2020 10:21
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 chrishyle/8c6e6080c21780e96d33e19fd62fe5d1 to your computer and use it in GitHub Desktop.
Save chrishyle/8c6e6080c21780e96d33e19fd62fe5d1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const gameMachine = Machine(
{
id: "game",
initial: "nogame",
context: {
players: [],
roles: [
{
name: "1",
isActive: true
}
]
},
states: {
nogame: {
on: {
CREATE: "notReadyToDeal"
}
},
notReadyToDeal: {
states: {
tooFewPlayers: {},
tooFewCards: {},
tooManyCards: {},
tooManyPlayers: {}
},
"": [{ target: "readyToDeal", cond: "playersAndRolesMatch" }]
},
readyToDeal: {},
notReadyToStart: {},
readyToStart: {}
}
},
{
guards: {
playersAndRolesMatch: (context, event) => {
const { players, roles } = context;
const numAactiveRoles = roles.filter(role => role.isActive).length;
const target = players.length + 3;
return target === numAactiveRoles;
// if (target === numAactiveRoles) {
// game.state = GameStates.ready;
// game.validationError = "";
// return;
// }
// const numRolesOff = target - numAactiveRoles;
// const moreOrFewer = numRolesOff > 0 ? "more" : "fewer";
// game.state = GameStates.invalid;
// game.validationError = `Please select ${Math.abs(
// numRolesOff
// )} ${moreOrFewer} roles.`;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment