feat: 실행 중 게임 옵션 변경을 지원
Gateway 관리 화면에서 현재 기수의 장수 생성 제한, 유저 자동턴, 턴 간격을 내구성 action으로 변경한다. 턴 간격 변경은 논리 tick과 현재 게임 시각을 보존하며 DB와 Redis의 tick 기반 시각을 재투영하고 기존 로그 timestamp는 유지한다.
This commit is contained in:
@@ -17,7 +17,10 @@ const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||
const integration = describe.skipIf(!databaseUrl);
|
||||
const requestId = 'integration:engine:runtime-clock-shift';
|
||||
const actionId = 'b9f68480-dba9-4e03-a62b-499e6234f18a';
|
||||
const generalIds = [990_301, 990_302] as const;
|
||||
const runtimeSettingsRequestId = 'integration:engine:runtime-game-settings';
|
||||
const runtimeSettingsActionId = 'c9f68480-dba9-4e03-a62b-499e6234f18a';
|
||||
const generalIds = [990_301, 990_302, 990_303] as const;
|
||||
const runtimeSettingsLogText = 'runtime-settings-existing-log';
|
||||
|
||||
const buildGeneral = (id: number, turnTime: Date): TurnGeneral =>
|
||||
({
|
||||
@@ -50,16 +53,23 @@ const buildGeneral = (id: number, turnTime: Date): TurnGeneral =>
|
||||
npcState: 0,
|
||||
}) as TurnGeneral;
|
||||
|
||||
const waitForSucceeded = async (db: GamePrismaClient): Promise<void> => {
|
||||
const waitForSucceeded = async (db: GamePrismaClient, targetRequestId = requestId): Promise<void> => {
|
||||
const deadline = Date.now() + 5_000;
|
||||
while (Date.now() < deadline) {
|
||||
const event = await db.inputEvent.findUnique({ where: { requestId }, select: { status: true } });
|
||||
const event = await db.inputEvent.findUnique({
|
||||
where: { requestId: targetRequestId },
|
||||
select: { status: true },
|
||||
});
|
||||
if (event?.status === 'SUCCEEDED') {
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 25));
|
||||
}
|
||||
throw new Error('runtime clock shift input event did not complete');
|
||||
const event = await db.inputEvent.findUnique({
|
||||
where: { requestId: targetRequestId },
|
||||
select: { status: true, error: true, attempts: true },
|
||||
});
|
||||
throw new Error(`runtime input event did not complete: ${JSON.stringify(event)}`);
|
||||
};
|
||||
|
||||
integration('runtime clock shift persistence', () => {
|
||||
@@ -71,17 +81,27 @@ integration('runtime clock shift persistence', () => {
|
||||
await connector.connect();
|
||||
db = connector.prisma;
|
||||
closeDb = () => connector.disconnect();
|
||||
await db.inputEvent.deleteMany({ where: { requestId } });
|
||||
await db.inputEvent.deleteMany({ where: { requestId: { in: [requestId, runtimeSettingsRequestId] } } });
|
||||
await db.votePoll.deleteMany({ where: { openerGeneralId: generalIds[2] } });
|
||||
await db.message.deleteMany({ where: { mailbox: generalIds[2] } });
|
||||
await db.logEntry.deleteMany({ where: { text: runtimeSettingsLogText } });
|
||||
await db.auction.deleteMany({ where: { hostGeneralId: { in: [...generalIds] } } });
|
||||
await db.general.deleteMany({ where: { id: { in: [...generalIds] } } });
|
||||
await db.worldState.deleteMany({ where: { scenarioCode: 'runtime-clock-shift' } });
|
||||
await db.worldState.deleteMany({
|
||||
where: { scenarioCode: { in: ['runtime-clock-shift', 'runtime-game-settings'] } },
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await db.inputEvent.deleteMany({ where: { requestId } });
|
||||
await db.inputEvent.deleteMany({ where: { requestId: { in: [requestId, runtimeSettingsRequestId] } } });
|
||||
await db.votePoll.deleteMany({ where: { openerGeneralId: generalIds[2] } });
|
||||
await db.message.deleteMany({ where: { mailbox: generalIds[2] } });
|
||||
await db.logEntry.deleteMany({ where: { text: runtimeSettingsLogText } });
|
||||
await db.auction.deleteMany({ where: { hostGeneralId: { in: [...generalIds] } } });
|
||||
await db.general.deleteMany({ where: { id: { in: [...generalIds] } } });
|
||||
await db.worldState.deleteMany({ where: { scenarioCode: 'runtime-clock-shift' } });
|
||||
await db.worldState.deleteMany({
|
||||
where: { scenarioCode: { in: ['runtime-clock-shift', 'runtime-game-settings'] } },
|
||||
});
|
||||
await closeDb?.();
|
||||
});
|
||||
|
||||
@@ -126,7 +146,7 @@ integration('runtime clock shift persistence', () => {
|
||||
db.auction.create({
|
||||
data: {
|
||||
type: 'BUY_RICE',
|
||||
hostGeneralId: generalIds[index % generalIds.length]!,
|
||||
hostGeneralId: generals[index % generals.length]!.id,
|
||||
detail: {},
|
||||
status,
|
||||
closeAt: new Date(`2099-07-30T1${index}:00:00.000Z`),
|
||||
@@ -278,4 +298,250 @@ integration('runtime clock shift persistence', () => {
|
||||
|
||||
await db.worldState.delete({ where: { id: row.id } });
|
||||
});
|
||||
|
||||
it('reprojects tick-owned dates for a live turn-term change without rewriting existing log timestamps', async () => {
|
||||
const base = new Date('2099-08-01T10:00:00.000Z');
|
||||
const row = await db.worldState.create({
|
||||
data: {
|
||||
scenarioCode: 'runtime-game-settings',
|
||||
currentYear: 191,
|
||||
currentMonth: 2,
|
||||
tickSeconds: 600,
|
||||
clockBaseTime: base,
|
||||
clockTick: 0,
|
||||
clockMode: 'manual',
|
||||
clockWallAnchor: base,
|
||||
lastTurnTick: 0,
|
||||
config: { turnTermMinutes: 10, blockGeneralCreate: 0 },
|
||||
meta: { lastTurnTime: base.toISOString(), turnterm: 10 },
|
||||
},
|
||||
});
|
||||
const general = {
|
||||
...buildGeneral(generalIds[2], new Date('2099-08-01T10:10:00.000Z')),
|
||||
turnTick: GAME_TICKS_PER_TURN,
|
||||
recentWarTime: new Date('2099-08-01T10:05:00.000Z'),
|
||||
recentWarTick: GAME_TICKS_PER_TURN / 2,
|
||||
};
|
||||
await db.general.create({
|
||||
data: {
|
||||
id: general.id,
|
||||
name: general.name,
|
||||
nationId: general.nationId,
|
||||
cityId: general.cityId,
|
||||
troopId: general.troopId,
|
||||
turnTime: general.turnTime,
|
||||
turnTick: BigInt(general.turnTick),
|
||||
recentWarTime: general.recentWarTime,
|
||||
recentWarTick: BigInt(general.recentWarTick),
|
||||
},
|
||||
});
|
||||
const auction = await db.auction.create({
|
||||
data: {
|
||||
type: 'BUY_RICE',
|
||||
hostGeneralId: general.id,
|
||||
detail: {},
|
||||
status: 'OPEN',
|
||||
closeAt: new Date('2099-08-01T10:10:00.000Z'),
|
||||
closeTick: BigInt(GAME_TICKS_PER_TURN),
|
||||
},
|
||||
});
|
||||
const message = await db.message.create({
|
||||
data: {
|
||||
mailbox: general.id,
|
||||
type: 'runtime-settings-test',
|
||||
src: 0,
|
||||
dest: general.id,
|
||||
time: new Date('2099-08-01T10:05:00.000Z'),
|
||||
timeTick: BigInt(GAME_TICKS_PER_TURN / 2),
|
||||
validUntil: new Date('2099-08-01T10:10:00.000Z'),
|
||||
validUntilTick: BigInt(GAME_TICKS_PER_TURN),
|
||||
message: {},
|
||||
},
|
||||
});
|
||||
const vote = await db.votePoll.create({
|
||||
data: {
|
||||
title: 'runtime settings test',
|
||||
options: ['yes', 'no'],
|
||||
revealMode: 'ALWAYS',
|
||||
openerGeneralId: general.id,
|
||||
openerName: general.name,
|
||||
startAt: new Date('2099-08-01T10:05:00.000Z'),
|
||||
startTick: BigInt(GAME_TICKS_PER_TURN / 2),
|
||||
endAt: new Date('2099-08-01T10:10:00.000Z'),
|
||||
endTick: BigInt(GAME_TICKS_PER_TURN),
|
||||
},
|
||||
});
|
||||
const originalLogTime = new Date('2026-01-02T03:04:05.000Z');
|
||||
const existingLog = await db.logEntry.create({
|
||||
data: {
|
||||
scope: 'SYSTEM',
|
||||
category: 'HISTORY',
|
||||
year: 191,
|
||||
month: 2,
|
||||
text: runtimeSettingsLogText,
|
||||
createdAt: originalLogTime,
|
||||
},
|
||||
});
|
||||
|
||||
const state: TurnWorldState = {
|
||||
id: row.id,
|
||||
currentYear: 191,
|
||||
currentMonth: 2,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: base,
|
||||
clockBaseTime: base,
|
||||
clockTick: 0,
|
||||
clockMode: 'manual',
|
||||
clockWallAnchor: base,
|
||||
lastTurnTick: 0,
|
||||
meta: row.meta as Record<string, unknown>,
|
||||
};
|
||||
const snapshot: TurnWorldSnapshot = {
|
||||
scenarioConfig: {
|
||||
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 70 },
|
||||
iconPath: '',
|
||||
map: {},
|
||||
const: {},
|
||||
environment: { mapName: 'test', unitSet: 'default' },
|
||||
},
|
||||
worldConfig: row.config as Record<string, unknown>,
|
||||
map: {
|
||||
id: 'test',
|
||||
name: 'test',
|
||||
cities: [],
|
||||
defaults: { trust: 50, trade: 100, supplyState: 1, frontState: 0 },
|
||||
},
|
||||
generals: [general],
|
||||
cities: [],
|
||||
nations: [],
|
||||
troops: [],
|
||||
diplomacy: [],
|
||||
events: [],
|
||||
initialEvents: [],
|
||||
};
|
||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||
});
|
||||
const stateStore = new InMemoryTurnStateStore(world);
|
||||
await stateStore.saveCheckpoint({
|
||||
turnTime: base.toISOString(),
|
||||
turnTick: 0,
|
||||
generalId: 0,
|
||||
year: 191,
|
||||
month: 2,
|
||||
});
|
||||
const hooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||
const queue = new DatabaseTurnDaemonCommandQueue(db);
|
||||
await queue.initialize();
|
||||
const stateManager = new EngineStateManager();
|
||||
stateManager.register('world', {
|
||||
capture: () => world.captureState(),
|
||||
restore: (saved) => world.restoreState(saved),
|
||||
});
|
||||
const lifecycle = new TurnDaemonLifecycle(
|
||||
{
|
||||
clock: new SystemClock(),
|
||||
controlQueue: queue,
|
||||
commandResponder: queue,
|
||||
commandHandler: createTurnDaemonCommandHandler({ world }),
|
||||
hooks: hooks.hooks,
|
||||
stateManager,
|
||||
stateStore,
|
||||
getNextTickTime: (lastTurnTime) =>
|
||||
getNextTickTime(lastTurnTime, Math.max(1, Math.round(world.getState().tickSeconds / 60))),
|
||||
processor: {
|
||||
run: async () => ({
|
||||
lastTurnTime: world.getState().lastTurnTime.toISOString(),
|
||||
processedGenerals: 0,
|
||||
processedTurns: 0,
|
||||
durationMs: 0,
|
||||
partial: false,
|
||||
}),
|
||||
},
|
||||
},
|
||||
{
|
||||
profile: 'integration',
|
||||
defaultBudget: { budgetMs: 1000, maxGenerals: 10, catchUpCap: 1 },
|
||||
}
|
||||
);
|
||||
|
||||
await db.inputEvent.create({
|
||||
data: {
|
||||
requestId: runtimeSettingsRequestId,
|
||||
target: 'ENGINE',
|
||||
eventType: 'updateRuntimeSettings',
|
||||
payload: {
|
||||
type: 'updateRuntimeSettings',
|
||||
requestId: runtimeSettingsRequestId,
|
||||
actionId: runtimeSettingsActionId,
|
||||
settings: {
|
||||
turnTermMinutes: 20,
|
||||
blockGeneralCreate: 2,
|
||||
autorunUser: {
|
||||
limitMinutes: 720,
|
||||
options: ['develop', 'recruit_high', 'chief'],
|
||||
},
|
||||
},
|
||||
} as GamePrisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
const loop = lifecycle.start();
|
||||
try {
|
||||
await waitForSucceeded(db, runtimeSettingsRequestId);
|
||||
} finally {
|
||||
await lifecycle.stop('test complete');
|
||||
await loop;
|
||||
await hooks.close();
|
||||
}
|
||||
|
||||
const storedWorld = await db.worldState.findUniqueOrThrow({ where: { id: row.id } });
|
||||
expect(storedWorld).toMatchObject({ tickSeconds: 1200, clockBaseTime: base, lastTurnTick: 0n });
|
||||
expect(storedWorld.config).toMatchObject({ turnTermMinutes: 20, blockGeneralCreate: 2 });
|
||||
expect(storedWorld.meta).toMatchObject({
|
||||
turnterm: 20,
|
||||
autorun_user: {
|
||||
limit_minutes: 720,
|
||||
options: { develop: true, recruit_high: true, chief: true },
|
||||
},
|
||||
});
|
||||
expect(await db.general.findUniqueOrThrow({ where: { id: general.id } })).toMatchObject({
|
||||
turnTime: new Date('2099-08-01T10:20:00.000Z'),
|
||||
turnTick: BigInt(GAME_TICKS_PER_TURN),
|
||||
recentWarTime: new Date('2099-08-01T10:10:00.000Z'),
|
||||
});
|
||||
expect((await db.auction.findUniqueOrThrow({ where: { id: auction.id } })).closeAt).toEqual(
|
||||
new Date('2099-08-01T10:20:00.000Z')
|
||||
);
|
||||
expect(await db.message.findUniqueOrThrow({ where: { id: message.id } })).toMatchObject({
|
||||
time: new Date('2099-08-01T10:10:00.000Z'),
|
||||
validUntil: new Date('2099-08-01T10:20:00.000Z'),
|
||||
});
|
||||
expect(await db.votePoll.findUniqueOrThrow({ where: { id: vote.id } })).toMatchObject({
|
||||
startAt: new Date('2099-08-01T10:10:00.000Z'),
|
||||
endAt: new Date('2099-08-01T10:20:00.000Z'),
|
||||
});
|
||||
expect(await db.logEntry.findUniqueOrThrow({ where: { id: existingLog.id } })).toMatchObject({
|
||||
text: runtimeSettingsLogText,
|
||||
createdAt: originalLogTime,
|
||||
});
|
||||
expect(await db.logEntry.findFirst({ where: { text: { contains: '턴시간이 <C>20분' } } })).not.toBeNull();
|
||||
expect(lifecycle.getStatus().nextTurnTime).toBe('2099-08-01T10:20:00.000Z');
|
||||
expect(await db.inputEvent.findUniqueOrThrow({ where: { requestId: runtimeSettingsRequestId } })).toMatchObject(
|
||||
{
|
||||
status: 'SUCCEEDED',
|
||||
result: {
|
||||
type: 'updateRuntimeSettings',
|
||||
ok: true,
|
||||
actionId: runtimeSettingsActionId,
|
||||
termChanged: true,
|
||||
previousTurnTermMinutes: 10,
|
||||
turnTermMinutes: 20,
|
||||
shiftedGenerals: 1,
|
||||
reprojectedAuctions: 1,
|
||||
reprojectedMessages: 1,
|
||||
reprojectedVotes: 1,
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user