mizzkey/packages/backend/src/server/api/stream/channels/mahjong-room.ts

161 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-01-26 14:25:00 +09:00
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { MahjongService } from '@/core/MahjongService.js';
2024-01-29 17:15:09 +09:00
import { GlobalEvents } from '@/core/GlobalEventService.js';
2024-01-26 14:25:00 +09:00
import Channel, { type MiChannelService } from '../channel.js';
class MahjongRoomChannel extends Channel {
public readonly chName = 'mahjongRoom';
public static shouldShare = false;
public static requireCredential = true as const;
public static kind = 'read:account';
private roomId: string | null = null;
constructor(
private mahjongService: MahjongService,
id: string,
connection: Channel['connection'],
) {
super(id, connection);
}
@bindThis
public async init(params: any) {
this.roomId = params.roomId as string;
2024-01-29 17:15:09 +09:00
this.subscriber.on(`mahjongRoomStream:${this.roomId}`, this.onMahjongRoomStreamMessage);
}
@bindThis
private async onMahjongRoomStreamMessage(message: GlobalEvents['mahjongRoom']['payload']) {
if (message.type === 'started') {
const packed = await this.mahjongService.packRoom(message.body.room, this.user!);
this.send('started', {
room: packed,
});
} else {
this.send(message.type, message.body);
}
2024-01-26 14:25:00 +09:00
}
@bindThis
public onMessage(type: string, body: any) {
switch (type) {
case 'ready': this.ready(body); break;
case 'updateSettings': this.updateSettings(body.key, body.value); break;
case 'addAi': this.addAi(); break;
2024-01-29 17:15:09 +09:00
case 'confirmNextKyoku': this.confirmNextKyoku(); break;
2024-01-28 20:20:18 +09:00
case 'dahai': this.dahai(body.tile, body.riichi); break;
2024-01-29 14:14:00 +09:00
case 'hora': this.hora(); break;
2024-01-28 17:31:32 +09:00
case 'ron': this.ron(); break;
2024-01-27 17:50:41 +09:00
case 'pon': this.pon(); break;
case 'nop': this.nop(); break;
2024-01-26 14:25:00 +09:00
case 'claimTimeIsUp': this.claimTimeIsUp(); break;
}
}
@bindThis
private async updateSettings(key: string, value: any) {
if (this.user == null) return;
this.mahjongService.updateSettings(this.roomId!, this.user, key, value);
}
@bindThis
private async ready(ready: boolean) {
if (this.user == null) return;
this.mahjongService.changeReadyState(this.roomId!, this.user, ready);
}
2024-01-29 17:15:09 +09:00
@bindThis
private async confirmNextKyoku() {
if (this.user == null) return;
this.mahjongService.confirmNextKyoku(this.roomId!, this.user);
}
2024-01-26 14:25:00 +09:00
@bindThis
private async addAi() {
if (this.user == null) return;
this.mahjongService.addAi(this.roomId!, this.user);
}
@bindThis
2024-01-28 20:20:18 +09:00
private async dahai(tile: string, riichi = false) {
2024-01-26 14:25:00 +09:00
if (this.user == null) return;
2024-01-29 10:46:23 +09:00
this.mahjongService.commit_dahai(this.roomId!, this.user, tile, riichi);
2024-01-27 17:50:41 +09:00
}
2024-01-29 14:14:00 +09:00
@bindThis
private async hora() {
if (this.user == null) return;
this.mahjongService.commit_hora(this.roomId!, this.user);
}
2024-01-28 17:31:32 +09:00
@bindThis
private async ron() {
if (this.user == null) return;
2024-01-29 10:46:23 +09:00
this.mahjongService.commit_ron(this.roomId!, this.user);
2024-01-28 17:31:32 +09:00
}
2024-01-27 17:50:41 +09:00
@bindThis
private async pon() {
if (this.user == null) return;
2024-01-29 10:46:23 +09:00
this.mahjongService.commit_pon(this.roomId!, this.user);
2024-01-27 17:50:41 +09:00
}
@bindThis
private async nop() {
if (this.user == null) return;
2024-01-29 10:46:23 +09:00
this.mahjongService.commit_nop(this.roomId!, this.user);
2024-01-26 14:25:00 +09:00
}
@bindThis
private async claimTimeIsUp() {
if (this.user == null) return;
this.mahjongService.checkTimeout(this.roomId!);
}
@bindThis
public dispose() {
// Unsubscribe events
2024-01-29 17:15:09 +09:00
this.subscriber.off(`mahjongRoomStream:${this.roomId}`, this.onMahjongRoomStreamMessage);
2024-01-26 14:25:00 +09:00
}
}
@Injectable()
export class MahjongRoomChannelService implements MiChannelService<true> {
public readonly shouldShare = MahjongRoomChannel.shouldShare;
public readonly requireCredential = MahjongRoomChannel.requireCredential;
public readonly kind = MahjongRoomChannel.kind;
constructor(
private mahjongService: MahjongService,
) {
}
@bindThis
public create(id: string, connection: Channel['connection']): MahjongRoomChannel {
return new MahjongRoomChannel(
this.mahjongService,
id,
connection,
);
}
}