summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2024-01-24 16:37:06 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2024-01-24 16:37:06 +0900
commit5719a929ad5a048b59cf59b8683dcaaef01f2311 (patch)
tree863fe9972e2ca7c63c06815c3f792acc755c14fc /packages/backend/src
parent2024.2.0-beta.6 (diff)
downloadsharkey-5719a929ad5a048b59cf59b8683dcaaef01f2311.tar.gz
sharkey-5719a929ad5a048b59cf59b8683dcaaef01f2311.tar.bz2
sharkey-5719a929ad5a048b59cf59b8683dcaaef01f2311.zip
enhance(reversi): 変則なしマッチングを可能に
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/core/ReversiService.ts33
-rw-r--r--packages/backend/src/core/entities/ReversiGameEntityService.ts2
-rw-r--r--packages/backend/src/models/ReversiGame.ts5
-rw-r--r--packages/backend/src/models/json-schema/reversi-game.ts8
-rw-r--r--packages/backend/src/server/api/endpoints/reversi/match.ts5
5 files changed, 43 insertions, 10 deletions
diff --git a/packages/backend/src/core/ReversiService.ts b/packages/backend/src/core/ReversiService.ts
index f74416a58a..84721b2217 100644
--- a/packages/backend/src/core/ReversiService.ts
+++ b/packages/backend/src/core/ReversiService.ts
@@ -85,6 +85,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
map: game.map,
bw: game.bw,
crc32: game.crc32,
+ noIrregularRules: game.noIrregularRules,
} satisfies Partial<MiReversiGame>;
}
@@ -138,7 +139,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
- public async matchAnyUser(me: MiUser, multiple = false): Promise<MiReversiGame | null> {
+ public async matchAnyUser(me: MiUser, options: { noIrregularRules: boolean }, multiple = false): Promise<MiReversiGame | null> {
if (!multiple) {
// 既にマッチしている対局が無いか探す(3分以内)
const games = await this.reversiGamesRepository.find({
@@ -177,19 +178,29 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
2, // 自分自身のIDが入っている場合もあるので2つ取得
'REV');
- const userIds = matchings.filter(id => id !== me.id);
+ const items = matchings.filter(id => !id.startsWith(me.id));
- if (userIds.length > 0) {
- const matchedUserId = userIds[0];
+ if (items.length > 0) {
+ const [matchedUserId, option] = items[0].split(':');
- await this.redisClient.zrem('reversi:matchAny', me.id, matchedUserId);
+ await this.redisClient.zrem('reversi:matchAny',
+ me.id,
+ matchedUserId,
+ me.id + ':noIrregularRules',
+ matchedUserId + ':noIrregularRules');
- const game = await this.matched(matchedUserId, me.id);
+ const game = await this.matched(matchedUserId, me.id, {
+ noIrregularRules: options.noIrregularRules || option === 'noIrregularRules',
+ });
return game;
} else {
const redisPipeline = this.redisClient.pipeline();
- redisPipeline.zadd('reversi:matchAny', Date.now(), me.id);
+ if (options.noIrregularRules) {
+ redisPipeline.zadd('reversi:matchAny', Date.now(), me.id + ':noIrregularRules');
+ } else {
+ redisPipeline.zadd('reversi:matchAny', Date.now(), me.id);
+ }
redisPipeline.expire('reversi:matchAny', 15, 'NX');
await redisPipeline.exec();
return null;
@@ -203,7 +214,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
@bindThis
public async matchAnyUserCancel(user: MiUser) {
- await this.redisClient.zrem('reversi:matchAny', user.id);
+ const redisPipeline = this.redisClient.pipeline();
+ redisPipeline.zrem('reversi:matchAny', user.id);
+ redisPipeline.zrem('reversi:matchAny', user.id + ':noIrregularRules');
+ await redisPipeline.exec();
}
@bindThis
@@ -265,7 +279,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
- private async matched(parentId: MiUser['id'], childId: MiUser['id']): Promise<MiReversiGame> {
+ private async matched(parentId: MiUser['id'], childId: MiUser['id'], options: { noIrregularRules: boolean; }): Promise<MiReversiGame> {
const game = await this.reversiGamesRepository.insert({
id: this.idService.gen(),
user1Id: parentId,
@@ -278,6 +292,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
map: Reversi.maps.eighteight.data,
bw: 'random',
isLlotheo: false,
+ noIrregularRules: options.noIrregularRules,
}).then(x => this.reversiGamesRepository.findOneOrFail({
where: { id: x.identifiers[0].id },
relations: ['user1', 'user2'],
diff --git a/packages/backend/src/core/entities/ReversiGameEntityService.ts b/packages/backend/src/core/entities/ReversiGameEntityService.ts
index 6c89a70599..1a689a7b53 100644
--- a/packages/backend/src/core/entities/ReversiGameEntityService.ts
+++ b/packages/backend/src/core/entities/ReversiGameEntityService.ts
@@ -61,6 +61,7 @@ export class ReversiGameEntityService {
canPutEverywhere: game.canPutEverywhere,
loopedBoard: game.loopedBoard,
timeLimitForEachTurn: game.timeLimitForEachTurn,
+ noIrregularRules: game.noIrregularRules,
logs: game.logs,
map: game.map,
});
@@ -105,6 +106,7 @@ export class ReversiGameEntityService {
canPutEverywhere: game.canPutEverywhere,
loopedBoard: game.loopedBoard,
timeLimitForEachTurn: game.timeLimitForEachTurn,
+ noIrregularRules: game.noIrregularRules,
});
}
diff --git a/packages/backend/src/models/ReversiGame.ts b/packages/backend/src/models/ReversiGame.ts
index 11d236e458..c03335dd63 100644
--- a/packages/backend/src/models/ReversiGame.ts
+++ b/packages/backend/src/models/ReversiGame.ts
@@ -109,6 +109,11 @@ export class MiReversiGame {
@Column('boolean', {
default: false,
})
+ public noIrregularRules: boolean;
+
+ @Column('boolean', {
+ default: false,
+ })
public isLlotheo: boolean;
@Column('boolean', {
diff --git a/packages/backend/src/models/json-schema/reversi-game.ts b/packages/backend/src/models/json-schema/reversi-game.ts
index f8a5e7451c..ff4c78eeb0 100644
--- a/packages/backend/src/models/json-schema/reversi-game.ts
+++ b/packages/backend/src/models/json-schema/reversi-game.ts
@@ -82,6 +82,10 @@ export const packedReversiGameLiteSchema = {
type: 'string',
optional: false, nullable: false,
},
+ noIrregularRules: {
+ type: 'boolean',
+ optional: false, nullable: false,
+ },
isLlotheo: {
type: 'boolean',
optional: false, nullable: false,
@@ -196,6 +200,10 @@ export const packedReversiGameDetailedSchema = {
type: 'string',
optional: false, nullable: false,
},
+ noIrregularRules: {
+ type: 'boolean',
+ optional: false, nullable: false,
+ },
isLlotheo: {
type: 'boolean',
optional: false, nullable: false,
diff --git a/packages/backend/src/server/api/endpoints/reversi/match.ts b/packages/backend/src/server/api/endpoints/reversi/match.ts
index 62682cfb50..f8dee21c4c 100644
--- a/packages/backend/src/server/api/endpoints/reversi/match.ts
+++ b/packages/backend/src/server/api/endpoints/reversi/match.ts
@@ -37,6 +37,7 @@ export const paramDef = {
type: 'object',
properties: {
userId: { type: 'string', format: 'misskey:id', nullable: true },
+ noIrregularRules: { type: 'boolean', default: false },
multiple: { type: 'boolean', default: false },
},
required: [],
@@ -57,7 +58,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw err;
}) : null;
- const game = target ? await this.reversiService.matchSpecificUser(me, target, ps.multiple) : await this.reversiService.matchAnyUser(me, ps.multiple);
+ const game = target
+ ? await this.reversiService.matchSpecificUser(me, target, ps.multiple)
+ : await this.reversiService.matchAnyUser(me, { noIrregularRules: ps.noIrregularRules }, ps.multiple);
if (game == null) return;