From 8a73df5a39248d082a85d0295bb271cec6c59d28 Mon Sep 17 00:00:00 2001
From: ezsh <ezsh.junk@gmail.com>
Date: Thu, 30 Apr 2020 03:43:40 +0200
Subject: [PATCH] Teach assignJob() and facilities to handle special slaves
 defined via IDs

---
 src/004-base/facility.js |  6 +++++-
 src/js/assignJS.js       | 25 +++++++++++++++++++------
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/004-base/facility.js b/src/004-base/facility.js
index e7ee3b917e0..164512d72f6 100644
--- a/src/004-base/facility.js
+++ b/src/004-base/facility.js
@@ -258,7 +258,11 @@ App.Entity.Facilities.ManagingJob = class extends App.Entity.Facilities.Job {
 
 	/** @returns {App.Entity.SlaveState} */
 	get currentEmployee() {
-		const obj = State.variables[capFirstChar(this.desc.position)];
+		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;
 	}
 
diff --git a/src/js/assignJS.js b/src/js/assignJS.js
index 3524622ddee..a9b182499f7 100644
--- a/src/js/assignJS.js
+++ b/src/js/assignJS.js
@@ -22,13 +22,26 @@ globalThis.assignJob = function(slave, job) {
 	removeJob(slave, slave.assignment, true);
 	const idx = V.slaveIndices[slave.ID];
 
-	const uniqueJob = function(propName) {
-		// this helper makes sure global references ($HeadGirl, etc) are set correctly
-		if (V[propName] !== 0 && V[propName].ID !== slave.ID) {
-			removeJob(V[propName], job, true);
+	/**
+	 * this helper makes sure global references ($HeadGirl, etc) or global IDs ($AttendantID, etc) are set correctly
+	 * @param {string} propName
+	 */
+	function uniqueJob(propName) {
+		if (App.Data.defaultGameStateVariables.hasOwnProperty(propName)) {
+			// TODO remove this branch when all special slaves migrate to IDs
+			if (V[propName] !== 0 && V[propName].ID !== slave.ID) {
+				removeJob(V[propName], job, true);
+			}
+			V[propName] = slave;
+		} else {
+			const specialIDProp = `${propName}ID`;
+			const prevAssigneeID = V[specialIDProp];
+			if (prevAssigneeID !== slave.ID) {
+				removeJob(slaveStateById(prevAssigneeID), job, true);
+			}
+			V[specialIDProp] = slave.ID;
 		}
-		V[propName] = slave;
-	};
+	}
 
 	/* Tracking for the following cases: */
 	if (oldJob !== job && V.assignmentRecords[slave.ID] !== job && oldJob !== "rest") { // Check that there is a real change happening. Sometimes when you send someone to a classroom or something, this fires twice.
-- 
GitLab