Skip to content

Instantly share code, notes, and snippets.

@dsetzer
Created September 13, 2019 21:33
Show Gist options
  • Save dsetzer/e71c9f75e0f218f1a5c94892cef2e3d2 to your computer and use it in GitHub Desktop.
Save dsetzer/e71c9f75e0f218f1a5c94892cef2e3d2 to your computer and use it in GitHub Desktop.
hooked play stats for bustadice
class PlayStats {
constructor(context, usingBetSkips){
this.context = context;
this._context = {context...};
this._usingBetSkips = usingBetSkips || false;
this._bet = async (value, target) => {
return new Promise((resolve, reject)=>{
this._context.bet(value, target).catch(reject).then((r)=>{
if(this._usingBetSkips && r.value === 100 && r.target === 1.01){
this.gameStats.skip.total += 1
if(r.multiplier < r.target){
this.gameStats.skip.cost -= 100;
this.profitStats.profit.net -= 100
if(this.profitStats.profit.net < this.profitStats.profit.atl){
this.profitStats.profit.atl = this.profitStats.profit.net;
}
}else{
this.gameStats.skip.cost += 1;
this.profitStats.profit.net += 1;
if(this.profitStats.profit.net > this.profitStats.profit.ath){
this.profitStats.profit.ath = this.profitStats.profit.net;
}
}
}else{
this.gameStats.play += 1;
this.profitStats.wagered.total += r.value;
if(r.value > this.profitStats.wagered.largest){
this.profitStats.wagered.largest = r.value;
}
if(r.multiplier < r.target){
this.gameStats.lose.total += 1;
this.gameStats.win._since += 1;
this.gameStats.lose._since = 0;
if(this.gameStats.win._since > this.gameStats.lose.streak){
this.gameStats.lose.streak = this.gameStats.win._since;
}
this.profitStats.losses.total += r.value;
if(r.value > this.profitStats.losses.largest){
this.profitStats.losses.largest = r.value;
}
this.profitStats.profit.net -= r.value;
if(this.profitStats.profit.net < this.profitStats.profit.atl){
this.profitStats.profit.atl = this.profitStats.profit.net;
}
}else{
this.gameStats.win.total += 1;
this.gameStats.lose._since += 1;
this.gameStats.win._since = 0;
if(this.gameStats.lose._since > this.gameStats.win.streak){
this.gameStats.win.streak = this.gameStats.lose._since;
}
let amount = (r.value * r.target);
this.profitStats.winnings.total += amount;
if(amount > this.profitStats.winnings.largest){
this.profitStats.winnings.largest = amount;
}
this.profitStats.profit.net += (amount - r.value);
if(this.profitStats.profit.net > this.profitStats.profit.ath){
this.profitStats.profit.ath = this.profitStats.profit.net;
}
}
}
return resolve(r);
})
});
}
this._skip = async () => {
return new Promise((resolve, reject) => {
this._context.skip().catch(reject).then((r)=>{
this.gameStats.skipped += 1;
return resolve(r);
});
});
}
this.reset();
this.attach();
}
attach(context){
context = context || this.context;
if(context !== null){
if(!this._context._playStats){
try{
this.context.bet = this._bet;
this.context.skip = this._skip;
}catch(e){ throw new Error('failed to attach'); }
}else { throw new Error('already attached'); }
}else{ throw new Error('no context provided'); }
}
reset(){
this.gameStats = {
win: { streak: 0, total: 0, _since: 0},
lose: { streak: 0, total: 0, _since: 0},
skip: { total: 0, cost: 0}
played: 0, skipped: 0
};
this.profitStats = {
wagered: { largest: 0, total: 0 },
winnings: { largest: 0, total: 0},
losses: { largest: 0, total: 0},
profit: { ath: 0, atl: 0, net: 0 }
};
this.targetStats = {
added: [], played: [], dropped: []
};
}
}
// Usage:
var playStats = new PlayStats(this, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment