enhance(reversi): tweak reversi

This commit is contained in:
syuilo
2024-01-21 10:07:43 +09:00
parent 576484835e
commit a17251d913
19 changed files with 395 additions and 135 deletions

View File

@@ -32,11 +32,6 @@ class ReversiGameChannel extends Channel {
public async init(params: any) {
this.gameId = params.gameId as string;
const game = await this.reversiGamesRepository.findOneBy({
id: this.gameId,
});
if (game == null) return;
this.subscriber.on(`reversiGameStream:${this.gameId}`, this.send);
}
@@ -46,7 +41,8 @@ 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 'heatbeat': this.heatbeat(body.crc32); break;
case 'checkState': this.checkState(body.crc32); break;
case 'claimTimeIsUp': this.claimTimeIsUp(); break;
}
}
@@ -54,51 +50,38 @@ class ReversiGameChannel extends Channel {
private async updateSettings(key: string, value: any) {
if (this.user == null) return;
// TODO: キャッシュしたい
const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! });
if (game == null) throw new Error('game not found');
this.reversiService.updateSettings(game, this.user, key, value);
this.reversiService.updateSettings(this.gameId!, this.user, key, value);
}
@bindThis
private async ready(ready: boolean) {
if (this.user == null) return;
const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! });
if (game == null) throw new Error('game not found');
this.reversiService.gameReady(game, this.user, ready);
this.reversiService.gameReady(this.gameId!, this.user, ready);
}
@bindThis
private async putStone(pos: number, id: string) {
if (this.user == null) return;
// TODO: キャッシュしたい
const game = await this.reversiGamesRepository.findOneBy({ id: this.gameId! });
if (game == null) throw new Error('game not found');
this.reversiService.putStoneToGame(game, this.user, pos, id);
this.reversiService.putStoneToGame(this.gameId!, this.user, pos, id);
}
@bindThis
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');
private async checkState(crc32: string | number) {
if (crc32 != null) return;
if (!game.isStarted) return;
if (crc32 != null) {
if (crc32.toString() !== game.crc32) {
this.send('rescue', await this.reversiGameEntityService.packDetail(game, this.user));
}
const game = await this.reversiService.checkCrc(this.gameId!, crc32);
if (game) {
this.send('rescue', game);
}
}
if (this.user && (game.user1Id === this.user.id || game.user2Id === this.user.id)) {
this.reversiService.heatbeat(game, this.user);
}
@bindThis
private async claimTimeIsUp() {
if (this.user == null) return;
this.reversiService.checkTimeout(this.gameId!);
}
@bindThis