boardgame.io
Version:
library for turn-based games
136 lines (132 loc) • 4.6 kB
JavaScript
import { I as InitializeGame } from './initialize-7316768f.js';
var Type;
(function (Type) {
Type[Type["SYNC"] = 0] = "SYNC";
Type[Type["ASYNC"] = 1] = "ASYNC";
})(Type || (Type = {}));
/**
* Type guard that checks if a storage implementation is synchronous.
*/
function isSynchronous(storageAPI) {
return storageAPI.type() === Type.SYNC;
}
class Async {
/* istanbul ignore next */
type() {
/* istanbul ignore next */
return Type.ASYNC;
}
/**
* Create a new match.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a match is created. For example, it might stow away the
* initial match state in a separate field for easier retrieval.
*/
/* istanbul ignore next */
async createMatch(matchID, opts) {
if (this.createGame) {
console.warn('The database connector does not implement a createMatch method.', '\nUsing the deprecated createGame method instead.');
return this.createGame(matchID, opts);
}
else {
console.error('The database connector does not implement a createMatch method.');
}
}
/**
* Return all matches.
*/
/* istanbul ignore next */
async listMatches(opts) {
if (this.listGames) {
console.warn('The database connector does not implement a listMatches method.', '\nUsing the deprecated listGames method instead.');
return this.listGames(opts);
}
else {
console.error('The database connector does not implement a listMatches method.');
}
}
}
class Sync {
type() {
return Type.SYNC;
}
/**
* Connect.
*/
connect() {
return;
}
/**
* Create a new match.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a match is created. For example, it might stow away the
* initial match state in a separate field for easier retrieval.
*/
/* istanbul ignore next */
createMatch(matchID, opts) {
if (this.createGame) {
console.warn('The database connector does not implement a createMatch method.', '\nUsing the deprecated createGame method instead.');
return this.createGame(matchID, opts);
}
else {
console.error('The database connector does not implement a createMatch method.');
}
}
/**
* Return all matches.
*/
/* istanbul ignore next */
listMatches(opts) {
if (this.listGames) {
console.warn('The database connector does not implement a listMatches method.', '\nUsing the deprecated listGames method instead.');
return this.listGames(opts);
}
else {
console.error('The database connector does not implement a listMatches method.');
}
}
}
/**
* Creates a new match metadata object.
*/
const createMetadata = ({ game, unlisted, setupData, numPlayers, }) => {
const metadata = {
gameName: game.name,
unlisted: !!unlisted,
players: {},
createdAt: Date.now(),
updatedAt: Date.now(),
};
if (setupData !== undefined)
metadata.setupData = setupData;
for (let playerIndex = 0; playerIndex < numPlayers; playerIndex++) {
metadata.players[playerIndex] = { id: playerIndex };
}
return metadata;
};
/**
* Creates initial state and metadata for a new match.
* If the provided `setupData` doesn’t pass the game’s validation,
* an error object is returned instead.
*/
const createMatch = ({ game, numPlayers, setupData, unlisted, }) => {
if (!numPlayers || typeof numPlayers !== 'number')
numPlayers = 2;
const setupDataError = game.validateSetupData && game.validateSetupData(setupData, numPlayers);
if (setupDataError !== undefined)
return { setupDataError };
const metadata = createMetadata({ game, numPlayers, setupData, unlisted });
const initialState = InitializeGame({ game, numPlayers, setupData });
return { metadata, initialState };
};
export { Async as A, Sync as S, createMatch as c, isSynchronous as i };