This commit is contained in:
dahyeon committed 2026-04-26 03:33:24 -07:00
1 parent 6d62de4ec4
commit 285f72b3ee
16 files changed
+2095 -136

No files matched your search

@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { RiotModule } from '../riot/riot.module';
import { AuctionGateway } from './auction.gateway';
import { AuctionController } from './auction.controller';
import { AuctionService } from './auction.service';
@Module({
imports: [RiotModule],
controllers: [AuctionController],
providers: [AuctionService, AuctionGateway],
exports: [AuctionService],
@@ -12,6 +12,7 @@ import {
Position,
TeamSide,
} from '../global/database/schema';
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
interface StartAuctionParams {
matchId: mongoose.Types.ObjectId;
@@ -35,12 +36,16 @@ interface MarkSoldParams {
@Injectable()
export class AuctionService {
constructor(private readonly connectionService: ConnectionService) {}
constructor(
private readonly connectionService: ConnectionService,
private readonly enrichmentService: RiotAccountEnrichmentService,
) {}
public async start(params: StartAuctionParams): Promise<AuctionDocument> {
const MatchModel = await this.connectionService.getModel('match');
const UserModel = await this.connectionService.getModel('user');
const AuctionModel = await this.connectionService.getModel('auction');
const CommunityModel = await this.connectionService.getModel('community');
const match = await MatchModel.findById(params.matchId);
if (!match) throw new NotFoundException('Match not found.');
@@ -66,16 +71,48 @@ export class AuctionService {
);
}
const nonCaptainPuuids = new Set<string>();
const playerUsers = await UserModel.find({
const basePlayerUsers = await UserModel.find({
_id: {
$in: match.participants
.filter((p) => !captainUserIds.some((cid) => cid.equals(p.userId)))
.map((p) => p.userId),
},
});
const community = await CommunityModel.findById(match.communityId)
.select({ riotPlatform: 1, riotRegion: 1 })
.lean()
.exec();
const platform = community?.riotPlatform ?? 'kr';
const region = community?.riotRegion ?? 'asia';
const enrichTargets = basePlayerUsers
.map((user) => ({
userId: user._id,
puuid: (user.riotAccounts.find((account) => account.isPrimary) ?? user.riotAccounts[0])
?.puuid,
}))
.filter((target): target is { userId: mongoose.Types.ObjectId; puuid: string } =>
typeof target.puuid === 'string' && target.puuid.length > 0,
);
await runWithConcurrency(enrichTargets, 3, async (target) => {
await this.enrichmentService.enrichRiotAccount({
userId: target.userId,
puuid: target.puuid,
options: {
force: false,
platform,
region,
},
});
});
const playerUsers = await UserModel.find({
_id: { $in: basePlayerUsers.map((user) => user._id) },
});
const userById = new Map(playerUsers.map((u) => [u._id.toString(), u]));
const nonCaptainPuuids = new Set<string>();
const order: AuctionPlayerSnapshot[] = [];
for (const participant of match.participants) {
if (captainUserIds.some((cid) => cid.equals(participant.userId))) {
@@ -290,3 +327,22 @@ export class AuctionService {
return Math.max(5, remainingSeconds);
}
}
async function runWithConcurrency<T>(
items: T[],
concurrency: number,
worker: (item: T) => Promise<void>,
): Promise<void> {
if (items.length === 0) return;
const queue = [...items];
const runners = Array.from({ length: Math.min(concurrency, queue.length) }, async () => {
while (queue.length > 0) {
const item = queue.shift();
if (item === undefined) return;
await worker(item);
}
});
await Promise.allSettled(runners);
}