summaryrefslogtreecommitdiff
path: root/src/server/api/stream/channels/games/reversi.ts
blob: d75025c944d16433d3547cc4852ac5711e37d8de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import autobind from 'autobind-decorator';
import * as mongo from 'mongodb';
import Matching, { pack } from '../../../../../models/games/reversi/matching';
import { publishMainStream } from '../../../../../stream';
import Channel from '../../channel';

export default class extends Channel {
	@autobind
	public async init(params: any) {
		// Subscribe reversi stream
		this.subscriber.on(`reversiStream:${this.user._id}`, data => {
			this.send(data);
		});
	}

	@autobind
	public async onMessage(type: string, body: any) {
		switch (type) {
			case 'ping':
				if (body.id == null) return;
				const matching = await Matching.findOne({
					parentId: this.user._id,
					childId: new mongo.ObjectID(body.id)
				});
				if (matching == null) return;
				publishMainStream(matching.childId, 'reversiInvited', await pack(matching, matching.childId));
				break;
		}
	}
}