summaryrefslogtreecommitdiff
path: root/src/server/api/stream/channels/games/reversi.ts
blob: 0498e5e0177d9476df21440c4b818c63db5a5ff4 (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
31
32
33
import autobind from 'autobind-decorator';
import { publishMainStream } from '../../../../../services/stream';
import Channel from '../../channel';
import { ReversiMatchings } from '../../../../../models';

export default class extends Channel {
	public readonly chName = 'gamesReversi';
	public static shouldShare = true;
	public static requireCredential = true;

	@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 ReversiMatchings.findOne({
					parentId: this.user.id,
					childId: body.id
				});
				if (matching == null) return;
				publishMainStream(matching.childId, 'reversiInvited', await ReversiMatchings.pack(matching, matching.childId));
				break;
		}
	}
}