From 3aaf8002319cdebd93f2292e6d18cb727c532550 Mon Sep 17 00:00:00 2001
From: gumi <mekolat@users.noreply.github.com>
Date: Thu, 18 Aug 2016 13:08:31 -0400
Subject: add FuzzyTime functions (human-readable time)

---
 npc/commands/scheduled-broadcasts.txt |  21 +++--
 npc/functions/time.txt                | 143 ++++++++++++++++++++++++++++++++++
 npc/scripts.conf                      |   1 +
 3 files changed, 158 insertions(+), 7 deletions(-)
 create mode 100644 npc/functions/time.txt

(limited to 'npc')

diff --git a/npc/commands/scheduled-broadcasts.txt b/npc/commands/scheduled-broadcasts.txt
index e8c16d6e..b520f0b0 100644
--- a/npc/commands/scheduled-broadcasts.txt
+++ b/npc/commands/scheduled-broadcasts.txt
@@ -114,6 +114,7 @@ function	script	StoneBoard	{
                         $@SCHED_Opt[1] = .@int;
                         $@SCHED_Opt[2] = 0;
                         $@SCHED_Opt[3] = .@max;
+                        $@SCHED_Opt[4] = 0;
                         $@SCHED_Msg$ = .@msg$;
                         if (.@int)
                         {
@@ -146,8 +147,9 @@ function	script	StoneBoard	{
             mes l("Sent on login: @@", ($@SCHED_Opt[0] ? l("yes") : l("no")));
             if ($@SCHED_Opt[1])
             {
+                .@next = max(1, ((3600000 * ($@SCHED_Opt[1] - $@SCHED_Opt[4])) - getnpctimer(0)));
                 mes l("Interval: every @@ hour(s)", $@SCHED_Opt[1]);
-                //mes l("Next broadcast: "); // FIXME: (needs a HumanTime function)
+                mes l("Next broadcast: @@", FuzzyTimeFromMs(.@next));
             }
             else
             {
@@ -183,14 +185,19 @@ function	script	StoneBoard	{
     end;
 
 OnTimer3600000:
-    stopnpctimer;
-    ++$@SCHED_Opt[2];
-    if ($@SCHED_Msg$ != "")
+    ++$@SCHED_Opt[4];
+    if ($@SCHED_Opt[4] == ($@SCHED_Opt[1] - 1))
     {
-        announce $@SCHED_Msg$, bc_all;
-        if ($@SCHED_Opt[2] < $@SCHED_Opt[3] && $@SCHED_Opt[3] > 0)
+        stopnpctimer;
+        ++$@SCHED_Opt[2];
+        if ($@SCHED_Msg$ != "")
         {
-            initnpctimer;
+            announce $@SCHED_Msg$, bc_all;
+            $@SCHED_Opt[4] = 0;
+            if ($@SCHED_Opt[2] < $@SCHED_Opt[3] && $@SCHED_Opt[3] > 0)
+            {
+                initnpctimer;
+            }
         }
     }
     end;
diff --git a/npc/functions/time.txt b/npc/functions/time.txt
new file mode 100644
index 00000000..eb19d030
--- /dev/null
+++ b/npc/functions/time.txt
@@ -0,0 +1,143 @@
+// FuzzyTime( <unix timestamp>, <options> )
+// gives time in a human-readable  format
+//
+// <options> is bitmasked:
+// 1  do not show "ago" when in past
+// 2  do not show "in" when in the future
+// 4  show "from now" instead of "in" when in the future
+
+function	script	FuzzyTime	{
+    .@now = gettimetick(2);
+    .@future = getarg(0, .@now);
+    .@options = getarg(1,0);
+    .@diff = max(.@future - .@now);
+    .@ret$ = "";
+    .@past = 0;
+    .@s = 0; // for serial comma
+
+    // define units
+    .@unit_second = 1;
+    .@unit_second$ = l("second");
+    .@unit_seconds$ = l("seconds");
+
+    .@unit_minute = (.@unit_second * 60);
+    .@unit_minute$ = l("minute");
+    .@unit_minutes$ = l("minutes");
+
+    .@unit_hour = (.@unit_minute * 60);
+    .@unit_hour$ = l("hour");
+    .@unit_hours$ = l("hours");
+
+    .@unit_day = (.@unit_hour * 24);
+    .@unit_day$ = l("day");
+    .@unit_days$ = l("days");
+
+    .@unit_year = (.@unit_day * 365);
+    .@unit_year$ = l("year");
+    .@unit_years$ = l("years");
+
+    // check if in the past, or in the future
+    if (.@diff < 0)
+    {
+        .@diff *= -1;
+        .@past = 1;
+    }
+
+    .@diff = max(1, .@diff);
+
+    if (.@diff >= .@unit_year)
+    {
+        .@years = (.@diff / .@unit_year);
+        .@diff = (.@diff % .@unit_year);
+        .@ret$ += .@years + " " + getd(".@unit_year" + (.@years > 1 ? "s$" : "$"));
+        ++.@s;
+    }
+
+    if (.@diff >= .@unit_day)
+    {
+        .@days = (.@diff / .@unit_day);
+        .@diff = (.@diff % .@unit_day);
+
+        if (.@ret$ != "")
+        {
+            .@ret$ += .@diff > 0 ? ", " : l(", and ");
+        }
+
+        .@ret$ += .@days + " " + getd(".@unit_day" + (.@days > 1 ? "s$" : "$"));
+        ++.@s;
+    }
+
+    if (.@diff >= .@unit_hour)
+    {
+        .@hours = (.@diff / .@unit_hour);
+        .@diff = (.@diff % .@unit_hour);
+
+        if (.@ret$ != "")
+        {
+            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
+        }
+
+        .@ret$ += .@hours + " " + getd(".@unit_hour" + (.@hours > 1 ? "s$" : "$"));
+        ++.@s;
+    }
+
+    if (.@diff >= .@unit_minute)
+    {
+        .@minutes = (.@diff / .@unit_minute);
+        .@diff = (.@diff % .@unit_minute);
+
+        if (.@ret$ != "")
+        {
+            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
+        }
+
+        .@ret$ += .@minutes + " " + getd(".@unit_minute" + (.@minutes > 1 ? "s$" : "$"));
+        ++.@s;
+    }
+
+    if (.@diff >= .@unit_second)
+    {
+        .@seconds = (.@diff / .@unit_second);
+        .@diff = (.@diff % .@unit_second);
+
+        if (.@ret$ != "")
+        {
+            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
+        }
+
+        .@ret$ += .@seconds + " " + getd(".@unit_second" + (.@seconds > 1 ? "s$" : "$"));
+        ++.@s;
+    }
+
+    if (.@past > 0 && !(.@options & 1))
+    {
+        .@ret$ += l(" ago");
+    }
+
+    if (.@past < 1 && !(.@options & 2))
+    {
+        .@ret$ = (.@options & 4) ? l("@@ from now", .@ret$) : l("in @@", .@ret$);
+    }
+
+    return .@ret$;
+}
+
+function	script	FuzzyTimeFromSeconds	{
+    return FuzzyTime((gettimetick(2) + getarg(0,0)), getarg(1,0));
+}
+
+function	script	FuzzyTimeFromMs	{
+    return FuzzyTimeFromSeconds((getarg(0,0) / 1000), getarg(1,0));
+}
+
+function	script	FuzzyTimeFromMinutes	{
+    return FuzzyTimeFromSeconds((getarg(0,0) * 60), getarg(1,0));
+}
+
+function	script	FuzzyTimeFromHours	{
+    return FuzzyTimeFromMinutes((getarg(0,0) * 60), getarg(1,0));
+}
+
+function	script	FuzzyTimeFromDays	{
+    return FuzzyTimeFromHours((getarg(0,0) * 24), getarg(1,0));
+}
diff --git a/npc/scripts.conf b/npc/scripts.conf
index bab98e83..31de12c5 100644
--- a/npc/scripts.conf
+++ b/npc/scripts.conf
@@ -31,6 +31,7 @@
 "npc/functions/fishing.txt",
 "npc/functions/mouboofunc.txt",
 "npc/functions/string.txt",
+"npc/functions/time.txt",
 
 // quest debug
 "npc/functions/quest-debug/functions.txt",
-- 
cgit v1.2.3-70-g09d2