boardgame.io
Version:
library for turn-based games
60 lines (57 loc) • 1.85 kB
JavaScript
import { t as Setup, E as Enhance, a as GetAPIs, q as FlushAndValidate } from './turn-order-8cc4909b.js';
import { P as ProcessGameConfig } from './reducer-24ea3e4c.js';
/*
* Copyright 2020 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
/**
* Creates the initial game state.
*/
function InitializeGame({ game, numPlayers, setupData, }) {
game = ProcessGameConfig(game);
if (!numPlayers) {
numPlayers = 2;
}
const ctx = game.flow.ctx(numPlayers);
let state = {
// User managed state.
G: {},
// Framework managed state.
ctx,
// Plugin related state.
plugins: {},
};
// Run plugins over initial state.
state = Setup(state, { game });
state = Enhance(state, { game, playerID: undefined });
const pluginAPIs = GetAPIs(state);
state.G = game.setup({ ...pluginAPIs, ctx: state.ctx }, setupData);
let initial = {
...state,
// List of {G, ctx} pairs that can be undone.
_undo: [],
// List of {G, ctx} pairs that can be redone.
_redo: [],
// A monotonically non-decreasing ID to ensure that
// state updates are only allowed from clients that
// are at the same version that the server.
_stateID: 0,
};
initial = game.flow.init(initial);
[initial] = FlushAndValidate(initial, { game });
// Initialize undo stack.
if (!game.disableUndo) {
initial._undo = [
{
G: initial.G,
ctx: initial.ctx,
plugins: initial.plugins,
},
];
}
return initial;
}
export { InitializeGame as I };