Skip to content

Instantly share code, notes, and snippets.

@AgileMantis
Created November 22, 2011 16:56
Show Gist options
  • Save AgileMantis/1386181 to your computer and use it in GitHub Desktop.
Save AgileMantis/1386181 to your computer and use it in GitHub Desktop.
slot_js
// (The MIT License)
//
// Copyright (c) 2011 Ledsworth Consulting LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
$(document).ready(function(){
$('div.machine_handle').click( function() { start_spin(); } );
});
function get_cash_amount() {
return parseInt($('span#cash').html());
}
function add_to_cash(amount) {
$('span#cash').html(get_cash_amount() + amount);
}
function start_spin() {
cash = get_cash_amount();
if(cash > 0) {
add_to_cash(-2);
$('div.message').hide();
spin();
}
else {
alert("Sorry, you're out of cash!");
}
};
function move_handle() {
$('div.ball').animate({top: '+=80'},900);
$('div.ball').animate({top: '-=80'},900);
}
function spin() {
move_handle();
var w1Spins = true; var w2Spins = true; var w3Spins = true;
var w1Spinstop = 10 + randStop();
var w2Spinstop = w1Spinstop + 15 + randStop();
var w3Spinstop = w2Spinstop + 15 + randStop();
var w1 = $('div#w1');
var w2 = $('div#w2');
var w3 = $('div#w3');
var spinCount = 0;
while(w3Spins) {
spinCount++;
w1Spins = (spinCount < w1Spinstop) ? true : false;
w2Spins = (spinCount < w2Spinstop) ? true : false;
w3Spins = (spinCount < w3Spinstop) ? true : false;
if(w1Spins)
advanceWheel(w1);
if(w2Spins)
advanceWheel(w2);
if(w3Spins)
advanceWheel(w3);
// Move back to top.
if(w1Spins)
w1.animate({"top": "-=30"},0);
if(w2Spins)
w2.animate({"top": "-=30"},0);
if(w3Spins)
w3.animate({"top": "-=30"},0);
}
// Trick to ensure spin result is only called after all of the
// above animations....
w3.animate({"top": "-=0"}, 1, function() { spin_result(); });
};
// To emulate spinning, after shifting all wheels down one row,
// quickly shift them back up and then move all symbols down
// one row. This approach saves having to move divs which roll
// off the display back to the very top such that they can scroll
// down again. 1) Shift wheel back up. 2) Shift symbols down
function advanceWheel(w) {
w.animate({"top": "+=29"}, 100);
w.animate({"top": "+=1"}, 0, function() { shiftSymbolsDown(this); });
};
function shiftSymbolsDown(w) {
var tmp = w.children[4].innerHTML;
var tmp2 = w.children[4].getAttribute('val');
for(var c=4; c>0; c--) {
w.children[c].innerHTML = w.children[c-1].innerHTML;
w.children[c].setAttribute('val',w.children[c-1].getAttribute('val'));
}
w.children[0].innerHTML = tmp;
w.children[0].setAttribute('val',tmp2);
};
function spin_result() {
var result = "";
for(var w=1; w<4; w++)
result += $('div#w'+w+' #s2').attr('val');
if(result.match(/777/gi))
won(500);
else if(result.match(/===/gi))
won(400);
else if(result.match(/---/gi))
won(300);
else if(result.match(/[=|-]{3}/gi))
won(100);
};
function won(amount) {
add_to_cash(amount);
$('div.message').show().html('You won '+amount+' Points');
}
function randStop() {
return Math.floor(Math.random()*11);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment