From e179a7ddbbb7a1873797b7919128e31afcbfd45d Mon Sep 17 00:00:00 2001 From: dsoloha <38593903+dsoloha@users.noreply.github.com> Date: Wed, 15 Jan 2020 00:46:01 -0500 Subject: [PATCH] Rewrote reminders as class --- src/Mods/Reminder/reminder.js | 119 ++++++++++++++++---- src/init/storyInit.tw | 2 +- src/pregmod/managePersonalAffairs.tw | 38 +++++-- src/uncategorized/BackwardsCompatibility.tw | 4 +- src/uncategorized/endWeek.tw | 2 +- src/uncategorized/main.tw | 25 +++- 6 files changed, 151 insertions(+), 39 deletions(-) diff --git a/src/Mods/Reminder/reminder.js b/src/Mods/Reminder/reminder.js index f8675e215bc..4fd8a9a6e1d 100644 --- a/src/Mods/Reminder/reminder.js +++ b/src/Mods/Reminder/reminder.js @@ -1,24 +1,99 @@ -App.Reminders.add = function(entry, week) { - if (entry.length > 0 && week.length > 0 && !isNaN(week) && week > 0) { - V.reminders.entries.push(entry); - V.reminders.weeks.push(week); - V.reminders.text.push(`${entry} in ${numberWithPlural(week, 'week')}`); - } -}; +App.Reminders = class { + constructor(array) { + this.array = array; + this.entries = V.reminders.entries; + this.weeks = V.reminders.weeks; + this.active = V.reminders.active; + this.overdue = V.reminders.overdue; + } + + add(entry, week) { + if (entry.length > 0 && week.length > 0 && !isNaN(week)) { + if (week <= 0) { + this.overdue.push(`${entry}`); + } else { + this.entries.push(entry); + this.weeks.push(week); + this.active.push(`${entry} in ${numberWithPlural(week, 'week')}`); + } + } + } + + update() { + let _overdue = [], + _active = []; + + for (let i = 0; i < this.entries.length; i++) { + this.weeks[i]--; + if (this.weeks[i] > 0) { + _active.push(`${this.entries[i]} in ${this.weeks[i]} ${numberWithPlural(this.weeks[i], 'week')}`); + } else { + _overdue.push(`<span class="yellow"></span>`); + this.entries.slice(i, 1); + this.weeks.slice(i, 1); + } + } + this.active = _active; + this.overdue = _overdue; + } + + render() { + let t = ''; + + if (this.array.length > 0) { + t += `You have ${numberWithPluralOne(this.array.length, this.array === this.overdue ? 'overdue reminder' : 'reminder')}:`; + if (this.array === this.overdue) { + this.printOverdue(); + } else { + this.printActive(); + } + // return array === overdue ? printOverdue() : printActive(); + } + + return t; + } + + clear() { + V.reminders = { + entries: [], + weeks: [], + active: [], + overdue: [], + }; + } + + printActive() { + let t = ''; + + for (let i = 0; i < this.active.length; i++) { + t += `<br><<print ${this.active[i]}>>${this.removeElement(this.active, i, 'Manage Personal Affairs')}`; // assumes passage, can be changed if needed + } + + return t; + } + + printOverdue() { + let t = ''; + + if (this.isPersonalAffairs()) { + for (let i = 0; i < this.overdue.length; i++) { + t += `<br><<print ${this.overdue[i]}>>${this.removeElement(this.overdue, i, 'Manage Personal Affairs')}>>`; + } + } else { + t += `<<print ${this.overdue}>>[[View reminders|Manage Personal Affairs]]`; + } + + return t; + } + + removeElement(index, passage) { + if (!index || !passage) { + return null; + } + return App.UI.passageLink('Clear', passage, this.array.slice(index, 1)); + } -App.Reminders.update = function() { - let entries = V.reminders.entries, - weeks = V.reminders.weeks, - text = []; - - for (let i = 0; i < entries.length; i++) { - weeks[i]--; - if (weeks[i] > 0) { - text.push(`${entries[i]} in ${numberWithPluralOne(weeks[i], 'week')}`); - } else { - entries.splice(i, 1); - weeks.splice(i, 1); - } - } - V.reminders.text = text; + isPersonalAffairs() { + return passage() === 'Manage Personal Affairs'; + } }; diff --git a/src/init/storyInit.tw b/src/init/storyInit.tw index 58cec7d6145..131161e29bb 100644 --- a/src/init/storyInit.tw +++ b/src/init/storyInit.tw @@ -466,7 +466,7 @@ You should have received a copy of the GNU General Public License along with thi <<set $REReductionCheckinIDs = []>> <<set $activeSlave = 0>> <<set $activeChild = 0>> -<<set $reminders = {entries: [], weeks: [], text: []}>> +<<set $reminders = {entries: [], weeks:[], active: [], overdue: []}>> <<set $boomerangSlave = 0, $boomerangWeeks = 0, $boomerangBuyer = 0>> diff --git a/src/pregmod/managePersonalAffairs.tw b/src/pregmod/managePersonalAffairs.tw index 7e1791e1e43..6750e71f045 100644 --- a/src/pregmod/managePersonalAffairs.tw +++ b/src/pregmod/managePersonalAffairs.tw @@ -4,6 +4,8 @@ <<run PCTitle()>> <<set _ML = $marrying.length>> +<<set _active = new App.Reminders($reminders.active)>> +<<set _overdue = new App.Reminders($reminders.overdue)>> <<if $cheatMode == 1>> <center>//[[Cheat Edit Player|PCCheatMenu][$cheater = 1]]<br><br>//</center> @@ -218,11 +220,34 @@ Sclera: <br><br> -<<if $reminders.text.length == 0>> +<<if $reminders.active.length == 0 && $reminders.overdue.length == 0>> You do not have any reminders set. <<else>> - You have <<=numberWithPluralOne($reminders.text.length, "reminder")>> set: - <<print $reminders.text>> + <<if $reminders.overdue.length > 0>> + /* + <span class="yellow"> + You have <<=numberWithPluralOne($reminders.overdue.length, "overdue reminder")>>: + </span> + */ + <<run _overdue.render()>> + /* + <<for _i = 0; _i < $reminders.overdue.length; _i++>> + <br> + <<print $reminders.overdue[_i]>> [[Clear|Manage Personal Affairs][$reminders.overdue.deleteAt(_i)]] + <<link "Clear" "Manage Personal Affairs">> + <<run $reminders.overdue.deleteAt(_i)>> + <</link>> + <</for>> + */ + <br> + <</if>> + <<if $reminders.active.length > 0>> + <<run _active.render()>> + /* + You have <<=numberWithPluralOne($reminders.active.length, "reminder")>> set: + <<print $reminders.active>> + */ + <</if>> <</if>> <br> Remind me @@ -232,11 +257,8 @@ in weeks. [[Add|Manage Personal Affairs][App.Reminders.add($reminderEntry, $reminderWeek)]] <br> -<<if $reminders.text.length > 0 || $cheatMode || $debugMode>> - [[Clear your reminders|Manage Personal Affairs][$reminders = {entries: [], weeks: [], text: []}]] - /* TODO: <<link "Clear a reminder" "Manage Personal Affairs">> - <<include "Clear Reminder">> - <</link>> */ +<<if $reminders.active.length > 0 || $reminders.overdue.length > 0 || $cheatMode || $debugMode>> + [[Clear your reminders|Manage Personal Affairs][_active.clear(), _overdue.clear()]] <</if>> <br><br> diff --git a/src/uncategorized/BackwardsCompatibility.tw b/src/uncategorized/BackwardsCompatibility.tw index 7ccbd01a813..5f5c6e09ddc 100644 --- a/src/uncategorized/BackwardsCompatibility.tw +++ b/src/uncategorized/BackwardsCompatibility.tw @@ -153,8 +153,8 @@ <<set $weddingPlanned = 0>> <</if>> -<<if ndef $reminders>> - <<set $reminders = {entries: [], weeks: [], text: []}>> +<<if ndef $reminders || ndef $reminders.overdue>> + <<set $reminders = {entries: [], weeks: [], active: [], overdue: []}>> <</if>> <<if $releaseID < 1057>> diff --git a/src/uncategorized/endWeek.tw b/src/uncategorized/endWeek.tw index bb42d234b12..55b795fb924 100644 --- a/src/uncategorized/endWeek.tw +++ b/src/uncategorized/endWeek.tw @@ -43,7 +43,7 @@ s.lastWeeksRepExpenses = 0; })>> -<<if $reminders.text.length > 0>> +<<if $reminders.active.length > 0>> <<run App.Reminders.update()>> <</if>> diff --git a/src/uncategorized/main.tw b/src/uncategorized/main.tw index 9714f4b24f2..54068b687c3 100644 --- a/src/uncategorized/main.tw +++ b/src/uncategorized/main.tw @@ -80,13 +80,28 @@ <<include "Office Description">> [[Hide|Main][$seeDesk = 0]] <</if>> -<<if $reminders.text.length > 0>> -<br> +<<if $reminders.overdue.length > 0>> + <br> - You have <<=numberWithPluralOne($reminders.text.length, "reminder")>>: - <<print $reminders.text>> - <br><br> + <span class="yellow">/* TODO: this would be a good place to add a class "attention" or "attn" */ + <<run App.Reminders.render($reminders.overdue)>> + /* + You have <<=numberWithPluralOne($reminders.overdue.length, "overdue reminder")>>: + <<print $reminders.overdue>> + */ + </span> + /*[[View reminders|Manage Personal Affairs]]*/ +<</if>> +<<if $reminders.active.length > 0>> + <br> + + <<run App.Reminders.render($reminders.active)>> + /* + You have <<=numberWithPluralOne($reminders.active.length, "reminder")>>: + <<print $reminders.active>> + */ <</if>> +<br><br> <<if $seeFCNN == 1>> <<if $FCNNstation != 1 && $week >= 90>> <center>FCNN: <<print "FCNN service has been temporarily suspended. Please stand by.">> [[Hide|Main][$seeFCNN = 0]]</center> -- GitLab