2018-03-06 16:54:56 +00:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-18 00:54:53 +00:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-03-29 11:32:18 +00:00
|
|
|
import db from '../db/mongodb';
|
2018-03-07 08:48:32 +00:00
|
|
|
import { IUser, pack as packUser } from './user';
|
2018-03-06 16:54:56 +00:00
|
|
|
|
2018-06-16 23:10:54 +00:00
|
|
|
const ReversiGame = db.get<IReversiGame>('reversiGames');
|
|
|
|
export default ReversiGame;
|
2018-03-06 16:54:56 +00:00
|
|
|
|
2018-06-16 23:10:54 +00:00
|
|
|
export interface IReversiGame {
|
2018-03-06 16:54:56 +00:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 05:48:47 +00:00
|
|
|
createdAt: Date;
|
|
|
|
startedAt: Date;
|
|
|
|
user1Id: mongo.ObjectID;
|
|
|
|
user2Id: mongo.ObjectID;
|
|
|
|
user1Accepted: boolean;
|
|
|
|
user2Accepted: boolean;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* どちらのプレイヤーが先行(黒)か
|
|
|
|
* 1 ... user1
|
|
|
|
* 2 ... user2
|
|
|
|
*/
|
|
|
|
black: number;
|
|
|
|
|
2018-03-29 05:48:47 +00:00
|
|
|
isStarted: boolean;
|
|
|
|
isEnded: boolean;
|
|
|
|
winnerId: mongo.ObjectID;
|
2018-03-12 16:49:54 +00:00
|
|
|
logs: Array<{
|
|
|
|
at: Date;
|
|
|
|
color: boolean;
|
|
|
|
pos: number;
|
|
|
|
}>;
|
2018-03-08 08:57:57 +00:00
|
|
|
settings: {
|
2018-03-09 09:11:10 +00:00
|
|
|
map: string[];
|
2018-03-08 08:57:57 +00:00
|
|
|
bw: string | number;
|
2018-03-29 05:48:47 +00:00
|
|
|
isLlotheo: boolean;
|
|
|
|
canPutEverywhere: boolean;
|
|
|
|
loopedBoard: boolean;
|
2018-03-08 08:57:57 +00:00
|
|
|
};
|
2018-03-10 22:07:17 +00:00
|
|
|
form1: any;
|
|
|
|
form2: any;
|
2018-03-11 08:23:59 +00:00
|
|
|
|
|
|
|
// ログのposを文字列としてすべて連結したもののCRC32値
|
|
|
|
crc32: string;
|
2018-03-06 16:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-16 23:10:54 +00:00
|
|
|
* Pack an reversi game for API response
|
2018-03-06 16:54:56 +00:00
|
|
|
*/
|
|
|
|
export const pack = (
|
2018-03-07 08:48:32 +00:00
|
|
|
game: any,
|
2018-03-09 09:11:10 +00:00
|
|
|
me?: string | mongo.ObjectID | IUser,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean
|
|
|
|
}
|
2018-03-06 16:54:56 +00:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
2018-03-09 09:11:10 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true
|
|
|
|
}, options);
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
let _game: any;
|
|
|
|
|
|
|
|
// Populate the game if 'game' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(game)) {
|
2018-06-16 23:10:54 +00:00
|
|
|
_game = await ReversiGame.findOne({
|
2018-03-08 08:57:57 +00:00
|
|
|
_id: game
|
|
|
|
});
|
|
|
|
} else if (typeof game === 'string') {
|
2018-06-16 23:10:54 +00:00
|
|
|
_game = await ReversiGame.findOne({
|
2018-03-08 08:57:57 +00:00
|
|
|
_id: new mongo.ObjectID(game)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_game = deepcopy(game);
|
|
|
|
}
|
2018-03-06 16:54:56 +00:00
|
|
|
|
2018-03-07 08:48:32 +00:00
|
|
|
// Me
|
|
|
|
const meId: mongo.ObjectID = me
|
|
|
|
? mongo.ObjectID.prototype.isPrototypeOf(me)
|
|
|
|
? me as mongo.ObjectID
|
|
|
|
: typeof me === 'string'
|
|
|
|
? new mongo.ObjectID(me)
|
|
|
|
: (me as IUser)._id
|
|
|
|
: null;
|
|
|
|
|
2018-03-06 16:54:56 +00:00
|
|
|
// Rename _id to id
|
|
|
|
_game.id = _game._id;
|
|
|
|
delete _game._id;
|
|
|
|
|
2018-03-09 09:11:10 +00:00
|
|
|
if (opts.detail === false) {
|
|
|
|
delete _game.logs;
|
|
|
|
delete _game.settings.map;
|
2018-03-09 09:29:27 +00:00
|
|
|
} else {
|
|
|
|
// 互換性のため
|
|
|
|
if (_game.settings.map.hasOwnProperty('size')) {
|
|
|
|
_game.settings.map = _game.settings.map.data.match(new RegExp(`.{1,${_game.settings.map.size}}`, 'g'));
|
|
|
|
}
|
2018-03-09 09:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 08:48:32 +00:00
|
|
|
// Populate user
|
2018-03-29 05:48:47 +00:00
|
|
|
_game.user1 = await packUser(_game.user1Id, meId);
|
|
|
|
_game.user2 = await packUser(_game.user2Id, meId);
|
|
|
|
if (_game.winnerId) {
|
|
|
|
_game.winner = await packUser(_game.winnerId, meId);
|
2018-03-07 09:55:02 +00:00
|
|
|
} else {
|
|
|
|
_game.winner = null;
|
|
|
|
}
|
2018-03-07 08:48:32 +00:00
|
|
|
|
2018-03-06 16:54:56 +00:00
|
|
|
resolve(_game);
|
|
|
|
});
|