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

View file

@ -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
});

View file

@ -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');

View file

@ -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,

View file

@ -3,17 +3,17 @@ import deepcopy = require('deepcopy');
import db from '../../db/mongodb';
import { IUser, pack as packUser } from './user';
const Game = db.get<IGame>('othello_games');
export default Game;
const OthelloGame = db.get<IOthelloGame>('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 {

View file

@ -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;

View file

@ -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);