node-go/packages/server/services/gameServices.js

39 lines
842 B
JavaScript
Raw Normal View History

const Game = require('./Game').Game;
const gamesInProgress = { }
const storeGame = (game) => {
2020-05-03 06:00:20 +00:00
gamesInProgress[game.id] = Game(game);
}
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-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-05-03 06:00:20 +00:00
const getDataForUI = (id) => {
return {
board: gamesInProgress[id].legalMoves,
...gamesInProgress[id].getMeta()
};
}
const getAllGames = () => {
return gamesInProgress;
}
module.exports = {
2020-01-31 06:50:34 +00:00
makeMove,
getAllGames,
2020-05-03 06:00:20 +00:00
getDataForUI,
initGame
2020-05-03 06:00:20 +00:00
}