From 2cd5d9a5413f722c7b0ef12f380dce6975ba19ec Mon Sep 17 00:00:00 2001
From: Arkerthan <arkerthan@gmail.com>
Date: Sat, 26 Dec 2020 01:08:03 +0100
Subject: [PATCH] Convert main.tw logic fully to JS

---
 src/js/main.js            | 82 +++++++++++++++++++++++++++++++--------
 src/uncategorized/main.tw | 28 -------------
 2 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/src/js/main.js b/src/js/main.js
index c5c79eaa41e..587163a2a7f 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -269,30 +269,78 @@ App.MainView.full = function() {
 		return div;
 	}
 
-	const fragment = document.createDocumentFragment();
+	function cleanup() {
+		if (V.tabChoice.SlaveInteract !== "Description") {
+			V.tabChoice.SlaveInteract = "Description";
+		}
 
-	fragment.append(App.Reminders.list({maxFuture: 5, link: true}));
+		penthouseCensus();
+		V.costs = Math.trunc(calculateCosts.predict());
+		V.currentRule = V.defaultRules[0];
+		SlaveSort.slaves(V.slaves);
 
-	if (V.seeFCNN === 1) {
-		const div = document.createElement("div");
-		div.classList.add("main-fcnn");
-		div.append(App.MainView.fcnn(),
-			App.UI.DOM.passageLink("Hide", passage(), () => { V.seeFCNN = 0; })
-		);
-		fragment.append(div);
+		App.UI.SlaveList.ScrollPosition.restore();
 	}
 
-	fragment.append(mainMenu());
+	const fragment = document.createDocumentFragment();
 
-	fragment.append(App.UI.SlaveList.penthousePage());
+	fragment.append(App.MainView.errors());
 
-	if (V.fucktoyInteractionsPosition === 0) {
-		fragment.append(App.MainView.useFucktoys());
-	}
-	if (V.useSlaveSummaryOverviewTab === 0) {
-		fragment.append(App.MainView.useGuard());
+	// wrap everything in a try/catch statement, so App.MainView.errors() always gets shown. At the end print error out
+	// as well.
+	try {
+		cleanup();
+
+		if (V.newModelUI === 1) {
+			fragment.append(V.building.render());
+		}
+
+		if (V.seeArcology === 1) {
+			fragment.append(App.Desc.playerArcology(
+				App.UI.DOM.passageLink("Hide", "Main", () => {V.seeArcology = 0;})));
+		}
+
+		if (V.seeDesk === 1) {
+			fragment.append(App.Desc.officeDescription(
+				App.UI.DOM.passageLink("Hide", "Main", () => {V.seeDesk = 0;})));
+		}
+
+		fragment.append(App.Reminders.list({maxFuture: 5, link: true}));
+
+		if (V.seeFCNN === 1) {
+			const div = document.createElement("div");
+			div.classList.add("main-fcnn");
+			div.append(App.MainView.fcnn(),
+				App.UI.DOM.passageLink("Hide", passage(), () => { V.seeFCNN = 0; })
+			);
+			fragment.append(div);
+		}
+
+		fragment.append(mainMenu());
+
+		fragment.append(App.UI.SlaveList.penthousePage());
+
+		if (V.fucktoyInteractionsPosition === 0) {
+			fragment.append(App.MainView.useFucktoys());
+		}
+		if (V.useSlaveSummaryOverviewTab === 0) {
+			fragment.append(App.MainView.useGuard());
+		}
+		fragment.append(App.MainView.walkPast());
+
+	} catch (ex) {
+		App.UI.DOM.appendNewElement("p", fragment, `${ex.name}: ${ex.message}`, ["bold", "red"]);
+
+		const p = document.createElement("p");
+		const lines = ex.stack.split("\n");
+		for (const ll of lines) {
+			const div = document.createElement("div");
+			// remove file path from error message
+			div.append(ll.replace(/file:.*\//, "<path>/"));
+			p.append(div);
+		}
+		fragment.prepend(p);
 	}
-	fragment.append(App.MainView.walkPast());
 
 	return fragment;
 };
diff --git a/src/uncategorized/main.tw b/src/uncategorized/main.tw
index cd73e8b5f03..983bec71f57 100644
--- a/src/uncategorized/main.tw
+++ b/src/uncategorized/main.tw
@@ -1,33 +1,5 @@
 :: Main [nobr jump-to-safe jump-from-safe]
 
-<<includeDOM App.MainView.errors()>>
-
 <<set $nextButton = "END WEEK", $nextLink = "End Week", $encyclopedia = "How to Play">>
 
-<<if $tabChoice.SlaveInteract != 'Description'>>
-	<<set $tabChoice.SlaveInteract = 'Description'>>
-<</if>>
-
-<<run penthouseCensus()>>
-
-<<set $costs = Math.trunc(calculateCosts.predict())>>
-
-<<set $currentRule = $defaultRules[0]>>
-
-<<run SlaveSort.slaves($slaves)>>
-
-<<if $newModelUI == 1>>
-	<<includeDOM V.building.render()>>
-<</if>>
-
-<<if $seeArcology == 1>>
-	<<includeDOM App.Desc.playerArcology(App.UI.DOM.passageLink("Hide", "Main", () => {V.seeArcology = 0}))>>
-<</if>>
-
-<<if $seeDesk == 1>>
-	<<includeDOM App.Desc.officeDescription(App.UI.DOM.passageLink("Hide", "Main", () => {V.seeDesk = 0}))>>
-<</if>>
-
 <<includeDOM App.MainView.full()>>
-
-<<run App.UI.SlaveList.ScrollPosition.restore()>>
-- 
GitLab