From a431dde53765fd362874dbf51810296e0952cb63 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Mon, 22 Jan 2024 09:29:06 +0900
Subject: [PATCH] refactor(reversi): refactoring of reversi backend

---
 packages/backend/src/core/ReversiService.ts | 77 +++++++--------------
 1 file 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 {
@@ -268,6 +220,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;
-- 
GitLab