diff --git a/common.js b/common.js index 575b6ed..bd22b7c 100644 --- a/common.js +++ b/common.js @@ -407,9 +407,13 @@ OS.karada = function (body) { return output; } -var dice_re = /(#tea|#dame|#tutturu|#muri|#flip|#when|#du|#fun|#fcount|#test|#sleep|#janken|#8ball|#imfey|#\?(?:\[(?:\w,*)+\])?|#\d{0,2}d\d{1,4}(?:[+-]\d{1,4})?)/i; +var dice_re = /(#tea|#dame|#tutturu|#muri|#flip|#when|#du|#fun|#fcount|#test|#sleep|#janken|#8ball|#imfey|#<(.*)|#\?(?:\[(?:\w,*)+\])?|#\d{0,2}d\d{1,4}(?:[+-]\d{1,4})?)/i; exports.dice_re = dice_re; +//TODO: XXX: Does this need to be exposed to the client? I don't think it does. +const ARBITRARY = config.ARBITRARY || {}; +exports.lookup_arbitrary = ARBITRARY; + var WHEN = [ 'Now', null, null, null, null, 'Later', null, null, null, null, @@ -476,18 +480,7 @@ function parse_dice(frag) { return {n:1, faces:2}; } - if (/^#\?/.test(frag)) - { - var l = frag.slice(2).split(/(\[|\]|,)/); - var j =0; - for(var i=0;i0) j+=1; - } - return {n:1, faces: j}; - } - if (frag == '#fun' || frag == '#fcount' || frag == '#du') + if (frag == '#fun' || frag == '#fcount' || frag == '#du') return {n: 1, faces: 2}; if (frag == '#tea') return {n: 1, faces: 2}; @@ -505,6 +498,23 @@ function parse_dice(frag) return {n: 1, faces: 2}; if (frag == '#janken') return {n: 1, faces: 3}; + if (/^#\?/.test(frag)) + { + var l = frag.slice(2).split(/(\[|\]|,)/); + var j =0; + for(var i=0;i0) j+=1; + } + return {n:1, faces: j}; + } + if (/^#` results... (pseudo-code, maybe come back to this when I can be bothered to write the needed utils, figure out how node capture groups works, and all the other bs needed...) + /* XXX: [[psuedo-code]] + const frag = ms[i].slice(2); + const lookup_result = match_regexes_in_object_keys(config.ARBITRARY, frag); // { lookup: , key: , matches: }; + const lookup = lookup_result.lookup; + + // XXX: Sets the extra.new_dice, and appends post.dice one value: `result`. + const set_dice = (result) => { + const dice = result ? [result] : []; + extra.new_dice = dice; + dice = post.dice ? post.dice.concat(dice) : dice; + post.dice = dice; + return dice; + }; + + if(!lookup) ;// XXX: no match + else if(is_function(lookup)) + { let string = lookup(frag, lookup_result.matches || {}, lookup_result.key); + dice.push([string]); } + else if(is_string(lookup)) dice.push([string]); + else if(is_promise(lookup) { + // We need to insert this via `attachToPost`. So we'll set `extra.new_dice` to be sure it's hit. + //set_dice("..."); //XXX: TODO: Won't this just mean the next result is *appeneded*, and treated as a result for the next command? So, we won't set an intermidiate, we'll just wait on `lookup().then(...)` + // When async lambda completes, we'll set `extra.new_dice()` and append `post.dice()` again. + lookup(frag, lookup_result.matches || {}, lookup_result.key).then(set_dice).catch((err) => { + set_dice("Error: " + (err || "(unbound)")); + }); + return; // XXX: Important to return here so we don't hit the below `if(dice.length)` + } else ; //XXX no match + */ + } else { var info = common.parse_dice(ms[i]); if (!info) @@ -142,6 +175,7 @@ exports.roll_dice = function (frag, post, extra) { if (dice.length + exist > rollLimit) dice = dice.slice(0, Math.max(0, rollLimit - exist)); if (dice.length) { + // TODO: for `#<` to work: The key to hitting `attachToPost` at all is in `extra.new_dice` vs `post.dice` extra.new_dice = dice; dice = post.dice ? post.dice.concat(dice) : dice; post.dice = dice;