feat: randomize auction player order

This commit is contained in:
dahyeon committed 2026-05-30 03:15:01 -07:00
1 parent da5f542292
commit e306d696a9
2 files changed
+110 -1

No files matched your search

@@ -66,6 +66,7 @@ describe('AuctionService', () => {
}
afterEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});
@@ -158,6 +159,99 @@ describe('AuctionService', () => {
expect(tournamentResultService.scheduleInitialPoll).not.toHaveBeenCalled();
});
it('randomizes the non-captain player order when starting an auction', async () => {
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0);
const matchId = new mongoose.Types.ObjectId();
const communityId = new mongoose.Types.ObjectId();
const userIds = Array.from(
{ length: 10 },
() => new mongoose.Types.ObjectId(),
);
const participants = userIds.map((userId, index) => ({
userId,
preferredPositions: [{ position: 'MID', priority: 1 }],
joinedAt: new Date(index),
excluded: false,
}));
const match = {
_id: matchId,
communityId,
participants,
status: 'recruiting',
teamBuiltAt: undefined,
save: jest.fn().mockResolvedValue(undefined),
};
const users = userIds.slice(2).map((userId, index) => ({
_id: userId,
riotAccounts: [
{
puuid: `puuid-${index}`,
gameName: `Player${index}`,
tagLine: 'KR1',
profileIconId: index,
tiers: [],
championMasteries: [],
recentStatsSummary: undefined,
lastRefreshRequestedAt: new Date(),
isPrimary: true,
},
],
}));
const auctionCreate = jest.fn().mockImplementation(async (payload) => ({
_id: new mongoose.Types.ObjectId(),
matchId,
toObject: jest.fn().mockReturnValue({ matchId, ...payload }),
}));
const { service } = createService({
match: {
findById: jest.fn().mockResolvedValue(match),
},
user: {
find: jest.fn().mockResolvedValue(users),
},
auction: {
findOne: jest.fn().mockResolvedValue(null),
create: auctionCreate,
},
community: {
findById: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
lean: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({
riotPlatform: 'kr',
riotRegion: 'asia',
}),
}),
}),
}),
},
});
await service.start({
matchId,
communityId,
captains: [
{ teamIndex: 1, userId: userIds[0] },
{ teamIndex: 2, userId: userIds[1] },
],
initialPoints: 1000,
maxTimerSeconds: 30,
bidExtensionSeconds: 5,
topTierThreshold: 'NONE',
manualTopTierUserIds: [],
});
const signupOrder = userIds.slice(2).map((userId) => userId.toString());
const auctionOrder = auctionCreate.mock.calls[0][0].order.map(
(player: { userId: mongoose.Types.ObjectId }) =>
player.userId.toString(),
);
expect(auctionOrder).not.toEqual(signupOrder);
expect([...auctionOrder].sort()).toEqual([...signupOrder].sort());
expect(randomSpy).toHaveBeenCalled();
});
it('rejects bids when the team already has 5 members including captain', async () => {
const matchId = new mongoose.Types.ObjectId();
const auction = {
@@ -167,8 +167,9 @@ export class AuctionService {
passCount: 0,
});
}
const shuffledOrder = this.shuffleAuctionPlayerOrder(order);
const prioritizedOrder = this.applyTopTierPriority(
order,
shuffledOrder,
params.topTierThreshold,
params.manualTopTierUserIds,
);
@@ -588,6 +589,20 @@ export class AuctionService {
return [...top, ...rest];
}
private shuffleAuctionPlayerOrder(
order: AuctionPlayerSnapshot[],
): AuctionPlayerSnapshot[] {
const shuffled = [...order];
for (let index = shuffled.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(Math.random() * (index + 1));
[shuffled[index], shuffled[swapIndex]] = [
shuffled[swapIndex],
shuffled[index],
];
}
return shuffled;
}
private forceAssignRemainingPasses(auction: AuctionDocument): void {
const forcedPlayers = auction.order.filter(
(player) =>