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}) => {
|
2020-05-05 06:22:50 +00:00
|
|
|
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-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const makeMove = ({id, move}) => {
|
2020-05-05 06:22:50 +00:00
|
|
|
// 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-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
|
|
|
}
|
|
|
|
|
2020-05-05 06:22:50 +00:00
|
|
|
const dropGame = (id) => {
|
|
|
|
return { message:
|
|
|
|
`${delete gamesInProgress[id]}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-05 06:22:50 +00:00
|
|
|
initGame,
|
|
|
|
dropGame
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|