enhance(reversi): tweak reversi

This commit is contained in:
syuilo
2024-01-20 21:23:33 +09:00
parent 7d57487026
commit fcd7ffe956
6 changed files with 79 additions and 13 deletions

View File

@@ -181,8 +181,8 @@ export interface ReversiGameEventTypes {
value: any;
};
log: Reversi.Serializer.Log & { id: string | null };
syncState: {
crc32: string;
heatbeat: {
userId: MiUser['id'];
};
started: {
game: Packed<'ReversiGameDetailed'>;

View File

@@ -405,6 +405,11 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
return this.reversiGamesRepository.findOneBy({ id });
}
@bindThis
public async heatbeat(game: MiReversiGame, user: MiUser) {
this.globalEventService.publishReversiGameStream(game.id, 'heatbeat', { userId: user.id });
}
@bindThis
public dispose(): void {
}

View File

@@ -46,7 +46,7 @@ class ReversiGameChannel extends Channel {
case 'ready': this.ready(body); break;
case 'updateSettings': this.updateSettings(body.key, body.value); break;
case 'putStone': this.putStone(body.pos, body.id); break;
case 'syncState': this.syncState(body.crc32); break;
case 'heatbeat': this.heatbeat(body.crc32); break;
}
}
@@ -83,15 +83,21 @@ class ReversiGameChannel extends Channel {
}
@bindThis
private async syncState(crc32: string | number) {
private async heatbeat(crc32?: string | number | null) {
// TODO: キャッシュしたい
const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! });
if (game == null) throw new Error('game not found');
if (!game.isStarted) return;
if (crc32.toString() !== game.crc32) {
this.send('rescue', await this.reversiGameEntityService.packDetail(game, this.user));
if (crc32 != null) {
if (crc32.toString() !== game.crc32) {
this.send('rescue', await this.reversiGameEntityService.packDetail(game, this.user));
}
}
if (this.user && (game.user1Id === this.user.id || game.user2Id === this.user.id)) {
this.reversiService.heatbeat(game, this.user);
}
}