SINGULARITY: Added getCompanyPositions (#459)

Also workForCompany will throw an error if provided a bad company name, instead of returning false
This commit is contained in:
T.J. Eckman 2023-04-01 21:30:46 -04:00 committed by GitHub
parent 8b83791515
commit df334ea6de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 6 deletions

@ -155,6 +155,7 @@ const singularity = {
purchaseTor: SF4Cost(RamCostConstants.SingularityFn1), purchaseTor: SF4Cost(RamCostConstants.SingularityFn1),
purchaseProgram: SF4Cost(RamCostConstants.SingularityFn1), purchaseProgram: SF4Cost(RamCostConstants.SingularityFn1),
getCurrentServer: SF4Cost(RamCostConstants.SingularityFn1), getCurrentServer: SF4Cost(RamCostConstants.SingularityFn1),
getCompanyPositions: SF4Cost(RamCostConstants.SingularityFn1),
connect: SF4Cost(RamCostConstants.SingularityFn1), connect: SF4Cost(RamCostConstants.SingularityFn1),
manualHack: SF4Cost(RamCostConstants.SingularityFn1), manualHack: SF4Cost(RamCostConstants.SingularityFn1),
installBackdoor: SF4Cost(RamCostConstants.SingularityFn1), installBackdoor: SF4Cost(RamCostConstants.SingularityFn1),

@ -674,6 +674,19 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
return Player.getUpgradeHomeRamCost(); return Player.getUpgradeHomeRamCost();
}, },
getCompanyPositions: (ctx) => (_companyName) => {
helpers.checkSingularityAccess(ctx);
const companyName = helpers.string(ctx, "companyName", _companyName);
// Make sure its a valid company
if (companyName == null || companyName === "" || !Companies[companyName]) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid company: '${companyName}'`);
}
return Object.entries(CompanyPositions)
.filter((_position) => Companies[companyName].hasPosition(_position[0]))
.map((_position) => _position[1]["name"]);
},
workForCompany: workForCompany:
(ctx) => (ctx) =>
(_companyName, _focus = true) => { (_companyName, _focus = true) => {
@ -683,22 +696,19 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
// Make sure its a valid company // Make sure its a valid company
if (companyName == null || companyName === "" || !Companies[companyName]) { if (companyName == null || companyName === "" || !Companies[companyName]) {
helpers.log(ctx, () => `Invalid company: '${companyName}'`); throw helpers.makeRuntimeErrorMsg(ctx, `Invalid company: '${companyName}'`);
return false;
} }
// Make sure player is actually employed at the company // Make sure player is actually employed at the company
if (!Object.keys(Player.jobs).includes(companyName)) { if (!Object.keys(Player.jobs).includes(companyName)) {
helpers.log(ctx, () => `You do not have a job at '${companyName}'`); throw helpers.makeRuntimeErrorMsg(ctx, `You do not have a job at: '${companyName}'`);
return false;
} }
// Check to make sure company position data is valid // Check to make sure company position data is valid
const companyPositionName = Player.jobs[companyName]; const companyPositionName = Player.jobs[companyName];
const companyPosition = CompanyPositions[companyPositionName]; const companyPosition = CompanyPositions[companyPositionName];
if (companyPositionName === "" || !companyPosition) { if (companyPositionName === "" || !companyPosition) {
helpers.log(ctx, () => "You do not have a job"); throw helpers.makeRuntimeErrorMsg(ctx, `You do not have a job`);
return false;
} }
const wasFocused = Player.focus; const wasFocused = Player.focus;

@ -1698,6 +1698,35 @@ export interface Singularity {
*/ */
getUpgradeHomeCoresCost(): number; getUpgradeHomeCoresCost(): number;
/**
* Get List of Company Positions.
* @remarks
* RAM cost: 2 GB * 16/4/1
*
*
* This function will return a list of positions at a specific company.
*
* This function will return the position list if the company name is valid.
*
* @example
* ```js
* // NS1:
* var COMPANY_NAME = "Noodle Bar";
*
* var jobList = singularity.getCompanyPositions(COMPANY_NAME);
* ```
* @example
* ```js
* // NS2:
* const COMPANY_NAME = "Noodle Bar";
*
* let jobList = ns.singularity.getCompanyPositions(COMPANY_NAME);
* ```
* @param companyName - Name of company to get the position list for. Must be an exact match.
* @returns The position list if the company name is valid.
*/
getCompanyPositions(companyName: string): CompanyPosName[];
/** /**
* Work for a company. * Work for a company.
* @remarks * @remarks