This commit is contained in:
syuilo 2018-03-28 13:32:05 +09:00
parent ac12e63a85
commit 69cef75b53
6 changed files with 59 additions and 39 deletions

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import $ from 'cafy';
import Game, { pack } from '../../models/othello-game'; import OthelloGame, { pack } from '../../models/othello-game';
module.exports = (params, user) => new Promise(async (res, rej) => { module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'my' parameter // Get 'my' parameter
@ -50,7 +50,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
} }
// Fetch games // Fetch games
const games = await Game.find(q, { const games = await OthelloGame.find(q, {
sort, sort,
limit limit
}); });

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import $ from 'cafy';
import Game, { pack } from '../../../models/othello-game'; import OthelloGame, { pack } from '../../../models/othello-game';
import Othello from '../../../../common/othello/core'; import Othello from '../../../../common/othello/core';
module.exports = (params, user) => new Promise(async (res, rej) => { module.exports = (params, user) => new Promise(async (res, rej) => {
@ -7,7 +7,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [gameId, gameIdErr] = $(params.game_id).id().$; const [gameId, gameIdErr] = $(params.game_id).id().$;
if (gameIdErr) return rej('invalid game_id param'); if (gameIdErr) return rej('invalid game_id param');
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (game == null) { if (game == null) {
return rej('game not found'); return rej('game not found');

View file

@ -1,6 +1,6 @@
import $ from 'cafy'; import $ from 'cafy';
import Matching, { pack as packMatching } from '../../models/othello-matching'; import Matching, { pack as packMatching } from '../../models/othello-matching';
import Game, { pack as packGame } from '../../models/othello-game'; import OthelloGame, { pack as packGame } from '../../models/othello-game';
import User from '../../models/user'; import User from '../../models/user';
import publishUserStream, { publishOthelloStream } from '../../event'; import publishUserStream, { publishOthelloStream } from '../../event';
import { eighteight } from '../../../common/othello/maps'; import { eighteight } from '../../../common/othello/maps';
@ -28,7 +28,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
}); });
// Create game // Create game
const game = await Game.insert({ const game = await OthelloGame.insert({
created_at: new Date(), created_at: new Date(),
user1_id: exist.parent_id, user1_id: exist.parent_id,
user2_id: user._id, user2_id: user._id,

View file

@ -3,17 +3,17 @@ import deepcopy = require('deepcopy');
import db from '../../db/mongodb'; import db from '../../db/mongodb';
import { IUser, pack as packUser } from './user'; import { IUser, pack as packUser } from './user';
const Game = db.get<IGame>('othello_games'); const OthelloGame = db.get<IOthelloGame>('othelloGames');
export default Game; export default OthelloGame;
export interface IGame { export interface IOthelloGame {
_id: mongo.ObjectID; _id: mongo.ObjectID;
created_at: Date; createdAt: Date;
started_at: Date; startedAt: Date;
user1_id: mongo.ObjectID; user1Id: mongo.ObjectID;
user2_id: mongo.ObjectID; user2Id: mongo.ObjectID;
user1_accepted: boolean; user1Accepted: boolean;
user2_accepted: boolean; user2Accepted: boolean;
/** /**
* () * ()
@ -22,9 +22,9 @@ export interface IGame {
*/ */
black: number; black: number;
is_started: boolean; isStarted: boolean;
is_ended: boolean; isEnded: boolean;
winner_id: mongo.ObjectID; winnerId: mongo.ObjectID;
logs: Array<{ logs: Array<{
at: Date; at: Date;
color: boolean; color: boolean;
@ -33,9 +33,9 @@ export interface IGame {
settings: { settings: {
map: string[]; map: string[];
bw: string | number; bw: string | number;
is_llotheo: boolean; isLlotheo: boolean;
can_put_everywhere: boolean; canPutEverywhere: boolean;
looped_board: boolean; loopedBoard: boolean;
}; };
form1: any; form1: any;
form2: any; form2: any;
@ -62,11 +62,11 @@ export const pack = (
// Populate the game if 'game' is ID // Populate the game if 'game' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(game)) { if (mongo.ObjectID.prototype.isPrototypeOf(game)) {
_game = await Game.findOne({ _game = await OthelloGame.findOne({
_id: game _id: game
}); });
} else if (typeof game === 'string') { } else if (typeof game === 'string') {
_game = await Game.findOne({ _game = await OthelloGame.findOne({
_id: new mongo.ObjectID(game) _id: new mongo.ObjectID(game)
}); });
} else { } else {

View file

@ -1,7 +1,7 @@
import * as websocket from 'websocket'; import * as websocket from 'websocket';
import * as redis from 'redis'; import * as redis from 'redis';
import * as CRC32 from 'crc-32'; import * as CRC32 from 'crc-32';
import Game, { pack } from '../models/othello-game'; import OthelloGame, { pack } from '../models/othello-game';
import { publishOthelloGameStream } from '../event'; import { publishOthelloGameStream } from '../event';
import Othello from '../../common/othello/core'; import Othello from '../../common/othello/core';
import * as maps from '../../common/othello/maps'; import * as maps from '../../common/othello/maps';
@ -60,14 +60,14 @@ export default function(request: websocket.request, connection: websocket.connec
}); });
async function updateSettings(settings) { async function updateSettings(settings) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (game.is_started) return; if (game.is_started) return;
if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
if (game.user1_id.equals(user._id) && game.user1_accepted) return; if (game.user1_id.equals(user._id) && game.user1_accepted) return;
if (game.user2_id.equals(user._id) && game.user2_accepted) return; if (game.user2_id.equals(user._id) && game.user2_accepted) return;
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: { $set: {
settings settings
} }
@ -77,7 +77,7 @@ export default function(request: websocket.request, connection: websocket.connec
} }
async function initForm(form) { async function initForm(form) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (game.is_started) return; if (game.is_started) return;
if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
@ -88,7 +88,7 @@ export default function(request: websocket.request, connection: websocket.connec
form2: form form2: form
}; };
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: set $set: set
}); });
@ -99,7 +99,7 @@ export default function(request: websocket.request, connection: websocket.connec
} }
async function updateForm(id, value) { async function updateForm(id, value) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (game.is_started) return; if (game.is_started) return;
if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
@ -118,7 +118,7 @@ export default function(request: websocket.request, connection: websocket.connec
form1: form form1: form
}; };
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: set $set: set
}); });
@ -138,14 +138,14 @@ export default function(request: websocket.request, connection: websocket.connec
} }
async function accept(accept: boolean) { async function accept(accept: boolean) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (game.is_started) return; if (game.is_started) return;
let bothAccepted = false; let bothAccepted = false;
if (game.user1_id.equals(user._id)) { if (game.user1_id.equals(user._id)) {
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: { $set: {
user1_accepted: accept user1_accepted: accept
} }
@ -158,7 +158,7 @@ export default function(request: websocket.request, connection: websocket.connec
if (accept && game.user2_accepted) bothAccepted = true; if (accept && game.user2_accepted) bothAccepted = true;
} else if (game.user2_id.equals(user._id)) { } else if (game.user2_id.equals(user._id)) {
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: { $set: {
user2_accepted: accept user2_accepted: accept
} }
@ -177,7 +177,7 @@ export default function(request: websocket.request, connection: websocket.connec
if (bothAccepted) { if (bothAccepted) {
// 3秒後、まだacceptされていたらゲーム開始 // 3秒後、まだacceptされていたらゲーム開始
setTimeout(async () => { setTimeout(async () => {
const freshGame = await Game.findOne({ _id: gameId }); const freshGame = await OthelloGame.findOne({ _id: gameId });
if (freshGame == null || freshGame.is_started || freshGame.is_ended) return; if (freshGame == null || freshGame.is_started || freshGame.is_ended) return;
if (!freshGame.user1_accepted || !freshGame.user2_accepted) return; if (!freshGame.user1_accepted || !freshGame.user2_accepted) return;
@ -196,7 +196,7 @@ export default function(request: websocket.request, connection: websocket.connec
const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap(); const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap();
await Game.update({ _id: gameId }, { await OthelloGame.update({ _id: gameId }, {
$set: { $set: {
started_at: new Date(), started_at: new Date(),
is_started: true, is_started: true,
@ -222,7 +222,7 @@ export default function(request: websocket.request, connection: websocket.connec
winner = null; winner = null;
} }
await Game.update({ await OthelloGame.update({
_id: gameId _id: gameId
}, { }, {
$set: { $set: {
@ -245,7 +245,7 @@ export default function(request: websocket.request, connection: websocket.connec
// 石を打つ // 石を打つ
async function set(pos) { async function set(pos) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (!game.is_started) return; if (!game.is_started) return;
if (game.is_ended) return; if (game.is_ended) return;
@ -288,7 +288,7 @@ export default function(request: websocket.request, connection: websocket.connec
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString()); const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString());
await Game.update({ await OthelloGame.update({
_id: gameId _id: gameId
}, { }, {
$set: { $set: {
@ -314,7 +314,7 @@ export default function(request: websocket.request, connection: websocket.connec
} }
async function check(crc32) { async function check(crc32) {
const game = await Game.findOne({ _id: gameId }); const game = await OthelloGame.findOne({ _id: gameId });
if (!game.is_started) return; if (!game.is_started) return;

View file

@ -1,3 +1,5 @@
// このスクリプトを走らせる前か後に notifications コレクションはdropしてください
db.access_tokens.renameCollection('accessTokens'); db.access_tokens.renameCollection('accessTokens');
db.accessTokens.update({}, { db.accessTokens.update({}, {
$rename: { $rename: {
@ -110,3 +112,21 @@ db.mute.update({}, {
} }
}, false, true); }, false, true);
db.othello_games.renameCollection('othelloGames');
db.othelloGames.update({}, {
$rename: {
created_at: 'createdAt',
started_at: 'startedAt',
is_started: 'isStarted',
is_ended: 'isEnded',
user1_id: 'user1Id',
user2_id: 'user2Id',
user1_accepted: 'user1Accepted',
user2_accepted: 'user2Accepted',
winner_id: 'winnerId',
'settings.is_llotheo': 'settings.isLlotheo',
'settings.can_put_everywhere': 'settings.canPutEverywhere',
'settings.looped_board': 'settings.loopedBoard',
}
}, false, true);