fix: sync auction timer with server time

This commit is contained in:
dahyeon committed 2026-06-22 12:16:18 -07:00
1 parent d2fe4604dd
commit 98c2323ff2
2 files changed
+24 -1

No files matched your search

+18
View File
@@ -30,6 +30,11 @@ interface ResetPayload {
matchId?: string; matchId?: string;
} }
interface TickPayload {
deadline?: string;
serverTime?: string;
}
const MAX_CHAT_MESSAGES = 200; const MAX_CHAT_MESSAGES = 200;
function mergeChatMessages( function mergeChatMessages(
@@ -55,6 +60,7 @@ export function useAuctionSocket(matchId: string | undefined) {
const [connectionStatus, setConnectionStatus] = const [connectionStatus, setConnectionStatus] =
useState<ConnectionStatus>('idle'); useState<ConnectionStatus>('idle');
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [serverTimeOffsetMs, setServerTimeOffsetMs] = useState(0);
useEffect(() => { useEffect(() => {
if (!matchId) { if (!matchId) {
@@ -100,6 +106,16 @@ export function useAuctionSocket(matchId: string | undefined) {
setError(null); setError(null);
}); });
socket.on('tick', (payload: TickPayload) => {
const serverTime =
typeof payload?.serverTime === 'string'
? Date.parse(payload.serverTime)
: NaN;
if (Number.isFinite(serverTime)) {
setServerTimeOffsetMs(serverTime - Date.now());
}
});
socket.on('chat-history', (payload: ChatHistoryPayload) => { socket.on('chat-history', (payload: ChatHistoryPayload) => {
setChatMessages(mergeChatMessages([], payload.messages ?? [])); setChatMessages(mergeChatMessages([], payload.messages ?? []));
}); });
@@ -129,6 +145,7 @@ export function useAuctionSocket(matchId: string | undefined) {
socket?.disconnect(); socket?.disconnect();
socketRef.current = null; socketRef.current = null;
setConnectionStatus('idle'); setConnectionStatus('idle');
setServerTimeOffsetMs(0);
}; };
}, [authUserId, matchId]); }, [authUserId, matchId]);
@@ -160,6 +177,7 @@ export function useAuctionSocket(matchId: string | undefined) {
resetAt, resetAt,
connectionStatus: visibleConnectionStatus, connectionStatus: visibleConnectionStatus,
error, error,
serverTimeOffsetMs: matchId ? serverTimeOffsetMs : 0,
placeBid(payload: BidPayload) { placeBid(payload: BidPayload) {
emit('bid', payload); emit('bid', payload);
}, },
+6 -1
View File
@@ -781,9 +781,14 @@ export default function AuctionPage() {
}, [showCancelModal]); }, [showCancelModal]);
const liveAuction = pickLatestAuction(socket.auction, auctionQuery.data); const liveAuction = pickLatestAuction(socket.auction, auctionQuery.data);
const syncedNow = now + socket.serverTimeOffsetMs;
const auctionState = const auctionState =
liveAuction && matchQuery.data liveAuction && matchQuery.data
? mapApiAuctionToAuctionState(liveAuction, matchQuery.data, new Date(now)) ? mapApiAuctionToAuctionState(
liveAuction,
matchQuery.data,
new Date(syncedNow),
)
: null; : null;
const availableTeamIndexes = useMemo(() => { const availableTeamIndexes = useMemo(() => {
const indexes = new Set((auctionState?.teams ?? []).map((team) => team.teamIndex)); const indexes = new Set((auctionState?.teams ?? []).map((team) => team.teamIndex));