summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/ReversiService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2024-01-22 09:29:06 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2024-01-22 09:29:06 +0900
commita431dde53765fd362874dbf51810296e0952cb63 (patch)
treec538102a379c74d4e68d488bfdfeeab41e426a0b /packages/backend/src/core/ReversiService.ts
parentfix(frontend/pizzax): デフォルト値が適用できないことがある... (diff)
downloadmisskey-a431dde53765fd362874dbf51810296e0952cb63.tar.gz
misskey-a431dde53765fd362874dbf51810296e0952cb63.tar.bz2
misskey-a431dde53765fd362874dbf51810296e0952cb63.zip
refactor(reversi): refactoring of reversi backend
Diffstat (limited to 'packages/backend/src/core/ReversiService.ts')
-rw-r--r--packages/backend/src/core/ReversiService.ts77
1 files changed, 26 insertions, 51 deletions
diff --git a/packages/backend/src/core/ReversiService.ts b/packages/backend/src/core/ReversiService.ts
index 39177322f3..0e59d0308d 100644
--- a/packages/backend/src/core/ReversiService.ts
+++ b/packages/backend/src/core/ReversiService.ts
@@ -104,23 +104,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
if (invitations.includes(targetUser.id)) {
await this.redisClient.zrem(`reversi:matchSpecific:${me.id}`, targetUser.id);
- const game = await this.reversiGamesRepository.insert({
- id: this.idService.gen(),
- user1Id: targetUser.id,
- user2Id: me.id,
- user1Ready: false,
- user2Ready: false,
- isStarted: false,
- isEnded: false,
- logs: [],
- map: Reversi.maps.eighteight.data,
- bw: 'random',
- isLlotheo: false,
- }).then(x => this.reversiGamesRepository.findOneByOrFail(x.identifiers[0]));
- this.cacheGame(game);
-
- const packed = await this.reversiGameEntityService.packDetail(game, { id: targetUser.id });
- this.globalEventService.publishReversiStream(targetUser.id, 'matched', { game: packed });
+ const game = await this.matched(targetUser.id, me.id);
return game;
} else {
@@ -147,23 +131,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
const invitorId = invitations[Math.floor(Math.random() * invitations.length)];
await this.redisClient.zrem(`reversi:matchSpecific:${me.id}`, invitorId);
- const game = await this.reversiGamesRepository.insert({
- id: this.idService.gen(),
- user1Id: invitorId,
- user2Id: me.id,
- user1Ready: false,
- user2Ready: false,
- isStarted: false,
- isEnded: false,
- logs: [],
- map: Reversi.maps.eighteight.data,
- bw: 'random',
- isLlotheo: false,
- }).then(x => this.reversiGamesRepository.findOneByOrFail(x.identifiers[0]));
- this.cacheGame(game);
-
- const packed = await this.reversiGameEntityService.packDetail(game, { id: invitorId });
- this.globalEventService.publishReversiStream(invitorId, 'matched', { game: packed });
+ const game = await this.matched(invitorId, me.id);
return game;
}
@@ -183,23 +151,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
await this.redisClient.zrem('reversi:matchAny', me.id, matchedUserId);
- const game = await this.reversiGamesRepository.insert({
- id: this.idService.gen(),
- user1Id: matchedUserId,
- user2Id: me.id,
- user1Ready: false,
- user2Ready: false,
- isStarted: false,
- isEnded: false,
- logs: [],
- map: Reversi.maps.eighteight.data,
- bw: 'random',
- isLlotheo: false,
- }).then(x => this.reversiGamesRepository.findOneByOrFail(x.identifiers[0]));
- this.cacheGame(game);
-
- const packed = await this.reversiGameEntityService.packDetail(game, { id: matchedUserId });
- this.globalEventService.publishReversiStream(matchedUserId, 'matched', { game: packed });
+ const game = await this.matched(matchedUserId, me.id);
return game;
} else {
@@ -269,6 +221,29 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
+ private async matched(parentId: MiUser['id'], childId: MiUser['id']): Promise<MiReversiGame> {
+ const game = await this.reversiGamesRepository.insert({
+ id: this.idService.gen(),
+ user1Id: parentId,
+ user2Id: childId,
+ user1Ready: false,
+ user2Ready: false,
+ isStarted: false,
+ isEnded: false,
+ logs: [],
+ map: Reversi.maps.eighteight.data,
+ bw: 'random',
+ isLlotheo: false,
+ }).then(x => this.reversiGamesRepository.findOneByOrFail(x.identifiers[0]));
+ this.cacheGame(game);
+
+ const packed = await this.reversiGameEntityService.packDetail(game, { id: parentId });
+ this.globalEventService.publishReversiStream(parentId, 'matched', { game: packed });
+
+ return game;
+ }
+
+ @bindThis
private async startGame(game: MiReversiGame) {
let bw: number;
if (game.bw === 'random') {