hfwhite

Untitled

May 16th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. (function($) {
  2. 'use strict';
  3.  
  4. // outcome keywords to detect on previous roll
  5. const OUTCOMES = ['Ai câștigat!', 'Ai pierdut!', 'Egal!'];
  6.  
  7. $('#roll-dice').on('click', function(e) {
  8. e.preventDefault();
  9.  
  10. // clear input
  11. $('[name=message]').val('');
  12.  
  13. // roll two dice
  14. const sideOne = Math.floor(Math.random() * 6) + 1;
  15. const sideTwo = Math.floor(Math.random() * 6) + 1;
  16. animateDice($('#dice1'), sideOne, 12);
  17. animateDice($('#dice2'), sideTwo, 8);
  18.  
  19. const total = sideOne + sideTwo;
  20. const tag1 = `[dice1=${sideOne}][/dice1]`;
  21. const tag2 = `[dice2=${sideTwo}][/dice2]`;
  22. let message = `[b][i] Ai dat cu zarul: ${total}`;
  23.  
  24. // look at the last up-to-10 messages for the last roll
  25. const lastTen = $('.mchat-text').slice(-10).toArray().map(el => $(el).text());
  26. const lastRollIdx = lastTen.map((txt, idx) => txt.includes('Ai dat cu zarul:') ? idx : -1)
  27. .filter(idx => idx >= 0)
  28. .pop();
  29. if (lastRollIdx !== undefined) {
  30. const lastText = lastTen[lastRollIdx];
  31.  
  32. // if that last roll had NO outcome keyword, this is the second of the pair => add outcome
  33. const hadOutcome = OUTCOMES.some(o => lastText.includes(o));
  34. if (!hadOutcome) {
  35. // extract its result to compare
  36. const m = lastText.match(/Ai dat cu zarul:\s*(\d+)/);
  37. if (m) {
  38. const prev = parseInt(m[1], 10);
  39. if (total > prev) {
  40. message += ' [color=green]Ai câștigat![/color]';
  41. } else if (total < prev) {
  42. message += ' [color=red]Ai pierdut![/color]';
  43. } else {
  44. message += ' [color=orange]Egal![/color]';
  45. }
  46. }
  47. }
  48. }
  49.  
  50. message += '[/i][/b]';
  51. // insert into chat
  52. insert_text(tag1 + tag2 + message);
  53. });
  54.  
  55.  
  56. function animateDice($die, finalValue, rollsLeft) {
  57. $die.removeClass().addClass('side_' + (Math.floor(Math.random() * 6) + 1));
  58. if (rollsLeft > 1) {
  59. setTimeout(() => animateDice($die, finalValue, rollsLeft - 1), 100);
  60. } else {
  61. $die.removeClass().addClass('side_' + finalValue);
  62. }
  63. }
  64.  
  65. })(jQuery);
  66.  
Advertisement
Add Comment
Please, Sign In to add comment