.
This commit is contained in:
1 parent
b152a5b0fa
commit
2379051196
32 files changed
+1682
-383
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
AuctionDocument,
|
||||
AuctionPlayerSnapshot,
|
||||
AuctionTeam,
|
||||
MatchParticipant,
|
||||
MatchTeamMember,
|
||||
Position,
|
||||
TeamSide,
|
||||
@@ -23,7 +24,11 @@ const AUCTION_MAX_TEAM_SIZE = 5;
|
||||
interface StartAuctionParams {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
captains: { side: TeamSide; userId: mongoose.Types.ObjectId }[];
|
||||
captains: {
|
||||
side: TeamSide;
|
||||
groupIndex: number;
|
||||
userId: mongoose.Types.ObjectId;
|
||||
}[];
|
||||
initialPoints: number;
|
||||
maxTimerSeconds: number;
|
||||
}
|
||||
@@ -31,6 +36,7 @@ interface StartAuctionParams {
|
||||
interface PlaceBidParams {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
side: TeamSide;
|
||||
groupIndex: number;
|
||||
amount: number;
|
||||
bidderUserId?: mongoose.Types.ObjectId;
|
||||
}
|
||||
@@ -66,30 +72,24 @@ export class AuctionService {
|
||||
throw new BadRequestException('Auction already exists for this match.');
|
||||
}
|
||||
|
||||
const activeParticipants = this.applyAuctionCut(match);
|
||||
const desiredAuctionCount = activeParticipants.length;
|
||||
if (desiredAuctionCount < 10) {
|
||||
throw new BadRequestException('Not enough participants (need at least 10).');
|
||||
}
|
||||
const expectedCaptainCount = desiredAuctionCount / AUCTION_MAX_TEAM_SIZE;
|
||||
const groupCount = expectedCaptainCount / 2;
|
||||
if (params.captains.length !== expectedCaptainCount) {
|
||||
throw new BadRequestException(
|
||||
`Expected ${expectedCaptainCount} captains for ${desiredAuctionCount} participants.`,
|
||||
);
|
||||
}
|
||||
this.assertCaptainAssignments(params.captains, activeParticipants, groupCount);
|
||||
const captainUserIds = params.captains.map((c) => c.userId);
|
||||
if (captainUserIds[0].equals(captainUserIds[1])) {
|
||||
throw new BadRequestException('Captains must be different users.');
|
||||
}
|
||||
const captainParticipants = match.participants.filter((p) =>
|
||||
captainUserIds.some((cid) => cid.equals(p.userId)),
|
||||
);
|
||||
if (captainParticipants.length !== 2) {
|
||||
throw new BadRequestException(
|
||||
'Both captains must be participants of the match.',
|
||||
);
|
||||
}
|
||||
if (
|
||||
match.participants.length >
|
||||
params.captains.length * AUCTION_MAX_TEAM_SIZE
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'Auction supports up to 5 members per team including captains.',
|
||||
);
|
||||
}
|
||||
|
||||
const basePlayerUsers = await UserModel.find({
|
||||
_id: {
|
||||
$in: match.participants
|
||||
$in: activeParticipants
|
||||
.filter((p) => !captainUserIds.some((cid) => cid.equals(p.userId)))
|
||||
.map((p) => p.userId),
|
||||
},
|
||||
@@ -130,7 +130,7 @@ export class AuctionService {
|
||||
|
||||
const nonCaptainPuuids = new Set<string>();
|
||||
const order: AuctionPlayerSnapshot[] = [];
|
||||
for (const participant of match.participants) {
|
||||
for (const participant of activeParticipants) {
|
||||
if (captainUserIds.some((cid) => cid.equals(participant.userId))) {
|
||||
continue;
|
||||
}
|
||||
@@ -163,6 +163,7 @@ export class AuctionService {
|
||||
phase: 'presenting',
|
||||
teams: params.captains.map((c) => ({
|
||||
side: c.side,
|
||||
groupIndex: c.groupIndex,
|
||||
captainUserId: c.userId,
|
||||
points: params.initialPoints,
|
||||
members: [],
|
||||
@@ -204,6 +205,7 @@ export class AuctionService {
|
||||
auction.phase = 'bidding';
|
||||
auction.currentBid = 0;
|
||||
auction.currentBidder = undefined;
|
||||
auction.currentBidderGroupIndex = undefined;
|
||||
auction.currentDeadline = this.createDeadline(auction.maxTimerSeconds);
|
||||
await auction.save();
|
||||
return auction;
|
||||
@@ -214,8 +216,8 @@ export class AuctionService {
|
||||
if (auction.phase !== 'bidding') {
|
||||
throw new BadRequestException('Auction is not in bidding phase.');
|
||||
}
|
||||
const team = auction.teams.find((t) => t.side === params.side);
|
||||
if (!team) throw new BadRequestException('Unknown team side.');
|
||||
const team = this.findAuctionTeam(auction, params.side, params.groupIndex);
|
||||
if (!team) throw new BadRequestException('Unknown team.');
|
||||
if (params.amount <= auction.currentBid) {
|
||||
throw new BadRequestException('Bid must exceed current bid.');
|
||||
}
|
||||
@@ -227,11 +229,13 @@ export class AuctionService {
|
||||
|
||||
auction.currentBid = params.amount;
|
||||
auction.currentBidder = params.side;
|
||||
auction.currentBidderGroupIndex = params.groupIndex;
|
||||
auction.currentDeadline = this.createDeadline(
|
||||
this.resolveBidExtensionSeconds(auction),
|
||||
);
|
||||
auction.bids.push({
|
||||
side: params.side,
|
||||
groupIndex: params.groupIndex,
|
||||
amount: params.amount,
|
||||
bidderUserId: team.captainUserId,
|
||||
timestamp: new Date(),
|
||||
@@ -248,7 +252,11 @@ export class AuctionService {
|
||||
if (!auction.currentBidder || auction.currentBid <= 0) {
|
||||
throw new BadRequestException('No bid to mark sold.');
|
||||
}
|
||||
const team = auction.teams.find((t) => t.side === auction.currentBidder);
|
||||
const team = this.findAuctionTeam(
|
||||
auction,
|
||||
auction.currentBidder,
|
||||
auction.currentBidderGroupIndex ?? 1,
|
||||
);
|
||||
if (!team) throw new BadRequestException('Winning team not found.');
|
||||
this.assertTeamHasSlot(team);
|
||||
|
||||
@@ -284,6 +292,7 @@ export class AuctionService {
|
||||
auction.phase = 'presenting';
|
||||
auction.currentBid = 0;
|
||||
auction.currentBidder = undefined;
|
||||
auction.currentBidderGroupIndex = undefined;
|
||||
}
|
||||
auction.currentDeadline = undefined;
|
||||
await auction.save();
|
||||
@@ -323,6 +332,9 @@ export class AuctionService {
|
||||
match.status = 'recruiting';
|
||||
match.teams = [];
|
||||
match.tournamentCodeIds = [];
|
||||
match.participants.forEach((participant) => {
|
||||
participant.excluded = false;
|
||||
});
|
||||
match.teamBuiltAt = undefined;
|
||||
match.result = undefined;
|
||||
match.resultPollState = undefined;
|
||||
@@ -337,6 +349,79 @@ export class AuctionService {
|
||||
}
|
||||
}
|
||||
|
||||
private applyAuctionCut(match: {
|
||||
participants: MatchParticipant[];
|
||||
}): MatchParticipant[] {
|
||||
const ordered = [...match.participants].sort(
|
||||
(left, right) => left.joinedAt.getTime() - right.joinedAt.getTime(),
|
||||
);
|
||||
const desiredAuctionCount = Math.floor(ordered.length / 10) * 10;
|
||||
const activeIds = new Set(
|
||||
ordered
|
||||
.slice(0, desiredAuctionCount)
|
||||
.map((participant) => participant.userId.toString()),
|
||||
);
|
||||
match.participants.forEach((participant) => {
|
||||
participant.excluded = !activeIds.has(participant.userId.toString());
|
||||
});
|
||||
return ordered.slice(0, desiredAuctionCount);
|
||||
}
|
||||
|
||||
private assertCaptainAssignments(
|
||||
captains: StartAuctionParams['captains'],
|
||||
activeParticipants: Array<{ userId: mongoose.Types.ObjectId }>,
|
||||
groupCount: number,
|
||||
): void {
|
||||
const activeUserIds = new Set(
|
||||
activeParticipants.map((participant) => participant.userId.toString()),
|
||||
);
|
||||
const captainUserIds = new Set<string>();
|
||||
const teamKeys = new Set<string>();
|
||||
const requiredKeys = new Set<string>();
|
||||
for (let groupIndex = 1; groupIndex <= groupCount; groupIndex += 1) {
|
||||
requiredKeys.add(`blue:${groupIndex}`);
|
||||
requiredKeys.add(`red:${groupIndex}`);
|
||||
}
|
||||
|
||||
for (const captain of captains) {
|
||||
const userId = captain.userId.toString();
|
||||
if (!activeUserIds.has(userId)) {
|
||||
throw new BadRequestException(
|
||||
'All captains must be active participants of the match.',
|
||||
);
|
||||
}
|
||||
if (captainUserIds.has(userId)) {
|
||||
throw new BadRequestException('Captains must be different users.');
|
||||
}
|
||||
captainUserIds.add(userId);
|
||||
|
||||
if (captain.groupIndex < 1 || captain.groupIndex > groupCount) {
|
||||
throw new BadRequestException('Captain groupIndex is out of range.');
|
||||
}
|
||||
const key = `${captain.side}:${captain.groupIndex}`;
|
||||
if (teamKeys.has(key)) {
|
||||
throw new BadRequestException('Duplicate captain team assignment.');
|
||||
}
|
||||
teamKeys.add(key);
|
||||
}
|
||||
|
||||
for (const key of requiredKeys) {
|
||||
if (!teamKeys.has(key)) {
|
||||
throw new BadRequestException('Each group needs one blue and one red captain.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private findAuctionTeam(
|
||||
auction: AuctionDocument,
|
||||
side: TeamSide,
|
||||
groupIndex: number,
|
||||
): AuctionTeam | undefined {
|
||||
return auction.teams.find(
|
||||
(team) => team.side === side && (team.groupIndex ?? 1) === groupIndex,
|
||||
);
|
||||
}
|
||||
|
||||
private createDeadline(secondsFromNow: number): Date {
|
||||
return new Date(Date.now() + secondsFromNow * 1000);
|
||||
}
|
||||
@@ -399,7 +484,8 @@ export class AuctionService {
|
||||
|
||||
return {
|
||||
side: team.side,
|
||||
name: this.resolveTeamName(team.side),
|
||||
groupIndex: team.groupIndex ?? 1,
|
||||
name: this.resolveTeamName(team.side, team.groupIndex ?? 1, auction.teams),
|
||||
captainUserId: team.captainUserId,
|
||||
members,
|
||||
};
|
||||
@@ -411,8 +497,14 @@ export class AuctionService {
|
||||
await this.tournamentResultService.scheduleInitialPoll(match);
|
||||
}
|
||||
|
||||
private resolveTeamName(side: TeamSide): string {
|
||||
return side === 'blue' ? '블루팀' : '레드팀';
|
||||
private resolveTeamName(
|
||||
side: TeamSide,
|
||||
groupIndex: number,
|
||||
teams: AuctionTeam[],
|
||||
): string {
|
||||
const groupCount = new Set(teams.map((team) => team.groupIndex ?? 1)).size;
|
||||
const sideLabel = side === 'blue' ? '블루팀' : '레드팀';
|
||||
return groupCount <= 1 ? sideLabel : `${groupIndex}조 ${sideLabel}`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user