From 69cef75b53671ccd7e0a199d31fa2d6d7c91fe78 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 28 Mar 2018 13:32:05 +0900 Subject: [PATCH] wip --- src/api/endpoints/othello/games.ts | 4 +-- src/api/endpoints/othello/games/show.ts | 4 +-- src/api/endpoints/othello/match.ts | 4 +-- src/api/models/othello-game.ts | 34 ++++++++++++------------- src/api/stream/othello-game.ts | 32 +++++++++++------------ tools/migration/shell.camel-case.js | 20 +++++++++++++++ 6 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts index 2a6bbb404..f6e38b8d8 100644 --- a/src/api/endpoints/othello/games.ts +++ b/src/api/endpoints/othello/games.ts @@ -1,5 +1,5 @@ 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) => { // Get 'my' parameter @@ -50,7 +50,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { } // Fetch games - const games = await Game.find(q, { + const games = await OthelloGame.find(q, { sort, limit }); diff --git a/src/api/endpoints/othello/games/show.ts b/src/api/endpoints/othello/games/show.ts index 2b0db4dd0..c7bd74a39 100644 --- a/src/api/endpoints/othello/games/show.ts +++ b/src/api/endpoints/othello/games/show.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import Game, { pack } from '../../../models/othello-game'; +import OthelloGame, { pack } from '../../../models/othello-game'; import Othello from '../../../../common/othello/core'; 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().$; 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) { return rej('game not found'); diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts index b73e105ef..f73386ba7 100644 --- a/src/api/endpoints/othello/match.ts +++ b/src/api/endpoints/othello/match.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; 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 publishUserStream, { publishOthelloStream } from '../../event'; import { eighteight } from '../../../common/othello/maps'; @@ -28,7 +28,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { }); // Create game - const game = await Game.insert({ + const game = await OthelloGame.insert({ created_at: new Date(), user1_id: exist.parent_id, user2_id: user._id, diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts index 01c6ca6c0..b9e57632d 100644 --- a/src/api/models/othello-game.ts +++ b/src/api/models/othello-game.ts @@ -3,17 +3,17 @@ import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; import { IUser, pack as packUser } from './user'; -const Game = db.get('othello_games'); -export default Game; +const OthelloGame = db.get('othelloGames'); +export default OthelloGame; -export interface IGame { +export interface IOthelloGame { _id: mongo.ObjectID; - created_at: Date; - started_at: Date; - user1_id: mongo.ObjectID; - user2_id: mongo.ObjectID; - user1_accepted: boolean; - user2_accepted: boolean; + createdAt: Date; + startedAt: Date; + user1Id: mongo.ObjectID; + user2Id: mongo.ObjectID; + user1Accepted: boolean; + user2Accepted: boolean; /** * どちらのプレイヤーが先行(黒)か @@ -22,9 +22,9 @@ export interface IGame { */ black: number; - is_started: boolean; - is_ended: boolean; - winner_id: mongo.ObjectID; + isStarted: boolean; + isEnded: boolean; + winnerId: mongo.ObjectID; logs: Array<{ at: Date; color: boolean; @@ -33,9 +33,9 @@ export interface IGame { settings: { map: string[]; bw: string | number; - is_llotheo: boolean; - can_put_everywhere: boolean; - looped_board: boolean; + isLlotheo: boolean; + canPutEverywhere: boolean; + loopedBoard: boolean; }; form1: any; form2: any; @@ -62,11 +62,11 @@ export const pack = ( // Populate the game if 'game' is ID if (mongo.ObjectID.prototype.isPrototypeOf(game)) { - _game = await Game.findOne({ + _game = await OthelloGame.findOne({ _id: game }); } else if (typeof game === 'string') { - _game = await Game.findOne({ + _game = await OthelloGame.findOne({ _id: new mongo.ObjectID(game) }); } else { diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts index 1c846f27a..45a931c7e 100644 --- a/src/api/stream/othello-game.ts +++ b/src/api/stream/othello-game.ts @@ -1,7 +1,7 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; 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 Othello from '../../common/othello/core'; import * as maps from '../../common/othello/maps'; @@ -60,14 +60,14 @@ export default function(request: websocket.request, connection: websocket.connec }); 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.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.user2_id.equals(user._id) && game.user2_accepted) return; - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: { settings } @@ -77,7 +77,7 @@ export default function(request: websocket.request, connection: websocket.connec } 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.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 }; - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: set }); @@ -99,7 +99,7 @@ export default function(request: websocket.request, connection: websocket.connec } 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.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 }; - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: set }); @@ -138,14 +138,14 @@ export default function(request: websocket.request, connection: websocket.connec } async function accept(accept: boolean) { - const game = await Game.findOne({ _id: gameId }); + const game = await OthelloGame.findOne({ _id: gameId }); if (game.is_started) return; let bothAccepted = false; if (game.user1_id.equals(user._id)) { - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: { user1_accepted: accept } @@ -158,7 +158,7 @@ export default function(request: websocket.request, connection: websocket.connec if (accept && game.user2_accepted) bothAccepted = true; } else if (game.user2_id.equals(user._id)) { - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: { user2_accepted: accept } @@ -177,7 +177,7 @@ export default function(request: websocket.request, connection: websocket.connec if (bothAccepted) { // 3秒後、まだacceptされていたらゲーム開始 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.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(); - await Game.update({ _id: gameId }, { + await OthelloGame.update({ _id: gameId }, { $set: { started_at: new Date(), is_started: true, @@ -222,7 +222,7 @@ export default function(request: websocket.request, connection: websocket.connec winner = null; } - await Game.update({ + await OthelloGame.update({ _id: gameId }, { $set: { @@ -245,7 +245,7 @@ export default function(request: websocket.request, connection: websocket.connec // 石を打つ 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_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()); - await Game.update({ + await OthelloGame.update({ _id: gameId }, { $set: { @@ -314,7 +314,7 @@ export default function(request: websocket.request, connection: websocket.connec } async function check(crc32) { - const game = await Game.findOne({ _id: gameId }); + const game = await OthelloGame.findOne({ _id: gameId }); if (!game.is_started) return; diff --git a/tools/migration/shell.camel-case.js b/tools/migration/shell.camel-case.js index 9f07bd946..11c6fe401 100644 --- a/tools/migration/shell.camel-case.js +++ b/tools/migration/shell.camel-case.js @@ -1,3 +1,5 @@ +// このスクリプトを走らせる前か後に notifications コレクションはdropしてください + db.access_tokens.renameCollection('accessTokens'); db.accessTokens.update({}, { $rename: { @@ -110,3 +112,21 @@ db.mute.update({}, { } }, 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); +