merge: 최신 main을 내부 예약 명령 변경에 통합한다
This commit is contained in:
@@ -5,6 +5,7 @@ import { asNumber, asRecord } from '@sammo-ts/common';
|
||||
import { zWorldStateConfig, zWorldStateMeta } from '../../context.js';
|
||||
import { isSelectionPoolWorld, resolveSelectionMaxGeneral } from '@sammo-ts/game-engine/turn/selectPoolService.js';
|
||||
import { loadCurrentGameTime } from '../../services/gameClock.js';
|
||||
import { loadTurnEngineRunning } from '../../services/turnEngineStatus.js';
|
||||
import { procedure, router } from '../../trpc.js';
|
||||
|
||||
export const lobbyRouter = router({
|
||||
@@ -35,6 +36,7 @@ export const lobbyRouter = router({
|
||||
.map(([option]) => option)
|
||||
: [];
|
||||
const gameTime = await loadCurrentGameTime(ctx.db);
|
||||
const turnEngineRunning = await loadTurnEngineRunning(ctx.profileStatusSource, ctx.db, ctx.profile.name);
|
||||
|
||||
let myGeneral = null;
|
||||
if (ctx.auth?.user.id) {
|
||||
@@ -72,6 +74,7 @@ export const lobbyRouter = router({
|
||||
clockMode: gameTime.mode ?? 'realtime',
|
||||
clockRunning: gameTime.running,
|
||||
clockStartsAt: gameTime.startsAt?.toISOString() ?? null,
|
||||
turnEngineRunning,
|
||||
otherTextInfo: worldState.meta.otherTextInfo ?? '',
|
||||
npcMode: worldState.config.npcMode ?? 0,
|
||||
defaultStatTotal: asNumber(asRecord(rawConfig.stat).total, 165),
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
} from './realtime/publicEvent.js';
|
||||
import { GatewayHttpAccountIconSource } from './auth/accountIconSource.js';
|
||||
import { GatewayHttpProfileStatusSource } from './auth/profileStatusSource.js';
|
||||
import { CachedTurnEngineStatus } from './services/turnEngineStatus.js';
|
||||
import { createAdminProfileIconResetFlushHandler } from './services/accountIconSync.js';
|
||||
import { AccountIconResetReconciler } from './services/accountIconResetReconciler.js';
|
||||
import { createBestEffortResourceCloser } from './services/bestEffortResourceCloser.js';
|
||||
@@ -115,6 +116,7 @@ export const createGameApiServer = async () => {
|
||||
config.gatewayInternalApiUrl,
|
||||
config.gameTokenSecret
|
||||
);
|
||||
const turnEngineStatus = new CachedTurnEngineStatus(profileStatusSource, postgres.prisma, config.profileName);
|
||||
|
||||
const turnDaemon = new DatabaseTurnDaemonTransport(postgres.prisma, config.daemonRequestTimeoutMs);
|
||||
const accountIconResetReconciler = new AccountIconResetReconciler(
|
||||
@@ -383,13 +385,24 @@ export const createGameApiServer = async () => {
|
||||
});
|
||||
});
|
||||
|
||||
let heartbeatPending = false;
|
||||
const heartbeat = setInterval(() => {
|
||||
sendFrame(
|
||||
formatSseFrame({
|
||||
event: 'ping',
|
||||
data: '{}',
|
||||
if (heartbeatPending) return;
|
||||
heartbeatPending = true;
|
||||
void turnEngineStatus
|
||||
.get()
|
||||
.then((turnEngineRunning) => {
|
||||
if (closed) return;
|
||||
sendFrame(
|
||||
formatSseFrame({
|
||||
event: 'ping',
|
||||
data: JSON.stringify({ turnEngineRunning }),
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
.finally(() => {
|
||||
heartbeatPending = false;
|
||||
});
|
||||
}, 15000);
|
||||
|
||||
const close = () => {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { gatewayProfileCapabilities } from '@sammo-ts/common';
|
||||
|
||||
import type { ProfileStatusSource } from '../auth/profileStatusSource.js';
|
||||
|
||||
interface TurnDaemonLeaseSource {
|
||||
turnDaemonLease: {
|
||||
findUnique(input: {
|
||||
where: { profile: string };
|
||||
select: { leaseUntil: true };
|
||||
}): Promise<{ leaseUntil: Date } | null>;
|
||||
};
|
||||
}
|
||||
|
||||
export const loadTurnEngineRunning = async (
|
||||
source: ProfileStatusSource | undefined,
|
||||
db: TurnDaemonLeaseSource,
|
||||
profileName: string,
|
||||
now = new Date()
|
||||
): Promise<boolean | null> => {
|
||||
if (!source) return null;
|
||||
try {
|
||||
const status = await source.get(profileName);
|
||||
if (status === null) return null;
|
||||
if (!gatewayProfileCapabilities(status).turnsRunning) return false;
|
||||
const lease = await db.turnDaemonLease.findUnique({
|
||||
where: { profile: profileName },
|
||||
select: { leaseUntil: true },
|
||||
});
|
||||
return lease !== null && lease.leaseUntil.getTime() > now.getTime();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export class CachedTurnEngineStatus {
|
||||
private cachedAt = Number.NEGATIVE_INFINITY;
|
||||
private cachedValue: boolean | null = null;
|
||||
private pending: Promise<boolean | null> | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly source: ProfileStatusSource,
|
||||
private readonly db: TurnDaemonLeaseSource,
|
||||
private readonly profileName: string,
|
||||
private readonly cacheMs = 2_000,
|
||||
private readonly now = () => Date.now()
|
||||
) {}
|
||||
|
||||
get(): Promise<boolean | null> {
|
||||
if (this.now() - this.cachedAt < this.cacheMs) {
|
||||
return Promise.resolve(this.cachedValue);
|
||||
}
|
||||
if (this.pending) return this.pending;
|
||||
|
||||
this.pending = loadTurnEngineRunning(this.source, this.db, this.profileName).then((value) => {
|
||||
this.cachedValue = value;
|
||||
this.cachedAt = this.now();
|
||||
return value;
|
||||
});
|
||||
return this.pending.finally(() => {
|
||||
this.pending = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user