From 2b7196bdf3b7a9ff2da79b4b87d26eb306b565a4 Mon Sep 17 00:00:00 2001
From: DCoded <dicoded@email.com>
Date: Sat, 4 Dec 2021 11:23:39 -0500
Subject: [PATCH] Gated purchases

---
 css/gui/buttons.css |  4 ++++
 src/js/Purchase.js  | 42 +++++++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/css/gui/buttons.css b/css/gui/buttons.css
index ce1e7762b37..650fc6b274a 100644
--- a/css/gui/buttons.css
+++ b/css/gui/buttons.css
@@ -10,3 +10,7 @@
 	background-color: var(--button-hover-color);
 	border: solid 2px var(--button-border-color);
 }
+
+.purchase-button.disabled, .purchase-button.disabled:hover {
+	background-color: black;
+}
diff --git a/src/js/Purchase.js b/src/js/Purchase.js
index 7a703288b40..1213dc0e8be 100644
--- a/src/js/Purchase.js
+++ b/src/js/Purchase.js
@@ -24,29 +24,45 @@ globalThis.Purchase = class Purchase {
 		const button = App.UI.DOM.makeElement("button", capFirstChar(price), ['purchase-button']);
 
 		if (V.purchaseStyle === 'button') {
-			const note = this.note.substring(0, 5) === ' and ' ? this.note.replace(' and ', '') : this.note;
+			if (V.cash > this.cost) {
+				const note = this.note.substring(0, 5) === ' and ' ? this.note.replace(' and ', '') : this.note;
 
-			button.onclick = () => {
-				cashX(forceNeg(this.cost), this.what);
+				button.onclick = () => {
+					cashX(forceNeg(this.cost), this.what);
 
-				if (this.handler) {
-					this.handler();
+					if (this.handler) {
+						this.handler();
+					}
+				};
+
+				if (this.note) {
+					tippy(button, {
+						content: capFirstChar(note),
+					});
 				}
-			};
+			} else {
+				button.classList.add("disabled");
 
-			if (this.note) {
 				tippy(button, {
-					content: capFirstChar(note),
+					content: `You cannot afford this purchase`,
 				});
 			}
 
 			div.append(text, button);
 		} else {
-			div.append(
-				link,
-				` `,
-				App.UI.DOM.makeElement("span", `${this.cost !== 0 ? `Costs ${price}` : capFirstChar(price)}${this.note}`, ['note'])
-			);
+			if (V.cash > this.cost) {
+				div.append(
+					link,
+					` `,
+					App.UI.DOM.makeElement("span", `${this.cost !== 0 ? `Costs ${price}` : capFirstChar(price)}${this.note}`, ['note'])
+				);
+			} else {
+				div.append(
+					App.UI.DOM.disabledLink(this.link, ['You cannot afford this purchase']),
+					` `,
+					App.UI.DOM.makeElement("span", `${this.cost !== 0 ? `Costs ${price}` : capFirstChar(price)}${this.note}`, ['note'])
+				);
+			}
 		}
 
 		return div;
-- 
GitLab