Sharkey/packages/backend/src/server/api/stream/channels/reversi-game.ts

129 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-01-19 12:51:49 +01:00
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
2024-01-19 12:51:49 +01:00
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
import type { MiReversiGame } from '@/models/_.js';
2024-01-19 12:51:49 +01:00
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { ReversiService } from '@/core/ReversiService.js';
import { ReversiGameEntityService } from '@/core/entities/ReversiGameEntityService.js';
import type { JsonObject, JsonValue } from '@/misc/json-value.js';
2024-01-19 12:51:49 +01:00
import Channel, { type MiChannelService } from '../channel.js';
class ReversiGameChannel extends Channel {
public readonly chName = 'reversiGame';
public static shouldShare = false;
public static requireCredential = false as const;
private gameId: MiReversiGame['id'] | null = null;
constructor(
private reversiService: ReversiService,
private reversiGameEntityService: ReversiGameEntityService,
id: string,
connection: Channel['connection'],
) {
super(id, connection);
}
@bindThis
public async init(params: JsonObject) {
if (typeof params.gameId !== 'string') return;
this.gameId = params.gameId;
2024-01-19 12:51:49 +01:00
this.subscriber.on(`reversiGameStream:${this.gameId}`, this.send);
}
@bindThis
public onMessage(type: string, body: JsonValue) {
2024-01-19 12:51:49 +01:00
switch (type) {
case 'ready':
if (typeof body !== 'boolean') return;
this.ready(body);
break;
case 'updateSettings':
if (typeof body !== 'object' || body === null || Array.isArray(body)) return;
if (typeof body.key !== 'string') return;
if (typeof body.value !== 'object' || body.value === null || Array.isArray(body.value)) return;
this.updateSettings(body.key, body.value);
break;
case 'cancel':
this.cancelGame();
break;
case 'putStone':
if (typeof body !== 'object' || body === null || Array.isArray(body)) return;
if (typeof body.pos !== 'number') return;
if (typeof body.id !== 'string') return;
this.putStone(body.pos, body.id);
break;
2024-01-21 02:07:43 +01:00
case 'claimTimeIsUp': this.claimTimeIsUp(); break;
2024-01-19 12:51:49 +01:00
}
}
@bindThis
private async updateSettings(key: string, value: JsonObject) {
2024-01-19 12:51:49 +01:00
if (this.user == null) return;
2024-01-21 02:07:43 +01:00
this.reversiService.updateSettings(this.gameId!, this.user, key, value);
2024-01-19 12:51:49 +01:00
}
@bindThis
private async ready(ready: boolean) {
if (this.user == null) return;
2024-01-21 02:07:43 +01:00
this.reversiService.gameReady(this.gameId!, this.user, ready);
2024-01-19 12:51:49 +01:00
}
2024-01-21 04:05:51 +01:00
@bindThis
private async cancelGame() {
if (this.user == null) return;
this.reversiService.cancelGame(this.gameId!, this.user);
}
2024-01-19 12:51:49 +01:00
@bindThis
2024-01-20 05:14:46 +01:00
private async putStone(pos: number, id: string) {
2024-01-19 12:51:49 +01:00
if (this.user == null) return;
2024-01-21 02:07:43 +01:00
this.reversiService.putStoneToGame(this.gameId!, this.user, pos, id);
2024-01-19 12:51:49 +01:00
}
2024-01-21 02:07:43 +01:00
@bindThis
private async claimTimeIsUp() {
if (this.user == null) return;
this.reversiService.checkTimeout(this.gameId!);
2024-01-19 12:51:49 +01:00
}
@bindThis
public dispose() {
// Unsubscribe events
this.subscriber.off(`reversiGameStream:${this.gameId}`, this.send);
}
}
@Injectable()
export class ReversiGameChannelService implements MiChannelService<false> {
public readonly shouldShare = ReversiGameChannel.shouldShare;
public readonly requireCredential = ReversiGameChannel.requireCredential;
public readonly kind = ReversiGameChannel.kind;
constructor(
private reversiService: ReversiService,
private reversiGameEntityService: ReversiGameEntityService,
) {
}
@bindThis
public create(id: string, connection: Channel['connection']): ReversiGameChannel {
return new ReversiGameChannel(
this.reversiService,
this.reversiGameEntityService,
id,
connection,
);
}
}