Skip to content

Instantly share code, notes, and snippets.

@tomraithel
Last active April 10, 2019 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomraithel/72485df8741d35c474e7d38d816be7a9 to your computer and use it in GitHub Desktop.
Save tomraithel/72485df8741d35c474e7d38d816be7a9 to your computer and use it in GitHub Desktop.
Statechart for munchkin rules
// Copy-Paste this Chart into the visualizer: https://statecharts.github.io/xstate-viz/
// Available variables:
// Machine (machine factory function)
// XState (all XState exports)
const { assign } = XState.actions;
const fightStates = {
initial: "fightOrRun",
states: {
fightOrRun: {
on: {
DEFEAT: { target: "victory" },
RUN: { target: "dice" }
}
},
victory: {
on: {
TAKE_TREASURE_AND_LEVEL_UP: { target: "end" }
}
},
dice: {
on: {
LOWER_THAN_FIVE: { target: "badThings" },
FIVE_OR_SIX: { target: "end" }
}
},
badThings: {
on: {
DONE: { target: "end" }
}
},
end: {
type: 'final'
}
}
};
const munchkinMachine = Machine({
id: "munchkin",
initial: "openDoor",
states: {
openDoor: {
on: {
DRAW_MONSTER: [{ target: "fight" }],
DRAW_CURSE_OR_TRAP: [{ target: "applyCurseOrTrap" }],
DRAW_OTHER: [{ target: "keepOrDrawCard" }]
}
},
applyCurseOrTrap: {
on: {
APPLY_CURSE: { target: "readyForTrouble" }
}
},
keepOrDrawCard: {
on: {
KEEP: { target: "readyForTrouble" },
DRAW: { target: "readyForTrouble" }
}
},
readyForTrouble: {
on: {
FIGHT_VOLUNTARY: { target: "fight" },
TAKE_LOOT: { target: "takeDungeonCard" }
}
},
takeDungeonCard: {
on: {
TAKE_CARD: { target: "mildGift" }
}
},
mildGift: {
on: {
MORE_THAN_FIVE_CARDS: { target: "hasLowestLevel" },
LESS_THAN_FIVE_CARDS: { target: "done" }
}
},
hasLowestLevel: {
on: {
YES: { target: "discard" },
NO: { target: "charity" }
}
},
discard: {
on: {
DROP_CARDS: { target: "done" }
}
},
charity: {
on: {
DISTRIBUTE_CARDS: { target: "done" }
}
},
fight: {
...fightStates,
on: {
"": { target: "mildGift", in: 'fight.end' }
}
},
done: {
on: {
NEXT_PLAYER: { target: "openDoor" }
}
}
},
on: {
CANCEL: { target: "openDoor" }
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment