From 3ac33f84603fdf3fca797bf27aad0281c9ef0674 Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Wed, 19 Aug 2020 17:49:25 -0700
Subject: [PATCH] Allow SlaveAssignmentReport to execute DOM reports directly
 without an intermediate SugarCube passage. Convert Lab Report and Rules
 Assistant Report to JS.

---
 src/endWeek/labReport.js                  | 49 +++++++++++++++++++++++
 src/endWeek/rulesAssistantReport.js       |  9 +++++
 src/endWeek/slaveAssignmentReport.js      | 29 +++++++-------
 src/uncategorized/labReport.tw            | 40 ------------------
 src/uncategorized/rulesAssistantReport.tw |  9 -----
 5 files changed, 72 insertions(+), 64 deletions(-)
 create mode 100644 src/endWeek/labReport.js
 create mode 100644 src/endWeek/rulesAssistantReport.js
 delete mode 100644 src/uncategorized/labReport.tw
 delete mode 100644 src/uncategorized/rulesAssistantReport.tw

diff --git a/src/endWeek/labReport.js b/src/endWeek/labReport.js
new file mode 100644
index 00000000000..98f897d8c82
--- /dev/null
+++ b/src/endWeek/labReport.js
@@ -0,0 +1,49 @@
+App.EndWeek.labReport = function() {
+	const frag = document.createDocumentFragment();
+	let r = [];
+	if (V.researchLab.hired + V.researchLab.menials > 0) {
+		if (V.researchLab.tasks.length === 0) {
+			const cash = (V.researchLab.speed * 90);
+			r.push(`Since the lab is currently not working on any projects it takes contract work and earns you <span class="yellowgreen">${cashFormat(cash)}.</span>`);
+			cashX(cash, "lab");
+		} else {
+			let work = V.researchLab.speed;
+			while (work > 0) {
+				const task = V.researchLab.tasks[0];
+				if (!task) {
+					const cash = Math.round(work * 90);
+					work = 0;
+					r.push(`<br>Your lab has <span class="green">finished all projects.</span> The remaining time is used for contract work earning you <span class="yellowgreen">${cashFormat(cash)}.</span>`);
+					cashX(cash, "lab");
+				} else if (work < task.workLeft) {
+					task.workLeft -= work;
+					work = 0;
+					r.push(`The lab continues ${task.type === "research" ? "research" : "work"} on <span class="yellow">${addA(setup.prosthetics[task.id].name)}.</span> It will take approximately ${(Math.floor(task.workLeft / V.researchLab.speed) + 1)} week(s) to complete.`);
+				} else {
+					work -= task.workLeft;
+					r.push(`Your lab staff have <span class="green">completed</span> their`);
+					switch (task.type) {
+						case "research":
+							V.prosthetics[task.id].research = 1;
+							r.push(`${setup.prosthetics[task.id].name} research project.`);
+							break;
+						case "craft":
+							V.prosthetics[task.id].amount += 1;
+							r.push(`${setup.prosthetics[task.id].name} construction project.`);
+							break;
+						case "craftFit":
+							task.workLeft = 0;
+							V.adjustProsthetics.push(task);
+							V.adjustProstheticsCompleted++;
+							r.push(`project to construct ${addA(setup.prosthetics[task.id].name)} for ${SlaveFullName(getSlave(task.slaveID))}.`);
+							break;
+					}
+				}
+				r.push(`<br>`);
+			}
+		}
+	} else if (V.researchLab.tasks.length > 0) {
+		r.push(`<span class="note">Without researchers your tasks will never be finished.</span>`);
+	}
+	return $(frag).append(r.join(' '))[0];
+};
diff --git a/src/endWeek/rulesAssistantReport.js b/src/endWeek/rulesAssistantReport.js
new file mode 100644
index 00000000000..e64a32c4dcf
--- /dev/null
+++ b/src/endWeek/rulesAssistantReport.js
@@ -0,0 +1,9 @@
+App.EndWeek.rulesAssistantReport = function() {
+	const frag = $(document.createDocumentFragment());
+	for (const slave of V.slaves) {
+		if (slave.useRulesAssistant === 1) {
+			frag.append(DefaultRules(slave));
+		}
+	}
+	return frag[0];
+};
diff --git a/src/endWeek/slaveAssignmentReport.js b/src/endWeek/slaveAssignmentReport.js
index ad99dff3244..5b53f171657 100644
--- a/src/endWeek/slaveAssignmentReport.js
+++ b/src/endWeek/slaveAssignmentReport.js
@@ -296,22 +296,17 @@ App.EndWeek.slaveAssignmentReport = function() {
 	 * @version 0.7RC
 	 * @author 000-250-006
 	 *
-	 * @param array _facListArr V.args
+	 * @param array _facListArr
 	 *	Multidimensional temporary array
-	 *	0: The passage name for the facility's report
-	 *	1: The facility name capitalized (@see todo)
-	 *	2: max number of slaves allowed in facility - > 0 implies open
-	 *	3: number of slaves assigned to facility
-	 *	4: ID of the slave assigned to run the facility ("Boss")
-	 *	5: Text title of the Boss
+	 *	0: The SC passage name or DOM function for the facility's report
+	 *	1: A facility object, or the title of the report if there is no facility obejct
+	 *	2: If there is no facility object, a truthy value indicating whether the facility exists
+	 *	3: If there is no facility object, the maximum capacity of the facility
 	 *
 	 * @todo This is a proof of concept construct, if it works and cuts overhead, intended to create an object
 	 *	for deeper use in multiple locations, including streamlining reports/facilities code to one widget
 	 * @todo Figure out if this would be better as an object rather than an array for overhead
 	 *	StoryInit also?
-	 * @todo Figure out why we're not using ndef/-1 for a bunch of these story variables. Leaky conditionals
-	 * @todo Figure out why we're using variable space with capitalized facility names when we can parse it from true
-	 *     name
 	 */
 
 	const facListArr = [
@@ -330,12 +325,12 @@ App.EndWeek.slaveAssignmentReport = function() {
 		["Incubator Report", App.Entity.facilities.incubator],
 		["Master Suite Report", App.Entity.facilities.masterSuite],
 		["Penthouse Report", "The Penthouse"],
-		["Rules Assistant Report", "Rules Assistant", V.rulesAssistantAuto], /** should be last — may reassign slaves **/
-		["Lab Report", "Lab", V.researchLab.level]
+		[App.EndWeek.rulesAssistantReport, "Rules Assistant", V.rulesAssistantAuto], /** should be last — may reassign slaves **/
+		[App.EndWeek.labReport, "Lab", V.researchLab.level]
 	];
 
 	function _getReportElementStats(ar) {
-		if (typeof ar[1] == "string") {
+		if (typeof ar[1] === "string") {
 			if (ar[1] === "The Penthouse") { // special case because we have to combine several facilities
 				return {
 					name: ar[1],
@@ -369,14 +364,18 @@ App.EndWeek.slaveAssignmentReport = function() {
 
 	for (const facSubArr of facListArr) {
 		const reportContent = document.createElement("div");
-		App.UI.DOM.includePassage(reportContent, facSubArr[0]);
+		if (typeof facSubArr[0] === "function") {
+			reportContent.append(facSubArr[0]());
+		} else if (typeof facSubArr[0] === "string") {
+			App.UI.DOM.includePassage(reportContent, facSubArr[0]);
+		}
 
 		// needs to be inside the loop after the report passage to get the employees number after re-assignments
 		const stats = _getReportElementStats(facSubArr);
 
 		if (stats.established) { // Do we have one of these facilities?
 			const header = document.createDocumentFragment();
-			App.UI.DOM.appendNewElement("span", header, `${stats.name}  Report`, "title");
+			App.UI.DOM.appendNewElement("span", header, `${stats.name} Report`, "title");
 
 			if (!stats.alwaysExists) {
 				const diffNum = stats.entriesNumber - stats.entriesNumberInitial;
diff --git a/src/uncategorized/labReport.tw b/src/uncategorized/labReport.tw
deleted file mode 100644
index 6c75186b14d..00000000000
--- a/src/uncategorized/labReport.tw
+++ /dev/null
@@ -1,40 +0,0 @@
-:: Lab Report [nobr]
-
-<<if ($researchLab.hired + $researchLab.menials) > 0>>
-	<<if $researchLab.tasks.length == 0>>
-		<<set _cash = ($researchLab.speed * 90)>>
-		Since the lab is currently not working on any projects it takes contract work and earns you @@.yellowgreen;<<= cashFormat(_cash)>>.@@
-		<<run cashX(_cash, "lab")>>
-	<<else>>
-		<<set _work = $researchLab.speed, _task = $researchLab.tasks[0]>>
-		<<for _work > 0>>
-			<<if _work < _task.workLeft>>
-				<<set _task.workLeft -= _work, _work = 0>>
-				The lab continues <<if _task.type == "research">>research<<else>>work<</if>> on @@.yellow;<<= addA(setup.prosthetics[_task.id].name)>>.@@ It will take approximately <<= (Math.floor(_task.workLeft / $researchLab.speed) + 1)>> week(s) to complete.
-			<<else>>
-				<<set _work -= _task.workLeft>>
-				Your lab staff have @@.green;completed@@ their
-				<<switch _task.type>>
-				<<case "research">>
-					<<= setup.prosthetics[_task.id].name>> research project.
-					<<set $prosthetics[_task.id].research = 1>>
-				<<case "craft">>
-					<<= setup.prosthetics[_task.id].name>> construction project.
-					<<set $prosthetics[_task.id].amount += 1>>
-				<<case "craftFit">>
-					<<set _i = $slaveIndices[_task.slaveID], _task.workLeft = 0, $adjustProsthetics.push(_task), $adjustProstheticsCompleted++>>
-					project to construct <<= addA(setup.prosthetics[_task.id].name)>> for <<= SlaveFullName($slaves[_i])>>.
-				<</switch>>
-				<<set $researchLab.tasks.shift(), _task = $researchLab.tasks[0]>>
-				<<if ndef _task>>
-					<<set _cashX = Math.round(_work * 90), _work = 0>>
-					<br>Your lab has @@.green;finished all projects.@@ The remaining time is used for contract work earning you @@.yellowgreen;<<= cashFormat(_cashX)>>.@@
-					<<run cashX(_cashX, "lab")>>
-				<</if>>
-			<</if>>
-			<br>
-		<</for>>
-	<</if>>
-<<elseif $researchLab.tasks.length > 0>>
-	//Without researchers your tasks will never be finished.//
-<</if>>
diff --git a/src/uncategorized/rulesAssistantReport.tw b/src/uncategorized/rulesAssistantReport.tw
deleted file mode 100644
index 820dbcd83e0..00000000000
--- a/src/uncategorized/rulesAssistantReport.tw
+++ /dev/null
@@ -1,9 +0,0 @@
-:: Rules Assistant Report [nobr]
-
-<<set _SL = $slaves.length>>
-
-<<for $i = 0; $i < _SL; $i++>>
-	<<if ($slaves[$i].useRulesAssistant == 1)>>
-		<<= DefaultRules($slaves[$i])>>
-	<</if>>
-<</for>>
-- 
GitLab