// Default options for the loading spinner
var spin_opts = {
lines: 13,
length: 2,
width: 2,
radius: 5,
corners: 1,
rotate: 0,
direction: 1,
color: '#fff'
};
var sound_enabled = true;
$game_html = $('<div class="game"><div class="game_left"><ul class="board"><li data-tile="1" class="tile"></li><li data-tile="2" class="tile"></li><li data-tile="3" class="tile"></li><li data-tile="4" class="tile"></li><li data-tile="5" class="tile"></li><li data-tile="6" class="tile"></li><li data-tile="7" class="tile"></li><li data-tile="8" class="tile"></li><li data-tile="9" class="tile"></li><li data-tile="10" class="tile"></li><li data-tile="11" class="tile"></li><li data-tile="12" class="tile"></li><li data-tile="13" class="tile"></li><li data-tile="14" class="tile"></li><li data-tile="15" class="tile"></li><li data-tile="16" class="tile"></li><li data-tile="17" class="tile"></li><li data-tile="18" class="tile"></li><li data-tile="19" class="tile"></li><li data-tile="20" class="tile"></li><li data-tile="21" class="tile"></li><li data-tile="22" class="tile"></li><li data-tile="23" class="tile"></li><li data-tile="24" class="tile"></li><li data-tile="25" class="tile"></li></ul></div><div class="game_right"><div class="control standings"><div class="col-left"><p class="standing_label">Next tile worth:</p><p class="stand_next"><span id="next"></span></p></div><div class="col-right"><button class="cashout">Cashout</button><p class="standing_label">In play:</p><p class="stand_stake"><span id="stake"></span></p></div></div><div class="messages"></div></div></div>');
var win = new Howl({urls: [webroot+'sound/win.mp3', webroot+'sound/win.ogg', webroot+'sound/win.wav']});
var lose = new Howl({urls: [webroot+'sound/lose.mp3', webroot+'sound/lose.ogg', webroot+'sound/lose.wav']});
var busy = false;
var consecutive_zeros = 0;
/* SATOSHIMINES.JS V0.81 */
function Game(bet, game_hash, num_mines){
num_mines = typeof num_mines !== 'undefined' ? num_mines : 3;
var game_hash_qs = (typeof game_hash==='undefined')?'':'&game_hash='+game_hash;
var _this = this;
var timer = 0;
var timer_int = setInterval(function(){
timer++;
}, 100);
// A new game
busy = true;
$.ajax({
url: webroot+'action/newgame.php',
data: 'player_hash='+playerhash+'&bet='+bet+game_hash_qs+'&num_mines='+num_mines,
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
busy = false;
clearInterval(timer_int);
clean_feed();
if (jqxhr.status == 'success'){
ga('send', 'event', 'game', 'new', 'wait', timer);
var returnedBet = parseFloat(jqxhr.bet);
returnedBet = (isNaN(returnedBet))?0:returnedBet;
if (jqxhr.gametype != 'practice' && bet != 0){
updateBalance(returnedBet * -1);
}
_this.betNumber = parseInt(jqxhr.betNumber);
_this.game_hash = jqxhr.game_hash;
_this.bet = jqxhr.bet;
_this.stake = jqxhr.stake;
_this.next = jqxhr.next;
_this.id = jqxhr.id;
_this.jqel = $game_html.clone();
if (jqxhr.gametype == 'practice'){
_this.jqel.addClass('practice_game');
}
// Mark tiles that have already been guessed
if (typeof jqxhr.guesses !== 'undefined' && jqxhr.guesses != null){
var guesses_ar = jqxhr.guesses.split('-');
for(var i=0; i<guesses_ar.length; i++){
_this.jqel.find('li[data-tile="'+guesses_ar[i]+'"]').addClass('pressed').html('<i class="icon-check"></i>');
}
}
_this.changed_stake_recently = false;
_this.jqel.attr('id', 'game_'+_this.game_hash).find('#stake').text('฿'+parseFloat(_this.stake).toFixed(6));
_this.jqel.find('#next').text('฿'+parseFloat(_this.next).toFixed(6));
_this.jqel.hide().addClass('hidegame').css('visibility','hidden').prependTo('.feed').slideDown(300, function(){
$(this).hide().css('visibility','visible').removeClass('hidegame').show();//.fadeIn(400,function(){
// Game is now available in the DOM as jqel.
//$(this).removeClass('hidegame');
_this.jqel.find('.cashout').click(function(e){
e.preventDefault();
if (!busy){
_this.cashout();
}
});
_this.jqel.find('.board li').click(function(e){
e.preventDefault();
if (!busy){
var $tile = $(this);
_this.guess($tile);
}
});
$(this).bind('mouseover', function(){
$('.feed div.game').addClass('faded');
_this.jqel.removeClass('faded');
});
_this.message('A game for ฿'+jqxhr.bet+' has started. Secret hash: <span class="secret_hash">'+jqxhr.secret+'</span>', 'first');
//}).removeClass('hidegame');
});
} else if (jqxhr.status == 'error'){
show_error(jqxhr.message);
}
}).fail(function(){
//alert('jqxhr failed');
show_error('This game could not be retrieved at this time.');
}).always(function(){
setTimeout(function(){
$('#start_game').removeAttr('disabled').spin(false);
}, 500);
});;
// end ajax
}
Game.prototype.message = function(message, msgtype){
//msgtype = (typeof msgtype==='undefined')?'':msgtype;
if(message.length < 1){
return true;
}
var $el = $('<p>'+message+'</p>');
if (arguments.length > 1){
for (var i = 1, len = arguments.length; i<=len; i++){
$el.addClass(arguments[i]);
}
}
$el.hide().prependTo(this.jqel.find('.messages')).slideDown(200);
}
Game.prototype.cashout = function(){
var timer = 0;
var timer_int = setInterval(function(){
timer++;
}, 100);
var _this = this;
_this.jqel.find('button.cashout').attr('disabled','disabled').spin(spin_opts);
$.ajax({
url: webroot+'action/cashout.php',
// terribly insecure. Fix later
data: 'game_hash='+_this.game_hash,
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
clearInterval(timer_int);
if (jqxhr.status == 'success'){
ga('send', 'event', 'game', 'cashout', 'wait', timer);
updateBalance(parseFloat(jqxhr.win));
_this.jqel.find('.inplay p').html('Won: <span>฿'+jqxhr.win+'</span>');
_this.jqel.find('.cashout').hide();
_this.message(jqxhr.message, 'won');
_this.message('Secret: '+jqxhr.mines+'-'+jqxhr.random_string);
var mines = jqxhr.mines.split('-');
for (i=0; i<mines.length; i++){
_this.jqel.find('li[data-tile="'+mines[i]+'"]').addClass('reveal').html('<i class="icon-alert"></i>');
}
} else if (jqxhr.status == 'error'){
_this.message(jqxhr.message,'error');
}
}).fail(function(){
alert('jqxhr failed');
//$('<li class="error" style="display:none;">This guess could not be completed at this time.</li>').prependTo('.history').slideDown();
}).always(function(){
_this.jqel.find('button.cashout').removeAttr('disabled').spin(false);
});
// end ajax
}
Game.prototype.guess = function($tile){
var timer = 0;
var timer_int = setInterval(function(){
timer++;
}, 100);
var _this = this;
var tile_number = parseInt($tile.attr('data-tile'));
if (tile_number > 0 && tile_number < 26){
var opts = {
lines: 13,
length: 2,
width: 2,
radius: 6,
corners: 1,
rotate: 0,
direction: 1,
color: '#000'
};
$tile.spin(opts);
$tile.addClass('active_tile');
busy = true;
$.ajax({
url: webroot+'action/checkboard.php',
data: 'game_hash='+_this.game_hash+'&guess='+tile_number+'&v04=1',
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
busy = false;
clearInterval(timer_int);
$tile.spin(false);
$tile.removeClass('active_tile');
_this.betNumber++;
if (jqxhr.status == 'success'){
ga('send', 'event', 'game', 'guess', 'wait', timer);
if (jqxhr.outcome == 'bitcoins'){
_this.change_stake(jqxhr.stake);
_this.jqel.find('#next').html('฿'+jqxhr.next.toFixed(6));
_this.message(jqxhr.message, 'find');
_this.jqel.find('li[data-tile="'+jqxhr.guess+'"]').addClass('pressed').html('<i class="icon-check"></i>');
}
if (jqxhr.outcome == 'bomb'){
_this.message(jqxhr.message, 'bomb');
_this.jqel.find('li[data-tile="'+jqxhr.guess+'"]').addClass('pressed bomb').html('<i class="icon-alert"></i>');
_this.message('Secret: '+jqxhr.bombs+'-'+jqxhr.random_string);
_this.change_stake(0);
_this.jqel.find('.cashout').hide();
var bombs = jqxhr.bombs.split('-');
for (i=0; i<bombs.length; i++){
_this.jqel.find('li[data-tile="'+bombs[i]+'"]').addClass('reveal').html('<i class="icon-alert"></i>');
}
if (sound_enabled){
lose.play();
}
}
} else if (jqxhr.status == 'error'){
//$('<li class="error" style="display:none;">'+jqxhr.message+'</li>').prependTo('.history').slideDown();
_this.message(jqxhr.message, 'error');
}
}).fail(function(){
$tile.removeClass('active_tile').spin(false);
this.message('There was a problem making that guess.', 'error');
//$('<li class="error" style="display:none;">This guess could not be completed at this time.</li>').prependTo('.history').slideDown();
});
} else {
//alert("Hey, wait a minute!");
this.message('Hey, wait a minute.');
}
}
Game.prototype.change_stake = function(stake){
// Set that the bank has been changed recently
// and change it back after 5 seconds
var _this = this;
this.changed_stake_recently = true;
var current_stake = parseFloat($('#stake').text().substr(1));
if (stake > current_stake){
// winnings going UP
_this.animate_stake(current_stake, stake);
} else {
$('#stake').html('฿'+stake.toFixed(6));
}
setTimeout(function(){
_this.changed_stake_recently = false;
}, 5000);
}
Game.prototype.animate_stake = function(old_stake, new_stake){
var _this = this;
if (!old_stake || !new_stake){
return false;
}
var difference = new_stake - old_stake;
//var steps = 0.0001;
var steps = difference / 20;
var current_stake = old_stake;
if (sound_enabled){
win.play();
}
for (var i=0; i<20; i++){
setTimeout(function(){
current_stake += steps;
_this.jqel.find('#stake').html('฿'+current_stake.toFixed(6));
}, 40*i);
}
setTimeout(function(){
_this.jqel.find('#stake').html('฿'+new_stake.toFixed(6));
}, 800);
}
/* Misc functions */
function updateBalance(change){
var currentBalance = parseFloat($('.player .balance .val').text().substr(1));
document.title = '฿'+(currentBalance+change).toFixed(6)+' - Satoshi Mines';
$('.player .balance .val').text('฿'+(currentBalance+change).toFixed(6));
if (currentBalance + change > 0){
$('#cashout_balance').removeClass('disabled');
} else {
$('#cashout_balance').addClass('disabled');
}
}
function show_error(message){
$('<p class="player_error">'+message+'</p>').hide().prependTo('.feed').slideDown(200);
}
function show_success(message){
$('<p class="player_success">'+message+'</p>').hide().prependTo('.feed').slideDown(200);
}
function clean_feed(){
$('.feed>*').not($('.feed>*').slice(0,19)).remove();
}
$(document).ready(function(){
if (document.addEventListener){
document.addEventListener("touchstart", function() {},false);
}
/* Navigation */
$('.nav .links a').click(function(e){
e.preventDefault();
$('.nav .links li').removeClass('selected');
$(this).parent().addClass('selected');
$.ajax({
url: $(this).attr('href'),
type: 'GET',
dataType: 'html',
cache: false
}).done(function(jqxhr){
$('.sub .sub_content').html(jqxhr).parent().slideDown(300);
$('.sub .sub_content form').submit(function(e){
var $button = $(this).find('button');
$button.attr('disabled', 'disabled').spin(spin_opts);
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
dataType: 'html',
cache: false,
data: $(this).serialize()
}).done(function(form_result){
if(form_result.indexOf('_green') > -1) {
$('.sub .sub_content form').slideUp(400);
$(''+form_result).hide().prependTo('.sub .sub_content .form-container').slideDown(400);
} else {
$(''+form_result).hide().prependTo('.sub .sub_content .form-container form').slideDown(400);
}
}).fail(function(){
$('<p class="player_error">An unknown error occued.</p>').hide().prependTo('.sub .sub_content .form-container').slideDown(400);
}).always(function(){
$button.removeAttr('disabled').spin(false);
});
});
});
});
$('.sub_close').click(function(e){
e.preventDefault();
$(this).parent().slideUp(300, function(){
$(this).find('.sub_content').html('');
$('.nav .links li').removeClass('selected');
});
});
$('.sound').click(function(e){
e.preventDefault();
if(sound_enabled){
$(this).attr('class', 'sound sound_disabled').find('i').attr('class', 'icon-volume-off');
sound_enabled = false;
} else {
$(this).attr('class', 'sound sound_enabled').find('i').attr('class', 'icon-volume-on');
sound_enabled = true;
}
});
/* Game bar */
$('.player_container').waypoint('sticky');
/* QR Code and name */
$('#player_name').blur(function(e){
e.preventDefault();
var player_name = $(this).val();
$.ajax({
url: webroot+'action/nameplayer.php',
data: 'secret='+playerhash+'&player_name='+player_name,
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
if (jqxhr.status == 'error'){
show_error(jqxhr.message);
}
}).fail(function(){
show_error('There was a problem updating your player name. Please contact the admin if this keeps happening.');
});
});
$('.qrcode').click(function(e){
e.preventDefault();
if ($(this).parent().find('img').length > 0){
$(this).parent().find('div').show();
} else {
$qrimg = $('<div><img width="200" height="200" src="https://blockchain.info/qr?data='+($(this).parent().attr('data-qr'))+'&size=200"><button>close</button></div>');
$qrimg.find('button').click(function(){
$(this).parent().hide();
});
$(this).parent().prepend($qrimg);
}
});
/* New game controls */
var presentHandler = function(e){
e.preventDefault();
if ($(this).hasClass('button_zero')){
consecutive_zeros++;
console.log(consecutive_zeros);
} else {
consecutive_zeros = 0;
}
var player_balance = parseFloat($('.player .balance .val').text().substr(1));
var currentBalance = parseFloat($('.player .bet').val());
if (isNaN(currentBalance)){
currentBalance = 0;
}
if ($(this).text().substr(0, 1) == '+'){
var newval = parseFloat($(this).text());
newval = (newval+currentBalance).toFixed(4);
} else if ($(this).text().substr(0, 1) == '-'){
var newval = parseFloat($(this).text());
newval = (currentBalance+newval).toFixed(4);
} else if ($(this).text() == 'MAX'){
var newval = maxbet;
} else if ($(this).text() == 'MIN'){
var newval = minbet;
} else {
var newval = parseFloat($(this).text()).toFixed(4);
}
if (newval == maxbet && newval > player_balance && player_balance > 0){
newval = player_balance;
}
$('.bet').val(parseFloat(newval));
if (consecutive_zeros >= 5){
edit_presets();
}
}
$('.bets button').bind('click', presentHandler);
$('#start_game').click(function(){
//var bet = parseFloat($('#bet').val(), 10);
//bet = (isNaN(bet))?0:bet;
$(this).attr('disabled', 'disabled').spin(spin_opts);
var bet = $('#bet').val();
var num_mines = $('input[name="num_mines"]:checked').val();
games.push(new Game(bet, undefined, num_mines));
});
/* Edit the preset buttons */
function edit_presets() {
//return false;
consecutive_zeros = 0;
$('ul.bets li').not(':first').each(function(){
$(this).append('<input type="text" value="'+$(this).find('button').text()+'" maxlength="7">').find('button').hide();
});
$('ul.bets li:first button').text('Done').unbind('click').click(function(){
consecutive_zeros = 0;
$('ul.bets li').not(':first').each(function(){
$(this).find('button').text($(this).find('input').val()).show();
$(this).find('button').removeClass('button_plus button_minus');
if ($(this).find('input').val().substr(0,1) == '+'){
$(this).find('button').addClass('button_plus');
} else if ($(this).find('input').val().substr(0,1) == '-'){
$(this).find('button').addClass('button_minus');
}
$(this).find('input').remove();
//$(this).hide();
});
$(this).text('0').unbind('click').bind('click', presentHandler);
//$(this).unbind('click');
});
}
/* Money */
$('#refresh_balance').click(function(e){
e.preventDefault();
$this = $(this);
$this.parent().siblings('.val').addClass('disabled').spin(spin_opts);
busy = true;
$.ajax({
url: webroot+'action/refresh_balance.php',
data: 'secret='+playerhash,
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
busy = false;
$this.parent().siblings('.val').removeClass('disabled').spin(false);
if (jqxhr.status == 'success'){
document.title = '฿'+jqxhr.balance+' - Satoshi Mines';
$('.player .balance .val').text('฿'+jqxhr.balance);
if (jqxhr.balance > 0){
$('#cashout_balance').removeClass('disabled');
} else {
$('#cashout_balance').addClass('disabled');
}
} else if (jqxhr.status == 'error'){
show_error('There was a problem refreshing your balance: '+jqxhr.message);
}
}).fail(function(){
show_error('A problem occured when trying to refresh your balance. Please try again in a minute, and then contact the admin');
//$('<li class="error" style="display:none;">This guess could not be completed at this time.</li>').prependTo('.history').slideDown();
});
});
$('#cashout_balance').click(function(e){
e.preventDefault();
var currentBalance = parseFloat($('.player .balance .val').text().substr(1));
$('.full_cashout #amount').val(''+currentBalance);
$('.full_cashout .cancel').click(function(e){
e.preventDefault();
$(this).parent().parent().slideUp(200);
});
$('.full_cashout').slideDown(200);
});
$('.full_cashout button').click(function(e){
e.preventDefault();
$this = $(this);
$this.attr('disabled', 'disabled').spin(spin_opts);
var payto_address = $('.full_cashout #payout_address').val();
var amount = $('.full_cashout #amount').val();
busy = true;
$.ajax({
url: webroot+'action/full_cashout.php',
data: 'secret='+playerhash+'&payto_address='+payto_address+'&amount='+amount,
type: 'POST',
dataType: 'json'
}).done(function(jqxhr){
busy = false;
if (jqxhr.status == 'success'){
//$('.player .balance .val').text('฿0.000000');
$('.player .balance .val').text('฿'+(parseFloat(jqxhr.balance)).toFixed(6));
$('.full_cashout').slideUp(200);
show_success(jqxhr.message);
} else if (jqxhr.status == 'error'){
show_error('A problem occured while trying to withdraw: '+jqxhr.message);
} else {
show_error('Unknown error.');
}
}).fail(function(){
show_error('A problem occured when trying to cash out your bitcoins. Please try again in a minute, and then contact the admin');
}).always(function(){
$this.removeAttr('disabled').spin(false);
});
});
});