make setFocus a little more robust

This commit is contained in:
Olivier Gagnon 2021-12-16 12:02:46 -05:00
parent f9499d3259
commit d7f9cb8cec

@ -622,18 +622,25 @@ export function NetscriptSingularity(
return Promise.resolve(); return Promise.resolve();
}); });
}, },
isFocused: function(): boolean { isFocused: function (): boolean {
helper.updateDynamicRam("isFocused", getRamCost("isFocused")); helper.updateDynamicRam("isFocused", getRamCost("isFocused"));
helper.checkSingularityAccess("isFocused", 2); helper.checkSingularityAccess("isFocused", 2);
return player.focus; return player.focus;
}, },
setFocus: function(focus: boolean): boolean { setFocus: function (afocus: any): boolean {
const focus = helper.boolean(afocus);
helper.updateDynamicRam("setFocus", getRamCost("setFocus")); helper.updateDynamicRam("setFocus", getRamCost("setFocus"));
helper.checkSingularityAccess("setFocus", 2); helper.checkSingularityAccess("setFocus", 2);
if (!player.isWorking) { if (!player.isWorking) {
throw helper.makeRuntimeErrorMsg("setFocus", "Not currently working"); throw helper.makeRuntimeErrorMsg("setFocus", "Not currently working");
} }
if (!(player.workType == CONSTANTS.WorkTypeFaction || player.workType == CONSTANTS.WorkTypeCompany || player.workType == CONSTANTS.WorkTypeCompanyPartTime)) { if (
!(
player.workType == CONSTANTS.WorkTypeFaction ||
player.workType == CONSTANTS.WorkTypeCompany ||
player.workType == CONSTANTS.WorkTypeCompanyPartTime
)
) {
throw helper.makeRuntimeErrorMsg("setFocus", "Cannot change focus for current job"); throw helper.makeRuntimeErrorMsg("setFocus", "Cannot change focus for current job");
} }
if (!player.focus && focus) { if (!player.focus && focus) {
@ -854,10 +861,8 @@ export function NetscriptSingularity(
player.startWork(companyName); player.startWork(companyName);
} }
if (!wasWorking || (wasWorking && !wasFocused)) if (!wasWorking || (wasWorking && !wasFocused)) player.stopFocusing();
player.stopFocusing(); else if (wasWorking && wasFocused) player.startFocusing();
else if (wasWorking && wasFocused)
player.startFocusing();
workerScript.log( workerScript.log(
"workForCompany", "workForCompany",
() => `Began working at '${player.companyName}' as a '${companyPositionName}'`, () => `Began working at '${player.companyName}' as a '${companyPositionName}'`,
@ -1097,10 +1102,8 @@ export function NetscriptSingularity(
return false; return false;
} }
player.startFactionHackWork(fac); player.startFactionHackWork(fac);
if (!wasWorking || (wasWorking && !wasFocused)) if (!wasWorking || (wasWorking && !wasFocused)) player.stopFocusing();
player.stopFocusing(); else if (wasWorking && wasFocused) player.startFocusing();
else if (wasWorking && wasFocused)
player.startFocusing();
workerScript.log("workForFaction", () => `Started carrying out hacking contracts for '${fac.name}'`); workerScript.log("workForFaction", () => `Started carrying out hacking contracts for '${fac.name}'`);
return true; return true;
case "field": case "field":
@ -1111,10 +1114,8 @@ export function NetscriptSingularity(
return false; return false;
} }
player.startFactionFieldWork(fac); player.startFactionFieldWork(fac);
if (!wasWorking || (wasWorking && !wasFocused)) if (!wasWorking || (wasWorking && !wasFocused)) player.stopFocusing();
player.stopFocusing(); else if (wasWorking && wasFocused) player.startFocusing();
else if (wasWorking && wasFocused)
player.startFocusing();
workerScript.log("workForFaction", () => `Started carrying out field missions for '${fac.name}'`); workerScript.log("workForFaction", () => `Started carrying out field missions for '${fac.name}'`);
return true; return true;
case "security": case "security":
@ -1125,10 +1126,8 @@ export function NetscriptSingularity(
return false; return false;
} }
player.startFactionSecurityWork(fac); player.startFactionSecurityWork(fac);
if (!wasWorking || (wasWorking && !wasFocused)) if (!wasWorking || (wasWorking && !wasFocused)) player.stopFocusing();
player.stopFocusing(); else if (wasWorking && wasFocused) player.startFocusing();
else if (wasWorking && wasFocused)
player.startFocusing();
workerScript.log("workForFaction", () => `Started carrying out security work for '${fac.name}'`); workerScript.log("workForFaction", () => `Started carrying out security work for '${fac.name}'`);
return true; return true;
default: default: