From bd79352e8005bd8fe0ffa846e3318bc49b3fdd36 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 11 Mar 2018 18:08:26 +0900 Subject: #1204 --- src/api/endpoints/othello/match.ts | 16 ++++++++++++++-- src/api/models/othello-matching.ts | 2 ++ src/api/stream/othello.ts | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts index 640be9cb57..b73e105ef0 100644 --- a/src/api/endpoints/othello/match.ts +++ b/src/api/endpoints/othello/match.ts @@ -2,7 +2,7 @@ import $ from 'cafy'; 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 publishUserStream, { publishOthelloStream } from '../../event'; import { eighteight } from '../../../common/othello/maps'; module.exports = (params, user) => new Promise(async (res, rej) => { @@ -48,6 +48,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => { res(await packGame(game, user)); publishOthelloStream(exist.parent_id, 'matched', await packGame(game, exist.parent_id)); + + const other = await Matching.count({ + child_id: user._id + }); + + if (other == 0) { + publishUserStream(user._id, 'othello_no_invites'); + } } else { // Fetch child const child = await User.findOne({ @@ -77,7 +85,11 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Reponse res(); + const packed = await packMatching(matching, child); + // 招待 - publishOthelloStream(child._id, 'invited', await packMatching(matching, child)); + publishOthelloStream(child._id, 'invited', packed); + + publishUserStream(child._id, 'othello_invited', packed); } }); diff --git a/src/api/models/othello-matching.ts b/src/api/models/othello-matching.ts index 89fcd6df6a..5cc39cae13 100644 --- a/src/api/models/othello-matching.ts +++ b/src/api/models/othello-matching.ts @@ -32,6 +32,8 @@ export const pack = ( const _matching = deepcopy(matching); + // Rename _id to id + _matching.id = _matching._id; delete _matching._id; // Populate user diff --git a/src/api/stream/othello.ts b/src/api/stream/othello.ts index 5056eb535c..bd3b4a7637 100644 --- a/src/api/stream/othello.ts +++ b/src/api/stream/othello.ts @@ -1,5 +1,8 @@ +import * as mongo from 'mongodb'; import * as websocket from 'websocket'; import * as redis from 'redis'; +import Matching, { pack } from '../models/othello-matching'; +import publishUserStream from '../event'; export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { // Subscribe othello stream @@ -7,4 +10,20 @@ export default function(request: websocket.request, connection: websocket.connec subscriber.on('message', (_, data) => { connection.send(data); }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'ping': + if (msg.id == null) return; + const matching = await Matching.findOne({ + parent_id: user._id, + child_id: new mongo.ObjectID(msg.id) + }); + if (matching == null) return; + publishUserStream(matching.child_id, 'othello_invited', await pack(matching, matching.child_id)); + break; + } + }); } -- cgit v1.2.3-freya