Skip to content

Instantly share code, notes, and snippets.

@larvata
Last active May 25, 2016 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larvata/6052372 to your computer and use it in GitHub Desktop.
Save larvata/6052372 to your computer and use it in GitHub Desktop.
auto script for just-dice
// ==UserScript==
// @name JUST-DICE
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match https://just-dice.com/*
// @copyright 2012+, You
// ==/UserScript==
///////// CONST /////////
var betRate=0.25;
var riskTimes=10;
/////// DO NOT EDIT BELOW ///////
var currentBet;
var startBalance;
var lastBalance;
var currentBalance;
var rollTimer,initTimer;
var startBtn,stopBtn,txtThresholdBalance,minBtn,maxBtn,spanCurrentBet;
// 0:game started, 2:clicked, 1:rolled, 2:result
var currentStep;
var justDiceMinBet=0.00000001;
///////// functions ////////
function hasClass(element, cls) {
return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function minMyBet(){
txtThresholdBalance.value=prettyFloat(calcBalance(justDiceMinBet,betRate,riskTimes));
showBetValue();
}
function maxMyBet(){
txtThresholdBalance.value=prettyFloat(currentBalance);
showBetValue();
}
function showBetValue(){
var thbl=txtThresholdBalance.value;
if (isNumber(thbl)) {
currentBet=calcBet(thbl,betRate,riskTimes);
if (currentBet<justDiceMinBet) {
startBtn.disabled=true;
spanCurrentBet.innerText=" thresholdBalance MUST >= "+prettyFloat(justDiceMinBet);
}
else{
startBtn.disabled=false;
spanCurrentBet.innerText=prettyFloat(currentBet);
}
}
else{
currentBet=0;
spanCurrentBet.innerText=thbl + ' is not a VAILD value.';
}
}
function calcBet(balance,betRate,riskTimes){
var riskCount=1;
for (var i = 1; i <= riskTimes; i++) {
riskCount+=Math.pow(2,i);
};
return balance*betRate/riskCount;
}
function calcBalance(bet,betRate,riskTimes){
var riskCount=1;
for (var i = 1; i <= riskTimes; i++) {
riskCount+=Math.pow(2,i);
};
return bet*riskCount/betRate;
}
function prettyFloat(value){
var dashPos=value.toString().indexOf('-');
if (value<1&&dashPos!=-1) {
return value.toFixed(value.toString().substr(dashPos+1));
}
else{
return value;
}
}
function roll(){
// GAME STARTED
if (currentStep==0) {
document.getElementById('pct_bet').value=prettyFloat(currentBet);
currentStep=1;
document.getElementById('a_hi').click();
}
// CLICKED
if (currentStep==1) {
if(hasClass(document.getElementById('a_hi'),'waiting')){
currentStep=2;
}
}
// ROLLED
if (currentStep==2) {
if(!hasClass(document.getElementById('a_hi'),'waiting')){
currentStep=3;
}
}
// RESULT
if (currentStep==3) {
currentBalance=document.getElementById('pct_balance').value;
if (currentBalance>lastBalance) {
currentBet=calcBet(currentBalance,betRate,riskTimes);
resultLog('win');
}
else if (currentBalance<lastBalance) {
currentBet*=2;
resultLog('losses');
}
else{
return;
}
lastBalance=currentBalance;
currentStep=0;
}
}
function startGame(){
console.log("Game Started.")
rollTimer=window.setInterval(roll,200);
}
function stopGame(){
window.clearInterval(rollTimer);
stopBtn.style.display='none';
startBtn.style.display='none';
}
function resultLog(resultStr){
console.log(resultStr+'. CurrentBet: '+ prettyFloat(currentBet));
}
function init(){
// Test is just-dice fully loaded
if (isNumber(document.getElementById('pct_balance').value)){
window.clearInterval(initTimer);
console.log('fully loaded'+document.getElementById('pct_balance').value);
}
else{
console.log('not fully loaded return');
return;
}
// Init Variables
startBalance=document.getElementById('pct_balance').value;
currentBalance=document.getElementById('pct_balance').value;
lastBalance=document.getElementById('pct_balance').value;
currentStep=0;
// Start BUTTON
startBtn=document.createElement('BUTTON');
var textStartBtn=document.createTextNode('Start!');
startBtn.disabled=true;
startBtn.appendChild(textStartBtn);
document.getElementById('msg').parentNode.appendChild(startBtn);
startBtn.addEventListener('click',startGame,false);
// Stop BUTTON
stopBtn=document.createElement('BUTTON');
var textStopBtn=document.createTextNode('Stop...');
stopBtn.disabled=true;
stopBtn.appendChild(textStopBtn);
document.getElementById('msg').parentNode.appendChild(stopBtn);
stopBtn.addEventListener('click',stopGame,false);
// Threshold INPUT BOX
txtThresholdBalance=document.createElement('INPUT');
txtThresholdBalance.type='text';
txtThresholdBalance.width='50px';
txtThresholdBalance.onkeypress=showBetValue;
document.getElementById('msg').parentNode.appendChild(txtThresholdBalance);
// Min BUTTON
minBtn=document.createElement('BUTTON');
var textMinBtn=document.createTextNode('Min');
minBtn.appendChild(textMinBtn);
document.getElementById('msg').parentNode.appendChild(minBtn);
minBtn.addEventListener('click',minMyBet,false);
// Max BUTTON
maxBtn=document.createElement('BUTTON');
var textMaxBtn=document.createTextNode('Max');
maxBtn.appendChild(textMaxBtn);
document.getElementById('msg').parentNode.appendChild(maxBtn);
maxBtn.addEventListener('click',maxMyBet,false);
// CurrentBet SPAN
spanCurrentBet=document.createElement('SPAN');
spanCurrentBet.text="abc";
document.getElementById('msg').parentNode.appendChild(spanCurrentBet);
// Default set min bet
minBtn.click();
}
//////////////// start from here ///////////////////
initTimer=window.setInterval(init,100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment