games_app/backend/src/domain/candle-predict/realtime/publishRoundState.ts
2026-07-29 15:35:11 +02:00

101 lines
3.2 KiB
TypeScript

import { config } from '../../../config';
import { setRoundCache, type CachedRoundDto } from '../../../realtime/redis/roundCache';
import { setPoolCache } from '../../../realtime/redis/poolCache';
import { publishRoundEvent } from '../../../realtime/redis/pubsub';
import { bumpRoundVersionSafe } from '../../../realtime/redis/stateVersions';
export type PlayerRoundDto = {
id: string;
candleOpenMs: number;
candleCloseMs: number;
intervalMinutes: number;
status: string;
outcome: string | null;
openPrice: number | null;
highPrice: number | null;
lowPrice: number | null;
closePrice: number | null;
settledAt: string | null;
serverNowMs: number;
secondsToBettingEnd: number;
secondsToBettingStart: number;
secondsToClose: number;
shortPoolPercent?: number;
longPoolPercent?: number;
version?: number;
};
/** Persist round snapshot to Redis and broadcast to subscribers. */
export async function publishRoundState(input: {
intervalMinutes: number;
openMs: number;
round: PlayerRoundDto;
pools?: { short: number; long: number };
eventType?: 'round.updated' | 'round.settled';
}): Promise<void> {
if (!config.redis.enabled) return;
const poolShort = input.pools?.short ?? 0;
const poolLong = input.pools?.long ?? 0;
const poolTotal = poolShort + poolLong;
const shortPoolPercent =
input.round.shortPoolPercent ??
(poolTotal > 0 ? Math.round((poolShort / poolTotal) * 1000) / 10 : 0);
const longPoolPercent =
input.round.longPoolPercent ??
(poolTotal > 0 ? Math.round((poolLong / poolTotal) * 1000) / 10 : 0);
const cachePayload: CachedRoundDto = {
id: input.round.id,
candleOpenMs: input.round.candleOpenMs,
candleCloseMs: input.round.candleCloseMs,
intervalMinutes: input.round.intervalMinutes,
status: input.round.status,
outcome: input.round.outcome,
openPrice: input.round.openPrice,
highPrice: input.round.highPrice,
lowPrice: input.round.lowPrice,
closePrice: input.round.closePrice,
settledAt: input.round.settledAt,
shortPoolPercent,
longPoolPercent,
serverNowMs: input.round.serverNowMs,
secondsToBettingEnd: input.round.secondsToBettingEnd,
secondsToBettingStart: input.round.secondsToBettingStart,
secondsToClose: input.round.secondsToClose,
version: (input.round as CachedRoundDto).version ?? 1,
};
await setRoundCache(input.intervalMinutes, input.openMs, cachePayload);
if (input.pools) {
await setPoolCache(input.intervalMinutes, input.openMs, input.pools);
}
// Business version for /state short-circuit (independent of per-openMs cache hash).
const roundVersion = await bumpRoundVersionSafe(input.intervalMinutes);
await publishRoundEvent(input.intervalMinutes, {
type: input.eventType ?? 'round.updated',
intervalMinutes: input.intervalMinutes,
payload: {
round: cachePayload,
pools: input.pools,
...(roundVersion != null ? { versions: { round: roundVersion } } : {}),
},
version: cachePayload.version,
});
}
export async function publishCandleAppend(
intervalMinutes: number,
candle: Record<string, unknown>
): Promise<void> {
if (!config.redis.enabled) return;
await publishRoundEvent(intervalMinutes, {
type: 'candle.append',
intervalMinutes,
payload: { candle },
});
}