Last active
April 11, 2020 12:13
-
-
Save sibartlett/b402d722b20b0b94cacfbce3bfd4b91d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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 PLAYER1_TURN = "PLAYER1_TURN"; | |
const PLAYER2_TURN = "PLAYER2_TURN"; | |
const PLAYER3_TURN = "PLAYER3_TURN"; | |
const PLAYER4_TURN = "PLAYER4_TURN"; | |
const createPlayer = (key, event) => ({ | |
// type: 'parallel', | |
initial: "ready", | |
on: { | |
[`done.state.catan.${key}`]: "idle", | |
}, | |
states: { | |
ready: { | |
on: { | |
END_TURN: "end", | |
}, | |
}, | |
end: { | |
type: "final", | |
exit: assign({ | |
turn: (context) => context.turn + 1, | |
}), | |
}, | |
}, | |
}); | |
const eventMapping = { | |
player1: PLAYER1_TURN, | |
player2: PLAYER2_TURN, | |
player3: PLAYER3_TURN, | |
player4: PLAYER4_TURN, | |
}; | |
const getPlayerEvent = (context) => { | |
if (!context.players) return { type: "idle" }; | |
const index = context.turn % context.players.length; | |
console.log(context.players[index]); | |
return { type: eventMapping[context.players[index]] }; | |
}; | |
const createTurnTransition = (key) => ({ | |
target: key, | |
cond: (context) => { | |
if (!context.players) return false; | |
const index = context.turn % context.players.length; | |
return context.players[index] === key; | |
}, | |
}); | |
const fetchMachine = Machine({ | |
id: "catan", | |
initial: "idle", | |
context: { | |
players: ["player3", "player1", "player4", "player2"], | |
turn: 0, | |
}, | |
states: { | |
idle: { | |
//entry: send(getPlayerEvent), | |
on: { | |
[PLAYER1_TURN]: createTurnTransition("player1"), | |
[PLAYER2_TURN]: createTurnTransition("player2"), | |
[PLAYER3_TURN]: createTurnTransition("player3"), | |
[PLAYER4_TURN]: createTurnTransition("player4"), | |
}, | |
}, | |
player1: createPlayer("player1", PLAYER1_TURN), | |
player2: createPlayer("player2", PLAYER2_TURN), | |
player3: createPlayer("player3", PLAYER3_TURN), | |
player4: createPlayer("player4", PLAYER4_TURN), | |
game_over: { type: "final" }, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment