Skip to content

Commit

Permalink
enhance(reversi): improve matching system
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Jan 24, 2024
1 parent 7beb4ed commit d27b352
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/backend/src/core/ReversiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Serialized } from '@/types.js';
import { ReversiGameEntityService } from './entities/ReversiGameEntityService.js';
import type { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';

const MATCHING_TIMEOUT_MS = 1000 * 20; // 20sec
const INVITATION_TIMEOUT_MS = 1000 * 20; // 20sec

@Injectable()
export class ReversiService implements OnApplicationShutdown, OnModuleInit {
Expand Down Expand Up @@ -112,7 +112,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
//#region 相手から既に招待されてないか確認
const invitations = await this.redisClient.zrange(
`reversi:matchSpecific:${me.id}`,
Date.now() - MATCHING_TIMEOUT_MS,
Date.now() - INVITATION_TIMEOUT_MS,
'+inf',
'BYSCORE');

Expand Down Expand Up @@ -157,7 +157,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
//#region まず自分宛ての招待を探す
const invitations = await this.redisClient.zrange(
`reversi:matchSpecific:${me.id}`,
Date.now() - MATCHING_TIMEOUT_MS,
Date.now() - INVITATION_TIMEOUT_MS,
'+inf',
'BYSCORE');

Expand All @@ -173,15 +173,14 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {

const matchings = await this.redisClient.zrange(
'reversi:matchAny',
Date.now() - MATCHING_TIMEOUT_MS,
'+inf',
'BYSCORE');
0,
2, // 自分自身のIDが入っている場合もあるので2つ取得
'REV');

const userIds = matchings.filter(id => id !== me.id);

if (userIds.length > 0) {
// pick random
const matchedUserId = userIds[Math.floor(Math.random() * userIds.length)];
const matchedUserId = userIds[0];

await this.redisClient.zrem('reversi:matchAny', me.id, matchedUserId);

Expand All @@ -191,7 +190,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
} else {
const redisPipeline = this.redisClient.pipeline();
redisPipeline.zadd('reversi:matchAny', Date.now(), me.id);
redisPipeline.expire('reversi:matchAny', 120, 'NX');
redisPipeline.expire('reversi:matchAny', 15, 'NX');
await redisPipeline.exec();
return null;
}
Expand Down Expand Up @@ -380,7 +379,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
public async getInvitations(user: MiUser): Promise<MiUser['id'][]> {
const invitations = await this.redisClient.zrange(
`reversi:matchSpecific:${user.id}`,
Date.now() - MATCHING_TIMEOUT_MS,
Date.now() - INVITATION_TIMEOUT_MS,
'+inf',
'BYSCORE');
return invitations;
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/src/pages/reversi/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ onMounted(() => {
misskeyApi('reversi/invitations').then(_invitations => {
invitations.value = _invitations;
});

window.addEventListener('beforeunload', cancelMatching);
});

onDeactivated(() => {
Expand Down

0 comments on commit d27b352

Please sign in to comment.