This commit is contained in:
syuilo 2024-01-18 17:43:59 +09:00
parent a78013d0e5
commit 7794e7ae2f
2 changed files with 11 additions and 23 deletions

View file

@ -180,13 +180,10 @@ export interface ReversiGameEventTypes {
key: string; key: string;
value: any; value: any;
}; };
message: { putStone: {
message: string;
};
set: {
pos: number; pos: number;
}; };
check: { syncState: {
crc32: string; crc32: string;
}; };
started: { started: {

View file

@ -4,12 +4,11 @@
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as CRC32 from 'crc-32'; import type { MiReversiGame, ReversiGamesRepository } from '@/models/_.js';
import type { MiReversiGame, MiUser, ReversiGamesRepository } from '@/models/_.js';
import type { Packed } from '@/misc/json-schema.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { ReversiService } from '@/core/ReversiService.js'; import { ReversiService } from '@/core/ReversiService.js';
import { ReversiGameEntityService } from '@/core/entities/ReversiGameEntityService.js';
import Channel, { type MiChannelService } from '../channel.js'; import Channel, { type MiChannelService } from '../channel.js';
class ReversiGameChannel extends Channel { class ReversiGameChannel extends Channel {
@ -21,6 +20,7 @@ class ReversiGameChannel extends Channel {
constructor( constructor(
private reversiService: ReversiService, private reversiService: ReversiService,
private reversiGamesRepository: ReversiGamesRepository, private reversiGamesRepository: ReversiGamesRepository,
private reversiGameEntityService: ReversiGameEntityService,
id: string, id: string,
connection: Channel['connection'], connection: Channel['connection'],
@ -46,9 +46,8 @@ class ReversiGameChannel extends Channel {
case 'accept': this.accept(true); break; case 'accept': this.accept(true); break;
case 'cancelAccept': this.accept(false); break; case 'cancelAccept': this.accept(false); break;
case 'updateSettings': this.updateSettings(body.key, body.value); break; case 'updateSettings': this.updateSettings(body.key, body.value); break;
case 'message': this.message(body); break;
case 'putStone': this.putStone(body.pos); break; case 'putStone': this.putStone(body.pos); break;
case 'check': this.check(body.crc32); break; case 'syncState': this.syncState(body.crc32); break;
} }
} }
@ -63,17 +62,6 @@ class ReversiGameChannel extends Channel {
this.reversiService.updateSettings(game, this.user, key, value); this.reversiService.updateSettings(game, this.user, key, value);
} }
@bindThis
private async message(message: any) {
if (this.user == null) return;
message.id = Math.random();
publishReversiGameStream(this.gameId!, 'message', {
userId: this.user.id,
message,
});
}
@bindThis @bindThis
private async accept(accept: boolean) { private async accept(accept: boolean) {
if (this.user == null) return; if (this.user == null) return;
@ -96,14 +84,15 @@ class ReversiGameChannel extends Channel {
} }
@bindThis @bindThis
private async check(crc32: string | number) { private async syncState(crc32: string | number) {
// TODO: キャッシュしたい
const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! }); const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! });
if (game == null) throw new Error('game not found'); if (game == null) throw new Error('game not found');
if (!game.isStarted) return; if (!game.isStarted) return;
if (crc32.toString() !== game.crc32) { if (crc32.toString() !== game.crc32) {
this.send('rescue', await ReversiGames.pack(game, this.user)); this.send('rescue', await this.reversiGameEntityService.pack(game, this.user));
} }
} }
@ -125,6 +114,7 @@ export class ReversiGameChannelService implements MiChannelService<false> {
private reversiGamesRepository: ReversiGamesRepository, private reversiGamesRepository: ReversiGamesRepository,
private reversiService: ReversiService, private reversiService: ReversiService,
private reversiGameEntityService: ReversiGameEntityService,
) { ) {
} }
@ -133,6 +123,7 @@ export class ReversiGameChannelService implements MiChannelService<false> {
return new ReversiGameChannel( return new ReversiGameChannel(
this.reversiService, this.reversiService,
this.reversiGamesRepository, this.reversiGamesRepository,
this.reversiGameEntityService,
id, id,
connection, connection,
); );