From 54c0d970489dccbfad2ab0fe397d4f640318998e Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Fri, 20 Jul 2018 00:06:59 -0400 Subject: [PATCH] added bladeburner functions related to action levels --- doc/source/netscriptbladeburnerapi.rst | 48 +++++++++ src/NetscriptBladeburner.js | 27 +++++ src/NetscriptFunctions.js | 131 +++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 src/NetscriptBladeburner.js diff --git a/doc/source/netscriptbladeburnerapi.rst b/doc/source/netscriptbladeburnerapi.rst index c420af9f5..426411b82 100644 --- a/doc/source/netscriptbladeburnerapi.rst +++ b/doc/source/netscriptbladeburnerapi.rst @@ -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 ------- diff --git a/src/NetscriptBladeburner.js b/src/NetscriptBladeburner.js new file mode 100644 index 000000000..57a63aefd --- /dev/null +++ b/src/NetscriptBladeburner.js @@ -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}; \ No newline at end of file diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 355d1baac..e86001df3 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -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);