Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($) {
- 'use strict';
- // outcome keywords to detect on previous roll
- const OUTCOMES = ['Ai câștigat!', 'Ai pierdut!', 'Egal!'];
- $('#roll-dice').on('click', function(e) {
- e.preventDefault();
- // clear input
- $('[name=message]').val('');
- // roll two dice
- const sideOne = Math.floor(Math.random() * 6) + 1;
- const sideTwo = Math.floor(Math.random() * 6) + 1;
- animateDice($('#dice1'), sideOne, 12);
- animateDice($('#dice2'), sideTwo, 8);
- const total = sideOne + sideTwo;
- const tag1 = `[dice1=${sideOne}][/dice1]`;
- const tag2 = `[dice2=${sideTwo}][/dice2]`;
- let message = `[b][i] Ai dat cu zarul: ${total}`;
- // look at the last up-to-10 messages for the last roll
- const lastTen = $('.mchat-text').slice(-10).toArray().map(el => $(el).text());
- const lastRollIdx = lastTen.map((txt, idx) => txt.includes('Ai dat cu zarul:') ? idx : -1)
- .filter(idx => idx >= 0)
- .pop();
- if (lastRollIdx !== undefined) {
- const lastText = lastTen[lastRollIdx];
- // if that last roll had NO outcome keyword, this is the second of the pair => add outcome
- const hadOutcome = OUTCOMES.some(o => lastText.includes(o));
- if (!hadOutcome) {
- // extract its result to compare
- const m = lastText.match(/Ai dat cu zarul:\s*(\d+)/);
- if (m) {
- const prev = parseInt(m[1], 10);
- if (total > prev) {
- message += ' [color=green]Ai câștigat![/color]';
- } else if (total < prev) {
- message += ' [color=red]Ai pierdut![/color]';
- } else {
- message += ' [color=orange]Egal![/color]';
- }
- }
- }
- }
- message += '[/i][/b]';
- // insert into chat
- insert_text(tag1 + tag2 + message);
- });
- function animateDice($die, finalValue, rollsLeft) {
- $die.removeClass().addClass('side_' + (Math.floor(Math.random() * 6) + 1));
- if (rollsLeft > 1) {
- setTimeout(() => animateDice($die, finalValue, rollsLeft - 1), 100);
- } else {
- $die.removeClass().addClass('side_' + finalValue);
- }
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment