From 8de4f59c1c979e679e63a89163c1a6b6af8d5482 Mon Sep 17 00:00:00 2001
From: Arkerthan <arkerthan@mailbox.org>
Date: Sat, 25 Feb 2023 15:05:16 +0100
Subject: [PATCH] color purchase links based on player cash

---
 src/js/makePurchase.js | 66 ++++++++++++++++++------------------------
 1 file changed, 28 insertions(+), 38 deletions(-)

diff --git a/src/js/makePurchase.js b/src/js/makePurchase.js
index e715a30d18b..1a13728e694 100644
--- a/src/js/makePurchase.js
+++ b/src/js/makePurchase.js
@@ -31,8 +31,9 @@ globalThis.makePurchase = function(text, cost, what, {notes, handler, prereqs, r
 		const price = cost !== 0 ? `${cashFormat(Math.trunc(cost))}` : `free`;
 		const button = App.UI.DOM.makeElement("button", capFirstChar(price), ['purchase-button']);
 
-		if (V.cash >= cost &&
-			(!prereqs || prereqs.every(prereq => prereq[0] === true))) {
+		const disabledReasons = isDisabled();
+
+		if (disabledReasons.length === 0) {
 			button.onclick = execute;
 
 			if (notes) {
@@ -59,22 +60,9 @@ globalThis.makePurchase = function(text, cost, what, {notes, handler, prereqs, r
 		} else {
 			const span = document.createElement("span");
 			const ul = document.createElement("ul");
-			const reasons = [];
-
-			if (V.cash < cost) {
-				reasons.push(`You lack the necessary funds to make this purchase`);
-			}
-
-			if (prereqs) {
-				prereqs.forEach(prereq => {
-					if (prereq[0] !== true) {
-						reasons.push(prereq[1]);
-					}
-				});
-			}
 
-			if (reasons.length > 1) {
-				for (const li of reasons.map(reason => {
+			if (disabledReasons.length > 1) {
+				for (const li of disabledReasons.map(reason => {
 					const li = document.createElement("li");
 					li.append(reason);
 					return li;
@@ -84,7 +72,7 @@ globalThis.makePurchase = function(text, cost, what, {notes, handler, prereqs, r
 
 				span.append(ul);
 			} else {
-				span.append(reasons[0]);
+				span.append(disabledReasons[0]);
 			}
 
 			button.classList.add("disabled");
@@ -104,35 +92,37 @@ globalThis.makePurchase = function(text, cost, what, {notes, handler, prereqs, r
 
 	function renderLink() {
 		const span = App.UI.DOM.makeElement("span", null, ['indent']);
-		const price = [`${cost !== 0 ? `Costs ${cashFormat(Math.trunc(cost))}` : `Free`}`];
+		const price = [`${cost !== 0 ? `Costs ${cashFormatColor(Math.trunc(cost), V.cash < cost)}` : `Free`}`];
 
 		if (notes) {
 			price.push(...notes);
 		}
 
-		if (V.cash >= cost &&
-			(!prereqs || prereqs.every(prereq => prereq[0] === true))) {
-			span.append(App.UI.DOM.link(text, execute, [], ''), " ");
-		} else {
-			const reasons = [];
+		const disabledReasons = isDisabled();
 
-			if (V.cash < cost) {
-				reasons.push(`You cannot afford this purchase`);
-			}
-
-			if (prereqs) {
-				prereqs.forEach(prereq => {
-					if (prereq[0] !== true) {
-						reasons.push(prereq[1]);
-					}
-				});
-			}
-
-			span.append(App.UI.DOM.disabledLink(text, reasons), " ");
+		if (disabledReasons.length === 0) {
+			span.append(App.UI.DOM.link(text, execute), " ");
+		} else {
+			span.append(App.UI.DOM.disabledLink(text, disabledReasons), " ");
 		}
 
-		App.UI.DOM.appendNewElement("span", span, toSentence(price), ['note']);
+		App.Events.addNode(span, [toSentence(price)], "span", ["note"]);
 
 		return span;
 	}
+
+	function isDisabled() {
+		const disabledReasons = [];
+		if (V.cash < cost) {
+			disabledReasons.push(`You cannot afford this purchase`);
+		}
+		if (prereqs) {
+			prereqs.forEach(prereq => {
+				if (prereq[0] !== true) {
+					disabledReasons.push(prereq[1]);
+				}
+			});
+		}
+		return disabledReasons;
+	}
 };
-- 
GitLab