mubot-bustabit
Version:
Mubot bustabit integration
44 lines (38 loc) • 1.25 kB
JavaScript
const SERVER = 'http://localhost:8080/';
const USER = engine.getUsername();
// Update the server with game information.
function updateServer(params) {
var http = new XMLHttpRequest();
http.open("POST", SERVER + 'bustabit/finished', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params)
}
// Check for win/lose on game end.
engine.on('game_crash', function(data) {
if (engine.getEngine().playerInfo.USER) {
var win = false;
if (engine.getEngine().playerInfo.USER.stopped_at) win = true;
updateServer('game_crash=' + data.game_crash + '&balance=' + engine.getBalance() + '&win=' + win)
}
CheckBet()
});
// User won.
engine.on('cashed_out', function(resp) {
if(resp.username === USER) {
updateServer('win=true&balance='+engine.getBalance())
}
});
// Check the server for new bets.
function CheckBet () {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", SERVER + 'bustabit/getbet', true);
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
bet = JSON.parse(this.responseText);
if (bet.size) {
engine.placeBet(bet.size*100, parseInt(bet.ratio*100), false);
}
}
};
xmlhttp.send()
}