2020-01-31 05:03:27 +00:00
|
|
|
const Game = require('./Game').Game;
|
|
|
|
|
|
|
|
const gamesInProgress = { }
|
|
|
|
|
|
|
|
const storeGame = (game) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
gamesInProgress[game.id] = Game(game);
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const initGame = ({id, gameRecord = [], ...gameData}) => {
|
|
|
|
gamesInProgress[id] = Game({ gameData, gameRecord })
|
|
|
|
gamesInProgress[id].initGame();
|
|
|
|
return getDataForUI(id)
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const makeMove = ({id, move}) => {
|
|
|
|
if (!gamesInProgress[id]) return { message: 'no game'};
|
|
|
|
gamesInProgress[id] = gamesInProgress[id].makeMove(move)
|
|
|
|
if (gamesInProgress[id].success === false) return { message: 'illegal move' };
|
|
|
|
return getDataForUI(id)
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const getDataForUI = (id) => {
|
|
|
|
return {
|
|
|
|
board: gamesInProgress[id].legalMoves,
|
|
|
|
...gamesInProgress[id].getMeta()
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getAllGames = () => {
|
|
|
|
return gamesInProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-31 06:50:34 +00:00
|
|
|
makeMove,
|
2020-01-31 05:03:27 +00:00
|
|
|
getAllGames,
|
2020-05-03 06:00:20 +00:00
|
|
|
getDataForUI,
|
2020-01-31 05:03:27 +00:00
|
|
|
initGame
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|