Sharkey/src/server/api/endpoints/othello/games/show.ts

33 lines
871 B
TypeScript
Raw Normal View History

2018-04-24 18:13:06 +09:00
import $ from 'cafy'; import ID from '../../../../../cafy-id';
2018-03-29 20:32:18 +09:00
import OthelloGame, { pack } from '../../../../../models/othello-game';
2018-04-02 12:58:53 +09:00
import Othello from '../../../../../othello/core';
2018-03-09 18:29:27 +09:00
module.exports = (params, user) => new Promise(async (res, rej) => {
2018-03-29 14:48:47 +09:00
// Get 'gameId' parameter
2018-04-27 19:12:15 +09:00
const [gameId, gameIdErr] = $(params.gameId).type(ID).get();
2018-03-29 14:48:47 +09:00
if (gameIdErr) return rej('invalid gameId param');
2018-03-09 18:29:27 +09:00
2018-03-29 14:48:47 +09:00
const game = await OthelloGame.findOne({ _id: gameId });
2018-03-09 18:29:27 +09:00
if (game == null) {
return rej('game not found');
}
const o = new Othello(game.settings.map, {
2018-03-29 14:48:47 +09:00
isLlotheo: game.settings.isLlotheo,
canPutEverywhere: game.settings.canPutEverywhere,
loopedBoard: game.settings.loopedBoard
});
game.logs.forEach(log => {
o.put(log.color, log.pos);
});
const packed = await pack(game, user);
res(Object.assign({
board: o.board,
turn: o.turn
}, packed));
2018-03-09 18:29:27 +09:00
});