diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-03-07 19:00:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-07 19:00:15 +0900 |
| commit | a227ef30e8bc8282605361b013d323bb3bbc4e7d (patch) | |
| tree | 8bd28f1d27a77e061e7b7f56f8794e483fe82f8f /src/api/stream | |
| parent | v3999 (diff) | |
| parent | oops (diff) | |
| download | sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.tar.gz sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.tar.bz2 sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.zip | |
Merge pull request #1197 from syuilo/othello
Othello
Diffstat (limited to 'src/api/stream')
| -rw-r--r-- | src/api/stream/messaging.ts | 2 | ||||
| -rw-r--r-- | src/api/stream/othello-game.ts | 86 | ||||
| -rw-r--r-- | src/api/stream/othello.ts | 10 | ||||
| -rw-r--r-- | src/api/stream/requests.ts | 2 | ||||
| -rw-r--r-- | src/api/stream/server.ts | 2 |
5 files changed, 99 insertions, 3 deletions
diff --git a/src/api/stream/messaging.ts b/src/api/stream/messaging.ts index 3f505cfafa..a4a12426a3 100644 --- a/src/api/stream/messaging.ts +++ b/src/api/stream/messaging.ts @@ -2,7 +2,7 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; import read from '../common/read-messaging-message'; -export default function messagingStream(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { const otherparty = request.resourceURL.query.otherparty; // Subscribe messaging stream diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts new file mode 100644 index 0000000000..59d964777d --- /dev/null +++ b/src/api/stream/othello-game.ts @@ -0,0 +1,86 @@ +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, user: any): void { + const gameId = request.resourceURL.query.game; + + // Subscribe game stream + 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; + set(msg.pos); + break; + } + }); + + async function set(pos) { + const game = await Game.findOne({ _id: gameId }); + + if (game.is_ended) return; + + 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 if (o.getPattern(myColor).length > 0) { + turn = myColor == 'black' ? game.black_user_id : game.white_user_id; + } else { + turn = null; + } + + const isEnded = turn === null; + + let winner; + if (isEnded) { + const blackCount = o.board.filter(s => s == 'black').length; + const whiteCount = o.board.filter(s => s == 'white').length; + winner = blackCount == whiteCount ? null : blackCount > whiteCount ? 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, + is_ended: isEnded, + winner_id: winner + }, + $push: { + logs: log + } + }); + + publishOthelloGameStream(gameId, 'set', { + color: myColor, + pos + }); + } +} 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); + }); +} diff --git a/src/api/stream/requests.ts b/src/api/stream/requests.ts index 2c36e58b6e..d7bb5e6c5c 100644 --- a/src/api/stream/requests.ts +++ b/src/api/stream/requests.ts @@ -3,7 +3,7 @@ import Xev from 'xev'; const ev = new Xev(); -export default function homeStream(request: websocket.request, connection: websocket.connection): void { +export default function(request: websocket.request, connection: websocket.connection): void { const onRequest = request => { connection.send(JSON.stringify({ type: 'request', diff --git a/src/api/stream/server.ts b/src/api/stream/server.ts index 0db6643d40..4ca2ad1b10 100644 --- a/src/api/stream/server.ts +++ b/src/api/stream/server.ts @@ -3,7 +3,7 @@ import Xev from 'xev'; const ev = new Xev(); -export default function homeStream(request: websocket.request, connection: websocket.connection): void { +export default function(request: websocket.request, connection: websocket.connection): void { const onStats = stats => { connection.send(JSON.stringify({ type: 'stats', |