PDA

View Full Version : Help on a Javascript (Slot Machine)



Informatics
07-31-2010, 07:08 PM
Hi to all, I need a little help.
I'm having a problem deciphering this script.
This is a 3 image slot machine script(I guess)
Can someone explain to me how this code works?
And how can I make it show the same result?

here it goes:

var chaosMachineSpinCtr = 0;
function chaos_machine_attempt(params) {
params.on_start(params);
$.post(params.post_url, {}, function(data) {
var ret = JSON.parse(data);
spinResults = ret.spin_results;
chaosMachineSpinCtr = 0;
chaos_machine_spin_slot(params, ret);
});

}

function chaos_machine_spin_slot(params, ret) {
spinResults = ret.spin_results;
if(chaosMachineSpinCtr < 25) {
$('#'+params.ul_id+' li[rel=1] > img').attr('src', params.images[array_rand(params.images)]);
} else if(chaosMachineSpinCtr == 25) {
$('#'+params.ul_id+' li[rel=1] > img').attr('src', spinResults.images[0]);
}

if(chaosMachineSpinCtr >= 25 && chaosMachineSpinCtr < 50) {
$('#'+params.ul_id+' li[rel=2] > img').attr('src', params.images[array_rand(params.images)]);
} else if(chaosMachineSpinCtr == 50) {
$('#'+params.ul_id+' li[rel=2] > img').attr('src', spinResults.images[1]);
}

if(chaosMachineSpinCtr >= 50 && chaosMachineSpinCtr < 75) {
$('#'+params.ul_id+' li[rel=3] > img').attr('src', params.images[array_rand(params.images)]);
} else if(chaosMachineSpinCtr == 75) {
$('#'+params.ul_id+' li[rel=3] > img').attr('src', spinResults.images[2]);
}

chaosMachineSpinCtr++;

if(chaosMachineSpinCtr <= 75) {
setTimeout(function() {chaos_machine_spin_slot(params, ret);}, 50);
} else {
params.on_end(params, ret);
}
}


<script type="text/javascript">
$(function() {
$('#balance_warning').hide();
$('#start_spinning').click(function() {
var params = JSON.parse('{"post_url":"\/promo\/chaos_machine2","images":["http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/large_sd_potion.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/wing_of_storm.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/devil_square_ticket_(1_use).jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/bone_blade.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/blood_castle_ticket_(1_use).jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/jewels_of_soul_(stack_of_30).jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/fruit_strength.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/blood_castle_ticket_(10_uses).jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/devil_square_ticket_(10_uses).jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/jewel_of_life.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/gem_stone.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/jewel_of_creation.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/large_complex_potion.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/fruit_energy.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/fruit_stamina.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/fruit_agility.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/swords_knightblade_9_skill_luck_12.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/bows_arrowviperbow_9_skill_luck_12.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/platina_staff.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/swords_darkreign_9_skill_luck_12_exc.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/jewel_of_harmony.jpg","http:\/\/img.mobiusonline.net\/images\/stores\/products\/52x52\/jewel_of_bless.jpg"],"ul_id":"spin_me"}');
params.on_start = function(params) {
$('#start_spinning').fadeOut();
$('#youLose').fadeOut();
};

params.on_end = function(params, spinResult) {
var prodPrice = 20.00;

$('#remaining_balance').html(spinResult.remaining_balance);
if(spinResult.awarded == 1) {
tb_show('Congratulations! You Won!', '/promo/chaos_machine_prize?width=400&height=350');
} else {
$('#youLose').fadeIn();
}

if(prodPrice > spinResult.remaining_balance) {
$('#balance_warning').show();
} else {
$('#start_spinning').fadeIn();
}
};
chaos_machine_attempt(params);
return false;
});
});
</script>

djr33
08-01-2010, 02:12 PM
This sounds a lot like a homework problem, and that's not really why we're here. If that's not the case, explain a bit more.
And why don't you understand the code? Is it not yours?

mburt
08-01-2010, 04:33 PM
There must be more to it, because "$" is used over and over, and isn't defined in any way. Sometimes people might make "$" a function like this:


$ = function() { }

Oh... I just figured out that this uses JSON. If you don't know what JSON is, or how to use it, there's no point to try to understand this code.

Informatics
08-01-2010, 05:54 PM
@drj33 It's not an assignment sir dj, I was just trying to understand it.

@mburt I edited it sir, added a new quote with the "$" function. Is this what you mean?

Thank you for your replies.