Draco18s

Dice Parser

Dec 29th, 2020 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ParseAndRollDice(dice) {
  2.     var output = [];
  3.     var trainyard = [];
  4.     var value = -1;
  5.     for(var i=0; i<dice.length;i++) {
  6.         var exp = dice[i];
  7.         if(exp == ' ') continue;
  8.         if(exp>='0' && exp<='9') {
  9.             if(value == -1) value = 0;
  10.             value = value*10+parseInt(exp.toString());
  11.             continue;
  12.         }
  13.         else if(value != -1) {
  14.             output.push(value);
  15.             value = -1;
  16.         }
  17.         switch(exp) {
  18.             case '{':
  19.             case '(':
  20.                 output.push(exp);
  21.             case '+':
  22.             case '-':
  23.             case '*':
  24.             case '/':
  25.             case 'd':
  26.             case 'p':
  27.             case 's':
  28.             case 'h':
  29.             case 'l':
  30.             case '<':
  31.             case '>':
  32.             case ',':
  33.                 while(trainyard.length > 0 && trainyard[trainyard.length-1] != '(' && GetPrecedence(exp) < GetPrecedence(trainyard[trainyard.length-1])) {
  34.                     var pp = trainyard.pop();
  35.                     output.push(pp);
  36.                 }
  37.                 trainyard.push(exp);
  38.                 break;
  39.             case '}':
  40.                 while(trainyard.length > 0) {
  41.                     var c = trainyard.pop();
  42.                     if(c == '{') break;
  43.                     if(c == '(') {
  44.                         state.message = "Unpaired (!";
  45.                         return 0;
  46.                     }
  47.                     output.push(c);
  48.                 }
  49.                 output.push(exp);
  50.                 break;
  51.             case ')':
  52.                 while(trainyard.length > 0) {
  53.                     var c = trainyard.pop();
  54.                     if(c == '(') break;
  55.                     if(c == '{') {
  56.                         state.message = "Unpaired {!";
  57.                         return 0;
  58.                     }
  59.                     output.push(c);
  60.                 }
  61.                 output.push(exp);
  62.                 break;
  63.             default:
  64.                 if(!(exp>='0' && exp<='9')) {
  65.                     state.message = "Unrecognized dice operation: " + exp;
  66.                     return 0;
  67.                 }
  68.                 break;
  69.         }
  70.     }
  71.     if(value > 0) {
  72.         output.push(value);
  73.     }
  74.     while(trainyard.length > 0) {
  75.         output.push(trainyard.pop());
  76.     }
  77.     var ops = [];
  78.     var v1=0;
  79.     var v2=0;
  80.     var collapse = 0;
  81.     var retreat = [];
  82.     while(output.length > 0) {
  83.         exp = output.shift();
  84.         switch(exp) {
  85.             case '+':
  86.                 v1 = ParseArray(ops.pop());
  87.                 v2 = ParseArray(ops.pop());
  88.                 ops.push(v2+v1);
  89.                 break;
  90.             case '-':
  91.                 v1 = ParseArray(ops.pop());
  92.                 v2 = ParseArray(ops.pop());
  93.                 ops.push(v2-v1);
  94.                 break;
  95.             case '*':
  96.                 v1 = ParseArray(ops.pop());
  97.                 v2 = ParseArray(ops.pop());
  98.                 ops.push(v2*v1);
  99.                 break;
  100.             case '/':
  101.                 v1 = ParseArray(ops.pop());
  102.                 v2 = ParseArray(ops.pop());
  103.                 ops.push(Math.floor(v2/v1));
  104.                 break;
  105.             case 'd':
  106.                 v1 = ParseArray(ops.pop());
  107.                 v2 = ParseArray(ops.pop());
  108.                 var r = [];
  109.                 for(var n=0; n < v2; n++) {
  110.                     r.push(Math.floor(Math.random()*v1)+1);
  111.                 }
  112.                 if(collapse == 0) {
  113.                     ops.push(ParseArray(r));
  114.                 }
  115.                 else {
  116.                     ops.push(r);
  117.                 }
  118.                 break;
  119.             case 'p':
  120.             case 's':
  121.                 v1 = ParseArray(ops.pop());
  122.                 v2 = ops.pop();
  123.                 if(!Array.isArray(v2)) {
  124.                     state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
  125.                     return 0;
  126.                 }
  127.                 if(v2.length < v1) {
  128.                     state.message = "Invalid dice operation: " + exp + "\nValue " + v1 + " was larger than the size of the collection " + v2 + ".";
  129.                     return 0;
  130.                 }
  131.                 var r = [];
  132.                 for(var h=0;h<v1;h++) {
  133.                     var i = Math.floor(Math.random()*v2.length);
  134.                     r.push(v2[i]);
  135.                     if(exp == 'p')
  136.                         v2.splice(i,1);
  137.                 }
  138.                 if(collapse == 0) {
  139.                     ops.push(ParseArray(r));
  140.                 }
  141.                 else {
  142.                     ops.push(r);
  143.                 }
  144.                 break;
  145.             case 'h':
  146.                 v1 = ParseArray(ops.pop());
  147.                 v2 = ops.pop();
  148.                 if(!Array.isArray(v2)) {
  149.                     state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
  150.                     return 0;
  151.                 }
  152.                 v2.sort(function(a, b){return a - b});
  153.                 v2.reverse();
  154.                 var r = [];
  155.                 for(var h=0;h<v1;h++) {
  156.                     r.push(v2[h]);
  157.                 }
  158.                 ops.push(r);
  159.                 break;
  160.             case 'l':
  161.                 v1 = ParseArray(ops.pop());
  162.                 v2 = ops.pop();
  163.                 if(!Array.isArray(v2)) {
  164.                     state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
  165.                     return 0;
  166.                 }
  167.                 v2.sort(function(a, b){return a - b});
  168.                 var r = [];
  169.                 for(var h=0;h<v1;h++) {
  170.                     r.push(v2[h]);
  171.                 }
  172.                 ops.push(r);
  173.                 break;
  174.             case '<':
  175.                 v1 = ParseArray(ops.pop());
  176.                 v2 = ParseArray(ops.pop());
  177.                 ops.push(v2 <= v1);
  178.                 break;
  179.                 break;
  180.             case '>':
  181.                 v1 = ParseArray(ops.pop());
  182.                 v2 = ParseArray(ops.pop());
  183.                 ops.push(v2 >= v1);
  184.                 break;
  185.             case '{':
  186.                 collapse++;
  187.                 break;
  188.             case '}':
  189.                 collapse--;
  190.                 break;
  191.             case '(':
  192.                 if(collapse > 0) {
  193.                     retreat.push(collapse);
  194.                     collapse = 0;
  195.                 }
  196.                 break;
  197.             case ')':
  198.                 if(retreat.length > 0) {
  199.                     collapse = retreat.pop();
  200.                 }
  201.                 break;
  202.             case ',':
  203.                 v1 = ops.pop();
  204.                 v2 = ops.pop();
  205.                 var p = [];
  206.                 if(collapse > 0) {
  207.                     if(Array.isArray(v2)) {
  208.                         p = v2.concat(v1);
  209.                     }
  210.                     else if(Array.isArray(v1)) {
  211.                         var v3 = [];
  212.                         v3.push(v2);
  213.                         p = v3.concat(v1);
  214.                     }
  215.                     else {
  216.                         p = [v2, v1];
  217.                     }
  218.                     ops.push(p);
  219.                 }
  220.                 else {
  221.                     state.message = "Invalid operation: " + exp + "\nTo create a collection, use {a,b}.";
  222.                     return 0;
  223.                 }
  224.                 break;
  225.             default:
  226.                 if(collapse > 0) {
  227.                     ops.push([exp]);
  228.                 }
  229.                 else {
  230.                     ops.push(exp);
  231.                 }
  232.                 break;
  233.         }
  234.     }
  235.     var ret = ops.pop();
  236.     if(ret === true) {
  237.         return "[Success!]";
  238.     }
  239.     else if(ret === false) {
  240.         return "[Failed!]";
  241.     }
  242.     if(Array.isArray(ret)) {
  243.         return "(" + ret + ")";
  244.     }
  245.     return ParseArray(ret);
  246. }
  247.  
  248. function GetPrecedence(op) {
  249.     if(op == '(') return 999;
  250.     if(op == 'd') return 50;
  251.     if(op == 'p') return 40;
  252.     if(op == 's') return 40;
  253.     if(op == '*') return 20;
  254.     if(op == '/') return 20;
  255.     if(op == 'h') return 10;
  256.     if(op == 'l') return 10;
  257.     if(op == '+') return 5;
  258.     if(op == '-') return 5;
  259.     if(op == '<') return 1;
  260.     if(op == '>') return 1;
  261.     if(op == ',') return 1;
  262.     return 0;
  263. }
  264.  
  265. function ParseArray(val) {
  266.     if(Array.isArray(val)) {
  267.         var r = 0;
  268.         for(var i = 0; i < val.length; i++) {
  269.             r+=val[i];
  270.         }
  271.         return r;
  272.     }
  273.     else
  274.         return val;
  275. }
Add Comment
Please, Sign In to add comment