2018-03-11 11:08:26 +02:00
|
|
|
import * as mongo from 'mongodb';
|
2018-03-07 04:40:40 +02:00
|
|
|
import * as websocket from 'websocket';
|
2018-07-30 01:20:27 +03:00
|
|
|
import Xev from 'xev';
|
2018-07-09 15:15:49 +03:00
|
|
|
import Matching, { pack } from '../../../../models/games/reversi/matching';
|
2018-07-30 01:20:27 +03:00
|
|
|
import { publishUserStream } from '../../../../stream';
|
2018-03-07 04:40:40 +02:00
|
|
|
|
2018-07-30 01:20:27 +03:00
|
|
|
export default function(request: websocket.request, connection: websocket.connection, subscriber: Xev, user: any): void {
|
2018-06-17 02:10:54 +03:00
|
|
|
// Subscribe reversi stream
|
2018-07-30 01:20:27 +03:00
|
|
|
subscriber.on(`reversi-stream:${user._id}`, data => {
|
|
|
|
connection.send(JSON.stringify(data));
|
2018-03-07 04:40:40 +02:00
|
|
|
});
|
2018-03-11 11:08:26 +02:00
|
|
|
|
|
|
|
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({
|
2018-03-29 08:48:47 +03:00
|
|
|
parentId: user._id,
|
|
|
|
childId: new mongo.ObjectID(msg.id)
|
2018-03-11 11:08:26 +02:00
|
|
|
});
|
|
|
|
if (matching == null) return;
|
2018-06-17 02:10:54 +03:00
|
|
|
publishUserStream(matching.childId, 'reversi_invited', await pack(matching, matching.childId));
|
2018-03-11 11:08:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-03-07 04:40:40 +02:00
|
|
|
}
|