Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ParseAndRollDice(dice) {
- var output = [];
- var trainyard = [];
- var value = -1;
- for(var i=0; i<dice.length;i++) {
- var exp = dice[i];
- if(exp == ' ') continue;
- if(exp>='0' && exp<='9') {
- if(value == -1) value = 0;
- value = value*10+parseInt(exp.toString());
- continue;
- }
- else if(value != -1) {
- output.push(value);
- value = -1;
- }
- switch(exp) {
- case '{':
- case '(':
- output.push(exp);
- case '+':
- case '-':
- case '*':
- case '/':
- case 'd':
- case 'p':
- case 's':
- case 'h':
- case 'l':
- case '<':
- case '>':
- case ',':
- while(trainyard.length > 0 && trainyard[trainyard.length-1] != '(' && GetPrecedence(exp) < GetPrecedence(trainyard[trainyard.length-1])) {
- var pp = trainyard.pop();
- output.push(pp);
- }
- trainyard.push(exp);
- break;
- case '}':
- while(trainyard.length > 0) {
- var c = trainyard.pop();
- if(c == '{') break;
- if(c == '(') {
- state.message = "Unpaired (!";
- return 0;
- }
- output.push(c);
- }
- output.push(exp);
- break;
- case ')':
- while(trainyard.length > 0) {
- var c = trainyard.pop();
- if(c == '(') break;
- if(c == '{') {
- state.message = "Unpaired {!";
- return 0;
- }
- output.push(c);
- }
- output.push(exp);
- break;
- default:
- if(!(exp>='0' && exp<='9')) {
- state.message = "Unrecognized dice operation: " + exp;
- return 0;
- }
- break;
- }
- }
- if(value > 0) {
- output.push(value);
- }
- while(trainyard.length > 0) {
- output.push(trainyard.pop());
- }
- var ops = [];
- var v1=0;
- var v2=0;
- var collapse = 0;
- var retreat = [];
- while(output.length > 0) {
- exp = output.shift();
- switch(exp) {
- case '+':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(v2+v1);
- break;
- case '-':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(v2-v1);
- break;
- case '*':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(v2*v1);
- break;
- case '/':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(Math.floor(v2/v1));
- break;
- case 'd':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- var r = [];
- for(var n=0; n < v2; n++) {
- r.push(Math.floor(Math.random()*v1)+1);
- }
- if(collapse == 0) {
- ops.push(ParseArray(r));
- }
- else {
- ops.push(r);
- }
- break;
- case 'p':
- case 's':
- v1 = ParseArray(ops.pop());
- v2 = ops.pop();
- if(!Array.isArray(v2)) {
- state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
- return 0;
- }
- if(v2.length < v1) {
- state.message = "Invalid dice operation: " + exp + "\nValue " + v1 + " was larger than the size of the collection " + v2 + ".";
- return 0;
- }
- var r = [];
- for(var h=0;h<v1;h++) {
- var i = Math.floor(Math.random()*v2.length);
- r.push(v2[i]);
- if(exp == 'p')
- v2.splice(i,1);
- }
- if(collapse == 0) {
- ops.push(ParseArray(r));
- }
- else {
- ops.push(r);
- }
- break;
- case 'h':
- v1 = ParseArray(ops.pop());
- v2 = ops.pop();
- if(!Array.isArray(v2)) {
- state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
- return 0;
- }
- v2.sort(function(a, b){return a - b});
- v2.reverse();
- var r = [];
- for(var h=0;h<v1;h++) {
- r.push(v2[h]);
- }
- ops.push(r);
- break;
- case 'l':
- v1 = ParseArray(ops.pop());
- v2 = ops.pop();
- if(!Array.isArray(v2)) {
- state.message = "Unrecognized dice operation: " + exp + "\nValue " + v2 + " was not a collection.";
- return 0;
- }
- v2.sort(function(a, b){return a - b});
- var r = [];
- for(var h=0;h<v1;h++) {
- r.push(v2[h]);
- }
- ops.push(r);
- break;
- case '<':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(v2 <= v1);
- break;
- break;
- case '>':
- v1 = ParseArray(ops.pop());
- v2 = ParseArray(ops.pop());
- ops.push(v2 >= v1);
- break;
- case '{':
- collapse++;
- break;
- case '}':
- collapse--;
- break;
- case '(':
- if(collapse > 0) {
- retreat.push(collapse);
- collapse = 0;
- }
- break;
- case ')':
- if(retreat.length > 0) {
- collapse = retreat.pop();
- }
- break;
- case ',':
- v1 = ops.pop();
- v2 = ops.pop();
- var p = [];
- if(collapse > 0) {
- if(Array.isArray(v2)) {
- p = v2.concat(v1);
- }
- else if(Array.isArray(v1)) {
- var v3 = [];
- v3.push(v2);
- p = v3.concat(v1);
- }
- else {
- p = [v2, v1];
- }
- ops.push(p);
- }
- else {
- state.message = "Invalid operation: " + exp + "\nTo create a collection, use {a,b}.";
- return 0;
- }
- break;
- default:
- if(collapse > 0) {
- ops.push([exp]);
- }
- else {
- ops.push(exp);
- }
- break;
- }
- }
- var ret = ops.pop();
- if(ret === true) {
- return "[Success!]";
- }
- else if(ret === false) {
- return "[Failed!]";
- }
- if(Array.isArray(ret)) {
- return "(" + ret + ")";
- }
- return ParseArray(ret);
- }
- function GetPrecedence(op) {
- if(op == '(') return 999;
- if(op == 'd') return 50;
- if(op == 'p') return 40;
- if(op == 's') return 40;
- if(op == '*') return 20;
- if(op == '/') return 20;
- if(op == 'h') return 10;
- if(op == 'l') return 10;
- if(op == '+') return 5;
- if(op == '-') return 5;
- if(op == '<') return 1;
- if(op == '>') return 1;
- if(op == ',') return 1;
- return 0;
- }
- function ParseArray(val) {
- if(Array.isArray(val)) {
- var r = 0;
- for(var i = 0; i < val.length; i++) {
- r+=val[i];
- }
- return r;
- }
- else
- return val;
- }
Add Comment
Please, Sign In to add comment