From 161fd4afab323ca6bf491def473f84bb7557b481 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 7 Mar 2018 17:48:32 +0900 Subject: wip --- src/api/stream/othello-game.ts | 63 ++++++++++++++++++++++++++++++++++++-- src/api/stream/othello-matching.ts | 12 -------- src/api/stream/othello.ts | 10 ++++++ 3 files changed, 70 insertions(+), 15 deletions(-) delete mode 100644 src/api/stream/othello-matching.ts create mode 100644 src/api/stream/othello.ts (limited to 'src/api/stream') diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts index ab91ef6422..17cdd3a9e7 100644 --- a/src/api/stream/othello-game.ts +++ b/src/api/stream/othello-game.ts @@ -1,12 +1,69 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; +import Game from '../models/othello-game'; +import { publishOthelloGameStream } from '../event'; +import Othello from '../../common/othello'; -export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient): void { - const game = request.resourceURL.query.game; +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { + const gameId = request.resourceURL.query.game; // Subscribe game stream - subscriber.subscribe(`misskey:othello-game-stream:${game}`); + subscriber.subscribe(`misskey:othello-game-stream:${gameId}`); subscriber.on('message', (_, data) => { connection.send(data); }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'set': + if (msg.pos == null) return; + const pos = msg.pos; + + const game = await Game.findOne({ _id: gameId }); + + const o = new Othello(); + + game.logs.forEach(log => { + o.set(log.color, log.pos); + }); + + const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; + const opColor = myColor == 'black' ? 'white' : 'black'; + + if (!o.canReverse(myColor, pos)) return; + o.set(myColor, pos); + + let turn; + if (o.getPattern(opColor).length > 0) { + turn = myColor == 'black' ? game.white_user_id : game.black_user_id; + } else { + turn = myColor == 'black' ? game.black_user_id : game.white_user_id; + } + + const log = { + at: new Date(), + color: myColor, + pos + }; + + await Game.update({ + _id: gameId + }, { + $set: { + turn_user_id: turn + }, + $push: { + logs: log + } + }); + + publishOthelloGameStream(gameId, 'set', { + color: myColor, + pos + }); + break; + } + }); } diff --git a/src/api/stream/othello-matching.ts b/src/api/stream/othello-matching.ts deleted file mode 100644 index f30ce6eb0a..0000000000 --- a/src/api/stream/othello-matching.ts +++ /dev/null @@ -1,12 +0,0 @@ -import * as websocket from 'websocket'; -import * as redis from 'redis'; - -export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { - const otherparty = request.resourceURL.query.otherparty; - - // Subscribe matching stream - subscriber.subscribe(`misskey:othello-matching:${user._id}-${otherparty}`); - subscriber.on('message', (_, data) => { - connection.send(data); - }); -} diff --git a/src/api/stream/othello.ts b/src/api/stream/othello.ts new file mode 100644 index 0000000000..5056eb535c --- /dev/null +++ b/src/api/stream/othello.ts @@ -0,0 +1,10 @@ +import * as websocket from 'websocket'; +import * as redis from 'redis'; + +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { + // Subscribe othello stream + subscriber.subscribe(`misskey:othello-stream:${user._id}`); + subscriber.on('message', (_, data) => { + connection.send(data); + }); +} -- cgit v1.2.3-freya