diff --git a/src/Corporation/OfficeSpace.ts b/src/Corporation/OfficeSpace.ts index 87a835864..1476a1a28 100644 --- a/src/Corporation/OfficeSpace.ts +++ b/src/Corporation/OfficeSpace.ts @@ -173,6 +173,40 @@ export class OfficeSpace { return false; } + setEmployeeToJob(job: string, amount: number): boolean { + let unassignedCount = 0; + let jobCount = 0; + for (let i = 0; i < this.employees.length; ++i) { + if (this.employees[i].pos === EmployeePositions.Unassigned) { + unassignedCount++; + } else if (this.employees[i].pos === job) { + jobCount++; + } + } + + if ((jobCount + unassignedCount) < amount) return false; + + for (let i = 0; i < this.employees.length; ++i) { + if (this.employees[i].pos === EmployeePositions.Unassigned) { + if (jobCount <= amount) { + this.employees[i].pos = job; + jobCount++; + unassignedCount--; + } + if (jobCount === amount) break; + } else if (this.employees[i].pos === job) { + if (jobCount >= amount) { + this.employees[i].pos = EmployeePositions.Unassigned; + jobCount--; + unassignedCount++; + } + if (jobCount === amount) break; + } + } + if (jobCount !== amount) return false; + return true; + } + toJSON(): any { return Generic_toJSON("OfficeSpace", this); } diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 9bf06d1fd..9d23dbef0 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -508,6 +508,21 @@ export function NetscriptCorporation( const researchName = helper.string("hasResearched", "researchName", aresearchName); return hasResearched(getDivision(divisionName), researchName); }, + setAutoJobAssignment: function (adivisionName: any, acityName: any, ajob: any, aamount: any): Promise { + checkAccess("setAutoJobAssignment", 8); + const divisionName = helper.string("setAutoJobAssignment", "divisionName", adivisionName); + const cityName = helper.string("setAutoJobAssignment", "cityName", acityName); + const amount = helper.number("setAutoJobAssignment", "amount", aamount); + const job = helper.string("setAutoJobAssignment", "job", ajob); + const office = getOffice(divisionName, cityName); + if (!Object.values(EmployeePositions).includes(job)) throw new Error(`'${job}' is not a valid job.`); + return netscriptDelay(1000, workerScript).then(function () { + if (workerScript.env.stopFlag) { + return Promise.reject(workerScript); + } + return Promise.resolve(office.setEmployeeToJob(job, amount)); + }); + }, assignJob: function (adivisionName: any, acityName: any, aemployeeName: any, ajob: any): Promise { checkAccess("assignJob", 8); const divisionName = helper.string("assignJob", "divisionName", adivisionName); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index f0750bae8..97e39a074 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6086,7 +6086,7 @@ export interface OfficeAPI { */ assignJob(divisionName: string, cityName: string, employeeName: string, job: string): Promise; /** - * Assign an employee to a job. + * Hire an employee. * @param divisionName - Name of the division * @param cityName - Name of the city * @returns The newly hired employee, if any @@ -6100,7 +6100,7 @@ export interface OfficeAPI { */ upgradeOfficeSize(divisionName: string, cityName: string, size: number): void; /** - * Assign an employee to a job. + * Throw a party for your employees * @param divisionName - Name of the division * @param cityName - Name of the city * @param costPerEmployee - Amount to spend per employee. @@ -6120,7 +6120,7 @@ export interface OfficeAPI { */ hireAdVert(divisionName: string): void; /** - * Hire AdVert. + * purchace a research * @param divisionName - Name of the division * @param researchName - Name of the research */ @@ -6166,6 +6166,15 @@ export interface OfficeAPI { * @returns true is unlocked, false if not */ hasResearched(divisionName: string, researchName: string): boolean; + /** + * Set the auto job assignment for a job + * @param divisionName - Name of the division + * @param cityName - Name of the city + * @param job - Name of the job + * @param amount - Number of employees to assign to that job + * @returns A promise that is fulfilled when the assignment is complete. + */ + setAutoJobAssignment(divisionName: string, cityName: string, job: string, amount: number): Promise; } /** @@ -6411,7 +6420,7 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { acceptInvestmentOffer(): boolean; /** * Go public - * @param numShares number of shares you would like to issue for your IPO + * @param numShares - number of shares you would like to issue for your IPO * @returns true if you successfully go public, false if not */ goPublic(numShares: number): boolean; @@ -6439,7 +6448,7 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { */ expandCity(divisionName: string, cityName: string): void; /** - * Unlock an upgrade. + * Unlock an upgrade.npm run doc * @param upgradeName - Name of the upgrade */ unlockUpgrade(upgradeName: string): void;