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

49 lines
1 KiB
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}) => {
if (gamesInProgress[id]) return getDataForUI(id);
2020-05-03 06:00:20 +00:00
gamesInProgress[id] = Game({ gameData, gameRecord })
gamesInProgress[id].initGame();
return getDataForUI(id)
}
2020-05-03 06:00:20 +00:00
const makeMove = ({id, move}) => {
// console.log(id, move)
// console.log(gamesInProgress[id])
if (!gamesInProgress[id]) storeGame({id});
2020-05-03 06:00:20 +00:00
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 dropGame = (id) => {
return { message:
`${delete gamesInProgress[id]}`
}
}
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,
dropGame
2020-05-03 06:00:20 +00:00
}