66 lines
3.0 KiB
JavaScript
66 lines
3.0 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Round timing helpers for Candle Predict (synthetic candles, no market data).
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.currentCandleOpenMs = currentCandleOpenMs;
|
|
exports.isMaturityBoundaryForInterval = isMaturityBoundaryForInterval;
|
|
exports.maxPredictionWindowSeconds = maxPredictionWindowSeconds;
|
|
exports.effectivePredictionWindowSeconds = effectivePredictionWindowSeconds;
|
|
exports.getActiveRoundTiming = getActiveRoundTiming;
|
|
exports.resolveDisplayStatus = resolveDisplayStatus;
|
|
function currentCandleOpenMs(nowMs, intervalMinutes) {
|
|
const intervalMs = intervalMinutes * 60 * 1000;
|
|
return Math.floor(nowMs / intervalMs) * intervalMs;
|
|
}
|
|
/** True when this timestamp is a candle close for the given interval grid. */
|
|
function isMaturityBoundaryForInterval(candleOpenMs, intervalMinutes) {
|
|
const intervalMs = intervalMinutes * 60 * 1000;
|
|
return candleOpenMs % intervalMs === 0;
|
|
}
|
|
/** Max configurable prediction window (leaves at least 5s of live candle per period). */
|
|
function maxPredictionWindowSeconds(intervalMinutes) {
|
|
const intervalSec = intervalMinutes * 60;
|
|
return Math.max(5, intervalSec - 5);
|
|
}
|
|
/** Cap window so at least 5s of "live candle" remains before the next betting phase. */
|
|
function effectivePredictionWindowSeconds(intervalMinutes, predictionWindowSeconds) {
|
|
return Math.min(predictionWindowSeconds, maxPredictionWindowSeconds(intervalMinutes));
|
|
}
|
|
function getActiveRoundTiming(nowMs, intervalMinutes, predictionWindowSeconds) {
|
|
const windowSec = effectivePredictionWindowSeconds(intervalMinutes, predictionWindowSeconds);
|
|
const intervalMs = intervalMinutes * 60 * 1000;
|
|
const currentOpen = currentCandleOpenMs(nowMs, intervalMinutes);
|
|
const nextOpen = currentOpen + intervalMs;
|
|
const bettingStart = nextOpen - windowSec * 1000;
|
|
if (nowMs >= bettingStart && nowMs < nextOpen) {
|
|
return { openMs: nextOpen, phase: 'betting', nextOpenMs: nextOpen, nextBettingStartMs: bettingStart };
|
|
}
|
|
if (nowMs >= currentOpen && nowMs < nextOpen) {
|
|
return {
|
|
openMs: currentOpen,
|
|
phase: 'locked',
|
|
nextOpenMs: nextOpen,
|
|
nextBettingStartMs: bettingStart,
|
|
};
|
|
}
|
|
return { openMs: nextOpen, phase: 'waiting', nextOpenMs: nextOpen, nextBettingStartMs: bettingStart };
|
|
}
|
|
/** Client-facing status: settled rounds stay "locked" until the candle interval ends. */
|
|
function resolveDisplayStatus(dbStatus, timingPhase, candleOpenMs, intervalMinutes, nowMs) {
|
|
const closeMs = candleOpenMs + intervalMinutes * 60 * 1000;
|
|
if (dbStatus === 'settled' && nowMs < closeMs) {
|
|
return 'locked';
|
|
}
|
|
if (timingPhase === 'betting' && dbStatus !== 'settled') {
|
|
return 'betting';
|
|
}
|
|
if (timingPhase === 'locked' && dbStatus !== 'settled') {
|
|
return dbStatus === 'betting' ? 'betting' : 'locked';
|
|
}
|
|
if (timingPhase === 'waiting') {
|
|
return 'waiting';
|
|
}
|
|
return dbStatus;
|
|
}
|
|
//# sourceMappingURL=gameTimingService.js.map
|