feat: redesign auction engine with redis sot

This commit is contained in:
dahyeon committed 2026-06-23 03:16:04 -07:00
1 parent 737702d78c
commit e8bb20d988
75 files changed
+12737 -542

No files matched your search

+71 -20
View File
@@ -119,6 +119,8 @@ export interface AuctionTeamView {
export interface AuctionStateView {
matchId: string;
phase: ApiAuction["phase"];
matchSideEffectRevision?: number;
matchSideEffectStatus?: ApiAuction["matchSideEffectStatus"];
teams: AuctionTeamView[];
currentPlayer: AuctionPlayerView | null;
currentPlayerIndex: number;
@@ -273,7 +275,7 @@ function createFallbackParticipant(userId: string): ParticipantView {
}
function mapAuctionTeamToView(
team: ApiAuctionTeam,
team: NormalizedApiAuctionTeam,
playerLookup: Map<string, AuctionPlayerView>,
participantLookup: Map<string, ParticipantView>,
): AuctionTeamView {
@@ -489,12 +491,33 @@ export function mapApiAuctionToAuctionState(
.map(mapMatchParticipantToView)
.map((participant) => [participant.userId, participant]),
);
const auctionOrder = auction.order.map(mapAuctionPlayerToView);
const passedQueue = (auction.passedQueue ?? []).map(mapAuctionPlayerToView);
const rawAuctionOrder = auction.auctionPlayerOrder ?? auction.order ?? [];
const rawPassedQueue =
auction.passedAuctionPlayerQueue ?? auction.passedQueue ?? [];
const rawAuctionTeams = auction.auctionTeams ?? auction.teams ?? [];
const currentAuctionPlayerOrderIndex =
auction.currentAuctionPlayerOrderIndex ?? auction.currentIndex ?? 0;
const currentBid =
auction.currentHighestBidPointAmount ?? auction.currentBid ?? 0;
const rawCurrentBidderTeamIndex =
auction.currentHighestBidderTeamIndex ?? auction.currentBidderTeamIndex;
const currentBidderTeamIndex =
rawCurrentBidderTeamIndex !== undefined && rawCurrentBidderTeamIndex > 0
? rawCurrentBidderTeamIndex
: undefined;
const maximumBidRoundDurationSeconds =
auction.maximumBidRoundDurationSeconds ?? auction.maxTimerSeconds ?? 30;
const bidRoundExtensionSeconds =
auction.bidRoundExtensionSeconds ?? auction.bidExtensionSeconds ?? 5;
const currentBidRoundDeadlineTimestampMS =
auction.currentBidRoundDeadlineTimestampMS ??
(auction.currentDeadline ? Date.parse(auction.currentDeadline) : undefined);
const auctionOrder = rawAuctionOrder.map(mapAuctionPlayerToView);
const passedQueue = rawPassedQueue.map(mapAuctionPlayerToView);
const auctionPlayerLookup = new Map(
auctionOrder.map((player) => [player.userId, player]),
);
const teams = [...auction.teams].sort(
const teams = rawAuctionTeams.map(normalizeApiAuctionTeam).sort(
(left, right) => left.teamIndex - right.teamIndex,
);
@@ -503,15 +526,15 @@ export function mapApiAuctionToAuctionState(
auction.phase === "complete"
? null
: (auctionPlayerLookup.get(
auction.order[auction.currentIndex]?.userId ?? "",
rawAuctionOrder[currentAuctionPlayerOrderIndex]?.userId ?? "",
) ?? null);
const currentPlayerIndex =
auction.phase === "complete" ? totalPlayers : auction.currentIndex;
const timer = auction.currentDeadline
auction.phase === "complete" ? totalPlayers : currentAuctionPlayerOrderIndex;
const timer = currentBidRoundDeadlineTimestampMS
? Math.max(
0,
Math.ceil(
(new Date(auction.currentDeadline).getTime() - now.getTime()) / 1000,
(currentBidRoundDeadlineTimestampMS - now.getTime()) / 1000,
),
)
: 0;
@@ -519,6 +542,8 @@ export function mapApiAuctionToAuctionState(
return {
matchId: auction.matchId,
phase: auction.phase,
matchSideEffectRevision: auction.matchSideEffectRevision,
matchSideEffectStatus: auction.matchSideEffectStatus,
teams: teams.map((team) =>
mapAuctionTeamToView(team, auctionPlayerLookup, participantLookup),
),
@@ -527,21 +552,47 @@ export function mapApiAuctionToAuctionState(
totalPlayers,
auctionOrder,
passedQueue,
auctionRound: auction.auctionRound ?? 1,
bids: auction.bids.map((bid) => ({
teamIndex: bid.teamIndex,
amount: bid.amount,
timestamp: bid.timestamp,
})),
currentBid: auction.currentBid,
currentBidder: auction.currentBidderTeamIndex
auctionRound: auction.currentAuctionRound ?? auction.auctionRound ?? 1,
bids: normalizeApiAuctionBids(auction),
currentBid,
currentBidder: currentBidderTeamIndex !== undefined
? {
teamIndex: auction.currentBidderTeamIndex,
teamIndex: currentBidderTeamIndex,
}
: null,
timer,
maxTimer: auction.maxTimerSeconds,
bidExtensionSeconds: auction.bidExtensionSeconds ?? 5,
deadline: auction.currentDeadline,
maxTimer: maximumBidRoundDurationSeconds,
bidExtensionSeconds: bidRoundExtensionSeconds,
deadline: currentBidRoundDeadlineTimestampMS
? new Date(currentBidRoundDeadlineTimestampMS).toISOString()
: undefined,
};
}
type NormalizedApiAuctionTeam = ApiAuctionTeam & { points: number };
function normalizeApiAuctionTeam(team: ApiAuctionTeam): NormalizedApiAuctionTeam {
return {
...team,
points: team.points ?? team.remainingBidPointAmount ?? 0,
members: (team.members ?? []).map((member) => ({
...member,
cost: member.cost ?? member.memberAcquisitionBidPointAmount ?? 0,
})),
};
}
function normalizeApiAuctionBids(auction: ApiAuction) {
if (auction.auctionBidHistory) {
return auction.auctionBidHistory.map((bid) => ({
amount: bid.bidPointAmount,
teamIndex: bid.bidderTeamIndex,
timestamp: new Date(bid.bidPlacedAtTimestampMS).toISOString(),
}));
}
return (auction.bids ?? []).map((bid) => ({
teamIndex: bid.teamIndex,
amount: bid.amount,
timestamp: bid.timestamp,
}));
}