Advertisement
Draco18s

AI Dungeon Clock

Oct 8th, 2020 (edited)
1,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***
  2.  *
  3.  *  Author: Draco18s
  4.  *
  5.  *  call this from Input Modifier
  6.  *  modifiedText = timePasses(modifiedText, state);
  7.  *  
  8.  *  Commands:
  9.  *  {@set:MM:DD:HH}
  10.  *    - Sets the date-time to Month (3 letter abbr), day, and hour (supports either numerical, 15, or am/pm notation)
  11.  *  {@query:<type>}
  12.  *    - Command is replaced with the result as if the player typed it.
  13.  *    - <type> can be either date, time, or datetime
  14.  *  {@query:<name>[:<status>]}
  15.  *    - Command is replaced with the result as if the player typed it.
  16.  *    - Will list all the conditions <name> is under (if no status is indicated) or the remaining duration of the indicated <status>.
  17.  *  {@<name>:<status>:<duration>:<on_complete>}
  18.  *    - Creates a status effect for the named object (dictionary of dictionaries)
  19.  *    - Duration is measured in days or hours (eg "4d" or "6h"). Day durations will replace with a random hour duration when reaching 0
  20.  *    - <on_complete> text is output literally in context when duration reaches 0
  21.  *        If it was more than 1 day ago, a "(x days ago)" will be added
  22.  *  {@pass:<duration>}
  23.  *    - Add duration to the clock (hours or days) and tick all statuses.
  24.  *        Does not add any text to the input on its own. The player will still have to add "six days later" themselves.
  25.  *    - Any expired status effects will have their <on_complete> text added to the end of the input as if the player typed it.
  26.  *
  27.  *  If you wish to require the user to set the date and time on story start, then include:
  28.  *  As part of the prompt text:
  29.  *    {@set:${Current month? (3 letter abbr)}:${Current day?}:${Current hour? (24hr numerical or am/pm)}}
  30.  *  Output modifier (if no other prompt text):
  31.  *    if(info.actionCount == 1) return {text:"",stop:true};
  32.  *
  33. ***/
  34.  
  35. const months = {
  36.   "Jan":31,
  37.   "Feb":28,
  38.   "Mar":31,
  39.   "Apr":30,
  40.   "May":31,
  41.   "Jun":30,
  42.   "Jul":31,
  43.   "Aug":31,
  44.   "Sept":30,
  45.   "Oct":31,
  46.   "Nov":30,
  47.   "Dec":31
  48. }
  49. const monthOrder = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec","Jan"];
  50. function timePasses(element, state) {
  51.   if(state.date == null) {
  52.     state.effects = {};
  53.     state.date = { "month":"Jan","day":1 };
  54.     state.time = 8;
  55.   }
  56.   const patt = /{@([ -z~]+)}/g;
  57.   var result = element.match(patt);
  58.   if(result != null && result.length > 0) {
  59.     result.forEach(res => {
  60.       var parts = res.split(":");
  61.       if(parts.length>3) {
  62.         var name = parts[0].substring(2,parts[0].length);
  63.         if(name == "set") {
  64.           var month = parts[1].substring(0,parts[1].length);
  65.           var day = parseInt(parts[2].substring(0,parts[2].length));
  66.           var hour = parseInt(parts[3].substring(0,parts[3].length-1));
  67.           if(months[month] == null || day < 1 || day > months[month] || hour < 0 || hour > 24) return;
  68.           if(hour+"" != parts[3].substring(0,parts[3].length-1)) {
  69.             var ampm = parts[3].substring(parts[3].length-3,parts[3].length-2);
  70.             if(ampm == "p") hour+=12;
  71.           }
  72.           state.date.month = month;
  73.           state.date.day = day;
  74.           state.time = hour;
  75.         }
  76.         else {
  77.           var effect = parts[1].substring(0,parts[1].length);
  78.           var durStr = parts[2].substring(0,parts[2].length);
  79.           var endStr = parts[3].substring(0,parts[3].length-1);
  80.           var dd = parseInt(durStr.substring(0,durStr.length-1));
  81.           var eu = durStr[durStr.length-1];
  82.           if(dd == NaN || !(eu == "d" || eu == "h")) return element;
  83.          
  84.           if(state.effects[name] == null){
  85.             state.effects[name] = {};
  86.           }
  87.           if(state.effects[name][effect] == null) {
  88.             state.effects[name][effect] = {};
  89.           }
  90.           state.effects[name][effect].duration = durStr;
  91.           state.effects[name][effect].endText = endStr;
  92.         }
  93.       }
  94.       else if(parts.length==3) {
  95.         var str = parts[0].substring(2,parts[0].length);
  96.         if(str == "query") {
  97.           var person = parts[1].substring(0,parts[1].length);
  98.           var eff = parts[2].substring(0,parts[2].length-1);
  99.           if(state.effects[person] != null) {
  100.             var dur = state.effects[person][eff];
  101.             var queryResult;
  102.             if(dur != null) {
  103.               var v = dur.duration.substring(0,dur.duration.length-1);
  104.               var unit = dur.duration[dur.duration.length-1];
  105.               queryResult = person + " will be " + eff + " for " + v + " more " + (unit == 'h' ? "hour" : "day") + (v > 1 ? "s" : "") + ".";
  106.             }
  107.             else {
  108.               queryResult = person + " is not " + eff + ".";
  109.             }
  110.           }
  111.           else {
  112.             queryResult = person + " is not " + eff + ".";
  113.           }
  114.           const patt2 = /{@(query:[ -z~]+)}/g;
  115.           element = element.replace(patt2,queryResult);
  116.         }
  117.       }
  118.       else if(parts.length==2) {
  119.         var str = parts[0].substring(2,parts[0].length);
  120.         if(str == "query") {
  121.           var query = parts[1].substring(0,parts[1].length-1);
  122.           var dv = state.date.day;
  123.           var queryResult = "";
  124.           if(query == "time") {
  125.             queryResult = (state.time > 12 ? (state.time - 12)+"pm" : (state.time + "am"));
  126.           }
  127.           else if(query == "date") {
  128.             var vd = dv%10;
  129.             if(dv > 10 && dv < 20) vd = 5;
  130.             console.log(vd);
  131.             queryResult = dv + (vd == 1?"st":(vd==2?"nd":(vd==3?"rd":"th"))) + " of " + state.date.month;
  132.           }
  133.           else if(query == "datetime") {
  134.             var vd = dv%10;
  135.             if(dv > 10 && dv < 20) vd = 5;
  136.             console.log(vd);
  137.             queryResult = (state.time > 12 ? (state.time - 12)+"pm" : (state.time + "am")) +
  138.               " on the " + dv + (vd == 1?"st":(vd==2?"nd":(vd==3?"rd":"th"))) + " of " + state.date.month;
  139.           }
  140.           else {
  141.             //assume key in status dict
  142.             var person = query;
  143.             if(state.effects[person] != null) {
  144.               var statusEffects = [];
  145.               for (const eff in state.effects[person]) {
  146.                 statusEffects.push(eff);
  147.               }
  148.               if(statusEffects.length > 0)
  149.                 queryResult = person + " is " + statusEffects.join(", ");
  150.               else
  151.                 queryResult = person + " is normal.";
  152.             }
  153.             else
  154.               queryResult = person + " is normal.";
  155.           }
  156.           const patt2 = /{@(query:[ -z~]+)}/g;
  157.           element = element.replace(patt2,queryResult);
  158.         }
  159.         if(str == "pass") {
  160.           var durStr = parts[1].substring(0,parts[1].length-1);
  161.           var days = 0;
  162.           var hours = 0;
  163.           if(durStr[durStr.length-1] == "h") {
  164.             hours = parseInt(durStr.substring(0,durStr.length-1));
  165.             if(hours == NaN) return element;
  166.             state.time += hours;
  167.             console.log(state.time);
  168.             while(state.time > 24) {
  169.               days++;
  170.               state.time -= 24;
  171.             }
  172.           }
  173.           if(durStr[durStr.length-1] == "d") {
  174.             state.time = 8;
  175.             days += parseInt(durStr.substring(0,durStr.length-1));
  176.           }
  177.           state.date.day += days;
  178.           while(state.date.day > months[state.date.month]) {
  179.             state.date.day -= months[state.date.month];
  180.             state.date.month = monthOrder[monthOrder.indexOf(state.date.month)+1];
  181.           }
  182.           for (const person in state.effects) {
  183.             for (const eff in state.effects[person]) {
  184.               var durStr = state.effects[person][eff].duration;
  185.               var effTime = parseInt(durStr.substring(0,durStr.length-1));
  186.               var effUnit = durStr[durStr.length-1];
  187.               if(effUnit == "h") {
  188.                 effTime -= hours + days*24;
  189.                 state.effects[person][eff].duration = effTime+"h";
  190.                 if(effTime <= 0) {
  191.                   element = element + " " + state.effects[person][eff].endText;
  192.                   if(days > 1) {
  193.                     element = element.substring(0,element.length-1) + " (" + days + " day" + (days > 1 ? "s"  :"") + " ago).";
  194.                   }
  195.                   delete state.effects[person][eff];
  196.                 }
  197.               }
  198.               if(effUnit == "d") {
  199.                 effTime -= days;
  200.                 state.effects[person][eff].duration = effTime+"d";
  201.                 if(effTime <= 0) {
  202.                   if(effTime == 0) {
  203.                     effTime = Math.floor(Math.random()*24);
  204.                     state.effects[person][eff].duration = effTime+"h";
  205.                   }
  206.                   else {
  207.                     element = element + " " + state.effects[person][eff].endText;
  208.                     if(effTime < -1) {
  209.                       var v = Math.abs(effTime+1);
  210.                       element = element.substring(0,element.length-1) + " (" + v + " day" + (v > 1 ? "s"  :"") + " ago).";
  211.                     }
  212.                     delete state.effects[person][eff];
  213.                   }
  214.                 }
  215.               }
  216.             }
  217.           }
  218.         }
  219.       }
  220.       element = element.replace(patt,"");
  221.     });
  222.   }
  223.   return element;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement