From 155012846d8f142515390adb2754b45cdfbb74ef Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 8 Mar 2018 17:57:57 +0900 Subject: #1200 --- src/api/endpoints/othello/games.ts | 9 ++- src/api/endpoints/othello/match.ts | 20 ++++-- src/api/models/othello-game.ts | 43 ++++++++++-- src/api/stream/othello-game.ts | 140 ++++++++++++++++++++++++++++++------- 4 files changed, 171 insertions(+), 41 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts index 39963fcd29..dd9fb5ef52 100644 --- a/src/api/endpoints/othello/games.ts +++ b/src/api/endpoints/othello/games.ts @@ -7,12 +7,15 @@ module.exports = (params, user) => new Promise(async (res, rej) => { if (myErr) return rej('invalid my param'); const q = my ? { + is_started: true, $or: [{ - black_user_id: user._id + user1_id: user._id }, { - white_user_id: user._id + user2_id: user._id }] - } : {}; + } : { + is_started: true + }; // Fetch games const games = await Game.find(q, { diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts index cb094bbc65..05b87a541a 100644 --- a/src/api/endpoints/othello/match.ts +++ b/src/api/endpoints/othello/match.ts @@ -3,6 +3,7 @@ import Matching, { pack as packMatching } from '../../models/othello-matching'; import Game, { pack as packGame } from '../../models/othello-game'; import User from '../../models/user'; import { publishOthelloStream } from '../../event'; +import { eighteight } from '../../../common/othello/maps'; module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'user_id' parameter @@ -26,16 +27,21 @@ module.exports = (params, user) => new Promise(async (res, rej) => { _id: exist._id }); - const parentIsBlack = Math.random() > 0.5; - - // Start game + // Create game const game = await Game.insert({ created_at: new Date(), - black_user_id: parentIsBlack ? exist.parent_id : user._id, - white_user_id: parentIsBlack ? user._id : exist.parent_id, - turn_user_id: parentIsBlack ? exist.parent_id : user._id, + user1_id: exist.parent_id, + user2_id: user._id, + user1_accepted: false, + user2_accepted: false, + is_started: false, is_ended: false, - logs: [] + logs: [], + settings: { + map: eighteight, + bw: 'random', + is_llotheo: false + } }); // Reponse diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts index 73a5c94b21..de7c804c46 100644 --- a/src/api/models/othello-game.ts +++ b/src/api/models/othello-game.ts @@ -2,6 +2,7 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; import { IUser, pack as packUser } from './user'; +import { Map } from '../../common/othello/maps'; const Game = db.get('othello_games'); export default Game; @@ -9,12 +10,28 @@ export default Game; export interface IGame { _id: mongo.ObjectID; created_at: Date; - black_user_id: mongo.ObjectID; - white_user_id: mongo.ObjectID; - turn_user_id: mongo.ObjectID; + started_at: Date; + user1_id: mongo.ObjectID; + user2_id: mongo.ObjectID; + user1_accepted: boolean; + user2_accepted: boolean; + + /** + * どちらのプレイヤーが先行(黒)か + * 1 ... user1 + * 2 ... user2 + */ + black: number; + + is_started: boolean; is_ended: boolean; winner_id: mongo.ObjectID; logs: any[]; + settings: { + map: Map; + bw: string | number; + is_llotheo: boolean; + }; } /** @@ -24,6 +41,20 @@ export const pack = ( game: any, me?: string | mongo.ObjectID | IUser ) => new Promise(async (resolve, reject) => { + let _game: any; + + // Populate the game if 'game' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(game)) { + _game = await Game.findOne({ + _id: game + }); + } else if (typeof game === 'string') { + _game = await Game.findOne({ + _id: new mongo.ObjectID(game) + }); + } else { + _game = deepcopy(game); + } // Me const meId: mongo.ObjectID = me @@ -34,15 +65,13 @@ export const pack = ( : (me as IUser)._id : null; - const _game = deepcopy(game); - // Rename _id to id _game.id = _game._id; delete _game._id; // Populate user - _game.black_user = await packUser(_game.black_user_id, meId); - _game.white_user = await packUser(_game.white_user_id, meId); + _game.user1 = await packUser(_game.user1_id, meId); + _game.user2 = await packUser(_game.user2_id, meId); if (_game.winner_id) { _game.winner = await packUser(_game.winner_id, meId); } else { diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts index d086478159..1dcd37efa1 100644 --- a/src/api/stream/othello-game.ts +++ b/src/api/stream/othello-game.ts @@ -1,8 +1,8 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; -import Game from '../models/othello-game'; +import Game, { pack } from '../models/othello-game'; import { publishOthelloGameStream } from '../event'; -import Othello from '../../common/othello'; +import Othello from '../../common/othello/core'; export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { const gameId = request.resourceURL.query.game; @@ -17,6 +17,19 @@ export default function(request: websocket.request, connection: websocket.connec const msg = JSON.parse(data.utf8Data); switch (msg.type) { + case 'accept': + accept(true); + break; + + case 'cancel-accept': + accept(false); + break; + + case 'update-settings': + if (msg.settings == null) return; + updateSettings(msg.settings); + break; + case 'set': if (msg.pos == null) return; set(msg.pos); @@ -24,38 +37,118 @@ export default function(request: websocket.request, connection: websocket.connec } }); - async function set(pos) { + async function updateSettings(settings) { const game = await Game.findOne({ _id: gameId }); - if (game.is_ended) return; - if (!game.black_user_id.equals(user._id) && !game.white_user_id.equals(user._id)) return; - - const o = new Othello(); + 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; - game.logs.forEach(log => { - o.set(log.color, log.pos); + await Game.update({ _id: gameId }, { + $set: { + settings + } }); - const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; - const opColor = myColor == 'black' ? 'white' : 'black'; + publishOthelloGameStream(gameId, 'update-settings', settings); + } + + async function accept(accept: boolean) { + const game = await Game.findOne({ _id: gameId }); + + if (game.is_started) return; + + let bothAccepted = false; + + if (game.user1_id.equals(user._id)) { + await Game.update({ _id: gameId }, { + $set: { + user1_accepted: accept + } + }); - if (!o.canReverse(myColor, pos)) return; - o.set(myColor, pos); + publishOthelloGameStream(gameId, 'change-accepts', { + user1: accept, + user2: game.user2_accepted + }); - let turn; - if (o.getPattern(opColor).length > 0) { - turn = myColor == 'black' ? game.white_user_id : game.black_user_id; - } else if (o.getPattern(myColor).length > 0) { - turn = myColor == 'black' ? game.black_user_id : game.white_user_id; + if (accept && game.user2_accepted) bothAccepted = true; + } else if (game.user2_id.equals(user._id)) { + await Game.update({ _id: gameId }, { + $set: { + user2_accepted: accept + } + }); + + publishOthelloGameStream(gameId, 'change-accepts', { + user1: game.user1_accepted, + user2: accept + }); + + if (accept && game.user1_accepted) bothAccepted = true; } else { - turn = null; + return; } - const isEnded = turn === null; + if (bothAccepted) { + // 3秒後、まだacceptされていたらゲーム開始 + setTimeout(async () => { + const freshGame = await Game.findOne({ _id: gameId }); + if (freshGame == null || freshGame.is_started || freshGame.is_ended) return; + + let bw: number; + if (freshGame.settings.bw == 'random') { + bw = Math.random() > 0.5 ? 1 : 2; + } else { + bw = freshGame.settings.bw as number; + } + + await Game.update({ _id: gameId }, { + $set: { + started_at: new Date(), + is_started: true, + black: bw + } + }); + + publishOthelloGameStream(gameId, 'started', await pack(gameId)); + }, 3000); + } + } + + async function set(pos) { + const game = await Game.findOne({ _id: gameId }); + + if (!game.is_started) return; + if (game.is_ended) return; + if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return; + + const o = new Othello(game.settings.map, { + isLlotheo: game.settings.is_llotheo + }); + + game.logs.forEach(log => { + o.put(log.color, log.pos); + }); + + const myColor = + (game.user1_id.equals(user._id) && game.black == 1) || (game.user2_id.equals(user._id) && game.black == 2) + ? 'black' + : 'white'; + + if (!o.canPut(myColor, pos)) return; + o.put(myColor, pos); let winner; - if (isEnded) { - winner = o.blackCount == o.whiteCount ? null : o.blackCount > o.whiteCount ? game.black_user_id : game.white_user_id; + if (o.isEnded) { + if (o.winner == 'black') { + winner = game.black == 1 ? game.user1_id : game.user2_id; + } else if (o.winner == 'white') { + winner = game.black == 1 ? game.user2_id : game.user1_id; + } else { + winner = null; + } } const log = { @@ -68,8 +161,7 @@ export default function(request: websocket.request, connection: websocket.connec _id: gameId }, { $set: { - turn_user_id: turn, - is_ended: isEnded, + is_ended: o.isEnded, winner_id: winner }, $push: { -- cgit v1.2.3-freya