From f6f764da7f8302848918d5060f17f281fe2d8795 Mon Sep 17 00:00:00 2001 From: hided62 Date: Fri, 21 Aug 2026 16:57:42 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=EA=B0=80=EC=98=A4=ED=94=88=20=EB=8F=99?= =?UTF-8?q?=EC=95=88=20=EC=84=9C=EB=B2=84=20=EA=B2=8C=EC=9E=84=20=EC=8B=9C?= =?UTF-8?q?=EA=B3=84=EB=A5=BC=20=EC=A0=95=EC=A7=80=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 정식 오픈 시각을 게임 시계 anchor로 전달하고, API가 실제 진행 상태와 시작 경계를 반환하게 한다. 프론트는 추가 조회 없이 경계에서 자동으로 시계를 시작한다. --- app/game-api/src/router/lobby/index.ts | 3 ++ app/game-api/src/services/gameClock.ts | 27 +++++++++- app/game-api/test/auctionWorker.test.ts | 3 ++ app/game-api/test/gameClock.test.ts | 52 +++++++++++++++++++ app/game-api/test/lobbyRouter.test.ts | 26 ++++++++++ .../src/scenario/scenarioSeeder.ts | 8 ++- app/game-engine/test/scenarioSeeder.test.ts | 4 ++ app/game-frontend/e2e/mainNavigation.spec.ts | 42 +++++++++++++++ .../src/components/main/CommandListPanel.vue | 40 +++++++++++--- app/game-frontend/src/views/MainView.vue | 6 +++ .../src/orchestrator/gatewayOrchestrator.ts | 2 + .../src/orchestrator/profileSeedCli.ts | 11 +++- .../test/profileSeedCli.integration.test.ts | 3 ++ app/gateway-api/test/profileSeedCli.test.ts | 7 ++- 14 files changed, 221 insertions(+), 13 deletions(-) create mode 100644 app/game-api/test/gameClock.test.ts diff --git a/app/game-api/src/router/lobby/index.ts b/app/game-api/src/router/lobby/index.ts index 0b611078..a2fe2dde 100644 --- a/app/game-api/src/router/lobby/index.ts +++ b/app/game-api/src/router/lobby/index.ts @@ -68,7 +68,10 @@ export const lobbyRouter = router({ preopenAt: worldState.meta.preopenAt ?? '', turntime: worldState.meta.turntime ?? '', serverTime: gameTime.now.toISOString(), + serverWallTime: gameTime.wallNow.toISOString(), clockMode: gameTime.mode ?? 'realtime', + clockRunning: gameTime.running, + clockStartsAt: gameTime.startsAt?.toISOString() ?? null, otherTextInfo: worldState.meta.otherTextInfo ?? '', npcMode: worldState.config.npcMode ?? 0, defaultStatTotal: asNumber(asRecord(rawConfig.stat).total, 165), diff --git a/app/game-api/src/services/gameClock.ts b/app/game-api/src/services/gameClock.ts index d8862e96..d64a103d 100644 --- a/app/game-api/src/services/gameClock.ts +++ b/app/game-api/src/services/gameClock.ts @@ -4,14 +4,25 @@ import type { DatabaseClient } from '../context.js'; export interface CurrentGameTime { now: Date; + wallNow: Date; tick: number | null; mode: GameClockMode | null; + running: boolean; + startsAt: Date | null; dateToTick(date: Date): number | null; } export const loadCurrentGameTime = async (db: DatabaseClient, wallNow = new Date()): Promise => { if (!db.worldState) { - return { now: wallNow, tick: null, mode: null, dateToTick: () => null }; + return { + now: wallNow, + wallNow, + tick: null, + mode: null, + running: true, + startsAt: null, + dateToTick: () => null, + }; } const state = await db.worldState.findFirst({ orderBy: { id: 'asc' }, @@ -24,7 +35,15 @@ export const loadCurrentGameTime = async (db: DatabaseClient, wallNow = new Date }, }); if (!state?.clockBaseTime || state.clockTick === null || !state.clockWallAnchor) { - return { now: wallNow, tick: null, mode: null, dateToTick: () => null }; + return { + now: wallNow, + wallNow, + tick: null, + mode: null, + running: true, + startsAt: null, + dateToTick: () => null, + }; } const mode: GameClockMode = state.clockMode === 'manual' ? 'manual' : 'realtime'; const storedTick = Number(state.clockTick); @@ -39,10 +58,14 @@ export const loadCurrentGameTime = async (db: DatabaseClient, wallNow = new Date turnSeconds: state.tickSeconds, }); const tick = clock.nowTick(wallNow); + const running = mode === 'realtime' && wallNow.getTime() >= state.clockWallAnchor.getTime(); return { now: clock.tickToDate(tick), + wallNow, tick, mode, + running, + startsAt: mode === 'realtime' && !running ? state.clockWallAnchor : null, dateToTick: (date) => clock.dateToTick(date), }; }; diff --git a/app/game-api/test/auctionWorker.test.ts b/app/game-api/test/auctionWorker.test.ts index 8c98cd83..cab054e9 100644 --- a/app/game-api/test/auctionWorker.test.ts +++ b/app/game-api/test/auctionWorker.test.ts @@ -57,8 +57,11 @@ describe('auction worker clock-shift race', () => { const now = new Date('2026-07-30T12:00:00.000Z'); const time = { now, + wallNow: now, tick: 36_000_000, mode: 'manual' as const, + running: false, + startsAt: null, dateToTick: () => 72_000_000, }; const closeAt = new Date('2099-01-01T00:00:00.000Z'); diff --git a/app/game-api/test/gameClock.test.ts b/app/game-api/test/gameClock.test.ts new file mode 100644 index 00000000..9d746e11 --- /dev/null +++ b/app/game-api/test/gameClock.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, it, vi } from 'vitest'; + +import type { DatabaseClient } from '../src/context.js'; +import { loadCurrentGameTime } from '../src/services/gameClock.js'; + +const buildDatabase = (mode: 'realtime' | 'manual' = 'realtime'): DatabaseClient => + ({ + worldState: { + findFirst: vi.fn(async () => ({ + clockBaseTime: new Date('2026-08-21T09:50:00.000Z'), + clockTick: 36_000_000n, + clockMode: mode, + clockWallAnchor: new Date('2026-08-21T11:00:00.000Z'), + tickSeconds: 600, + })), + }, + }) as unknown as DatabaseClient; + +describe('current game time projection', () => { + it('holds a realtime clock at its persisted tick until the future wall anchor', async () => { + const db = buildDatabase(); + + const preopen = await loadCurrentGameTime(db, new Date('2026-08-21T10:30:00.000Z')); + expect(preopen).toMatchObject({ + now: new Date('2026-08-21T10:00:00.000Z'), + wallNow: new Date('2026-08-21T10:30:00.000Z'), + tick: 36_000_000, + mode: 'realtime', + running: false, + startsAt: new Date('2026-08-21T11:00:00.000Z'), + }); + + const opened = await loadCurrentGameTime(db, new Date('2026-08-21T11:00:05.000Z')); + expect(opened).toMatchObject({ + now: new Date('2026-08-21T10:00:05.000Z'), + tick: 36_300_000, + running: true, + startsAt: null, + }); + }); + + it('keeps a manual clock stopped without scheduling an automatic start', async () => { + const result = await loadCurrentGameTime(buildDatabase('manual'), new Date('2026-08-21T12:00:00.000Z')); + + expect(result).toMatchObject({ + now: new Date('2026-08-21T10:00:00.000Z'), + tick: 36_000_000, + running: false, + startsAt: null, + }); + }); +}); diff --git a/app/game-api/test/lobbyRouter.test.ts b/app/game-api/test/lobbyRouter.test.ts index fe228c12..c9c09ec8 100644 --- a/app/game-api/test/lobbyRouter.test.ts +++ b/app/game-api/test/lobbyRouter.test.ts @@ -68,6 +68,32 @@ describe('lobby season state', () => { expect(result.serverTime).toBe('2026-08-15T02:00:00.000Z'); expect(result.clockMode).toBe('manual'); + expect(result.clockRunning).toBe(false); + expect(result.clockStartsAt).toBeNull(); + expect(new Date(result.serverWallTime).getTime()).not.toBeNaN(); + }); + + it('exposes the future realtime wall anchor without advancing the preopen clock', async () => { + const wallAnchor = new Date('2099-08-21T11:00:00.000Z'); + const result = await appRouter + .createCaller( + buildContext( + {}, + { + baseTime: new Date('2026-08-21T09:00:00.000Z'), + tick: 36_000_000n, + mode: 'realtime', + wallAnchor, + } + ) + ) + .lobby.info(); + + expect(result.serverTime).toBe('2026-08-21T10:00:00.000Z'); + expect(result.clockMode).toBe('realtime'); + expect(result.clockRunning).toBe(false); + expect(result.clockStartsAt).toBe(wallAnchor.toISOString()); + expect(new Date(result.serverWallTime).getTime()).toBeLessThan(wallAnchor.getTime()); }); it('preserves zero as the first official game index', async () => { diff --git a/app/game-engine/src/scenario/scenarioSeeder.ts b/app/game-engine/src/scenario/scenarioSeeder.ts index 5bf328fe..fabe1c5d 100644 --- a/app/game-engine/src/scenario/scenarioSeeder.ts +++ b/app/game-engine/src/scenario/scenarioSeeder.ts @@ -50,6 +50,7 @@ export interface ScenarioInstallOptions { joinMode?: 'full' | 'onlyRandom'; autorunUser?: ScenarioAutorunOptions | null; preopenAt?: Date | null; + openAt?: Date | null; season?: number; firstGameIdx?: number; serverId?: string; @@ -229,11 +230,14 @@ export const seedScenarioToDatabase = async (options: ScenarioSeedOptions): Prom const sync = install?.sync ?? false; const startState = resolveStartState(scenario.startYear ?? null, now, turnTermMinutes, sync); const gameClockMode = options.gameClockMode ?? 'realtime'; + // A realtime season prepared before its formal opening must not consume + // wall time while users are only allowed to edit reserved commands. + const initialClockWallAnchor = install?.openAt && install.openAt.getTime() > now.getTime() ? install.openAt : now; const initialClock = new GameClock({ baseTime: startState.startTime, tick: 0, mode: gameClockMode, - wallAnchor: now, + wallAnchor: initialClockWallAnchor, turnSeconds: tickSeconds, }); const initialClockTick = initialClock.dateToTick(now); @@ -410,7 +414,7 @@ export const seedScenarioToDatabase = async (options: ScenarioSeedOptions): Prom clockBaseTime: initialClock.baseTime, clockTick: BigInt(initialClockTick), clockMode: gameClockMode, - clockWallAnchor: now, + clockWallAnchor: initialClock.wallAnchor, lastTurnTick: BigInt(initialClockTick), config: asJson({ ...scenarioConfig, ...worldConfig }), meta: asJson(worldMeta), diff --git a/app/game-engine/test/scenarioSeeder.test.ts b/app/game-engine/test/scenarioSeeder.test.ts index 10ea88b4..2e072812 100644 --- a/app/game-engine/test/scenarioSeeder.test.ts +++ b/app/game-engine/test/scenarioSeeder.test.ts @@ -50,6 +50,7 @@ type ScenarioSeederPrismaClient = { tickSeconds: number; currentYear: number; currentMonth: number; + clockWallAnchor: Date | null; } | null>; }; gameHistory: { @@ -364,6 +365,8 @@ describeDb('scenario database seed', () => { develop: true, }, }, + preopenAt: new Date('2030-01-01T01:00:00Z'), + openAt: new Date('2030-01-01T02:00:00Z'), }, }); @@ -385,6 +388,7 @@ describeDb('scenario database seed', () => { } expect(worldState.tickSeconds).toBe(3600); expect(worldState.currentMonth).toBe(1); + expect(worldState.clockWallAnchor).toEqual(new Date('2030-01-01T02:00:00.000Z')); const config = (worldState.config ?? {}) as Record; expect(config.extendedGeneral).toBe(false); diff --git a/app/game-frontend/e2e/mainNavigation.spec.ts b/app/game-frontend/e2e/mainNavigation.spec.ts index 2f5f31ea..12e29e3c 100644 --- a/app/game-frontend/e2e/mainNavigation.spec.ts +++ b/app/game-frontend/e2e/mainNavigation.spec.ts @@ -34,7 +34,10 @@ type NavigationFixture = { generalName?: string; generalTurnTime?: string; serverTime?: string; + serverWallTime?: string; clockMode?: 'realtime' | 'manual'; + clockRunning?: boolean; + clockStartsAt?: string | null; cityDefence?: number; cityState?: number; nationRate?: number; @@ -555,7 +558,10 @@ const installFixture = async (page: Page, state: NavigationFixture) => { month: state.currentMonth ?? 1, turnTerm: 10, serverTime: state.serverTime ?? '2026-08-13T00:00:00.000Z', + serverWallTime: state.serverWallTime ?? '2026-08-13T00:00:00.000Z', clockMode: state.clockMode ?? 'realtime', + clockRunning: state.clockRunning ?? true, + clockStartsAt: state.clockStartsAt ?? null, scenarioTitle: state.scenarioTitle ?? '', }); } @@ -1711,12 +1717,48 @@ test('main general card uses local turn time and command clock tracks corrected } state.clockMode = 'manual'; + state.clockRunning = false; state.serverTime = '2026-08-13T00:08:30.000Z'; await page.reload(); const frozenClock = page.locator('[data-main-target="commands"] [data-command-current-time]').first(); await expect(frozenClock).toHaveText('09:08:30'); await page.clock.runFor(2_000); await expect(frozenClock).toHaveText('09:08:30'); + + state.clockMode = 'realtime'; + state.clockRunning = false; + state.serverTime = '2026-08-13T00:10:00.000Z'; + state.serverWallTime = '2026-08-21T10:00:00.000Z'; + state.clockStartsAt = '2026-08-21T10:00:02.000Z'; + await page.reload(); + const preopenClock = page.locator('[data-main-target="commands"] [data-command-current-time]').first(); + await expect(preopenClock).toHaveText('09:10:00'); + const operationsBeforePreopenBoundary = state.operations.length; + await page.clock.runFor(1_500); + await expect(preopenClock).toHaveText('09:10:00'); + if (artifactRoot) { + const preopenGeometry = await preopenClock.evaluate((element) => ({ + rect: element.getBoundingClientRect().toJSON(), + overflow: element.scrollWidth - element.clientWidth, + fontSize: getComputedStyle(element).fontSize, + lineHeight: getComputedStyle(element).lineHeight, + value: element.textContent, + })); + expect(preopenGeometry.overflow).toBeLessThanOrEqual(0); + await Promise.all([ + page.screenshot({ + path: resolve(artifactRoot, 'main-preopen-clock-frozen-mobile-500.png'), + fullPage: true, + }), + writeFile( + resolve(artifactRoot, 'main-preopen-clock-frozen-mobile-500.json'), + `${JSON.stringify(preopenGeometry, null, 2)}\n` + ), + ]); + } + await page.clock.runFor(1_500); + await expect(preopenClock).toHaveText('09:10:01'); + expect(state.operations).toHaveLength(operationsBeforePreopenBoundary); }); test('message targets keep reply behavior and use nation-color contrast in labels and select options', async ({ diff --git a/app/game-frontend/src/components/main/CommandListPanel.vue b/app/game-frontend/src/components/main/CommandListPanel.vue index 82846b40..260037dd 100644 --- a/app/game-frontend/src/components/main/CommandListPanel.vue +++ b/app/game-frontend/src/components/main/CommandListPanel.vue @@ -20,7 +20,10 @@ const props = defineProps<{ currentMonth?: number; turnTermMinutes?: number; serverTime?: string; + serverWallTime?: string; clockMode?: 'realtime' | 'manual'; + clockRunning?: boolean; + clockStartsAt?: string | null; autorunLimit?: number | null; storageKey?: string; mapData?: CommandMapData | null; @@ -86,8 +89,10 @@ 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 serverClockTimer: ReturnType | undefined; const updateServerClock = () => { @@ -97,21 +102,42 @@ const updateServerClock = () => { currentServerTime.value = '--:--:--'; return; } - const projectedTime = new Date( - props.clockMode === 'manual' ? sampledServerTimeMs : sampledServerTimeMs + Date.now() - sampledClientTimeMs - ); + 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); currentServerTime.value = formatLocalTimeSeconds(projectedTime); - if (props.clockMode !== 'manual') { - serverClockTimer = setTimeout(updateServerClock, 1_000 - projectedTime.getMilliseconds()); + if (props.clockMode !== 'manual' && sampledStartDelayMs !== null) { + const untilStartMs = sampledStartDelayMs - clientElapsedMs; + serverClockTimer = setTimeout( + updateServerClock, + untilStartMs > 0 + ? Math.min(untilStartMs, MAX_SERVER_CLOCK_TIMER_DELAY_MS) + : 1_000 - projectedTime.getMilliseconds() + ); } }; watch( - () => [props.serverTime, props.clockMode] as const, - ([serverTime]) => { + () => [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; + } updateServerClock(); }, { immediate: true } diff --git a/app/game-frontend/src/views/MainView.vue b/app/game-frontend/src/views/MainView.vue index e69e0537..1b6618eb 100644 --- a/app/game-frontend/src/views/MainView.vue +++ b/app/game-frontend/src/views/MainView.vue @@ -267,7 +267,10 @@ watch( :current-month="lobbyInfo?.month" :turn-term-minutes="lobbyInfo?.turnTerm" :server-time="lobbyInfo?.serverTime" + :server-wall-time="lobbyInfo?.serverWallTime" :clock-mode="lobbyInfo?.clockMode" + :clock-running="lobbyInfo?.clockRunning" + :clock-starts-at="lobbyInfo?.clockStartsAt" :autorun-limit="reservedGeneralAutorunLimit" :map-data="worldMap" :map-layout="mapLayout" @@ -437,7 +440,10 @@ watch( :current-month="lobbyInfo?.month" :turn-term-minutes="lobbyInfo?.turnTerm" :server-time="lobbyInfo?.serverTime" + :server-wall-time="lobbyInfo?.serverWallTime" :clock-mode="lobbyInfo?.clockMode" + :clock-running="lobbyInfo?.clockRunning" + :clock-starts-at="lobbyInfo?.clockStartsAt" :autorun-limit="reservedGeneralAutorunLimit" :map-data="worldMap" :map-layout="mapLayout" diff --git a/app/gateway-api/src/orchestrator/gatewayOrchestrator.ts b/app/gateway-api/src/orchestrator/gatewayOrchestrator.ts index 9f653a0c..a7a1961b 100644 --- a/app/gateway-api/src/orchestrator/gatewayOrchestrator.ts +++ b/app/gateway-api/src/orchestrator/gatewayOrchestrator.ts @@ -396,6 +396,7 @@ const parseInstallOptions = ( joinMode: joinMode === 'full' || joinMode === 'onlyRandom' ? joinMode : undefined, autorunUser: autorunUser ?? null, preopenAt: preopenAt ?? null, + openAt: openAt ?? null, installOperationId: action.installOperationId, }; @@ -2173,6 +2174,7 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle { ? { ...options.installOptions, preopenAt: options.installOptions.preopenAt?.toISOString() ?? null, + openAt: options.installOptions.openAt?.toISOString() ?? null, } : undefined, adminUser: options.adminUser, diff --git a/app/gateway-api/src/orchestrator/profileSeedCli.ts b/app/gateway-api/src/orchestrator/profileSeedCli.ts index 7d7194cd..3bafe67d 100644 --- a/app/gateway-api/src/orchestrator/profileSeedCli.ts +++ b/app/gateway-api/src/orchestrator/profileSeedCli.ts @@ -9,7 +9,10 @@ interface ProfileSeedRequest { scenarioId: number; tickSeconds?: number; now: string; - installOptions?: Omit & { preopenAt?: string | null }; + installOptions?: Omit & { + preopenAt?: string | null; + openAt?: string | null; + }; adminUser?: AdminSeedUser | null; } @@ -49,6 +52,11 @@ export const runProfileSeedCli = async (env: NodeJS.ProcessEnv = process.env): P if (preopenAt && Number.isNaN(preopenAt.getTime())) { throw new Error('Profile seed preopenAt must be an ISO date-time.'); } + const rawOpenAt = request.installOptions?.openAt; + const openAt = typeof rawOpenAt === 'string' ? new Date(rawOpenAt) : null; + if (openAt && Number.isNaN(openAt.getTime())) { + throw new Error('Profile seed openAt must be an ISO date-time.'); + } const resourceRoot = path.join(process.cwd(), 'resources'); await seedProfileDatabase({ @@ -61,6 +69,7 @@ export const runProfileSeedCli = async (env: NodeJS.ProcessEnv = process.env): P ? { ...request.installOptions, preopenAt, + openAt, } : undefined, scenarioOptions: { scenarioRoot: path.join(resourceRoot, 'scenario') }, diff --git a/app/gateway-api/test/profileSeedCli.integration.test.ts b/app/gateway-api/test/profileSeedCli.integration.test.ts index d0c26e8d..7d3220e3 100644 --- a/app/gateway-api/test/profileSeedCli.integration.test.ts +++ b/app/gateway-api/test/profileSeedCli.integration.test.ts @@ -54,6 +54,8 @@ describeDatabase('selected workspace profile seed CLI', () => { firstGameIdx: 0, installOperationId: 'selected-cli-operation', installCommitSha: 'selected-cli-commit', + preopenAt: '2036-03-03T01:00:00.000Z', + openAt: '2036-03-03T02:00:00.000Z', }, adminUser: { id: 'selected-cli-admin', @@ -69,6 +71,7 @@ describeDatabase('selected workspace profile seed CLI', () => { const world = await connector.prisma.worldState.findFirstOrThrow(); expect(world).toMatchObject({ scenarioCode: '1010', + clockWallAnchor: new Date('2036-03-03T02:00:00.000Z'), meta: { firstGameIdx: 0, gameIdx: completedGameCount, diff --git a/app/gateway-api/test/profileSeedCli.test.ts b/app/gateway-api/test/profileSeedCli.test.ts index b0d5dfcc..62b11a39 100644 --- a/app/gateway-api/test/profileSeedCli.test.ts +++ b/app/gateway-api/test/profileSeedCli.test.ts @@ -13,13 +13,18 @@ describe('parseProfileSeedRequest', () => { installOperationId: 'operation-id', installCommitSha: 'abcdef', preopenAt: null, + openAt: '2030-01-01T02:00:00.000Z', }, adminUser: { id: 'admin', username: 'admin' }, }) ).toMatchObject({ scenarioId: 1010, tickSeconds: 60, - installOptions: { installOperationId: 'operation-id', installCommitSha: 'abcdef' }, + installOptions: { + installOperationId: 'operation-id', + installCommitSha: 'abcdef', + openAt: '2030-01-01T02:00:00.000Z', + }, }); });