mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-19 12:45:45 +01:00
update ns2 doc
This commit is contained in:
parent
d83650c3bc
commit
ec76c2ecca
BIN
doc/source/netscript/autocomplete.png
Normal file
BIN
doc/source/netscript/autocomplete.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
@ -1,37 +1,37 @@
|
|||||||
.. _netscriptjs:
|
.. _netscriptjs:
|
||||||
|
|
||||||
NetscriptJS (Netscript 2.0)
|
NS2
|
||||||
===========================
|
===
|
||||||
Netscript 2.0, or Netscript JS, is the improved version of Netscript that
|
The improved version of Netscript that
|
||||||
allows users to write full-fledged Javascript code in their scripts, while
|
allows users to write full-fledged Javascript code in their scripts, while
|
||||||
still being able to access the Netscript functions.
|
still being able to access the Netscript functions.
|
||||||
|
|
||||||
NetscriptJS was developed primarily by `Github user jaguilar <https://github.com/jaguilar>`_
|
ns2 was developed primarily by `Github user jaguilar <https://github.com/jaguilar>`_
|
||||||
|
|
||||||
On top of having almost all of the features and capabilities of JavaScript, NetscriptJS is also
|
On top of having almost all of the features and capabilities of JavaScript, ns2 is also
|
||||||
significantly faster than Netscript 1.0.
|
significantly faster than ns1.
|
||||||
|
|
||||||
This documentation will not go over any of the additional features of NetscriptJS, since
|
This documentation will not go over any of the additional features of ns2, since
|
||||||
there is plenty of documentation on Javascript available on the web.
|
there is plenty of documentation on Javascript available on the web.
|
||||||
|
|
||||||
Browser compatibility
|
Browser compatibility
|
||||||
---------------------
|
---------------------
|
||||||
As of the time of writing this, a few browsers do not support `dynamic import <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>`_ functionality and therefore cannot run NetscriptJS scripts. These browsers will thus only be capable of using Netscript 1.0.
|
As of the time of writing this, a few browsers do not support `dynamic import <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>`_ functionality and therefore cannot run ns2 scripts. These browsers will thus only be capable of using ns1.
|
||||||
|
|
||||||
How to use NetscriptJS
|
How to use ns2
|
||||||
----------------------
|
----------------------
|
||||||
Working with NetscriptJS scripts is the same as Netscript 1.0 scripts. The only difference
|
Working with ns2 scripts is the same as ns1 scripts. The only difference
|
||||||
is that NetscriptJS scripts use the ".ns" or ".js" extension rather than ".script". E.g.::
|
is that ns2 scripts use the ".ns" or ".js" extension rather than ".script". E.g.::
|
||||||
|
|
||||||
$ nano foo.ns
|
$ nano foo.ns
|
||||||
$ run foo.ns -t 100 arg1 arg2 arg3
|
$ run foo.ns -t 100 arg1 arg2 arg3
|
||||||
exec("foo.ns", "purchasedServer1", "100", "randomArg");
|
exec("foo.ns", "purchasedServer1", "100", "randomArg");
|
||||||
|
|
||||||
The caveat when using NetscriptJS to write scripts is that your code must be
|
The caveat when using ns2 to write scripts is that your code must be
|
||||||
asynchronous. Furthermore, instead of using the global scope and executing your code
|
asynchronous. Furthermore, instead of using the global scope and executing your code
|
||||||
sequentially, NetscriptJS uses a :code:`main()` function as an entry point.
|
sequentially, ns2 uses a :code:`main()` function as an entry point.
|
||||||
|
|
||||||
Furthermore, the "Netscript environment" must be passed into a NetscriptJS script through
|
Furthermore, the "Netscript environment" must be passed into a ns2 script through
|
||||||
the main function. This environment includes all of the pre-defined Netscript functions
|
the main function. This environment includes all of the pre-defined Netscript functions
|
||||||
(:code:`hack()`, :code:`exec`, etc.) as well as the arguments you pass to the script.
|
(:code:`hack()`, :code:`exec`, etc.) as well as the arguments you pass to the script.
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
|
|||||||
|
|
||||||
* **Do not write any infinite loops without using a** :code:`sleep` **or one of the timed Netscript functions like** :code:`hack`. Doing so will freeze your game.
|
* **Do not write any infinite loops without using a** :code:`sleep` **or one of the timed Netscript functions like** :code:`hack`. Doing so will freeze your game.
|
||||||
|
|
||||||
* Any global variable declared in a NetscriptJS script is shared between all instances of that
|
* Any global variable declared in a ns2 script is shared between all instances of that
|
||||||
script. For example, assume you write a script *foo.ns* and declared a global variable like so::
|
script. For example, assume you write a script *foo.ns* and declared a global variable like so::
|
||||||
|
|
||||||
//foo.ns
|
//foo.ns
|
||||||
@ -91,69 +91,68 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
|
|||||||
the script will repeatedly print the value 5).
|
the script will repeatedly print the value 5).
|
||||||
|
|
||||||
These global variables can be thought of as `C++ static class members <https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm>`_,
|
These global variables can be thought of as `C++ static class members <https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm>`_,
|
||||||
where a NetscriptJS script is a class and a global variable is a static member within that class.
|
where a ns2 script is a class and a global variable is a static member within that class.
|
||||||
|
|
||||||
Examples
|
Example
|
||||||
--------
|
-------
|
||||||
|
|
||||||
**Script Scheduler (scriptScheduler.ns)**
|
early-hack-template.script
|
||||||
|
|
||||||
This script shows some of the new functionality that is available in NetscriptJS,
|
.. code-block:: javascript
|
||||||
including objects and object constructors, changing an object's prototype, and
|
|
||||||
importing other NetscriptJS scripts::
|
|
||||||
|
|
||||||
import {tprintColored} from "tprintColored.ns"; //Importing from other NetscriptJS scripts works!
|
var target = args[0];
|
||||||
|
var moneyThresh = getServerMaxMoney(target) * 0.75;
|
||||||
function ScriptJob(params) {
|
var securityThresh = getServerMinSecurityLevel(target) + 5;
|
||||||
if (params.fn == null) {
|
if (fileExists("BruteSSH.exe", "home")) {
|
||||||
throw new Error("No Filename (fn) passed into ScriptJob ctor");
|
brutessh(target);
|
||||||
|
}
|
||||||
|
nuke(target);
|
||||||
|
while(true) {
|
||||||
|
if (getServerSecurityLevel(target) > securityThresh) {
|
||||||
|
weaken(target);
|
||||||
|
} else if (getServerMoneyAvailable(target) < moneyThresh) {
|
||||||
|
grow(target);
|
||||||
|
} else {
|
||||||
|
hack(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fn = params.fn;
|
|
||||||
this.threads = params.threads ? params.threads : 1;
|
|
||||||
this.args = params.args ? params.args : [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptJob.prototype.run = function(ns) {
|
early-hack-template.ns
|
||||||
let runArgs = [this.fn, this.threads].concat(this.args);
|
|
||||||
if (!ns.run.apply(this, runArgs)) {
|
|
||||||
throw new Error("Unable to run " + this.fn + " on " +ns.getHostname());
|
|
||||||
}
|
|
||||||
tprintColored("Running " + this.fn + " on " + ns.getHostname(), "blue");
|
|
||||||
}
|
|
||||||
|
|
||||||
ScriptJob.prototype.exec = function(ns, target) {
|
.. code-block:: javascript
|
||||||
ns.scp(this.fn, target);
|
|
||||||
|
|
||||||
let execArgs = [this.fn, target, this.threads].concat(this.args);
|
|
||||||
if (!ns.exec.apply(this, execArgs)) {
|
|
||||||
throw new Error("Unable to execute " + this.fn + " on " + target);
|
|
||||||
}
|
|
||||||
tprintColored("Executing " + this.fn + " on " + target, "blue");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function main(ns) {
|
export async function main(ns) {
|
||||||
tprintColored("Starting scriptScheduler.ns", "red");
|
var target = ns.args[0];
|
||||||
try {
|
var moneyThresh = ns.getServerMaxMoney(target) * 0.75;
|
||||||
let job = new ScriptJob({
|
var securityThresh = ns.getServerMinSecurityLevel(target) + 5;
|
||||||
fn: "test.js",
|
if (ns.fileExists("BruteSSH.exe", "home")) {
|
||||||
threads: 1,
|
ns.brutessh(target);
|
||||||
args: ["foodnstuff"]
|
}
|
||||||
});
|
ns.nuke(target);
|
||||||
job.run(ns);
|
while(true) {
|
||||||
job.exec(ns, "foodnstuff");
|
if (ns.getServerSecurityLevel(target) > securityThresh) {
|
||||||
} catch (e) {
|
await ns.weaken(target);
|
||||||
ns.tprint("Exception thrown in scriptScheduler.ns: " + e);
|
} else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
|
||||||
|
await ns.grow(target);
|
||||||
|
} else {
|
||||||
|
await ns.hack(target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Final Note
|
What's with the weird comment
|
||||||
----------
|
-----------------------------
|
||||||
NetscriptJS opens up a lot of possibilities when scripting. I look forward to seeing
|
|
||||||
the scripts that people come up with. Just remember that the power and capabilities of
|
|
||||||
NetscriptJS come with risks. Please backup your save if you're going to experiment with
|
|
||||||
NetscriptJS and report any serious exploits.
|
|
||||||
|
|
||||||
With great power comes great responsibility
|
You may have noticed that every new ns2 file will contains the following comment.
|
||||||
|
|
||||||
Happy hacking
|
.. code-block:: javascript
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {NS} ns
|
||||||
|
**/
|
||||||
|
|
||||||
|
This command is used to help the text editor autocomplete functions in the Netscript API. You can enabling it by pressing ctrl+space after `ns.`
|
||||||
|
|
||||||
|
.. image:: autocomplete.png
|
||||||
|
|
||||||
|
The comment can be safely removed but it is recommended to keep it as it will help you.
|
@ -22,5 +22,4 @@ into a script using::
|
|||||||
|
|
||||||
args.length
|
args.length
|
||||||
|
|
||||||
**WARNING: Do not try to modify the args array. This will break the game.
|
**WARNING: Do not try to modify the args array. This will break the game.**
|
||||||
I will do my best to prevent players from doing this.**
|
|
||||||
|
Loading…
Reference in New Issue
Block a user