104 lines
4.2 KiB
JavaScript
104 lines
4.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.serializeRoundToHash = serializeRoundToHash;
|
|
exports.parseRoundFromHash = parseRoundFromHash;
|
|
exports.setRoundCache = setRoundCache;
|
|
exports.getRoundCache = getRoundCache;
|
|
exports.incrementRoundVersion = incrementRoundVersion;
|
|
const keys_1 = require("./keys");
|
|
const client_1 = require("./client");
|
|
const ROUND_TTL_SECONDS = 86400;
|
|
function empty(value) {
|
|
return value ?? '';
|
|
}
|
|
function parseOptionalNumber(value) {
|
|
if (!value || value === '')
|
|
return null;
|
|
const n = Number(value);
|
|
return Number.isFinite(n) ? n : null;
|
|
}
|
|
/** Serialize a player-visible round DTO into a Redis HASH. */
|
|
function serializeRoundToHash(round) {
|
|
return {
|
|
id: round.id,
|
|
candleOpenMs: String(round.candleOpenMs),
|
|
candleCloseMs: String(round.candleCloseMs),
|
|
intervalMinutes: String(round.intervalMinutes),
|
|
status: round.status,
|
|
outcome: empty(round.outcome),
|
|
openPrice: empty(round.openPrice != null ? String(round.openPrice) : ''),
|
|
highPrice: empty(round.highPrice != null ? String(round.highPrice) : ''),
|
|
lowPrice: empty(round.lowPrice != null ? String(round.lowPrice) : ''),
|
|
closePrice: empty(round.closePrice != null ? String(round.closePrice) : ''),
|
|
settledAt: empty(round.settledAt),
|
|
shortPoolPercent: String(round.shortPoolPercent ?? 0),
|
|
longPoolPercent: String(round.longPoolPercent ?? 0),
|
|
serverNowMs: String(round.serverNowMs),
|
|
secondsToBettingEnd: String(round.secondsToBettingEnd),
|
|
secondsToBettingStart: String(round.secondsToBettingStart),
|
|
secondsToClose: String(round.secondsToClose),
|
|
bettingEndMs: String(round.bettingEndMs ?? round.candleOpenMs),
|
|
bettingStartMs: empty(round.bettingStartMs != null ? String(round.bettingStartMs) : ''),
|
|
version: String(round.version ?? 1),
|
|
};
|
|
}
|
|
/** Parse a Redis HASH back into a round DTO. */
|
|
function parseRoundFromHash(hash) {
|
|
if (!hash.id || !hash.candleOpenMs)
|
|
return null;
|
|
return {
|
|
id: hash.id,
|
|
candleOpenMs: Number(hash.candleOpenMs),
|
|
candleCloseMs: Number(hash.candleCloseMs),
|
|
intervalMinutes: Number(hash.intervalMinutes),
|
|
status: hash.status,
|
|
outcome: hash.outcome ? hash.outcome : null,
|
|
openPrice: parseOptionalNumber(hash.openPrice),
|
|
highPrice: parseOptionalNumber(hash.highPrice),
|
|
lowPrice: parseOptionalNumber(hash.lowPrice),
|
|
closePrice: parseOptionalNumber(hash.closePrice),
|
|
settledAt: hash.settledAt ? hash.settledAt : null,
|
|
shortPoolPercent: Number(hash.shortPoolPercent ?? 0),
|
|
longPoolPercent: Number(hash.longPoolPercent ?? 0),
|
|
serverNowMs: Number(hash.serverNowMs),
|
|
secondsToBettingEnd: Number(hash.secondsToBettingEnd ?? 0),
|
|
secondsToBettingStart: Number(hash.secondsToBettingStart ?? 0),
|
|
secondsToClose: Number(hash.secondsToClose ?? 0),
|
|
bettingEndMs: Number(hash.bettingEndMs ?? hash.candleOpenMs),
|
|
bettingStartMs: parseOptionalNumber(hash.bettingStartMs) ?? undefined,
|
|
version: Number(hash.version ?? 1),
|
|
};
|
|
}
|
|
async function setRoundCache(intervalMinutes, openMs, round) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return;
|
|
const key = keys_1.redisKeys.round(intervalMinutes, openMs);
|
|
const hash = serializeRoundToHash(round);
|
|
await redis.hset(key, hash);
|
|
await redis.expire(key, ROUND_TTL_SECONDS);
|
|
}
|
|
async function getRoundCache(intervalMinutes, openMs) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return null;
|
|
try {
|
|
const key = keys_1.redisKeys.round(intervalMinutes, openMs);
|
|
const hash = await redis.hgetall(key);
|
|
if (!hash || !Object.keys(hash).length)
|
|
return null;
|
|
return parseRoundFromHash(hash);
|
|
}
|
|
catch (e) {
|
|
console.error('[roundCache] getRoundCache failed:', e);
|
|
return null;
|
|
}
|
|
}
|
|
async function incrementRoundVersion(intervalMinutes, openMs) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return 1;
|
|
const key = keys_1.redisKeys.round(intervalMinutes, openMs);
|
|
return redis.hincrby(key, 'version', 1);
|
|
}
|
|
//# sourceMappingURL=roundCache.js.map
|