Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- *
- * Author: Draco18s
- *
- * call this from Input Modifier
- * modifiedText = timePasses(modifiedText, state);
- *
- * Commands:
- * {@set:MM:DD:HH}
- * - Sets the date-time to Month (3 letter abbr), day, and hour (supports either numerical, 15, or am/pm notation)
- * {@query:<type>}
- * - Command is replaced with the result as if the player typed it.
- * - <type> can be either date, time, or datetime
- * {@query:<name>[:<status>]}
- * - Command is replaced with the result as if the player typed it.
- * - Will list all the conditions <name> is under (if no status is indicated) or the remaining duration of the indicated <status>.
- * {@<name>:<status>:<duration>:<on_complete>}
- * - Creates a status effect for the named object (dictionary of dictionaries)
- * - Duration is measured in days or hours (eg "4d" or "6h"). Day durations will replace with a random hour duration when reaching 0
- * - <on_complete> text is output literally in context when duration reaches 0
- * If it was more than 1 day ago, a "(x days ago)" will be added
- * {@pass:<duration>}
- * - Add duration to the clock (hours or days) and tick all statuses.
- * Does not add any text to the input on its own. The player will still have to add "six days later" themselves.
- * - Any expired status effects will have their <on_complete> text added to the end of the input as if the player typed it.
- *
- * If you wish to require the user to set the date and time on story start, then include:
- * As part of the prompt text:
- * {@set:${Current month? (3 letter abbr)}:${Current day?}:${Current hour? (24hr numerical or am/pm)}}
- * Output modifier (if no other prompt text):
- * if(info.actionCount == 1) return {text:"",stop:true};
- *
- ***/
- const months = {
- "Jan":31,
- "Feb":28,
- "Mar":31,
- "Apr":30,
- "May":31,
- "Jun":30,
- "Jul":31,
- "Aug":31,
- "Sept":30,
- "Oct":31,
- "Nov":30,
- "Dec":31
- }
- const monthOrder = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec","Jan"];
- function timePasses(element, state) {
- if(state.date == null) {
- state.effects = {};
- state.date = { "month":"Jan","day":1 };
- state.time = 8;
- }
- const patt = /{@([ -z~]+)}/g;
- var result = element.match(patt);
- if(result != null && result.length > 0) {
- result.forEach(res => {
- var parts = res.split(":");
- if(parts.length>3) {
- var name = parts[0].substring(2,parts[0].length);
- if(name == "set") {
- var month = parts[1].substring(0,parts[1].length);
- var day = parseInt(parts[2].substring(0,parts[2].length));
- var hour = parseInt(parts[3].substring(0,parts[3].length-1));
- if(months[month] == null || day < 1 || day > months[month] || hour < 0 || hour > 24) return;
- if(hour+"" != parts[3].substring(0,parts[3].length-1)) {
- var ampm = parts[3].substring(parts[3].length-3,parts[3].length-2);
- if(ampm == "p") hour+=12;
- }
- state.date.month = month;
- state.date.day = day;
- state.time = hour;
- }
- else {
- var effect = parts[1].substring(0,parts[1].length);
- var durStr = parts[2].substring(0,parts[2].length);
- var endStr = parts[3].substring(0,parts[3].length-1);
- var dd = parseInt(durStr.substring(0,durStr.length-1));
- var eu = durStr[durStr.length-1];
- if(dd == NaN || !(eu == "d" || eu == "h")) return element;
- if(state.effects[name] == null){
- state.effects[name] = {};
- }
- if(state.effects[name][effect] == null) {
- state.effects[name][effect] = {};
- }
- state.effects[name][effect].duration = durStr;
- state.effects[name][effect].endText = endStr;
- }
- }
- else if(parts.length==3) {
- var str = parts[0].substring(2,parts[0].length);
- if(str == "query") {
- var person = parts[1].substring(0,parts[1].length);
- var eff = parts[2].substring(0,parts[2].length-1);
- if(state.effects[person] != null) {
- var dur = state.effects[person][eff];
- var queryResult;
- if(dur != null) {
- var v = dur.duration.substring(0,dur.duration.length-1);
- var unit = dur.duration[dur.duration.length-1];
- queryResult = person + " will be " + eff + " for " + v + " more " + (unit == 'h' ? "hour" : "day") + (v > 1 ? "s" : "") + ".";
- }
- else {
- queryResult = person + " is not " + eff + ".";
- }
- }
- else {
- queryResult = person + " is not " + eff + ".";
- }
- const patt2 = /{@(query:[ -z~]+)}/g;
- element = element.replace(patt2,queryResult);
- }
- }
- else if(parts.length==2) {
- var str = parts[0].substring(2,parts[0].length);
- if(str == "query") {
- var query = parts[1].substring(0,parts[1].length-1);
- var dv = state.date.day;
- var queryResult = "";
- if(query == "time") {
- queryResult = (state.time > 12 ? (state.time - 12)+"pm" : (state.time + "am"));
- }
- else if(query == "date") {
- var vd = dv%10;
- if(dv > 10 && dv < 20) vd = 5;
- console.log(vd);
- queryResult = dv + (vd == 1?"st":(vd==2?"nd":(vd==3?"rd":"th"))) + " of " + state.date.month;
- }
- else if(query == "datetime") {
- var vd = dv%10;
- if(dv > 10 && dv < 20) vd = 5;
- console.log(vd);
- queryResult = (state.time > 12 ? (state.time - 12)+"pm" : (state.time + "am")) +
- " on the " + dv + (vd == 1?"st":(vd==2?"nd":(vd==3?"rd":"th"))) + " of " + state.date.month;
- }
- else {
- //assume key in status dict
- var person = query;
- if(state.effects[person] != null) {
- var statusEffects = [];
- for (const eff in state.effects[person]) {
- statusEffects.push(eff);
- }
- if(statusEffects.length > 0)
- queryResult = person + " is " + statusEffects.join(", ");
- else
- queryResult = person + " is normal.";
- }
- else
- queryResult = person + " is normal.";
- }
- const patt2 = /{@(query:[ -z~]+)}/g;
- element = element.replace(patt2,queryResult);
- }
- if(str == "pass") {
- var durStr = parts[1].substring(0,parts[1].length-1);
- var days = 0;
- var hours = 0;
- if(durStr[durStr.length-1] == "h") {
- hours = parseInt(durStr.substring(0,durStr.length-1));
- if(hours == NaN) return element;
- state.time += hours;
- console.log(state.time);
- while(state.time > 24) {
- days++;
- state.time -= 24;
- }
- }
- if(durStr[durStr.length-1] == "d") {
- state.time = 8;
- days += parseInt(durStr.substring(0,durStr.length-1));
- }
- state.date.day += days;
- while(state.date.day > months[state.date.month]) {
- state.date.day -= months[state.date.month];
- state.date.month = monthOrder[monthOrder.indexOf(state.date.month)+1];
- }
- for (const person in state.effects) {
- for (const eff in state.effects[person]) {
- var durStr = state.effects[person][eff].duration;
- var effTime = parseInt(durStr.substring(0,durStr.length-1));
- var effUnit = durStr[durStr.length-1];
- if(effUnit == "h") {
- effTime -= hours + days*24;
- state.effects[person][eff].duration = effTime+"h";
- if(effTime <= 0) {
- element = element + " " + state.effects[person][eff].endText;
- if(days > 1) {
- element = element.substring(0,element.length-1) + " (" + days + " day" + (days > 1 ? "s" :"") + " ago).";
- }
- delete state.effects[person][eff];
- }
- }
- if(effUnit == "d") {
- effTime -= days;
- state.effects[person][eff].duration = effTime+"d";
- if(effTime <= 0) {
- if(effTime == 0) {
- effTime = Math.floor(Math.random()*24);
- state.effects[person][eff].duration = effTime+"h";
- }
- else {
- element = element + " " + state.effects[person][eff].endText;
- if(effTime < -1) {
- var v = Math.abs(effTime+1);
- element = element.substring(0,element.length-1) + " (" + v + " day" + (v > 1 ? "s" :"") + " ago).";
- }
- delete state.effects[person][eff];
- }
- }
- }
- }
- }
- }
- }
- element = element.replace(patt,"");
- });
- }
- return element;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement