fix(auction): prevent skipped players and incomplete completion
This commit is contained in:
1 parent
de84ad5309
commit
5bc748dc4d
10 files changed
+677
-32
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import type { ApiAuction } from './types';
|
||||
|
||||
type AuctionActionState = Pick<
|
||||
ApiAuction,
|
||||
| 'auctionLifecycleId'
|
||||
| 'currentAuctionPlayerOrderIndex'
|
||||
| 'currentBidRoundPlayerUserId'
|
||||
| 'phase'
|
||||
| 'revision'
|
||||
>;
|
||||
|
||||
interface AuctionActionObservation {
|
||||
clientObservedAuctionLifecycleId?: string;
|
||||
clientObservedCurrentAuctionPlayerOrderIndex?: number;
|
||||
clientObservedCurrentBidRoundPlayerUserId?: string;
|
||||
clientObservedPhase?: string;
|
||||
clientObservedRevision?: number;
|
||||
}
|
||||
|
||||
export function resolveAuctionActionRejection(
|
||||
params: Parameters<typeof shouldRetryAuctionAction>[0],
|
||||
): { applyServerState: boolean; retry: boolean } {
|
||||
const { request, currentState, serverState } = params;
|
||||
// Decide from the live screen before applying the ACK: an old response may force resync.
|
||||
const applyServerState = Boolean(
|
||||
currentState &&
|
||||
serverState &&
|
||||
request.clientObservedAuctionLifecycleId &&
|
||||
currentState.auctionLifecycleId ===
|
||||
request.clientObservedAuctionLifecycleId &&
|
||||
(serverState.auctionLifecycleId !== currentState.auctionLifecycleId ||
|
||||
(Number.isSafeInteger(serverState.revision) &&
|
||||
(serverState.revision ?? -1) > (currentState.revision ?? -1))),
|
||||
);
|
||||
return { applyServerState, retry: shouldRetryAuctionAction(params) };
|
||||
}
|
||||
|
||||
export function shouldRetryAuctionAction(params: {
|
||||
action: string;
|
||||
request: AuctionActionObservation;
|
||||
serverState?: AuctionActionState | null;
|
||||
currentState?: AuctionActionState | null;
|
||||
resyncRecommended: boolean;
|
||||
retryAttempt: number;
|
||||
}): boolean {
|
||||
const { action, request, serverState, currentState } = params;
|
||||
if (
|
||||
!params.resyncRecommended ||
|
||||
params.retryAttempt >= 1 ||
|
||||
!serverState ||
|
||||
!currentState
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const phase = request.clientObservedPhase;
|
||||
const phaseAllowsAction =
|
||||
(action === 'next' && (phase === 'sold' || phase === 'passed')) ||
|
||||
(action === 'open-bidding' && phase === 'presenting') ||
|
||||
((action === 'sold' || action === 'passed') && phase === 'bidding');
|
||||
if (
|
||||
!phaseAllowsAction ||
|
||||
!request.clientObservedAuctionLifecycleId ||
|
||||
!request.clientObservedCurrentBidRoundPlayerUserId ||
|
||||
!Number.isSafeInteger(
|
||||
request.clientObservedCurrentAuctionPlayerOrderIndex,
|
||||
) ||
|
||||
(request.clientObservedCurrentAuctionPlayerOrderIndex ?? -1) < 0 ||
|
||||
!Number.isSafeInteger(request.clientObservedRevision) ||
|
||||
(request.clientObservedRevision ?? -1) < 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// A delayed rejection must not turn the original action into one for another player.
|
||||
return [serverState, currentState].every(
|
||||
(state) =>
|
||||
state.auctionLifecycleId === request.clientObservedAuctionLifecycleId &&
|
||||
state.currentAuctionPlayerOrderIndex ===
|
||||
request.clientObservedCurrentAuctionPlayerOrderIndex &&
|
||||
state.currentBidRoundPlayerUserId ===
|
||||
request.clientObservedCurrentBidRoundPlayerUserId &&
|
||||
state.phase === phase &&
|
||||
Number.isSafeInteger(state.revision) &&
|
||||
(state.revision ?? -1) >= request.clientObservedRevision!,
|
||||
);
|
||||
}
|
||||
@@ -14,11 +14,13 @@
|
||||
const REASON_TO_USER_MESSAGE: Record<string, string> = {
|
||||
// 구조 전환
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_LIVE_STATE_CHANGED_BEFORE_APPLY:
|
||||
'경매 상태가 바뀌어 최신 상태로 맞췄어요. 다시 시도해 주세요.',
|
||||
'경매 상태가 바뀌었어요. 현재 선수와 진행 상태를 확인해 주세요.',
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_AUCTION_NOT_IN_BIDDING_PHASE:
|
||||
'이미 입찰 단계가 아니에요. 최신 상태로 맞췄어요.',
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_PHASE_DOES_NOT_ALLOW_ADVANCE:
|
||||
'이미 다음 단계로 넘어간 상태예요. 최신 상태로 맞췄어요.',
|
||||
'현재 단계에서는 다음 선수로 넘어갈 수 없어요. 현재 선수를 확인해 주세요.',
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_ROSTER_INVALID:
|
||||
'팀 배정 상태를 확인해야 해 경매를 완료하지 못했습니다.',
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_NO_WINNING_BID_TO_MARK_SOLD:
|
||||
'낙찰할 입찰이 없어요. 최신 상태로 맞췄어요.',
|
||||
AUCTION_TRANSITION_REJECTED_BECAUSE_ACTIVE_BID_PRESENT:
|
||||
@@ -56,7 +58,9 @@ const ENGLISH_MESSAGE_TO_USER_MESSAGE: Record<string, string> = {
|
||||
'Unknown team.': '알 수 없는 팀이에요.',
|
||||
'Bid request conflicts with an earlier request.': '중복된 요청이에요.',
|
||||
'Cannot advance from current phase.':
|
||||
'이미 다음 단계로 넘어간 상태예요. 최신 상태로 맞췄어요.',
|
||||
'현재 단계에서는 다음 선수로 넘어갈 수 없어요. 현재 선수를 확인해 주세요.',
|
||||
'Auction roster validation failed.':
|
||||
'팀 배정 상태를 확인해야 해 경매를 완료하지 못했습니다.',
|
||||
'No bid to mark sold.': '낙찰할 입찰이 없어요. 최신 상태로 맞췄어요.',
|
||||
'Cannot pass a player with an active bid.':
|
||||
'진행 중인 입찰이 있어 유찰할 수 없어요.',
|
||||
|
||||
@@ -3,6 +3,7 @@ import { io, type Socket } from 'socket.io-client';
|
||||
import { API_BASE_URL, useAuthUserId } from '../config';
|
||||
import { acquireAccessToken } from '../lib/auth/token-provider';
|
||||
import type { ApiAuction, ApiAuctionChatMessage, Position } from './types';
|
||||
import { resolveAuctionActionRejection } from './auction-action-retry';
|
||||
import {
|
||||
isAuctionResyncRecommended,
|
||||
resolveAuctionErrorMessage,
|
||||
@@ -691,13 +692,21 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
clearPendingActionRequest(
|
||||
ack?.auctionActionRequestId ?? auctionActionRequestId,
|
||||
);
|
||||
// 2) 자동 재시도: "낡은 화면 때문에 거부"였고 동기 rebaseline 소스(state)가
|
||||
// 있을 때만 1회. assignedPosition 등 원래 payload는 유지하고 관측값/요청
|
||||
// id만 새로 만든다. 재시도 시에는 중간 에러 문구를 띄우지 않는다(무음 복구).
|
||||
const canAutoRetry =
|
||||
Boolean(ack.state) &&
|
||||
isAuctionResyncRecommended(ack) &&
|
||||
retryAttempt < 1;
|
||||
// 최신 화면과 거부 응답이 모두 원래 요청의 대상일 때만 한 번 재시도한다.
|
||||
const { applyServerState, retry: canAutoRetry } =
|
||||
resolveAuctionActionRejection({
|
||||
action: eventName,
|
||||
request: payload,
|
||||
serverState: ack.state,
|
||||
currentState: latestAuctionRef.current,
|
||||
resyncRecommended: isAuctionResyncRecommended(ack),
|
||||
retryAttempt,
|
||||
});
|
||||
if (ack.state && applyServerState) {
|
||||
commitAuctionState(ack.state, 'action-reject-rebaseline');
|
||||
} else if (!ack.state && isAuctionResyncRecommended(ack)) {
|
||||
resyncAuctionState();
|
||||
}
|
||||
// 거부 시 "클라가 보낸 관측 revision"과 "서버가 동봉한 진짜 state"를 함께
|
||||
// 기록한다. observedRevision < serverRevision 이면 stale-client CONFLICT 확정.
|
||||
addNewRelicPageAction('AuctionClientActionRejected', {
|
||||
@@ -725,18 +734,14 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
...describeAuctionForTelemetry('observed', latestAuctionRef.current),
|
||||
...describeAuctionForTelemetry('server', ack?.state ?? null),
|
||||
});
|
||||
// 1) rebaseline: 서버가 동봉한 최신 state를 즉시 반영(없으면 join으로 재요청).
|
||||
// commitAuctionState는 latestAuctionRef를 동기로 갱신하므로, 이후
|
||||
// buildAuctionActionPayload는 최신 관측값(revision/phase/선수)을 읽는다.
|
||||
if (ack.state) {
|
||||
commitAuctionState(ack.state, 'action-reject-rebaseline');
|
||||
} else if (isAuctionResyncRecommended(ack)) {
|
||||
resyncAuctionState();
|
||||
}
|
||||
if (canAutoRetry) {
|
||||
emitAuctionAction(
|
||||
eventName,
|
||||
{ ...payload, ...buildAuctionActionPayload() },
|
||||
{
|
||||
...payload,
|
||||
auctionActionRequestId: createClientRequestId(),
|
||||
clientObservedRevision: latestAuctionRef.current?.revision,
|
||||
},
|
||||
retryAttempt + 1,
|
||||
);
|
||||
} else {
|
||||
|
||||
@@ -2144,7 +2144,10 @@ export default function AuctionPage() {
|
||||
</Link>
|
||||
|
||||
{errorMessage && (
|
||||
<div className="mb-4 border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-500">
|
||||
<div
|
||||
role="alert"
|
||||
className="mb-4 border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-500"
|
||||
>
|
||||
{errorMessage}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
resolveAuctionActionRejection,
|
||||
shouldRetryAuctionAction,
|
||||
} from "../src/api/auction-action-retry.ts";
|
||||
|
||||
const request = {
|
||||
clientObservedAuctionLifecycleId: "auction-1",
|
||||
clientObservedCurrentAuctionPlayerOrderIndex: 11,
|
||||
clientObservedCurrentBidRoundPlayerUserId: "player-11",
|
||||
clientObservedPhase: "sold",
|
||||
clientObservedRevision: 130,
|
||||
};
|
||||
const state = {
|
||||
auctionLifecycleId: "auction-1",
|
||||
currentAuctionPlayerOrderIndex: 11,
|
||||
currentBidRoundPlayerUserId: "player-11",
|
||||
phase: "sold" as const,
|
||||
revision: 131,
|
||||
};
|
||||
const input = {
|
||||
action: "next",
|
||||
request,
|
||||
serverState: state,
|
||||
currentState: state,
|
||||
resyncRecommended: true,
|
||||
retryAttempt: 0,
|
||||
};
|
||||
|
||||
test("a delayed old-lifecycle rejection cannot roll back the screen and retry", () => {
|
||||
const currentState = {
|
||||
...state,
|
||||
auctionLifecycleId: "auction-2",
|
||||
revision: 1,
|
||||
};
|
||||
const decision = resolveAuctionActionRejection({ ...input, currentState });
|
||||
assert.deepEqual(decision, { applyServerState: false, retry: false });
|
||||
});
|
||||
|
||||
test("an older forced-resync rejection cannot roll back an already advanced player", () => {
|
||||
const serverState = { ...state, shouldForceAuctionStateResync: true };
|
||||
const currentState = {
|
||||
...state,
|
||||
revision: 132,
|
||||
currentAuctionPlayerOrderIndex: 12,
|
||||
};
|
||||
assert.deepEqual(
|
||||
resolveAuctionActionRejection({ ...input, serverState, currentState }),
|
||||
{
|
||||
applyServerState: false,
|
||||
retry: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("a fresh rejection updates the screen to the new player without retrying", () => {
|
||||
const serverState = {
|
||||
...state,
|
||||
currentAuctionPlayerOrderIndex: 12,
|
||||
phase: "presenting" as const,
|
||||
};
|
||||
assert.deepEqual(
|
||||
resolveAuctionActionRejection({
|
||||
...input,
|
||||
serverState,
|
||||
currentState: { ...state, revision: 130 },
|
||||
}),
|
||||
{ applyServerState: true, retry: false },
|
||||
);
|
||||
});
|
||||
|
||||
test("a same-target revision update is applied and permits one retry", () => {
|
||||
assert.deepEqual(
|
||||
resolveAuctionActionRejection({
|
||||
...input,
|
||||
currentState: { ...state, revision: 130 },
|
||||
}),
|
||||
{ applyServerState: true, retry: true },
|
||||
);
|
||||
});
|
||||
|
||||
test("retries a revision conflict only while the original action target is unchanged", () => {
|
||||
assert.equal(shouldRetryAuctionAction(input), true);
|
||||
});
|
||||
|
||||
test("does not retry next for the following player after a conflict", () => {
|
||||
const nextPlayer = {
|
||||
...state,
|
||||
currentAuctionPlayerOrderIndex: 12,
|
||||
currentBidRoundPlayerUserId: "player-12",
|
||||
phase: "presenting" as const,
|
||||
};
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
serverState: nextPlayer,
|
||||
currentState: nextPlayer,
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
for (const patch of [
|
||||
{ auctionLifecycleId: "auction-2" },
|
||||
{ currentAuctionPlayerOrderIndex: 12 },
|
||||
{ currentBidRoundPlayerUserId: "player-12" },
|
||||
{ phase: "passed" as const },
|
||||
]) {
|
||||
test(`does not retarget a retry when ${Object.keys(patch)[0]} changes`, () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
serverState: { ...state, ...patch },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test("does not retry from an older acknowledgement after the live screen has advanced", () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
currentState: { ...state, currentAuctionPlayerOrderIndex: 12 },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("does not retry with missing original target identity", () => {
|
||||
for (const field of Object.keys(request)) {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
request: { ...request, [field]: undefined },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("does not retry without a current state, a server state, or a retry recommendation", () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({ ...input, currentState: null }),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({ ...input, serverState: undefined }),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({ ...input, resyncRecommended: false }),
|
||||
false,
|
||||
);
|
||||
assert.equal(shouldRetryAuctionAction({ ...input, retryAttempt: 1 }), false);
|
||||
});
|
||||
|
||||
test("does not retry next in presenting even if the target is unchanged", () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
request: { ...request, clientObservedPhase: "presenting" },
|
||||
serverState: { ...state, phase: "presenting" },
|
||||
currentState: { ...state, phase: "presenting" },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("does not retry using a revision older than the original request", () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
serverState: { ...state, revision: 129 },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
for (const [action, phase] of [
|
||||
["open-bidding", "presenting"],
|
||||
["sold", "bidding"],
|
||||
["passed", "bidding"],
|
||||
["next", "passed"],
|
||||
] as const) {
|
||||
test(`allows a same-target ${action} retry in ${phase}`, () => {
|
||||
assert.equal(
|
||||
shouldRetryAuctionAction({
|
||||
...input,
|
||||
action,
|
||||
request: { ...request, clientObservedPhase: phase },
|
||||
serverState: { ...state, phase },
|
||||
currentState: { ...state, phase },
|
||||
}),
|
||||
true,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
resolveAuctionErrorMessage,
|
||||
resolveAuctionRejectionMessage,
|
||||
} from "../src/api/auctionRejectionMessages.ts";
|
||||
|
||||
const rosterMessage = "팀 배정 상태를 확인해야 해 경매를 완료하지 못했습니다.";
|
||||
|
||||
test("shows the common error message when roster validation blocks completion", () => {
|
||||
assert.equal(
|
||||
resolveAuctionRejectionMessage({
|
||||
auctionRejectionReason:
|
||||
"AUCTION_TRANSITION_REJECTED_BECAUSE_ROSTER_INVALID",
|
||||
code: "AUCTION_ROSTER_INVALID",
|
||||
message: "Auction roster validation failed.",
|
||||
}),
|
||||
rosterMessage,
|
||||
);
|
||||
});
|
||||
|
||||
test("shows the same roster message for the socket error event", () => {
|
||||
assert.equal(
|
||||
resolveAuctionErrorMessage("Auction roster validation failed."),
|
||||
rosterMessage,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user