Skip to content

Instantly share code, notes, and snippets.

@dsetzer
Last active May 10, 2020 13:28
Show Gist options
  • Save dsetzer/37619b08f4860310dd7b0bc551ca1caf to your computer and use it in GitHub Desktop.
Save dsetzer/37619b08f4860310dd7b0bc551ca1caf to your computer and use it in GitHub Desktop.
Replaces the bet function with one that provides authentic results at no cost.
this.bet = (value, target, balance) => {
return new Promise((resolve, reject) => {
if (value % 100 != 0) return reject("bet size must be evenly divisible by 100");
if (value < 100) return reject("bet size must be at least 100 (= 1 bit)");
if (target < 1.01 || target > 1e6) return reject("target multiplier must be between 1.01 and 1,000,000");
if (balance && balance < value) return reject("insufficient balance"); else if(!balance) balance = 0;
let id = (++this.fid || (this.fid = 0, 0)), timestamp = new Date().toDateString();
let multiplier = Math.round(Math.min(Math.max(1, (0.99 / Math.random())), 1e6) * 1e2) / 1e2;
if(multiplier < target) { balance -= value; } else { balance += value * (target - 1); }
return resolve({id, timestamp, value, target, multiplier, balance, bankroll: Infinity});
}).catch((r)=>{throw new Error(r);});
}
// Example Usage: Pass the wager amount and target payout as you normally would however there's an additional
// but optional third parameter to specify a balance amount, if this is provided it will be appropriately
// adjusted with the resulting winnings/losses and returned within the result as expected.
let testBalance = this.balance;
let result = await this.bet(100, 2.9, testBalance);
this.log(JSON.stringify(result));
// {"id":0,"timestamp":"Sat May 09 2020","value":100,"target":2.9,"multiplier":9.07,"balance":1000190,"bankroll":null}
// If no balance is specified then the balance returned is the resulting winnings/losses for the roll
// (which can be recorded and then summed to calculate the net profit over the course of multiple bets.
let netProfit = 0;
let result = await this.bet(100, 2.9, netProfit);
this.log(JSON.stringify(result));
// {"id":0,"timestamp":"Sat May 09 2020","value":100,"target":2.9,"multiplier":1.16,"balance":-100,"bankroll":null}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment