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

67 lines
2.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishRoundState = publishRoundState;
exports.publishCandleAppend = publishCandleAppend;
const config_1 = require("../../../config");
const roundCache_1 = require("../../../realtime/redis/roundCache");
const poolCache_1 = require("../../../realtime/redis/poolCache");
const pubsub_1 = require("../../../realtime/redis/pubsub");
const stateVersions_1 = require("../../../realtime/redis/stateVersions");
/** Persist round snapshot to Redis and broadcast to subscribers. */
async function publishRoundState(input) {
if (!config_1.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 = {
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.version ?? 1,
};
await (0, roundCache_1.setRoundCache)(input.intervalMinutes, input.openMs, cachePayload);
if (input.pools) {
await (0, poolCache_1.setPoolCache)(input.intervalMinutes, input.openMs, input.pools);
}
// Business version for /state short-circuit (independent of per-openMs cache hash).
const roundVersion = await (0, stateVersions_1.bumpRoundVersionSafe)(input.intervalMinutes);
await (0, pubsub_1.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,
});
}
async function publishCandleAppend(intervalMinutes, candle) {
if (!config_1.config.redis.enabled)
return;
await (0, pubsub_1.publishRoundEvent)(intervalMinutes, {
type: 'candle.append',
intervalMinutes,
payload: { candle },
});
}
//# sourceMappingURL=publishRoundState.js.map