2018-03-07 03:40:40 +01:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import * as redis from 'redis';
|
2018-03-11 09:23:59 +01:00
|
|
|
import * as CRC32 from 'crc-32';
|
2018-03-29 07:48:47 +02:00
|
|
|
import OthelloGame, { 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-13 20:37:20 +01:00
|
|
|
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user?: any): void {
|
2018-03-07 09:48:32 +01:00
|
|
|
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-10 23:07:17 +01:00
|
|
|
case 'init-form':
|
|
|
|
if (msg.body == null) return;
|
|
|
|
initForm(msg.body);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update-form':
|
|
|
|
if (msg.id == null || msg.value === undefined) return;
|
|
|
|
updateForm(msg.id, msg.value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'message':
|
|
|
|
if (msg.body == null) return;
|
|
|
|
message(msg.body);
|
|
|
|
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-11 09:23:59 +01:00
|
|
|
|
|
|
|
case 'check':
|
|
|
|
if (msg.crc32 == null) return;
|
|
|
|
check(msg.crc32);
|
|
|
|
break;
|
2018-03-07 09:48:32 +01:00
|
|
|
}
|
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
async function updateSettings(settings) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (game.isStarted) return;
|
|
|
|
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
|
|
|
|
if (game.user1Id.equals(user._id) && game.user1Accepted) return;
|
|
|
|
if (game.user2Id.equals(user._id) && game.user2Accepted) return;
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-08 09:57:57 +01:00
|
|
|
$set: {
|
|
|
|
settings
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
});
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
publishOthelloGameStream(gameId, 'update-settings', settings);
|
|
|
|
}
|
|
|
|
|
2018-03-10 23:07:17 +01:00
|
|
|
async function initForm(form) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-10 23:07:17 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (game.isStarted) return;
|
|
|
|
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
|
2018-03-10 23:07:17 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
const set = game.user1Id.equals(user._id) ? {
|
2018-03-10 23:07:17 +01:00
|
|
|
form1: form
|
|
|
|
} : {
|
|
|
|
form2: form
|
|
|
|
};
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-10 23:07:17 +01:00
|
|
|
$set: set
|
|
|
|
});
|
|
|
|
|
|
|
|
publishOthelloGameStream(gameId, 'init-form', {
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id,
|
2018-03-10 23:07:17 +01:00
|
|
|
form
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateForm(id, value) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-10 23:07:17 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (game.isStarted) return;
|
|
|
|
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
|
2018-03-10 23:07:17 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
const form = game.user1Id.equals(user._id) ? game.form2 : game.form1;
|
2018-03-10 23:07:17 +01:00
|
|
|
|
|
|
|
const item = form.find(i => i.id == id);
|
|
|
|
|
|
|
|
if (item == null) return;
|
|
|
|
|
|
|
|
item.value = value;
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
const set = game.user1Id.equals(user._id) ? {
|
2018-03-10 23:07:17 +01:00
|
|
|
form2: form
|
|
|
|
} : {
|
|
|
|
form1: form
|
|
|
|
};
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-10 23:07:17 +01:00
|
|
|
$set: set
|
|
|
|
});
|
|
|
|
|
|
|
|
publishOthelloGameStream(gameId, 'update-form', {
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id,
|
2018-03-10 23:07:17 +01:00
|
|
|
id,
|
|
|
|
value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function message(message) {
|
|
|
|
message.id = Math.random();
|
|
|
|
publishOthelloGameStream(gameId, 'message', {
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id,
|
2018-03-10 23:07:17 +01:00
|
|
|
message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
async function accept(accept: boolean) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-08 09:57:57 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (game.isStarted) return;
|
2018-03-08 09:57:57 +01:00
|
|
|
|
|
|
|
let bothAccepted = false;
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (game.user1Id.equals(user._id)) {
|
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-08 09:57:57 +01:00
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
user1Accepted: accept
|
2018-03-08 09:57:57 +01:00
|
|
|
}
|
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-08 09:57:57 +01:00
|
|
|
publishOthelloGameStream(gameId, 'change-accepts', {
|
|
|
|
user1: accept,
|
2018-03-29 07:48:47 +02:00
|
|
|
user2: game.user2Accepted
|
2018-03-08 09:57:57 +01:00
|
|
|
});
|
2018-03-07 10:45:16 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (accept && game.user2Accepted) bothAccepted = true;
|
|
|
|
} else if (game.user2Id.equals(user._id)) {
|
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-08 09:57:57 +01:00
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
user2Accepted: accept
|
2018-03-08 09:57:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
publishOthelloGameStream(gameId, 'change-accepts', {
|
2018-03-29 07:48:47 +02:00
|
|
|
user1: game.user1Accepted,
|
2018-03-08 09:57:57 +01:00
|
|
|
user2: accept
|
|
|
|
});
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (accept && game.user1Accepted) 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 () => {
|
2018-03-29 07:48:47 +02:00
|
|
|
const freshGame = await OthelloGame.findOne({ _id: gameId });
|
|
|
|
if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return;
|
|
|
|
if (!freshGame.user1Accepted || !freshGame.user2Accepted) 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-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({ _id: gameId }, {
|
2018-03-08 09:57:57 +01:00
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
startedAt: new Date(),
|
|
|
|
isStarted: 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-29 07:48:47 +02:00
|
|
|
isLlotheo: freshGame.settings.isLlotheo,
|
|
|
|
canPutEverywhere: freshGame.settings.canPutEverywhere,
|
|
|
|
loopedBoard: freshGame.settings.loopedBoard
|
2018-03-10 05:07:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (o.isEnded) {
|
|
|
|
let winner;
|
2018-03-12 17:49:54 +01:00
|
|
|
if (o.winner === true) {
|
2018-03-29 07:48:47 +02:00
|
|
|
winner = freshGame.black == 1 ? freshGame.user1Id : freshGame.user2Id;
|
2018-03-12 17:49:54 +01:00
|
|
|
} else if (o.winner === false) {
|
2018-03-29 07:48:47 +02:00
|
|
|
winner = freshGame.black == 1 ? freshGame.user2Id : freshGame.user1Id;
|
2018-03-10 05:07:17 +01:00
|
|
|
} else {
|
|
|
|
winner = null;
|
|
|
|
}
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({
|
2018-03-10 05:07:17 +01:00
|
|
|
_id: gameId
|
|
|
|
}, {
|
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
isEnded: true,
|
|
|
|
winnerId: winner
|
2018-03-10 05:07:17 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
publishOthelloGameStream(gameId, 'ended', {
|
2018-03-29 07:48:47 +02:00
|
|
|
winnerId: winner,
|
2018-03-11 09:26:07 +01:00
|
|
|
game: await pack(gameId, user)
|
2018-03-10 05:07:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-03-11 09:23:59 +01:00
|
|
|
publishOthelloGameStream(gameId, 'started', await pack(gameId, user));
|
2018-03-08 09:57:57 +01:00
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 09:23:59 +01:00
|
|
|
// 石を打つ
|
2018-03-08 09:57:57 +01:00
|
|
|
async function set(pos) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-08 09:57:57 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (!game.isStarted) return;
|
|
|
|
if (game.isEnded) return;
|
|
|
|
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
|
2018-03-08 09:57:57 +01:00
|
|
|
|
|
|
|
const o = new Othello(game.settings.map, {
|
2018-03-29 07:48:47 +02:00
|
|
|
isLlotheo: game.settings.isLlotheo,
|
|
|
|
canPutEverywhere: game.settings.canPutEverywhere,
|
|
|
|
loopedBoard: game.settings.loopedBoard
|
2018-03-08 09:57:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
game.logs.forEach(log => {
|
|
|
|
o.put(log.color, log.pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
const myColor =
|
2018-03-29 07:48:47 +02:00
|
|
|
(game.user1Id.equals(user._id) && game.black == 1) || (game.user2Id.equals(user._id) && game.black == 2)
|
2018-03-12 17:49:54 +01:00
|
|
|
? true
|
|
|
|
: false;
|
2018-03-08 09:57:57 +01:00
|
|
|
|
|
|
|
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) {
|
2018-03-12 17:49:54 +01:00
|
|
|
if (o.winner === true) {
|
2018-03-29 07:48:47 +02:00
|
|
|
winner = game.black == 1 ? game.user1Id : game.user2Id;
|
2018-03-12 17:49:54 +01:00
|
|
|
} else if (o.winner === false) {
|
2018-03-29 07:48:47 +02:00
|
|
|
winner = game.black == 1 ? game.user2Id : game.user1Id;
|
2018-03-08 09:57:57 +01:00
|
|
|
} else {
|
|
|
|
winner = null;
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const log = {
|
|
|
|
at: new Date(),
|
|
|
|
color: myColor,
|
|
|
|
pos
|
|
|
|
};
|
|
|
|
|
2018-03-11 09:23:59 +01:00
|
|
|
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString());
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
await OthelloGame.update({
|
2018-03-07 10:45:16 +01:00
|
|
|
_id: gameId
|
|
|
|
}, {
|
|
|
|
$set: {
|
2018-03-11 09:23:59 +01:00
|
|
|
crc32,
|
2018-03-29 07:48:47 +02:00
|
|
|
isEnded: o.isEnded,
|
|
|
|
winnerId: winner
|
2018-03-07 10:45:16 +01:00
|
|
|
},
|
|
|
|
$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', {
|
2018-03-29 07:48:47 +02:00
|
|
|
winnerId: winner,
|
2018-03-12 20:06:14 +01:00
|
|
|
game: await pack(gameId, user)
|
2018-03-09 14:06:16 +01:00
|
|
|
});
|
|
|
|
}
|
2018-03-07 10:45:16 +01:00
|
|
|
}
|
2018-03-11 09:23:59 +01:00
|
|
|
|
|
|
|
async function check(crc32) {
|
2018-03-29 07:48:47 +02:00
|
|
|
const game = await OthelloGame.findOne({ _id: gameId });
|
2018-03-11 09:23:59 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (!game.isStarted) return;
|
2018-03-11 09:23:59 +01:00
|
|
|
|
|
|
|
// 互換性のため
|
|
|
|
if (game.crc32 == null) return;
|
|
|
|
|
|
|
|
if (crc32 !== game.crc32) {
|
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'rescue',
|
|
|
|
body: await pack(game, user)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 03:40:40 +01:00
|
|
|
}
|