Skip to content

Instantly share code, notes, and snippets.

@robertpenner
Last active September 26, 2021 23:38
Show Gist options
  • Save robertpenner/a6053b02b62489767f898d3f828f2a20 to your computer and use it in GitHub Desktop.
Save robertpenner/a6053b02b62489767f898d3f828f2a20 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/**
* Abstraction of:
* assign({ Player_1: ({ Player_1 }) => Player_1 + 1 })
*/
const addShot = (player) =>
assign({
[player]: (context) => context[player] + 1,
});
const machine = Machine(
{
id: "Playoff: 3-hole aggregate - 2 players",
context: {
Player_1: 0,
Player_2: 0,
},
initial: "Aggregate 3 Holes",
states: {
"Aggregate 3 Holes": {
onDone: "checking",
initial: "Hole A",
states: {
"Hole A": {
onDone: "Hole B",
type: "parallel",
states: {
Player_1: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_1": "sunk" },
},
sunk: { type: "final" },
},
},
Player_2: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_2": "sunk" },
},
sunk: { type: "final" },
},
},
},
},
"Hole B": {
onDone: "Hole C",
type: "parallel",
states: {
Player_1: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_1": "sunk" },
},
sunk: { type: "final" },
},
},
Player_2: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_2": "sunk" },
},
sunk: { type: "final" },
},
},
},
},
"Hole C": {
onDone: "done playoff segment",
type: "parallel",
states: {
Player_1: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_1": "sunk" },
},
sunk: { type: "final" },
},
},
Player_2: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_2": "sunk" },
},
sunk: { type: "final" },
},
},
},
},
"done playoff segment": { type: "final" },
},
on: {
"shot.Player_1": { actions: "+Player_1" },
"shot.Player_2": { actions: "+Player_2" },
},
},
checking: {
id: "checking",
on: {
"": [
{ cond: "Player_1 lowest", target: "winner.Player_1" },
{ cond: "Player_2 lowest", target: "winner.Player_2" },
{ cond: "tied", target: "still tied" },
],
},
},
winner: {
initial: 'none',
states: {
none: {},
Player_1: { type: "final" },
Player_2: { type: "final" },
},
},
"still tied": {
on: { "next hole": "Sudden Death Hole" },
},
"Sudden Death Hole": {
onDone: "checking",
type: "parallel",
states: {
Player_1: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_1": "sunk" },
},
sunk: { type: "final" },
},
},
Player_2: {
initial: "shooting",
states: {
shooting: {
on: { "sink.Player_2": "sunk" },
},
sunk: { type: "final" },
},
},
},
on: {
"shot.Player_1": { actions: "+Player_1" },
"shot.Player_2": { actions: "+Player_2" },
},
},
},
},
{
actions: {
"+Player_1": addShot("Player_1"),
"+Player_2": addShot("Player_2"),
},
guards: {
"Player_1 lowest": ({ Player_1, Player_2 }) => Player_1 < Player_2,
"Player_2 lowest": ({ Player_1, Player_2 }) => Player_2 < Player_1,
tied: ({ Player_1, Player_2 }) => Player_1 === Player_2,
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment