Merge pull request #363 from hydroflame/bladeburner-api-actions-levels

added bladeburner functions related to action levels
This commit is contained in:
danielyxie 2018-07-20 09:59:21 -05:00 committed by GitHub
commit ffb55206fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 206 additions and 0 deletions

@ -158,6 +158,54 @@ getActionCountRemaining
Note that this is meant to be used for Contracts and Operations.
This function will return 'Infinity' for actions such as Training and Field Analysis.
getActionMaxLevel
-----------------
.. js:function:: getActionMaxLevel(type, name)
:param string type: Type of action. See :ref:`bladeburner_action_types`
:param string name: Name of action. Must be an exact match
Return the maximum level for this action.
getActionCurrentLevel
---------------------
.. js:function:: getActionCurrentLevel(type, name)
:param string type: Type of action. See :ref:`bladeburner_action_types`
:param string name: Name of action. Must be an exact match
Return the current level of this action.
getActionAutolevel
------------------
.. js:function:: getActionAutolevel(type, name)
:param string type: Type of action. See :ref:`bladeburner_action_types`
:param string name: Name of action. Must be an exact match
Return wether of not this action is currently autoleveling.
setActionAutolevel
------------------
.. js:function:: setActionAutolevel(type, name, autoLevel)
:param string type: Type of action. See :ref:`bladeburner_action_types`
:param string name: Name of action. Must be an exact match
:param boolean autoLevel: wether or not to autolevel this action
setActionLevel
--------------
.. js:function:: setActionLevel(type, name, level)
:param string type: Type of action. See :ref:`bladeburner_action_types`
:param string name: Name of action. Must be an exact match
:param level int: the level to set this action to
getRank
-------

@ -0,0 +1,27 @@
import {Player} from "./Player";
import {Bladeburner} from "./Bladeburner";
import {makeRuntimeRejectMsg} from "./NetscriptEvaluator";
function unknownBladeburnerActionErrorMessage(functionName, actionType, actionName) {
return `ERROR: bladeburner.${functionName}() failed due to an invalid action specified. ` +
`Type: ${actionType}, Name: ${actionName}. Note that for contracts and operations, the ` +
`name of the operation is case-sensitive.`;
}
function unknownBladeburnerExceptionMessage(functionName, err) {
return `Bladeburner.${functionName}() failed with exception: ` + err;
}
function checkBladeburnerAccess(workerScript, functionName) {
const accessDenied = `${functionName}() failed because you do not` +
" currently have access to the Bladeburner API. This is either" +
" because you are not currently employed at the Bladeburner division" +
" or because you do not have Source-File 7";
console.log(Player.sourceFiles);
const hasAccess = Player.bladeburner instanceof Bladeburner && (Player.bitNodeN === 7 || Player.sourceFiles.some(a=>{return a.n === 7}));
if(!hasAccess) {
throw makeRuntimeRejectMsg(workerScript, accessDenied);
}
}
export {unknownBladeburnerActionErrorMessage, unknownBladeburnerExceptionMessage, checkBladeburnerAccess};

@ -38,6 +38,9 @@ import {StockMarket, StockSymbols, SymbolToStockMap, initStockSymbols,
import {post} from "./Terminal";
import {TextFile, getTextFile, createTextFile} from "./TextFile";
import {unknownBladeburnerActionErrorMessage,
unknownBladeburnerExceptionMessage,
checkBladeburnerAccess} from "./NetscriptBladeburner.js";
import {WorkerScript, workerScripts,
killWorkerScript, NetscriptPorts} from "./NetscriptWorker";
import {makeRuntimeRejectMsg, netscriptDelay, runScriptFromScript,
@ -3356,6 +3359,134 @@ function NetscriptFunctions(workerScript) {
throw makeRuntimeRejectMsg(workerScript, "getActionCountRemaining() failed because you do not currently have access to the Bladeburner API. This is either because you are not currently employed " +
"at the Bladeburner division or because you do not have Source-File 7");
},
getActionMaxLevel: function(type="", name="") {
if (workerScript.checkingRam) {
return updateStaticRam("getActionMaxLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
}
updateDynamicRam("getActionMaxLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
checkBladeburnerAccess(workerScript, "getActionMaxLevel");
try {
var errorLogText = unknownBladeburnerActionErrorMessage("getActionMaxLevel", type, name);
const actionId = Player.bladeburner.getActionIdFromTypeAndName(type, name);
if (actionId == null) {
workerScript.log(errorLogText);
return -1;
}
const actionObj = Player.bladeburner.getActionObject(actionId);
if (actionObj == null) {
workerScript.log(errorLogText);
return -1;
}
return actionObj.maxLevel;
} catch(err) {
throw makeRuntimeRejectMsg(workerScript, unknownBladeburnerExceptionMessage("getActionMaxLevel", err));
}
},
getActionCurrentLevel: function(type="", name="") {
if (workerScript.checkingRam) {
return updateStaticRam("getActionCurrentLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
}
updateDynamicRam("getActionCurrentLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
checkBladeburnerAccess(workerScript, "getActionCurrentLevel");
try {
var errorLogText = unknownBladeburnerActionErrorMessage("getActionCurrentLevel", type, name);
const actionId = Player.bladeburner.getActionIdFromTypeAndName(type, name);
if (actionId == null) {
workerScript.log(errorLogText);
return -1;
}
const actionObj = Player.bladeburner.getActionObject(actionId);
if (actionObj == null) {
workerScript.log(errorLogText);
return -1;
}
return actionObj.level;
} catch(err) {
throw makeRuntimeRejectMsg(workerScript, unknownBladeburnerExceptionMessage("getActionCurrentLevel", err));
}
},
getActionAutolevel: function(type="", name="") {
if (workerScript.checkingRam) {
return updateStaticRam("getActionAutolevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
}
updateDynamicRam("getActionAutolevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
checkBladeburnerAccess(workerScript, "getActionAutolevel");
try {
var errorLogText = unknownBladeburnerActionErrorMessage("getActionAutolevel", type, name);
const actionId = Player.bladeburner.getActionIdFromTypeAndName(type, name);
if (actionId == null) {
workerScript.log(errorLogText);
return false;
}
const actionObj = Player.bladeburner.getActionObject(actionId);
if (actionObj == null) {
workerScript.log(errorLogText);
return false;
}
return actionObj.autoLevel;
} catch(err) {
throw makeRuntimeRejectMsg(workerScript, unknownBladeburnerExceptionMessage("getActionAutolevel", err));
}
},
setActionAutolevel: function(type="", name="", autoLevel=true) {
if (workerScript.checkingRam) {
return updateStaticRam("setActionAutolevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
}
updateDynamicRam("setActionAutolevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
checkBladeburnerAccess(workerScript, "setActionAutolevel");
try {
var errorLogText = unknownBladeburnerActionErrorMessage("setActionAutolevel", type, name);
const actionId = Player.bladeburner.getActionIdFromTypeAndName(type, name);
if (actionId == null) {
workerScript.log(errorLogText);
return;
}
const actionObj = Player.bladeburner.getActionObject(actionId);
if (actionObj == null) {
workerScript.log(errorLogText);
return;
}
actionObj.autoLevel = autoLevel;
} catch(err) {
throw makeRuntimeRejectMsg(workerScript, unknownBladeburnerExceptionMessage("setActionAutolevel", err));
}
},
setActionLevel: function(type="", name="", level=1) {
if (workerScript.checkingRam) {
return updateStaticRam("setActionLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
}
updateDynamicRam("setActionLevel", CONSTANTS.ScriptBladeburnerApiBaseRamCost);
checkBladeburnerAccess(workerScript, "setActionLevel");
try {
var errorLogText = unknownBladeburnerActionErrorMessage("setActionLevel", type, name);
const actionId = Player.bladeburner.getActionIdFromTypeAndName(type, name);
if (actionId == null) {
workerScript.log(errorLogText);
return;
}
const actionObj = Player.bladeburner.getActionObject(actionId);
if (actionObj == null) {
workerScript.log(errorLogText);
return;
}
if(level > actionObj.maxLevel) {
workerScript.log(`ERROR: bladeburner.${setActionLevel}() failed because level exceeds max level for given action.`);
return;
}
if(level < 1) {
workerScript.log(`ERROR: bladeburner.${setActionLevel}() failed because level is below 1.`);
return;
}
actionObj.level = level;
} catch(err) {
throw makeRuntimeRejectMsg(workerScript, unknownBladeburnerExceptionMessage("setActionLevel", err));
}
},
getRank : function() {
if (workerScript.checkingRam) {
return updateStaticRam("getRank", CONSTANTS.ScriptBladeburnerApiBaseRamCost);