games_app/backend/dist-test-faq/services/candleGenerationService.js

188 lines
6.3 KiB
JavaScript

"use strict";
/**
* Synthetic candle generation and stake-pool outcome resolution.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_MAX_CONSECUTIVE_SAME_DIRECTION = void 0;
exports.defaultSeedCandle = defaultSeedCandle;
exports.resolvePoolNeutrality = resolvePoolNeutrality;
exports.resolvePoolOutcome = resolvePoolOutcome;
exports.normalizeMaxConsecutiveSameDirection = normalizeMaxConsecutiveSameDirection;
exports.countConsecutiveDirectionalOutcomes = countConsecutiveDirectionalOutcomes;
exports.resolveAutoNoBetOutcome = resolveAutoNoBetOutcome;
exports.replicateNeutralCandle = replicateNeutralCandle;
exports.generateSyntheticCandle = generateSyntheticCandle;
function defaultSeedCandle(price) {
const ref = price > 0 ? price : 100;
const bodyMove = ref * 0.0025;
const wick = ref * 0.0012;
const open = ref;
const close = ref + bodyMove;
return {
open,
close,
high: close + wick,
low: open - wick * 0.35,
};
}
function neutralOutcome(shortPct, longPct) {
let neutralColor = 'green';
if (shortPct < longPct)
neutralColor = 'red';
else if (longPct < shortPct)
neutralColor = 'green';
return { type: 'neutral', winner: null, neutralColor };
}
/**
* Shared neutrality checks used by majority/minority and daily-revenue modes.
* Returns a neutral outcome when rules apply; otherwise null.
*/
function resolvePoolNeutrality(shortPool, longPool, rules) {
const total = shortPool + longPool;
if (total <= 0) {
return { type: 'neutral', winner: null, neutralColor: 'green' };
}
const shortPct = (shortPool / total) * 100;
const longPct = (longPool / total) * 100;
const gap = Math.abs(shortPct - longPct);
const majorityPct = Math.max(shortPct, longPct);
if (rules.minToleranceEnabled && gap <= rules.minTolerancePercent) {
return neutralOutcome(shortPct, longPct);
}
// Max imbalance (e.g. 95/5 or solo 100/0): when enabled, extreme one-sided
// pools are neutral (refund), including a single participant at 100%.
if (rules.maxToleranceEnabled && majorityPct >= rules.maxTolerancePercent) {
return neutralOutcome(shortPct, longPct);
}
// Exact equal pools are always a push (refund), even if min-tolerance is off.
if (shortPool === longPool) {
return neutralOutcome(shortPct, longPct);
}
return null;
}
function resolvePoolOutcome(shortPool, longPool, rules, poolWinnerRule = 'minority') {
const neutral = resolvePoolNeutrality(shortPool, longPool, rules);
if (neutral)
return neutral;
const shortWins = poolWinnerRule === 'majority' ? shortPool > longPool : shortPool < longPool;
if (shortWins) {
return { type: 'short_win', winner: 'short', neutralColor: null };
}
return { type: 'long_win', winner: 'long', neutralColor: null };
}
/** Default streak cap when admin config is missing / unset. */
exports.DEFAULT_MAX_CONSECUTIVE_SAME_DIRECTION = 5;
/**
* Clamp admin value: `0` disables the cap; otherwise `1..50`.
*/
function normalizeMaxConsecutiveSameDirection(value) {
const n = typeof value === 'number' ? value : Number(value);
if (!Number.isFinite(n) || n <= 0)
return 0;
return Math.min(50, Math.max(1, Math.floor(n)));
}
/**
* Counts how many of the most recent directional outcomes (short/long) share the same type.
* Neutral outcomes break the streak and are skipped.
*/
function countConsecutiveDirectionalOutcomes(recentOutcomesNewestFirst) {
let type = null;
let count = 0;
for (const outcome of recentOutcomesNewestFirst) {
if (outcome !== 'short_win' && outcome !== 'long_win') {
break;
}
if (type === null) {
type = outcome;
count = 1;
continue;
}
if (outcome === type) {
count += 1;
continue;
}
break;
}
return { type, count };
}
/**
* Random short/long outcome for rounds with no player predictions.
* After `maxConsecutiveSameDirection` consecutive candles of the same direction,
* the next must be the opposite. Pass `0` to disable the cap (pure random).
*/
function resolveAutoNoBetOutcome(recentOutcomesNewestFirst, maxConsecutiveSameDirection = exports.DEFAULT_MAX_CONSECUTIVE_SAME_DIRECTION) {
const maxConsecutive = normalizeMaxConsecutiveSameDirection(maxConsecutiveSameDirection);
const { type, count } = countConsecutiveDirectionalOutcomes(recentOutcomesNewestFirst);
if (maxConsecutive > 0 && type && count >= maxConsecutive) {
return type === 'short_win'
? { type: 'long_win', winner: 'long', neutralColor: null }
: { type: 'short_win', winner: 'short', neutralColor: null };
}
if (Math.random() < 0.5) {
return { type: 'short_win', winner: 'short', neutralColor: null };
}
return { type: 'long_win', winner: 'long', neutralColor: null };
}
/**
* Neutral = exact copy of previous OHLC (same body, wicks, and price level).
* Only colorHint differs for display.
*/
function replicateNeutralCandle(prev, colorHint) {
return {
open: prev.open,
high: prev.high,
low: prev.low,
close: prev.close,
colorHint,
};
}
/**
* Builds OHLC for the synthetic candle following the resolved outcome.
* Long candles anchor at the top-right of the previous body; short at the bottom-right.
*/
function generateSyntheticCandle(prev, outcome) {
if (outcome.type === 'neutral') {
return replicateNeutralCandle(prev, outcome.neutralColor);
}
const ref = prev.close > 0 ? prev.close : 100;
const bodyMove = ref * 0.0025;
const wick = ref * 0.0012;
const prevTop = Math.max(prev.open, prev.close);
const prevBottom = Math.min(prev.open, prev.close);
if (outcome.type === 'short_win') {
const open = prevBottom;
const close = open - bodyMove;
return {
open,
high: open + wick * 0.35,
low: close - wick,
close,
colorHint: 'red',
};
}
const open = prevTop;
const close = open + bodyMove;
return {
open,
high: close + wick,
low: open - wick * 0.35,
close,
colorHint: 'green',
};
}
//# sourceMappingURL=candleGenerationService.js.map