/* Dice Rolling Script (v3.0) by Reid of ZNR (resources.zetaboards.com) Feel free to edit/redistribute */ (function () { var dr = { roll_dice: function (sides, times) { var hasil = [], hasil_num = 0, hasil_holder; while (times--) { hasil_holder = Math.ceil(Math.random() * sides); hasil_num += hasil_holder; hasil[hasil.length] = hasil_holder; } return [hasil_num, hasil]; }, regex: /\[hasil\](.+?)\[\/hasil\]/gi, old_regex: /\[hasil=(.+?)\](.+?)\[\/hasil\]/gi, // Intended to be used as an event handler update_post: function (event_handler) { // Malformed post checker var somebody_set_up_the_bomb = false, t = $('textarea[name=post]')[0]; if (dr.regex.test(t.value) && !$('#menu_acp').length) { $("
Psst, jangan coba-coba curang >|
").hide().insertBefore(t).fadeIn(); return false; } else if (!dr.regex.test(t.value) && $('#cheat').length) { $('#cheat').fadeOut(function () { $(this).remove(); }); } $('#malformed').remove(); t.value = t.value.replace(/\[roll\](.+?)\[\/roll\]/gi, function (total, to_roll) { var tr = to_roll.toLowerCase().replace(/ /g, "").split('+'), hasil = [], hasil_num = 0, hasil_holder, i, k, c, excess_holder; if (/[^d+\d]|([+d])\1/.test(to_roll)) { $('#malformed').remove(); $("").insertBefore(t).fadeIn(); somebody_set_up_the_bomb = true; event_handler.preventDefault ? event_handler.preventDefault() : event_handler.returnValue = false; return total; } for (i = 0, k = tr.length; i < k; i++) { if (tr[i].indexOf('d') === -1) continue; c = tr[i].split('d'); excess_holder = tr[i + 1] && tr[i + 1].indexOf('d') === -1 ? +tr[i + 1] : 0; hasil_holder = dr.roll_dice(+c[1], +(c[0] || 1)); hasil_num += hasil_holder[0] + excess_holder; hasil[hasil.length] = [hasil_holder[1].join(':'), tr[i], excess_holder, hasil_holder[0] + excess_holder].join(); } return "[hasil]" + hasil_num + "&" + hasil.join("|") + "&" + to_roll + "[/hasil]"; }); // Malformed roll check if (somebody_set_up_the_bomb) { t.value = t.value.replace(dr.regex, "[roll]$1[/roll]"); } }, insert_roll: function () { $('textarea[name=post]').val(function (i, v) { return v + '[roll]' + prompt("Type the roll you which to use\n(Format: [num]d[sides]+[extra(opt.)])") + '[/roll]'; }); }, // Used as an event handler // 'this' is the span hasil clicked roll_display: function () { var information = $(this).next().html().split('%'), dice_used = information[1].split('|'), i, k, tmp, each_die, excess, formatted = ["Original dice used: " + information[0] + "\n\nDissected rolls:"]; for (i = 0, k = dice_used.length; i < k; i++) { tmp = dice_used[i].split(','); each_die = tmp[0].split(':'); excess = tmp[2] !== '0' ? " (+" + tmp[2] + ")" : ""; formatted[formatted.length] = tmp[1] + excess + (each_die.length > 1 ? " => " + each_die.join(' + ') + excess : "") + " => " + tmp[3]; } formatted[formatted.length] = ["\nHasil: " + information[2]]; alert(formatted.join("\n")); } }; if (location.href.indexOf('/post') !== -1) { $('form:has(textarea[name=post])').submit(dr.update_post); $("").insertAfter('#c_bbcode button:last').click(dr.insert_roll); } else if (/topic|single/.test(location.href)) { $('td.c_post').each(function () { if (dr.regex.test(this.innerHTML)) { this.innerHTML = this.innerHTML.replace(dr.regex, function (t, hasil) { // t[0] should be the hasil in number format // t[1] should be the dice used to roll and their hasil, separated by | // t[2] should be the entire dice used var t = hasil.split(/&(?:amp;)?/); return "" + t[0] + ""; }); // Remove edit button unless admin if (!$('#menu_acp').length) { $(this).parents('tr').next().next().find('a[href*=mode=3]').remove(); } } // Support old hasil format (yuck) if (dr.old_regex.test(this.innerHTML)) { this.innerHTML = this.innerHTML.replace(dr.old_regex, "$2"); if (!$('#menu_acp').length) { $(this).parents('tr').next().next().find('a[href*=mode=3]').remove(); } } }); // Bind the hasil function $('span.roll_hasil').click(dr.roll_display); $('a.old_roll_hasil').click(function () { alert("Rolled with: " + $(this).next().text()); }); $('form:has(textarea[name=post])').submit(dr.update_post); } }());