Added better job assignment function, and fix docs

This commit is contained in:
pigalot 2022-01-13 18:47:54 +00:00
parent abdc786403
commit 1384b86810
3 changed files with 63 additions and 5 deletions

@ -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);
}

@ -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<boolean> {
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<void> {
checkAccess("assignJob", 8);
const divisionName = helper.string("assignJob", "divisionName", adivisionName);

@ -6086,7 +6086,7 @@ export interface OfficeAPI {
*/
assignJob(divisionName: string, cityName: string, employeeName: string, job: string): Promise<void>;
/**
* 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<boolean>;
}
/**
@ -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;