From ad8738ecd2289f69da5bf65cfba27a56dd92956d Mon Sep 17 00:00:00 2001
From: DCoded <dcoded@live.com>
Date: Fri, 31 Jul 2020 14:46:28 -0400
Subject: [PATCH] Refactored rules() with new makeRule() function

---
 src/facilities/farmyard/farmyard.js | 171 +++++++++++++---------------
 1 file changed, 79 insertions(+), 92 deletions(-)

diff --git a/src/facilities/farmyard/farmyard.js b/src/facilities/farmyard/farmyard.js
index e0fc533929c..872dc00a9ad 100644
--- a/src/facilities/farmyard/farmyard.js
+++ b/src/facilities/farmyard/farmyard.js
@@ -393,118 +393,105 @@ App.Facilities.Farmyard.farmyard = function() {
 
 		if (App.Entity.facilities.farmyard.employeesIDs().size > 0) {	// TODO: redo this with V.farmyardShowgirls
 			if (V.farmyardShows && (V.canines || V.hooved || V.felines)) {
-				const
-					desc = document.createElement("div"),
-					option = document.createElement("span"),
-					bold = App.UI.DOM.makeElement("span", '', "bold");
-
-				bold.append(`are`);
-				desc.append(`Slaves `, bold, ` putting on shows with animals. `);
-
-				option.append(App.UI.DOM.passageLink("End shows", "Farmyard", () => {
-					V.farmyardShows = 0;
-					V.farmyardBreeding = 0;
-					V.farmyardRestraints = 0;
-				}));
+				const rule = makeRule(
+					['Slaves', 'are putting on shows with animals'],
+					'are',
+					"End shows",
+					[],
+					["farmyardShows", "farmyardBreeding", "farmyardRestraints"]
+				);
 
-				desc.append(option);
-				frag.append(desc);
+				frag.append(rule);
 
 				if (V.seeBestiality) {
 					if (V.farmyardBreeding) {
-						const
-							desc = document.createElement("div"),
-							option = document.createElement("span"),
-							bold = App.UI.DOM.makeElement("span", '', "bold");
-
-						bold.append(`are`);
-						desc.append(`Slaves `, bold, ` being bred with animals. `);
-
-						option.append(App.UI.DOM.passageLink("End breeding", "Farmyard", () => {
-							V.farmyardShows = 1;
-							V.farmyardBreeding = 0;
-							V.farmyardRestraints = 0;
-						}));
+						const rule = makeRule(
+							['Slaves', 'are being bred with animals'],
+							'are',
+							"End breeding",
+							["farmyardShows"],
+							[ "farmyardBreeding", "farmyardRestraints"]
+						);
 
-						desc.append(option);
-						frag.append(desc);
+						frag.append(rule);
 
 						if (V.farmyardRestraints) {
-							const
-								desc = document.createElement("div"),
-								option = document.createElement("span"),
-								bold = App.UI.DOM.makeElement("span", '', "bold");
-
-							bold.append(`All of the slaves`);
-							desc.append(` are being restrained. `);
-
-							desc.prepend(bold);
-
-							option.append(App.UI.DOM.passageLink("Restrain only disobedient slaves", "Farmyard", () => {
-								V.farmyardShows = 1;
-								V.farmyardBreeding = 1;
-								V.farmyardRestraints = 0;
-							}));
+							const rule = makeRule(
+								'are being restrained',
+								'All of the slaves',
+								"Restrain only disobedient slaves",
+								["farmyardShows", "farmyardBreeding"],
+								["farmyardRestraints"]
+							);
 
-							desc.append(option);
-							frag.append(desc);
+							frag.append(rule);
 						} else {
-							const
-								desc = document.createElement("div"),
-								option = document.createElement("span"),
-								bold = App.UI.DOM.makeElement("span", '', "bold");
-
-							bold.append(`Only disobedient slaves`);
-							desc.append(` are being restrained. `);
-
-							desc.prepend(bold);
-
-							option.append(App.UI.DOM.passageLink("Restrain all slaves", "Farmyard", () => {
-								V.farmyardShows = 1;
-								V.farmyardBreeding = 1;
-								V.farmyardRestraints = 1;
-							}));
+							const rule = makeRule(
+								'are being restrained',
+								'Only disobedient slaves',
+								"Restrain all slaves",
+								["farmyardShows", "farmyardBreeding", "farmyardRestraints"],
+								[]
+							);
 
-							desc.append(option);
-							frag.append(desc);
+							frag.append(rule);
 						}
 					} else {
-						const
-							desc = document.createElement("div"),
-							option = document.createElement("span"),
-							bold = App.UI.DOM.makeElement("span", '', "bold");
-
-						bold.append(`are not`);
-						desc.append(`Slaves `, bold, ` being bred with animals. `);
-
-						option.append(App.UI.DOM.passageLink("Begin breeding", "Farmyard", () => {
-							V.farmyardShows = 1;
-							V.farmyardBreeding = 1;
-							V.farmyardRestraints = 0;
-						}));
+						const rule = makeRule(
+							['Slaves', 'being bred with animals'],
+							'are not',
+							"Begin breeding",
+							["farmyardShows", "farmyardBreeding"],
+							["farmyardRestraints"]
+						);
 
-						desc.append(option);
-						frag.append(desc);
+						frag.append(rule);
 					}
 				}
 			} else {
-				const
-					desc = document.createElement("div"),
-					option = document.createElement("span"),
-					bold = App.UI.DOM.makeElement("span", '', "bold");
+				const rule = makeRule(
+					['Slaves', 'putting on shows with animals'],
+					'are not',
+					"Begin shows",
+					["farmyardShows"],
+					["farmyardBreeding", "farmyardRestraints"]
+				);
 
-				bold.append(`are not`);
-				desc.append(`Slaves `, bold, ` putting on shows with animals. `);
+				frag.append(rule);
+			}
+		}
 
-				option.append(App.UI.DOM.passageLink("Begin shows", "Farmyard", () => {
-					V.farmyardShows = 1;
-					V.farmyardBreeding = 0;
-					V.farmyardRestraints = 0;
-				}));
+		/**
+		 * Creates a new rule button
+		 * @param {string|[string]} descText The base description for the rule
+		 * @param {string} boldText The part in bold
+		 * @param {string} linkText The link text
+		 * @param {[string]} enabled Variables to be set to 1
+		 * @param {[string]} disabled Variables to be set to 0
+		 */
+		function makeRule(descText, boldText, linkText, enabled, disabled) {
+			const
+				frag = new DocumentFragment(),
 
-				desc.append(option);
-				frag.append(desc);
+				desc = document.createElement("div"),
+				bold = App.UI.DOM.makeElement("span", boldText, "bold"),
+				link = document.createElement("span");
+
+			if (Array.isArray(descText)) {
+				desc.append(`${descText[0]} `, bold, ` ${descText[1]}. `);
+			} else {
+				desc.append(bold, ` ${descText}. `);
 			}
+
+			link.append(App.UI.DOM.passageLink(linkText, "Farmyard", () => {
+				enabled.forEach(i => V[i] = 1);
+				disabled.forEach(i => V[i] = 0);
+			}));
+
+			desc.append(link);
+			frag.append(desc);
+
+			return frag;
 		}
 
 		return frag;
-- 
GitLab