Files
lolympus-tournament-support/apps/web/src/api/auction-state.ts
T
dahyeon f649490cd2 fix(auction): harden lifecycle and durable bid storage
Serialize lifecycle changes and make cancellation, deadlines, and match side effects recoverable and idempotent.

Archive accepted bids durably, quarantine invalid payloads, and preserve compatibility with empty round IDs. Synchronize HTTP polling and socket state with scoped resync events.

Add cancellation cleanup index migration and regression coverage. Validated with 744 API tests, 65 Redis integration tests, and 28 Mongo integration tests.
2026-11-09 23:24:11 +09:00

240 lines
7.2 KiB
TypeScript

import type { ApiAuction } from "./types";
export interface BidDeltaPayload {
currentBidRoundId?: string;
auctionBidHistoryEntry?: {
bidderTeamIndex?: number;
bidPointAmount?: number;
bidderUserId?: string;
bidPlacedAtTimestampMS?: number;
bidRoundId?: string;
playerUserId?: string;
};
auctionLifecycleId?: string;
bidRequestId?: string;
currentBidRoundDeadlineTimestampMS?: number;
currentHighestBidderTeamIndex?: number;
currentHighestBidPointAmount?: number;
currentServerTimestampMS?: number;
revision?: number;
}
export function shouldApplyIncomingAuctionState(
current: ApiAuction | null,
incoming: ApiAuction,
): boolean {
if (!current) return true;
if (incoming.shouldForceAuctionStateResync) return true;
if (
current.auctionLifecycleId &&
incoming.auctionLifecycleId &&
current.auctionLifecycleId !== incoming.auctionLifecycleId
) {
return true;
}
if (
typeof current.revision === "number" &&
typeof incoming.revision === "number" &&
incoming.revision < current.revision
) {
return false;
}
return true;
}
/** Full state can repair a delta at the same revision; side effects have a
* separate revision and must not regress when a delayed full state arrives. */
export function mergeAuctionState(
current: ApiAuction | null,
incoming: ApiAuction,
): ApiAuction {
if (
current?.auctionLifecycleId === incoming.auctionLifecycleId &&
current?.matchSideEffectTransitionId ===
incoming.matchSideEffectTransitionId &&
(current?.matchSideEffectRevision ?? -1) >
(incoming.matchSideEffectRevision ?? -1)
) {
return {
...incoming,
matchSideEffectRevision: current!.matchSideEffectRevision,
matchSideEffectStatus: current!.matchSideEffectStatus,
};
}
return incoming;
}
export function shouldResyncForBidDelta(
current: ApiAuction | null,
payload: BidDeltaPayload,
): boolean {
if (!current) return true;
if (
payload.auctionLifecycleId &&
current.auctionLifecycleId &&
payload.auctionLifecycleId !== current.auctionLifecycleId
)
return true;
if (
typeof payload.revision !== "number" ||
typeof current.revision !== "number"
)
return true;
if (payload.revision <= current.revision) return false;
return (
payload.revision !== current.revision + 1 ||
current.phase !== "bidding" ||
Boolean(
current.currentBidRoundId &&
payload.currentBidRoundId !== current.currentBidRoundId,
)
);
}
export function applyBidDelta(
current: ApiAuction | null,
payload: BidDeltaPayload,
): ApiAuction | null {
if (!current) return current;
if (shouldResyncForBidDelta(current, payload)) return current;
if (
payload.auctionLifecycleId &&
current.auctionLifecycleId &&
payload.auctionLifecycleId !== current.auctionLifecycleId
) {
return current;
}
if (
typeof current.revision === "number" &&
typeof payload.revision === "number" &&
payload.revision <= current.revision
) {
return current;
}
const bidPointAmount = Number(payload.currentHighestBidPointAmount);
const bidderTeamIndex = Number(payload.currentHighestBidderTeamIndex);
if (!Number.isFinite(bidPointAmount) || !Number.isFinite(bidderTeamIndex)) {
return current;
}
const bidHistoryEntry = normalizeBidHistoryEntry(
payload.auctionBidHistoryEntry,
);
const currentBidRoundDeadlineTimestampMS =
typeof payload.currentBidRoundDeadlineTimestampMS === "number"
? payload.currentBidRoundDeadlineTimestampMS
: current.currentBidRoundDeadlineTimestampMS;
return {
...current,
auctionBidHistory: appendUniqueAuctionBidHistoryEntry(
current.auctionBidHistory ?? [],
bidHistoryEntry,
),
bids: bidHistoryEntry
? appendUniqueLegacyBidEntry(current.bids ?? [], bidHistoryEntry)
: current.bids,
currentBid: bidPointAmount,
currentBidderTeamIndex: bidderTeamIndex,
currentDeadline: currentBidRoundDeadlineTimestampMS
? new Date(currentBidRoundDeadlineTimestampMS).toISOString()
: current.currentDeadline,
currentHighestBidPointAmount: bidPointAmount,
currentHighestBidderTeamIndex: bidderTeamIndex,
currentBidRoundDeadlineTimestampMS,
currentServerTimestampMS:
payload.currentServerTimestampMS ?? current.currentServerTimestampMS,
revision: payload.revision ?? current.revision,
currentBidRoundId: payload.currentBidRoundId ?? current.currentBidRoundId,
};
}
function normalizeBidHistoryEntry(
entry: BidDeltaPayload["auctionBidHistoryEntry"],
): NonNullable<ApiAuction["auctionBidHistory"]>[number] | null {
if (!entry) return null;
const bidderTeamIndex = Number(entry.bidderTeamIndex);
const bidPointAmount = Number(entry.bidPointAmount);
const bidPlacedAtTimestampMS = Number(entry.bidPlacedAtTimestampMS);
if (!Number.isFinite(bidderTeamIndex) || !Number.isFinite(bidPointAmount)) {
return null;
}
return {
bidRoundId: entry.bidRoundId,
playerUserId: entry.playerUserId,
bidderTeamIndex,
bidPointAmount,
bidderUserId: entry.bidderUserId ?? "",
bidPlacedAtTimestampMS: Number.isFinite(bidPlacedAtTimestampMS)
? bidPlacedAtTimestampMS
: Date.now(),
};
}
function appendUniqueAuctionBidHistoryEntry(
current: NonNullable<ApiAuction["auctionBidHistory"]>,
entry: NonNullable<ApiAuction["auctionBidHistory"]>[number] | null,
) {
if (!entry) return current;
if (
current.some(
(existing) =>
existing.bidderTeamIndex === entry.bidderTeamIndex &&
existing.bidPointAmount === entry.bidPointAmount &&
existing.bidPlacedAtTimestampMS === entry.bidPlacedAtTimestampMS,
)
) {
return current;
}
return [...current, entry].slice(-1000);
}
function appendUniqueLegacyBidEntry(
current: ApiAuction["bids"],
entry: NonNullable<ApiAuction["auctionBidHistory"]>[number],
) {
const currentBids = current ?? [];
const legacyBid = {
amount: entry.bidPointAmount,
bidderUserId: entry.bidderUserId,
teamIndex: entry.bidderTeamIndex,
timestamp: new Date(entry.bidPlacedAtTimestampMS).toISOString(),
};
if (
currentBids.some(
(existing) =>
existing.teamIndex === legacyBid.teamIndex &&
existing.amount === legacyBid.amount &&
existing.timestamp === legacyBid.timestamp,
)
) {
return currentBids;
}
return [...currentBids, legacyBid].slice(-1000);
}
/** HTTP 폴링 결과를 소켓 내부 상태(입찰 기준가·라운드 ID)에도 반영할지 결정한다.
* lifecycle 교체는 소켓 이벤트로만 받는다. 늦게 도착한 이전 경매의 응답이
* 새 경매를 덮어쓰지 않도록 기존 상태가 있으면 같은 lifecycle만 반영한다. */
export function shouldApplyPolledAuctionState(
current: ApiAuction | null,
incoming: ApiAuction,
matchId: string | undefined,
): boolean {
if (!matchId || incoming.matchId !== matchId) return false;
if (!incoming.auctionLifecycleId || !Number.isFinite(incoming.revision))
return false;
if (!current) return true;
if (
!current.auctionLifecycleId ||
current.auctionLifecycleId !== incoming.auctionLifecycleId
)
return false;
return (
typeof current.revision === "number" &&
typeof incoming.revision === "number" &&
incoming.revision >= current.revision
);
}