Skip to content

Instantly share code, notes, and snippets.

@BitChop
Forked from dsetzer/bustadice-bab-demo.js
Created August 18, 2019 04:10
Show Gist options
  • Save BitChop/455352c2d1a111b7c77ab4d4c7072fa8 to your computer and use it in GitHub Desktop.
Save BitChop/455352c2d1a111b7c77ab4d4c7072fa8 to your computer and use it in GitHub Desktop.
Bustabit script wrapper for use on bustadice. (proof of concept / demo! extremely minimal bustabit script API functionality)
var config = {
baseBet: { type: 'balance', label: 'Base Bet', value: 100 },
payout: { type: 'multiplier', label: 'Payout', value: 2 }
};
const dcheck = () => { try{ engine.getState() }catch(e){ return true } return false }
const emitter = {
on(name, func){this._s[name] = this._s[name] || []; this._s[name].push(func)},_s:{},
emit(name, ...args){if(!this._s[name]){return; } this._s[name].forEach(f=>f(...args))}
}
const dice = dcheck();
if(dice){
var engine = emitter;
}
//---------BEGIN BUSTABIT SCRIPT ----------
let baseBet = config.baseBet.value, currentBet = baseBet, payout = config.payout.value;
engine.on('GAME_STARTING', () => {
engine.bet(currentBet, payout);
});
engine.on('GAME_ENDED', () => {
let lastGame = engine.history.first();
if(!lastGame.wager) return;
if(lastGame.cashedAt){
console.log(`We won! ${(lastGame.wager*lastGame.cashedAt-lastGame.wager)/100} bits`);
currentBet = baseBet;
}else{
console.log(`We lost ${lastGame.wager/100} bits`);
currentBet *= (payout / (payout - 1));
}
});
//----------END BUSTABIT SCRIPT -----------
if(dice){
function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms));}
engine.bet = function(value, target){ this.wager = {v: value, t: target}; };
engine.history = {first(){ return this.lastGame; }}, engine.wager = null;
for(;;){
engine.lastGame = null, engine.wager = null;
await engine.emit('GAME_STARTING');
const result = engine.wager ? await this.bet(engine.wager.v, engine.wager.t) : await this.skip();
engine.history.lastGame = { bust: result.multiplier, wager: result.value || 0,
cashedAt: (result.target && result.target <= result.multiplier ? result.target : 0)
}
await engine.emit('GAME_ENDED');
await sleep(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment