This commit is contained in:
@@ -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, {
|
||||
|
@@ -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
|
||||
|
@@ -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<IGame>('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<any>(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 {
|
||||
|
@@ -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 updateSettings(settings) {
|
||||
const game = await Game.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 }, {
|
||||
$set: {
|
||||
settings
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
});
|
||||
|
||||
publishOthelloGameStream(gameId, 'change-accepts', {
|
||||
user1: accept,
|
||||
user2: game.user2_accepted
|
||||
});
|
||||
|
||||
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 {
|
||||
return;
|
||||
}
|
||||
|
||||
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.black_user_id.equals(user._id) && !game.white_user_id.equals(user._id)) return;
|
||||
if (!game.user1_id.equals(user._id) && !game.user2_id.equals(user._id)) return;
|
||||
|
||||
const o = new Othello();
|
||||
|
||||
game.logs.forEach(log => {
|
||||
o.set(log.color, log.pos);
|
||||
const o = new Othello(game.settings.map, {
|
||||
isLlotheo: game.settings.is_llotheo
|
||||
});
|
||||
|
||||
const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white';
|
||||
const opColor = myColor == 'black' ? 'white' : 'black';
|
||||
game.logs.forEach(log => {
|
||||
o.put(log.color, log.pos);
|
||||
});
|
||||
|
||||
if (!o.canReverse(myColor, pos)) return;
|
||||
o.set(myColor, pos);
|
||||
const myColor =
|
||||
(game.user1_id.equals(user._id) && game.black == 1) || (game.user2_id.equals(user._id) && game.black == 2)
|
||||
? 'black'
|
||||
: 'white';
|
||||
|
||||
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;
|
||||
} else {
|
||||
turn = null;
|
||||
}
|
||||
|
||||
const isEnded = turn === null;
|
||||
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: {
|
||||
|
Reference in New Issue
Block a user