2018-03-07 03:40:40 +01:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import * as redis from 'redis';
|
2018-03-08 09:57:57 +01:00
|
|
|
import Game, { pack } from '../models/othello-game';
|
2018-03-07 09:48:32 +01:00
|
|
|
import { publishOthelloGameStream } from '../event';
|
2018-03-08 09:57:57 +01:00
|
|
|
import Othello from '../../common/othello/core';
|
2018-03-10 05:42:26 +01:00
|
|
|
import * as maps from '../../common/othello/maps';
|
2018-03-07 03:40:40 +01:00
|
|
|
|
2018-03-07 09:48:32 +01:00
|
|
|
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
|
|
|
|
const gameId = request.resourceURL.query.game;
|
2018-03-07 03:40:40 +01:00
|
|
|
|
|
|
|
// Subscribe game stream
|
2018-03-07 09:48:32 +01:00
|
|
|
subscriber.subscribe(`misskey:othello-game-stream:${gameId}`);
|
2018-03-07 03:40:40 +01:00
|
|
|
subscriber.on('message', (_, data) => {
|
|
|
|
connection.send(data);
|
|
|
|
});
|
2018-03-07 09:48:32 +01:00
|
|
|
|
|
|
|
connection.on('message', async (data) => {
|
|
|
|
const msg = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (msg.type) {
|
2018-03-08 09:57:57 +01:00
|
|
|
case 'accept':
|
|
|
|
accept(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'cancel-accept':
|
|
|
|
accept(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update-settings':
|
|
|
|
if (msg.settings == null) return;
|
|
|
|
updateSettings(msg.settings);
|
|
|
|
break;
|
|
|
|
|
2018-03-07 09:48:32 +01:00
|
|
|
case 'set':
|
|
|
|
if (msg.pos == null) return;
|
2018-03-07 10:45:16 +01:00
|
|
|
set(msg.pos);
|
2018-03-07 09:48:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
async function updateSettings(settings) {
|
2018-03-07 10:45:16 +01:00
|
|
|
const game = await Game.findOne({ _id: gameId });
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
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;
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
await Game.update({ _id: gameId }, {
|
|
|
|
$set: {
|
|
|
|
settings
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
});
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
publishOthelloGameStream(gameId, 'change-accepts', {
|
|
|
|
user1: accept,
|
|
|
|
user2: game.user2_accepted
|
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
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;
|
2018-03-07 10:45:16 +01:00
|
|
|
} else {
|
2018-03-08 09:57:57 +01:00
|
|
|
return;
|
2018-03-07 10:45:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
if (bothAccepted) {
|
|
|
|
// 3秒後、まだacceptされていたらゲーム開始
|
|
|
|
setTimeout(async () => {
|
|
|
|
const freshGame = await Game.findOne({ _id: gameId });
|
|
|
|
if (freshGame == null || freshGame.is_started || freshGame.is_ended) return;
|
2018-03-08 10:30:28 +01:00
|
|
|
if (!freshGame.user1_accepted || !freshGame.user2_accepted) return;
|
2018-03-08 09:57:57 +01:00
|
|
|
|
|
|
|
let bw: number;
|
|
|
|
if (freshGame.settings.bw == 'random') {
|
|
|
|
bw = Math.random() > 0.5 ? 1 : 2;
|
|
|
|
} else {
|
|
|
|
bw = freshGame.settings.bw as number;
|
|
|
|
}
|
|
|
|
|
2018-03-10 05:42:26 +01:00
|
|
|
function getRandomMap() {
|
|
|
|
const mapCount = Object.entries(maps).length;
|
|
|
|
const rnd = Math.floor(Math.random() * mapCount);
|
|
|
|
return Object.entries(maps).find((x, i) => i == rnd)[1].data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap();
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
await Game.update({ _id: gameId }, {
|
|
|
|
$set: {
|
|
|
|
started_at: new Date(),
|
|
|
|
is_started: true,
|
2018-03-10 05:42:26 +01:00
|
|
|
black: bw,
|
|
|
|
'settings.map': map
|
2018-03-08 09:57:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-10 05:07:17 +01:00
|
|
|
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
|
2018-03-10 05:42:26 +01:00
|
|
|
const o = new Othello(map, {
|
2018-03-10 10:22:54 +01:00
|
|
|
isLlotheo: freshGame.settings.is_llotheo,
|
|
|
|
canPutEverywhere: freshGame.settings.can_put_everywhere
|
2018-03-10 05:07:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (o.isEnded) {
|
|
|
|
let winner;
|
|
|
|
if (o.winner == 'black') {
|
|
|
|
winner = freshGame.black == 1 ? freshGame.user1_id : freshGame.user2_id;
|
|
|
|
} else if (o.winner == 'white') {
|
|
|
|
winner = freshGame.black == 1 ? freshGame.user2_id : freshGame.user1_id;
|
|
|
|
} else {
|
|
|
|
winner = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Game.update({
|
|
|
|
_id: gameId
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
is_ended: true,
|
|
|
|
winner_id: winner
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
publishOthelloGameStream(gameId, 'ended', {
|
|
|
|
winner_id: winner
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
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, {
|
2018-03-10 10:22:54 +01:00
|
|
|
isLlotheo: game.settings.is_llotheo,
|
|
|
|
canPutEverywhere: game.settings.can_put_everywhere
|
2018-03-08 09:57:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2018-03-07 10:45:16 +01:00
|
|
|
|
|
|
|
let winner;
|
2018-03-08 09:57:57 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const log = {
|
|
|
|
at: new Date(),
|
|
|
|
color: myColor,
|
|
|
|
pos
|
|
|
|
};
|
|
|
|
|
|
|
|
await Game.update({
|
|
|
|
_id: gameId
|
|
|
|
}, {
|
|
|
|
$set: {
|
2018-03-08 09:57:57 +01:00
|
|
|
is_ended: o.isEnded,
|
2018-03-07 10:45:16 +01:00
|
|
|
winner_id: winner
|
|
|
|
},
|
|
|
|
$push: {
|
|
|
|
logs: log
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-09 14:30:02 +01:00
|
|
|
publishOthelloGameStream(gameId, 'set', Object.assign(log, {
|
|
|
|
next: o.turn
|
|
|
|
}));
|
2018-03-09 14:06:16 +01:00
|
|
|
|
|
|
|
if (o.isEnded) {
|
|
|
|
publishOthelloGameStream(gameId, 'ended', {
|
|
|
|
winner_id: winner
|
|
|
|
});
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
}
|
2018-03-07 03:40:40 +01:00
|
|
|
}
|