From b0dc857463dbc06606cc56a81805863cfd912a08 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Mon, 22 Jun 2026 04:37:06 -0700 Subject: [PATCH] fix: reserve auction points for remaining slots --- .../modules/auction/auction.service.spec.ts | 85 +++++++++++++++++ .../src/modules/auction/auction.service.ts | 16 ++++ apps/web/src/pages/AuctionPage.tsx | 94 +++++++++++++++---- 3 files changed, 175 insertions(+), 20 deletions(-) diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts index 82992acc..8283132e 100644 --- a/apps/api/src/modules/auction/auction.service.spec.ts +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -459,6 +459,91 @@ describe('AuctionService', () => { expect(auction.save).not.toHaveBeenCalled(); }); + it('rejects bids that do not reserve minimum points for remaining team slots', async () => { + const matchId = new mongoose.Types.ObjectId(); + const auction = { + _id: new mongoose.Types.ObjectId(), + matchId, + phase: 'bidding', + teams: [ + { + teamIndex: 1, + captainUserId: new mongoose.Types.ObjectId(), + points: 1000, + members: [], + }, + ], + currentBid: 100, + currentDeadline: new Date(), + maxTimerSeconds: 30, + bidExtensionSeconds: 5, + bids: [], + save: jest.fn().mockResolvedValue(undefined), + toObject: jest.fn().mockReturnValue({ _id: 'auction', matchId }), + }; + const { service } = createService({ + auction: { + findOne: jest.fn().mockResolvedValue(auction), + }, + }); + + await expect( + service.placeBid({ + matchId, + teamIndex: 1, + amount: 980, + }), + ).rejects.toThrow('Bid must reserve minimum points for remaining slots.'); + expect(auction.save).not.toHaveBeenCalled(); + }); + + it('allows bids up to the maximum after reserving minimum points', async () => { + const matchId = new mongoose.Types.ObjectId(); + const captainUserId = new mongoose.Types.ObjectId(); + const auction = { + _id: new mongoose.Types.ObjectId(), + matchId, + phase: 'bidding', + teams: [ + { + teamIndex: 1, + captainUserId, + points: 1000, + members: [], + }, + ], + currentBid: 100, + currentDeadline: new Date(), + maxTimerSeconds: 30, + bidExtensionSeconds: 5, + bids: [], + save: jest.fn().mockResolvedValue(undefined), + toObject: jest.fn().mockReturnValue({ _id: 'auction', matchId }), + }; + const { service } = createService({ + auction: { + findOne: jest.fn().mockResolvedValue(auction), + }, + }); + + await service.placeBid({ + matchId, + teamIndex: 1, + amount: 970, + }); + + expect(auction.currentBid).toBe(970); + expect(auction.currentBidderTeamIndex).toBe(1); + expect(auction.bids).toEqual([ + expect.objectContaining({ + teamIndex: 1, + amount: 970, + bidderUserId: captainUserId, + }), + ]); + expect(auction.save).toHaveBeenCalledTimes(1); + }); + it('extends a short bid deadline using the configured bid extension seconds', async () => { const now = new Date('2026-05-02T00:00:00.000Z'); jest.useFakeTimers().setSystemTime(now); diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index 557bc8eb..ded8b1d1 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -26,6 +26,7 @@ import { TournamentCodeService } from '../tournament-code/tournament-code.servic import { TournamentResultService } from '../tournament-result/tournament-result.service'; const AUCTION_MAX_TEAM_SIZE = 5; +const AUCTION_MIN_BID_POINTS = 10; const AUCTION_BACKGROUND_ENRICHMENT_CONCURRENCY = 1; const AUCTION_ENRICHMENT_COOLDOWN_MS = 3 * 60 * 60 * 1000; const AUCTION_CHAT_MAX_MESSAGES = 200; @@ -318,6 +319,11 @@ export class AuctionService { throw new BadRequestException('Bid exceeds team points.'); } this.assertTeamHasSlot(team); + if (params.amount > this.calculateMaxBidForTeam(team)) { + throw new BadRequestException( + 'Bid must reserve minimum points for remaining slots.', + ); + } // 임시 운영용: 클라이언트에서 선택한 팀장 모드를 신뢰하고 실제 로그인 사용자 검증은 건너뛴다. await this.recordSnapshot(auction, 'bid', params.bidderUserId); @@ -809,6 +815,16 @@ export class AuctionService { } } + private calculateMaxBidForTeam(team: AuctionTeam): number { + const remainingSlotsAfterCurrentBid = Math.max( + 0, + AUCTION_MAX_TEAM_SIZE - this.countTeamMembersIncludingCaptain(team) - 1, + ); + const reservedPoints = + remainingSlotsAfterCurrentBid * AUCTION_MIN_BID_POINTS; + return Math.max(0, team.points - reservedPoints); + } + private countTeamMembersIncludingCaptain(team: AuctionTeam): number { const userIds = new Set([team.captainUserId.toString()]); for (const member of team.members) { diff --git a/apps/web/src/pages/AuctionPage.tsx b/apps/web/src/pages/AuctionPage.tsx index ee137279..c2564fa1 100644 --- a/apps/web/src/pages/AuctionPage.tsx +++ b/apps/web/src/pages/AuctionPage.tsx @@ -125,6 +125,7 @@ function pickLatestAuction( } const AUCTION_MAX_TEAM_SIZE = 5; +const AUCTION_MIN_BID_POINTS = 10; const LANE_ROWS: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP']; const POSITION_TO_RIOT_LANE = { TOP: 'TOP', @@ -134,6 +135,24 @@ const POSITION_TO_RIOT_LANE = { SUP: 'UTILITY', } as const; +function getAuctionTeamSize(team: AuctionStateView['teams'][number]) { + return team.members.length + 1; +} + +function getReservedPointsAfterCurrentBid( + team: AuctionStateView['teams'][number], +) { + const remainingSlotsAfterCurrentBid = Math.max( + 0, + AUCTION_MAX_TEAM_SIZE - getAuctionTeamSize(team) - 1, + ); + return remainingSlotsAfterCurrentBid * AUCTION_MIN_BID_POINTS; +} + +function getMaxBidForTeam(team: AuctionStateView['teams'][number]) { + return Math.max(0, team.points - getReservedPointsAfterCurrentBid(team)); +} + function PosBadge({ pos }: { pos: Position }) { return ( @@ -291,7 +310,7 @@ function TeamPanel({ }) { const { data: ddragon } = useDdragonManifest(); const tone = teamIndexColorClasses(team.teamIndex); - const teamSize = team.members.length + 1; + const teamSize = getAuctionTeamSize(team); const isFull = teamSize >= AUCTION_MAX_TEAM_SIZE; return ( @@ -553,16 +572,22 @@ function BidControls({ onBid: (teamIndex: number, amount: number) => void; }) { const [bidAmount, setBidAmount] = useState(100); - const minBid = state.currentBid + 10; + const minBid = state.currentBid + AUCTION_MIN_BID_POINTS; const activeTeam = captainRole ? state.teams.find((team) => team.teamIndex === captainRole.teamIndex) : null; - const activeTeamSize = activeTeam ? activeTeam.members.length + 1 : 0; + const activeTeamSize = activeTeam ? getAuctionTeamSize(activeTeam) : 0; const isActiveTeamFull = activeTeamSize >= AUCTION_MAX_TEAM_SIZE; + const reservedPoints = activeTeam + ? getReservedPointsAfterCurrentBid(activeTeam) + : 0; + const maxBid = activeTeam ? getMaxBidForTeam(activeTeam) : 0; + const canAffordNextBid = !isActiveTeamFull && maxBid >= minBid; useEffect(() => { - setBidAmount(Math.max(minBid, Math.ceil(minBid / 10) * 10)); - }, [state.currentBid, minBid]); + const nextBid = Math.max(minBid, Math.ceil(minBid / 10) * 10); + setBidAmount(activeTeam ? Math.min(maxBid, nextBid) : nextBid); + }, [activeTeam, maxBid, minBid]); if (state.phase !== 'bidding') return null; @@ -578,16 +603,40 @@ function BidControls({ return (
+
+
+
보유
+
+ {formatPoints(activeTeam.points)}P +
+
+
+
이번 최대
+
+ {formatPoints(maxBid)}P +
+
+
+
보존
+
+ {formatPoints(reservedPoints)}P +
+
+
{presets.map((preset) => (
@@ -624,10 +676,12 @@ function BidControls({ } function BidLog({ bids }: { bids: AuctionStateView['bids'] }) { - const endRef = useRef(null); + const scrollRef = useRef(null); useEffect(() => { - endRef.current?.scrollIntoView({ behavior: 'smooth' }); + const scrollContainer = scrollRef.current; + if (!scrollContainer) return; + scrollContainer.scrollTop = scrollContainer.scrollHeight; }, [bids.length]); if (bids.length === 0) return null; @@ -639,7 +693,7 @@ function BidLog({ bids }: { bids: AuctionStateView['bids'] }) { 입찰 기록
-
+
{bids.map((bid, index) => (
))} -
); @@ -1391,13 +1444,15 @@ function ChatPanel({ className?: string; }) { const [draft, setDraft] = useState(''); - const messagesEndRef = useRef(null); + const messagesScrollRef = useRef(null); const trimmedDraft = draft.trim(); const isConnected = connectionStatus === 'connected'; const canSend = isConnected && trimmedDraft.length > 0; useEffect(() => { - messagesEndRef.current?.scrollIntoView({ block: 'end' }); + const scrollContainer = messagesScrollRef.current; + if (!scrollContainer) return; + scrollContainer.scrollTop = scrollContainer.scrollHeight; }, [messages.length]); function handleSubmit(event: FormEvent) { @@ -1439,7 +1494,7 @@ function ChatPanel({
-
+
{messages.length === 0 ? (
아직 메시지가 없습니다 @@ -1463,7 +1518,6 @@ function ChatPanel({ ))} )} -