CODEBASE: Refactor Player.applyForJob (#1947)

This commit is contained in:
catloversg
2025-02-03 00:29:18 +07:00
committed by GitHub
parent 72d2528e9a
commit de6b202341
4 changed files with 133 additions and 37 deletions
@@ -53,6 +53,7 @@ import { canAccessBitNodeFeature } from "../../BitNode/BitNodeUtils";
import { AlertEvents } from "../../ui/React/AlertManager";
import { Augmentations } from "../../Augmentation/Augmentations";
import { PlayerEventType, PlayerEvents } from "./PlayerEvents";
import { Result } from "../../types";
export function init(this: PlayerObject): void {
/* Initialize Player's home computer */
@@ -288,22 +289,25 @@ export function applyForJob(
this: PlayerObject,
company: Company,
position: CompanyPosition,
sing = false,
): JobName | null {
if (!company) return null;
): Result<{ jobName: JobName }> {
if (!company) {
return { success: false, message: `Invalid company: ${company}.` };
}
// Start searching the job track from the provided point (which may not be the entry position)
let pos = position;
if (!this.isQualified(company, pos)) {
if (!sing) {
dialogBoxCreate(`Unfortunately, you do not qualify for this position.\n${getJobRequirementText(company, pos)}`);
}
return null;
return {
success: false,
message: `Unfortunately, you do not qualify for this position.\n${getJobRequirementText(company, pos)}`,
};
}
if (!company.hasPosition(pos)) {
console.error(`Company ${company.name} does not have position ${pos.name}. Player.applyToCompany() failed.`);
return null;
return {
success: false,
message: `Company ${company.name} does not have position ${pos.name}.`,
};
}
let nextPos = getNextCompanyPositionHelper(pos);
@@ -312,31 +316,27 @@ export function applyForJob(
nextPos = getNextCompanyPositionHelper(pos);
}
//Check if player already has the assigned job
// Check if player already has the assigned job
if (this.jobs[company.name] === pos.name) {
if (!sing) {
const nextPos = getNextCompanyPositionHelper(pos);
if (nextPos == null) {
dialogBoxCreate(`You are already ${pos.name}! No promotion available`);
} else if (!company.hasPosition(nextPos)) {
dialogBoxCreate(
`You already have the highest ${pos.field} position available at ${company.name}! No promotion available`,
);
} else {
dialogBoxCreate(
`Unfortunately, you do not qualify for a promotion.\n${getJobRequirementText(company, nextPos)}`,
);
}
let errorMessage;
const nextPos = getNextCompanyPositionHelper(pos);
if (nextPos === null) {
errorMessage = `You are already ${pos.name}! No promotion available.`;
} else if (!company.hasPosition(nextPos)) {
errorMessage = `You already have the highest ${pos.field} position available at ${company.name}! No promotion available.`;
} else {
errorMessage = `Unfortunately, you do not qualify for a promotion.\n${getJobRequirementText(company, nextPos)}`;
}
return null;
return { success: false, message: errorMessage };
}
this.jobs[company.name] = pos.name;
if (!sing) {
dialogBoxCreate(`${pos.hiredText} at ${company.name}!`);
}
return pos.name;
return {
success: true,
message: `${pos.hiredText} at ${company.name}!`,
jobName: pos.name,
};
}
/**