From 9e0441014690f82fa94c2ce1fb10db20cce0feaa Mon Sep 17 00:00:00 2001
From: ezsh <ezsh.junk@gmail.com>
Date: Thu, 7 May 2020 14:47:50 +0200
Subject: [PATCH] Refactor facilities code to acces emploees via the JobIDArray

---
 src/004-base/facility.js                      | 110 +++++++++---------
 src/facilities/armory/armoryFramework.js      |   2 +-
 src/facilities/brothel/brothelFramework.js    |   5 -
 .../cellblock/cellblockFramework.js           |   5 -
 .../masterSuite/masterSuiteFramework.js       |   4 -
 .../penthouse/penthouseFramework.js           |  49 +-------
 src/facilities/pit/pitFramework.js            |  13 +--
 .../schoolroom/schoolroomFramework.js         |   5 -
 .../servantsQuartersFramework.js              |   5 -
 src/js/assignJS.js                            |   4 +
 10 files changed, 63 insertions(+), 139 deletions(-)

diff --git a/src/004-base/facility.js b/src/004-base/facility.js
index 2bed02162c3..d782fed8254 100644
--- a/src/004-base/facility.js
+++ b/src/004-base/facility.js
@@ -118,7 +118,9 @@ App.Entity.Facilities.Job = class {
 	 * @returns {App.Entity.SlaveState[]}
 	 */
 	employees() {
-		return V.slaves.filter( s => s.assignment === this.desc.assignment);
+		const slaves = V.slaves;
+		const slaveIndices = V.slaveIndices;
+		return this.employeesIds().map(id => slaves[slaveIndices[id]]);
 	}
 
 	/**
@@ -126,8 +128,8 @@ App.Entity.Facilities.Job = class {
 	 * @returns {number[]}
 	 */
 	employeesIndices() {
-		return V.slaves.reduce(
-			(acc, cur, idx) => { if (cur.assignment === this.desc.assignment) { acc.push(idx); } return acc; }, []);
+		const slaveIndices = V.slaveIndices;
+		return this.employeesIds().map(id => slaveIndices[id]);
 	}
 
 	/**
@@ -135,7 +137,7 @@ App.Entity.Facilities.Job = class {
 	 * @returns {number[]}
 	 */
 	employeesIds() {
-		return this.employees().map(s => s.ID);
+		return V.JobIDArray[this.desc.assignment];
 	}
 
 	/**
@@ -168,7 +170,7 @@ App.Entity.Facilities.Job = class {
 		return `${slave.slaveName} must be either more fearful of you or devoted to you.`;
 	}
 
-	/** @private */
+	/** @protected */
 	get _facilityHasFreeSpace() {
 		return this.facility.hasFreeSpace;
 	}
@@ -223,15 +225,11 @@ App.Entity.Facilities.ManagingJob = class extends App.Entity.Facilities.Job {
 
 	/** @returns {App.Entity.SlaveState} */
 	get currentEmployee() {
-		let obj = slaveStateById(V[`${capFirstChar(this.desc.position)}ID`]);
-		// TODO Remove when all facility head migrate to IDs
-		if (!obj) {
-			obj = V[capFirstChar(this.desc.position)];
-		}
-		return obj === undefined || obj === 0 ? null : obj;
+		const employees = this.employees();
+		return employees.length > 0 ? employees[0] : null;
 	}
 
-	/** @private */
+	/** @protected */
 	get _facilityHasFreeSpace() {
 		return true;
 	}
@@ -300,6 +298,10 @@ App.Entity.Facilities.Facility = class {
 		return this._jobs[name || this.desc.defaultJob];
 	}
 
+	get jobs() {
+		return Object.values(this._jobs);
+	}
+
 	get manager() {
 		return this._manager;
 	}
@@ -317,8 +319,7 @@ App.Entity.Facilities.Facility = class {
 	/** Number of already hosted slaves
 	 * @returns {number} */
 	get hostedSlaves() {
-		const job = this.job();
-		return job ? job.employeesIds().length : 0;
+		return this.jobs.reduce((acc, job) => { return acc + job.employeesIds().length; }, 0);
 	}
 
 	get hasFreeSpace() {
@@ -366,13 +367,7 @@ App.Entity.Facilities.Facility = class {
 	 * @returns {boolean}
 	 */
 	isHosted(slave) {
-		for (const j in this._jobs) {
-			if (this._jobs[j].isEmployed(slave)) {
-				return true;
-			}
-		}
-
-		return false;
+		return this.jobs.some(job => job.isEmployed(slave));
 	}
 
 	/**
@@ -458,32 +453,37 @@ App.Entity.Facilities.Facility = class {
 	 * @returns {App.Entity.SlaveState[]}
 	 */
 	employees() {
-		if (Object.keys(this._jobs).length === 1) {
-			return this.job().employees();
-		}
-		/** @type {App.Entity.Facilities.Job[]} */
-		let jobArray = [];
-		for (const jn in this._jobs) {
-			jobArray.push(this._jobs[jn]);
+		const jobArray = this.jobs;
+		if (jobArray.length === 1) {
+			return jobArray[0].employees();
 		}
 		return V.slaves.filter(s => jobArray.some(j => j.isEmployed(s)));
 	}
 
 	/**
-	 * Indices in the slaves array for all slaves that are employed at this job
+	 * Indices in the slaves array for all slaves that are employed at this facility (excluding the manager)
 	 * @returns {number[]}
 	 */
 	employeesIndices() {
-		if (Object.keys(this._jobs).length === 1) {
+		const jobArray = Object.values(this._jobs);
+		if (jobArray.length === 1) {
 			return this.job().employeesIndices();
 		}
-		/** @type {App.Entity.Facilities.Job[]} */
-		let jobArray = [];
-		for (const jn in this._jobs) {
-			jobArray.push(this._jobs[jn]);
+		return jobArray.reduce(
+			(acc, cur, idx) => { acc = acc.concat(cur.employeesIndices()); return acc; }, []);
+	}
+
+	/**
+	 * IDs for all slaves that are employed at this facility (excluding the manager)
+	 * @returns {number[]}
+	 */
+	employeesIds() {
+		const jobArray = Object.values(this._jobs);
+		if (jobArray.length === 1) {
+			return this.job().employeesIds();
 		}
-		return V.slaves.reduce(
-			(acc, cur, idx) => {if (jobArray.some(j => j.isEmployed(cur))) {acc.push(idx);} return acc; }, []);
+		return jobArray.reduce(
+			(acc, cur, idx) => { acc = acc.concat(cur.employeesIds()); return acc; }, []);
 	}
 
 	/**
@@ -531,31 +531,21 @@ App.Entity.Facilities.FacilitySingleJob = class extends App.Entity.Facilities.Jo
 				assignmentTransition(slave, assignment, targetPassage || passage());
 			}, linkText);
 	}
-
-	/** @returns {number[]} */
-	employeesIndices() {
-		const si = V.slaveIndices;
-		const ids = V[this._employeeIDsVariableName]; // updated by assignJob()/removeJob()
-		return ids.map(id => si[id]);
-	}
-
-	employeesIds() {
-		return V[this._employeeIDsVariableName]; // updated by assignJob()/removeJob();
-	}
-
-	/** @returns {App.Entity.SlaveState[]} */
-	employees() {
-		const slaves = V.slaves;
-		return this.employeesIndices().map(ind => slaves[ind]);
-	}
-
-	/** @private */
-	get _employeeIDsVariableName() {
-		return this.facility.genericName + "iIDs";
-	}
 };
 
 App.Entity.Facilities.SingleJobFacility = class extends App.Entity.Facilities.Facility {
+	/**
+	 * @param {App.Data.FacilityDesc} desc defines state variable for this facility
+	 * @param {Object.<string, App.Entity.Facilities.Job>} [jobs] job object that are not default
+	 * @param {App.Entity.Facilities.ManagingJob} [manager]
+	 */
+	constructor(desc, jobs, manager) {
+		super(desc, jobs, manager);
+		if (this.jobs.length !== 1) {
+			throw `SingleJobFacility accepts only a single job, but ${this.jobs.length} were provided`;
+		}
+		this._job = this.job(); // cache the only job
+	}
 	/**
 	 * @override
 	 * @protected
@@ -564,6 +554,10 @@ App.Entity.Facilities.SingleJobFacility = class extends App.Entity.Facilities.Fa
 	_createJob() {
 		return new App.Entity.Facilities.FacilitySingleJob();
 	}
+
+	get facilityJob() {
+		return this._job;
+	}
 };
 
 /** Instances of all facility objects  */
diff --git a/src/facilities/armory/armoryFramework.js b/src/facilities/armory/armoryFramework.js
index 1910909cf56..2f866ee1672 100644
--- a/src/facilities/armory/armoryFramework.js
+++ b/src/facilities/armory/armoryFramework.js
@@ -21,6 +21,6 @@ App.Data.Facilities.armory = {
 	}
 };
 
-App.Entity.facilities.armory = new App.Entity.Facilities.SingleJobFacility(
+App.Entity.facilities.armory = new App.Entity.Facilities.Facility(
 	App.Data.Facilities.armory
 );
diff --git a/src/facilities/brothel/brothelFramework.js b/src/facilities/brothel/brothelFramework.js
index 30eadfeef61..cfca519f4a5 100644
--- a/src/facilities/brothel/brothelFramework.js
+++ b/src/facilities/brothel/brothelFramework.js
@@ -48,11 +48,6 @@ App.Entity.Facilities.BrothelJob = class extends App.Entity.Facilities.FacilityS
 		}
 		return r;
 	}
-
-	/** @private @override */
-	get _employeeIDsVariableName() {
-		return "BrothiIDs";
-	}
 };
 
 App.Entity.Facilities.MadamJob = class extends App.Entity.Facilities.ManagingJob {
diff --git a/src/facilities/cellblock/cellblockFramework.js b/src/facilities/cellblock/cellblockFramework.js
index e38c18e5857..c3f9c8b1a66 100644
--- a/src/facilities/cellblock/cellblockFramework.js
+++ b/src/facilities/cellblock/cellblockFramework.js
@@ -50,11 +50,6 @@ App.Entity.Facilities.CellblockJob = class extends App.Entity.Facilities.Facilit
 
 		return r;
 	}
-
-	/** @private @override */
-	get _employeeIDsVariableName() {
-		return "CellBiIDs";
-	}
 };
 
 App.Entity.facilities.cellblock = new App.Entity.Facilities.SingleJobFacility(
diff --git a/src/facilities/masterSuite/masterSuiteFramework.js b/src/facilities/masterSuite/masterSuiteFramework.js
index 8ee5cf340e3..e94886e242a 100644
--- a/src/facilities/masterSuite/masterSuiteFramework.js
+++ b/src/facilities/masterSuite/masterSuiteFramework.js
@@ -46,10 +46,6 @@ App.Entity.Facilities.MasterSuiteFuckToyJob = class extends App.Entity.Facilitie
 
 		return r;
 	}
-
-	get _employeeIDsVariableName() {
-		return "MastSiIDs";
-	}
 };
 
 App.Entity.Facilities.ConcubineJob = class extends App.Entity.Facilities.ManagingJob {
diff --git a/src/facilities/penthouse/penthouseFramework.js b/src/facilities/penthouse/penthouseFramework.js
index 267bbcfaa33..f0b464b0600 100644
--- a/src/facilities/penthouse/penthouseFramework.js
+++ b/src/facilities/penthouse/penthouseFramework.js
@@ -94,29 +94,9 @@ App.Data.Facilities.penthouse = {
 		requiredDevotion: 51
 	}
 };
-App.Entity.Facilities.PenthouseJob = class extends App.Entity.Facilities.Job {
-	/**
-	 * @override
-	 * @returns {number[]} */
-	employeesIndices() {
-		const employees = V.JobIDArray[this.desc.assignment];
-		if (!employees) { return []; }
-		const si = V.slaveIndices;
-		return employees.map(id => si[id]);
-	}
-
-	/**
-	 * @override
-	 * @returns {App.Entity.SlaveState[]}
-	 */
-	employees() {
-		const slaves = V.slaves;
-		return this.employeesIndices().map(idx => slaves[idx]);
-	}
-};
 
 App.Entity.Facilities.PenthouseJobs = {
-	Classes: class extends App.Entity.Facilities.PenthouseJob {
+	Classes: class extends App.Entity.Facilities.Job {
 		checkRequirements(slave) {
 			let r = super.checkRequirements(slave);
 			if (slave.intelligenceImplant >= 15) {
@@ -132,7 +112,7 @@ App.Entity.Facilities.PenthouseJobs = {
 			return r;
 		}
 	},
-	HouseServant: class extends App.Entity.Facilities.PenthouseJob {
+	HouseServant: class extends App.Entity.Facilities.Job {
 		checkRequirements(slave) {
 			let r = super.checkRequirements(slave);
 
@@ -151,7 +131,7 @@ App.Entity.Facilities.PenthouseJobs = {
 		}
 	},
 
-	SubordinateSlave: class extends App.Entity.Facilities.PenthouseJob {
+	SubordinateSlave: class extends App.Entity.Facilities.Job {
 		checkRequirements(slave) {
 			let r = super.checkRequirements(slave);
 			if (!App.Entity.Facilities.Job._isBrokenEnough(slave, -20, -50, -19, -51)) {
@@ -179,7 +159,7 @@ App.Entity.Facilities.PenthouseJobs = {
 				}, linkText);
 		}
 	},
-	Cow: class extends App.Entity.Facilities.PenthouseJob {
+	Cow: class extends App.Entity.Facilities.Job {
 		checkRequirements(slave) {
 			let r = super.checkRequirements(slave);
 
@@ -227,27 +207,6 @@ App.Entity.Facilities.Penthouse = class extends App.Entity.Facilities.Facility {
 	isHosted(slave) {
 		return assignmentVisible(slave);
 	}
-
-	/**
-	 * all slaves that are at the penthouse
-	 * @returns {App.Entity.SlaveState[]}
-	 */
-	employees() {
-		return V.slaves.filter( s => assignmentVisible(s));
-	}
-
-	/**
-	 * Indices in the slaves array for all slaves that are at the penthouse
-	 * @returns {number[]}
-	 */
-	employeesIndices() {
-		return V.slaves.reduce(
-			(acc, cur, idx) => { if (assignmentVisible(cur)) { acc.push(idx); } return acc; }, []);
-	}
-
-	_createJob() {
-		return new App.Entity.Facilities.PenthouseJob();
-	}
 };
 
 App.Entity.facilities.penthouse = new App.Entity.Facilities.Penthouse();
diff --git a/src/facilities/pit/pitFramework.js b/src/facilities/pit/pitFramework.js
index 06329f3acd0..82c6db8a383 100644
--- a/src/facilities/pit/pitFramework.js
+++ b/src/facilities/pit/pitFramework.js
@@ -34,12 +34,7 @@ App.Entity.Facilities.PitFighterJob = class extends App.Entity.Facilities.Facili
 	}
 
 	isEmployed(slave) {
-		return V.fighterIDs.includes(slave.ID);
-	}
-
-	employeesIndices() {
-		const si = V.slaveIndices;
-		return V.fighterIDs.map(id => si[id]);
+		return this.employeesIds().includes(slave.ID);
 	}
 };
 
@@ -52,11 +47,7 @@ App.Entity.Facilities.Pit = class extends App.Entity.Facilities.SingleJobFacilit
 	}
 
 	get capacity() {
-		return V[this.desc.baseName] > 0 ? Number.MAX_VALUE : 0;
-	}
-
-	get hostedSlaves() {
-		return V.fighterIDs.length;
+		return this.established ? Number.MAX_VALUE : 0;
 	}
 };
 
diff --git a/src/facilities/schoolroom/schoolroomFramework.js b/src/facilities/schoolroom/schoolroomFramework.js
index e52bc95783e..04b487df197 100644
--- a/src/facilities/schoolroom/schoolroomFramework.js
+++ b/src/facilities/schoolroom/schoolroomFramework.js
@@ -59,11 +59,6 @@ App.Entity.Facilities.SchoolroomStudentJob = class extends App.Entity.Facilities
 
 		return r;
 	}
-
-	/** @private @override */
-	get _employeeIDsVariableName() {
-		return "SchlRiIDs";
-	}
 };
 
 App.Entity.facilities.schoolroom = new App.Entity.Facilities.SingleJobFacility(
diff --git a/src/facilities/servantsQuarters/servantsQuartersFramework.js b/src/facilities/servantsQuarters/servantsQuartersFramework.js
index 32b02277ed9..b07dd100fc9 100644
--- a/src/facilities/servantsQuarters/servantsQuartersFramework.js
+++ b/src/facilities/servantsQuarters/servantsQuartersFramework.js
@@ -52,11 +52,6 @@ App.Entity.Facilities.ServantsQuartersServantJob = class extends App.Entity.Faci
 		}
 		return r;
 	}
-
-	/** @private @override */
-	get _employeeIDsVariableName() {
-		return "ServQiIDs";
-	}
 };
 
 App.Entity.Facilities.ServantsQuartersStewardessJob = class extends App.Entity.Facilities.ManagingJob {
diff --git a/src/js/assignJS.js b/src/js/assignJS.js
index 7beb1a286ea..8f988a64aab 100644
--- a/src/js/assignJS.js
+++ b/src/js/assignJS.js
@@ -740,6 +740,10 @@ globalThis.resetJobIDArray = function() {
 		JobIDArray[slave.assignment].push(slave.ID);
 	}
 
+	// special cases
+	JobIDArray[Job.PIT] = V.fighterIDs;
+	JobIDArray[Job.LURCHER].push(V.LurcherID);
+
 	return JobIDArray;
 };
 
-- 
GitLab