feat: 메인 현재 시각을 최근 통신 기준으로 갱신
완료 턴 시각 대신 lobby serverTime을 분 경계에 맞춰 표시한다. 게임 API 응답과 SSE 및 같은 계정 탭 전달을 최근 통신으로 기록하고, 45초 공백에는 시계를 멈춘다. 공통 시계 투영과 단위 및 Chromium 회귀 검증을 추가한다.
This commit is contained in:
@@ -3,6 +3,7 @@ import { computed, onUnmounted, ref, watch } from 'vue';
|
||||
import { addMinutes } from 'date-fns';
|
||||
import ReservedCommandEditor from '../command/ReservedCommandEditor.vue';
|
||||
import { formatLocalDateTime, formatLocalTimeSeconds } from '../../utils/legacyDateTime';
|
||||
import { projectServerClock, sampleServerClock, type SampledServerClock } from '../../utils/serverClockProjection';
|
||||
import type {
|
||||
CommandMapData,
|
||||
CommandMapLayout,
|
||||
@@ -90,27 +91,20 @@ const autonomousUntil = computed(() => {
|
||||
|
||||
const currentServerTime = ref('--:--:--');
|
||||
const MAX_SERVER_CLOCK_TIMER_DELAY_MS = 60_000;
|
||||
let sampledServerTimeMs: number | null = null;
|
||||
let sampledClientTimeMs = 0;
|
||||
let sampledStartDelayMs: number | null = 0;
|
||||
let serverClockSample: SampledServerClock | null = null;
|
||||
let serverClockTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const updateServerClock = () => {
|
||||
if (serverClockTimer !== undefined) clearTimeout(serverClockTimer);
|
||||
serverClockTimer = undefined;
|
||||
if (sampledServerTimeMs === null) {
|
||||
if (serverClockSample === null) {
|
||||
currentServerTime.value = '--:--:--';
|
||||
return;
|
||||
}
|
||||
const clientElapsedMs = Math.max(0, Date.now() - sampledClientTimeMs);
|
||||
const elapsedGameMs =
|
||||
props.clockMode === 'manual' || sampledStartDelayMs === null
|
||||
? 0
|
||||
: Math.max(0, clientElapsedMs - sampledStartDelayMs);
|
||||
const projectedTime = new Date(sampledServerTimeMs + elapsedGameMs);
|
||||
const { clientElapsedMs, time: projectedTime } = projectServerClock(serverClockSample);
|
||||
currentServerTime.value = formatLocalTimeSeconds(projectedTime);
|
||||
if (props.clockMode !== 'manual' && sampledStartDelayMs !== null) {
|
||||
const untilStartMs = sampledStartDelayMs - clientElapsedMs;
|
||||
if (serverClockSample.clockMode !== 'manual' && serverClockSample.startDelayMs !== null) {
|
||||
const untilStartMs = serverClockSample.startDelayMs - clientElapsedMs;
|
||||
serverClockTimer = setTimeout(
|
||||
updateServerClock,
|
||||
untilStartMs > 0
|
||||
@@ -123,21 +117,7 @@ const updateServerClock = () => {
|
||||
watch(
|
||||
() => [props.serverTime, props.serverWallTime, props.clockMode, props.clockRunning, props.clockStartsAt] as const,
|
||||
([serverTime, serverWallTime, clockMode, clockRunning, clockStartsAt]) => {
|
||||
const parsed = serverTime ? new Date(serverTime).getTime() : Number.NaN;
|
||||
sampledServerTimeMs = Number.isFinite(parsed) ? parsed : null;
|
||||
sampledClientTimeMs = Date.now();
|
||||
if (clockMode === 'manual') {
|
||||
sampledStartDelayMs = null;
|
||||
} else if (clockRunning !== false) {
|
||||
sampledStartDelayMs = 0;
|
||||
} else {
|
||||
const wallTimeMs = serverWallTime ? new Date(serverWallTime).getTime() : Number.NaN;
|
||||
const startsAtMs = clockStartsAt ? new Date(clockStartsAt).getTime() : Number.NaN;
|
||||
sampledStartDelayMs =
|
||||
Number.isFinite(wallTimeMs) && Number.isFinite(startsAtMs)
|
||||
? Math.max(0, startsAtMs - wallTimeMs)
|
||||
: null;
|
||||
}
|
||||
serverClockSample = sampleServerClock({ serverTime, serverWallTime, clockMode, clockRunning, clockStartsAt });
|
||||
updateServerClock();
|
||||
},
|
||||
{ immediate: true }
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
import { formatServerDateTime } from '@sammo-ts/common/time/ServerDateTime';
|
||||
import { computed } from 'vue';
|
||||
import { computed, onUnmounted, ref, watch } from 'vue';
|
||||
import { resolveTournamentStageName } from '../../utils/tournamentStatus';
|
||||
import {
|
||||
GAME_SERVER_ACTIVITY_FRESHNESS_MS,
|
||||
gameServerActivity,
|
||||
isRecentGameServerActivity,
|
||||
} from '../../utils/gameServerActivity';
|
||||
import {
|
||||
millisecondsUntilNextMinute,
|
||||
projectServerClock,
|
||||
sampleServerClock,
|
||||
type SampledServerClock,
|
||||
} from '../../utils/serverClockProjection';
|
||||
|
||||
const props = defineProps<{
|
||||
tournamentStage: number;
|
||||
serverTime?: string;
|
||||
serverWallTime?: string;
|
||||
clockMode?: 'realtime' | 'manual';
|
||||
clockRunning?: boolean;
|
||||
clockStartsAt?: string | null;
|
||||
status: {
|
||||
onlineUserCount: number;
|
||||
onlineNations: string;
|
||||
@@ -20,16 +36,75 @@ const props = defineProps<{
|
||||
}>();
|
||||
|
||||
const tournamentStatus = computed(() => resolveTournamentStageName(props.tournamentStage));
|
||||
const lastExecutedStatus = computed(() =>
|
||||
formatServerDateTime(props.status?.lastExecuted, { format: 'monthDayTime', fallback: '기록 없음' })
|
||||
const currentServerTime = ref('기록 없음');
|
||||
const hasServerClock = ref(false);
|
||||
const serverClockFresh = ref(false);
|
||||
const serverClockTitle = computed(() => {
|
||||
if (!hasServerClock.value) return '서버 시각을 아직 받지 못했습니다.';
|
||||
if (!serverClockFresh.value) return '최근 45초 동안 서버 통신이 없어 시각 갱신을 멈췄습니다.';
|
||||
return undefined;
|
||||
});
|
||||
|
||||
let serverClockSample: SampledServerClock | null = null;
|
||||
let serverClockTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const updateServerClock = () => {
|
||||
if (serverClockTimer !== undefined) clearTimeout(serverClockTimer);
|
||||
serverClockTimer = undefined;
|
||||
if (serverClockSample === null) {
|
||||
currentServerTime.value = '기록 없음';
|
||||
hasServerClock.value = false;
|
||||
serverClockFresh.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const projection = projectServerClock(serverClockSample, now);
|
||||
currentServerTime.value = formatServerDateTime(projection.time, {
|
||||
format: 'monthDayTime',
|
||||
fallback: '기록 없음',
|
||||
});
|
||||
hasServerClock.value = true;
|
||||
|
||||
const lastContactAt = gameServerActivity.lastContactAt.value;
|
||||
serverClockFresh.value = isRecentGameServerActivity(lastContactAt, now);
|
||||
if (!serverClockFresh.value || lastContactAt === null) return;
|
||||
|
||||
const nextDelays = [lastContactAt + GAME_SERVER_ACTIVITY_FRESHNESS_MS - now + 1];
|
||||
if (serverClockSample.clockMode !== 'manual' && serverClockSample.startDelayMs !== null) {
|
||||
const untilStartMs = serverClockSample.startDelayMs - projection.clientElapsedMs;
|
||||
nextDelays.push(untilStartMs > 0 ? untilStartMs : millisecondsUntilNextMinute(projection.time));
|
||||
}
|
||||
serverClockTimer = setTimeout(updateServerClock, Math.max(1, Math.min(...nextDelays)));
|
||||
};
|
||||
|
||||
watch(
|
||||
() => [props.serverTime, props.serverWallTime, props.clockMode, props.clockRunning, props.clockStartsAt] as const,
|
||||
([serverTime, serverWallTime, clockMode, clockRunning, clockStartsAt]) => {
|
||||
serverClockSample = sampleServerClock({ serverTime, serverWallTime, clockMode, clockRunning, clockStartsAt });
|
||||
updateServerClock();
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
watch(() => gameServerActivity.lastContactAt.value, updateServerClock);
|
||||
|
||||
onUnmounted(() => {
|
||||
if (serverClockTimer !== undefined) clearTimeout(serverClockTimer);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="front-status" aria-label="접속 현황과 국가 방침">
|
||||
<div class="activity-status" aria-label="동작 시각, 토너먼트와 설문 진행 현황">
|
||||
<div class="status-row execution-status" :class="{ 'execution-status--empty': !status?.lastExecuted }">
|
||||
동작 시각: {{ lastExecutedStatus }}
|
||||
<div class="activity-status" aria-label="현재 시각, 토너먼트와 설문 진행 현황">
|
||||
<div
|
||||
class="status-row execution-status"
|
||||
:class="{
|
||||
'execution-status--empty': !hasServerClock,
|
||||
'execution-status--stale': hasServerClock && !serverClockFresh,
|
||||
}"
|
||||
:title="serverClockTitle"
|
||||
>
|
||||
현재 시각: {{ currentServerTime }}
|
||||
</div>
|
||||
<div class="status-row tournament-status">
|
||||
<RouterLink to="/tournament">
|
||||
@@ -120,6 +195,10 @@ const lastExecutedStatus = computed(() =>
|
||||
color: magenta;
|
||||
}
|
||||
|
||||
.execution-status--stale {
|
||||
color: magenta;
|
||||
}
|
||||
|
||||
.vote-label {
|
||||
color: cyan;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
import { createBroadcastTabCoordinator, type BroadcastTabCoordinator } from '../utils/broadcastTabCoordinator';
|
||||
import { resolveWithReadModelSnapshotFallback } from '../utils/readModelDeltaRecovery';
|
||||
import { createRealtimeRequestOptions } from '../utils/realtimeAccessGrant';
|
||||
import { markGameServerContact } from '../utils/gameServerActivity';
|
||||
|
||||
const REALTIME_FULL_REFRESH_MIN_INTERVAL_MS = 5_000;
|
||||
|
||||
@@ -1097,10 +1098,12 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
onPayload: (message) => {
|
||||
if (!isRealtimeParticipant()) return;
|
||||
if (message.kind === 'patch') {
|
||||
markGameServerContact();
|
||||
applyDashboardPatch(message.patch);
|
||||
return;
|
||||
}
|
||||
realtimeStatus.value = message.status;
|
||||
if (message.status === 'connected') markGameServerContact();
|
||||
},
|
||||
});
|
||||
realtimeCoordinator.start();
|
||||
@@ -1153,6 +1156,7 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
realtimeSource = source;
|
||||
|
||||
source.addEventListener('open', () => {
|
||||
markGameServerContact();
|
||||
realtimeStatus.value = 'connected';
|
||||
realtimeCoordinator?.postFromLeader({ kind: 'status', status: 'connected' });
|
||||
});
|
||||
@@ -1166,6 +1170,7 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
if (!payload || payload.type !== 'readModelInvalidated') {
|
||||
return;
|
||||
}
|
||||
markGameServerContact();
|
||||
readModelRefreshQueue.request(payload.invalidation, payload.refreshGrant);
|
||||
});
|
||||
source.addEventListener('messagesInvalidated', (event) => {
|
||||
@@ -1174,6 +1179,7 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
if (!payload || payload.type !== 'messagesInvalidated') {
|
||||
return;
|
||||
}
|
||||
markGameServerContact();
|
||||
void refreshMessages(payload.refreshGrant);
|
||||
});
|
||||
|
||||
@@ -1182,14 +1188,17 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
for (const legacyEventType of ['turnCompleted', 'readModelChanged'] as const) {
|
||||
source.addEventListener(legacyEventType, () => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
markGameServerContact();
|
||||
realtimeRefreshQueue.request();
|
||||
});
|
||||
}
|
||||
source.addEventListener('messageCreated', () => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
markGameServerContact();
|
||||
void refreshMessages();
|
||||
});
|
||||
source.addEventListener('ping', () => {
|
||||
markGameServerContact();
|
||||
if (realtimeEnabled.value) {
|
||||
realtimeStatus.value = 'connected';
|
||||
realtimeCoordinator?.postFromLeader({ kind: 'status', status: 'connected' });
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { readonly, ref, type Ref } from 'vue';
|
||||
|
||||
export const GAME_SERVER_ACTIVITY_FRESHNESS_MS = 45_000;
|
||||
|
||||
export type GameServerActivityTracker = {
|
||||
lastContactAt: Readonly<Ref<number | null>>;
|
||||
markContact: (contactAt?: number) => void;
|
||||
};
|
||||
|
||||
export const createGameServerActivityTracker = (): GameServerActivityTracker => {
|
||||
const lastContactAt = ref<number | null>(null);
|
||||
|
||||
return {
|
||||
lastContactAt: readonly(lastContactAt),
|
||||
markContact(contactAt = Date.now()) {
|
||||
if (!Number.isFinite(contactAt)) return;
|
||||
lastContactAt.value = contactAt;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const isRecentGameServerActivity = (
|
||||
lastContactAt: number | null,
|
||||
now = Date.now(),
|
||||
freshnessMs = GAME_SERVER_ACTIVITY_FRESHNESS_MS
|
||||
): boolean =>
|
||||
lastContactAt !== null &&
|
||||
Number.isFinite(lastContactAt) &&
|
||||
Number.isFinite(now) &&
|
||||
Math.max(0, now - lastContactAt) <= freshnessMs;
|
||||
|
||||
export const gameServerActivity = createGameServerActivityTracker();
|
||||
|
||||
export const markGameServerContact = (contactAt = Date.now()) => gameServerActivity.markContact(contactAt);
|
||||
@@ -0,0 +1,67 @@
|
||||
export type ServerClockProjectionInput = {
|
||||
serverTime?: string;
|
||||
serverWallTime?: string;
|
||||
clockMode?: 'realtime' | 'manual';
|
||||
clockRunning?: boolean;
|
||||
clockStartsAt?: string | null;
|
||||
};
|
||||
|
||||
export type SampledServerClock = {
|
||||
serverTimeMs: number;
|
||||
sampledClientTimeMs: number;
|
||||
clockMode: 'realtime' | 'manual';
|
||||
startDelayMs: number | null;
|
||||
};
|
||||
|
||||
const parseInstant = (value?: string | null): number | null => {
|
||||
if (!value) return null;
|
||||
const parsed = new Date(value).getTime();
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
};
|
||||
|
||||
export const sampleServerClock = (
|
||||
input: ServerClockProjectionInput,
|
||||
sampledClientTimeMs = Date.now()
|
||||
): SampledServerClock | null => {
|
||||
const serverTimeMs = parseInstant(input.serverTime);
|
||||
if (serverTimeMs === null) return null;
|
||||
|
||||
let startDelayMs: number | null;
|
||||
if (input.clockMode === 'manual') {
|
||||
startDelayMs = null;
|
||||
} else if (input.clockRunning !== false) {
|
||||
startDelayMs = 0;
|
||||
} else {
|
||||
const serverWallTimeMs = parseInstant(input.serverWallTime);
|
||||
const clockStartsAtMs = parseInstant(input.clockStartsAt);
|
||||
startDelayMs =
|
||||
serverWallTimeMs !== null && clockStartsAtMs !== null
|
||||
? Math.max(0, clockStartsAtMs - serverWallTimeMs)
|
||||
: null;
|
||||
}
|
||||
|
||||
return {
|
||||
serverTimeMs,
|
||||
sampledClientTimeMs,
|
||||
clockMode: input.clockMode ?? 'realtime',
|
||||
startDelayMs,
|
||||
};
|
||||
};
|
||||
|
||||
export const projectServerClock = (sample: SampledServerClock, clientTimeMs = Date.now()) => {
|
||||
const clientElapsedMs = Math.max(0, clientTimeMs - sample.sampledClientTimeMs);
|
||||
const elapsedGameMs =
|
||||
sample.clockMode === 'manual' || sample.startDelayMs === null
|
||||
? 0
|
||||
: Math.max(0, clientElapsedMs - sample.startDelayMs);
|
||||
|
||||
return {
|
||||
clientElapsedMs,
|
||||
time: new Date(sample.serverTimeMs + elapsedGameMs),
|
||||
};
|
||||
};
|
||||
|
||||
export const millisecondsUntilNextMinute = (time: Date): number => {
|
||||
const remainder = ((time.getTime() % 60_000) + 60_000) % 60_000;
|
||||
return remainder === 0 ? 60_000 : 60_000 - remainder;
|
||||
};
|
||||
@@ -3,6 +3,7 @@ import { REALTIME_ACCESS_GRANT_HEADER } from '@sammo-ts/common/realtime/types';
|
||||
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
|
||||
import type { AppRouter } from '@sammo-ts/game-api';
|
||||
import { resolveBatchRealtimeAccessGrant } from './realtimeAccessGrant';
|
||||
import { markGameServerContact } from './gameServerActivity';
|
||||
|
||||
const getGameToken = (): string | null => {
|
||||
if (typeof window === 'undefined') {
|
||||
@@ -17,6 +18,11 @@ export const trpc = createTRPCProxyClient<AppRouter>({
|
||||
httpBatchLink({
|
||||
url: import.meta.env.VITE_GAME_API_URL ?? '/api/trpc',
|
||||
...trpcJsonBodyHttpClientOptions,
|
||||
async fetch(input, init) {
|
||||
const result = await globalThis.fetch(input, init);
|
||||
markGameServerContact();
|
||||
return result;
|
||||
},
|
||||
headers({ opList }) {
|
||||
const token = getGameToken();
|
||||
const refreshGrant = resolveBatchRealtimeAccessGrant(opList);
|
||||
|
||||
@@ -243,7 +243,15 @@ watch(
|
||||
</div>
|
||||
|
||||
<div data-main-target="policy">
|
||||
<MainFrontStatus :status="frontStatus" :tournament-stage="tournamentStage" />
|
||||
<MainFrontStatus
|
||||
:status="frontStatus"
|
||||
:tournament-stage="tournamentStage"
|
||||
:server-time="lobbyInfo?.serverTime"
|
||||
:server-wall-time="lobbyInfo?.serverWallTime"
|
||||
:clock-mode="lobbyInfo?.clockMode"
|
||||
:clock-running="lobbyInfo?.clockRunning"
|
||||
:clock-starts-at="lobbyInfo?.clockStartsAt"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<aside v-if="surveyNotice" class="survey-notice" role="status" aria-live="polite">
|
||||
|
||||
Reference in New Issue
Block a user