Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Below is a scheme for client and service to obtain server values. The service polls values and
- writes them to ports where clients read them. It could be modified to suit your needs. It could
- be something to help you fall asleep. :::<name>::: marks the start of a script.
- :::po-methods.js:::
- /** Port size must be the same as in Options->System */
- export const portSize = 50;
- /** Methods in the form []["method", intervalMin, intervalStart, intervalMax, valuePort, noticePort].
- * "method" is the name of an NS.get method taking one argument, "server", and returning (string | number).
- * Note: getHackTime is used by the service, so if not wanted, the service will need to be modified.
- * intervalMin and intervalMax are used as multipliers of hack time. (0, 1).
- * intervalStart is the starting interval between intervalMin and intervalMax. (0, 1).
- * valuePort is where service will write value. noticePort is where client will write notice.
- * [1 .. 20]. Unique across indexes.
- */
- export const methods = [["getServerMoneyAvailable", 1/250, 1/2, 3/2 * 1/250, 1, 2],
- ["getServerSecurityLevel", 3/2 * 1/250, 1/2, 5/2 * 1/250, 3, 4],
- ["getHackTime", 2 * 1/250, 1/2, 3 * 1/250, 5, 6] // only remove this method if you know what you are doing
- ];
- /** getValue()
- * @param {NS} ns NS2 namespace
- * @param {number} indexM index of method
- * @param {number} numClients number of clients (threads)
- * @returns {string | number} value
- */
- function getValue(ns, indexM, numClients) {
- if (Math.random() < portSize / numClients) {
- ns.tryWritePort(methods[indexM][5], 0); // write notice
- }
- return ns.peek(methods[indexM][4]);
- }
- /** getMoneyAvail()
- * @param {NS} ns NS2 namespace
- * @param {number} numClients number of clients (threads)
- * @returns {number} money available
- */
- export function getMoneyAvail(ns, numClients) {
- return getValue(ns, 0, numClients);
- }
- /** getSecLevel()
- * @param {NS} ns NS2 namespace
- * @param {number} numClients number of clients (threads)
- * @returns {number} security level
- */
- export function getSecLevel(ns, numClients) {
- return getValue(ns, 1, numClients);
- }
- /** getTimeHack()
- * @param {NS} ns NS2 namespace
- * @param {number} numClients number of clients (threads)
- * @returns {number} hack time (ms)
- */
- export function getTimeHack(ns, numClients) {
- return getValue(ns, 2, numClients);
- }
- /** @version 0.47.0 */
- :::po-service.js:::
- import { portSize, methods } from "po-methods.js";
- /** getWhenUpdate()
- * @param {number} intervalMin minimum interval (0, 1)
- * @param {number} interval interval (0, 1)
- * @param {number} intervalMax maximum interval (0, 1)
- * @param {number} lastUpdate time of last update (ms)
- * @param {number} timeHack hack time (ms)
- * @returns {number} when to update value (ms)
- */
- function getWhenUpdate(intervalMin, interval, intervalMax, lastUpdate, timeHack) {
- var delta = intervalMax - intervalMin;
- return lastUpdate + (intervalMin + delta * interval) * timeHack;
- }
- /** @param {NS} ns NS2 namespace */
- export async function main(ns) {
- // Takes one argument: the server to get values for
- if (ns.args.length < 1) { ns.exit(); }
- var server = ns.args[0];
- // methodRefs should correspond to methods[][0]
- const methodRefs = [ns.getServerMoneyAvailable, ns.getServerSecurityLevel, ns.getHackTime];
- ns.disableLog("ALL");
- // check methods
- if (methods.length != methodRefs.length) {
- ns.tprint("Method/ref count mismatch. Exiting.");
- ns.exit();
- }
- // swap intervalMin and intervalMax if needed
- for (var i = 0; i < methods.length; i++) {
- if (methods[i][1] > methods[i][3]) {
- var intervalMax = methods[i][1];
- methods[i][1] = methods[i][3];
- methods[i][3] = intervalMax;
- }
- }
- var values = []; // array with methods.length values (string | number)
- var lastUpdate = []; // array with methods.length times (ms)
- var whenUpdate = []; // array with methods.length times (ms)
- var intervals = []; // array with methods.length intervals (0, 1)
- var indexTH = -1; // index of getHackTime method
- // initialize method values
- for (var i = 0; i < methods.length; i++) {
- if (methods[i][0] == 'getHackTime') { indexTH = i; }
- values[i] = methodRefs[i](server);
- lastUpdate[i] = Date.now();
- ns.writePort(methods[i][4], values[i]);
- intervals[i] = methods[i][2];
- whenUpdate[i] = getWhenUpdate(methods[i][1], intervals[i], methods[i][3], lastUpdate[i], values[indexTH]);
- }
- if (indexTH == -1) {
- ns.tprint("getHackTime method not found. Exiting.");
- ns.exit();
- }
- // Loop interval is (shortest intervalMin) / 2
- var intervalL = methods[0][1];
- for (var i = 1; i < methods.length; i++) {
- if (methods[i][1] < intervalL) { intervalL = methods[i][1]; }
- }
- intervalL /= 2;
- var cntLoop = 0, cost = 0;
- for (var i = 0; i < methods.length; i++) {
- cost += ns.getFunctionRamCost(methods[i][0]);
- }
- ns.tprint("Polling " + methods.length + " methods with " + ns.formatRam(cost) + " RAM cost.");
- while (true) {
- await ns.sleep(values[indexTH] * intervalL);
- if (cntLoop % 2 == 1) {
- // read up to (portSize / 4) notices for each method
- for (var i = 0; i < methods.length; i++) {
- var notice, cntNotice = 0;
- do {
- notice = ns.readPort(methods[i][5]);
- if (notice != "NULL PORT DATA") { cntNotice++; }
- } while ((notice != "NULL PORT DATA") && (cntNotice < portSize / 4));
- // adjust method interval
- if (cntNotice == 0) {
- intervals[i] += (1 - intervals[i]) / 4;
- } else {
- for (var j = 0; j < cntNotice; j++) {
- intervals[i] -= intervals[i] / 2;
- }
- }
- whenUpdate[i] = getWhenUpdate(methods[i][1], intervals[i], methods[i][3], lastUpdate[i], values[indexTH]);
- //if (cntNotice > 2) { ns.tprint("debug: cntNotice=" + cntNotice); }
- }
- }
- if (cntLoop < Number.MAX_SAFE_INTEGER) {
- cntLoop++;
- } else {
- cntLoop = 0;
- }
- // update method values as needed
- var bTHChanged = false;
- for (var i = 0; i < methods.length; i++) {
- var curTime = Date.now();
- if (curTime >= whenUpdate[i]) {
- var valuePrev = values[i];
- values[i] = methodRefs[i](server);
- lastUpdate[i] = curTime;
- if (values[i] != valuePrev) {
- // value changed, so write new value then read old value
- ns.writePort(methods[i][4], values[i]);
- ns.readPort(methods[i][4]);
- if (i == indexTH) { bTHChanged = true; }
- }
- }
- }
- if (bTHChanged) {
- // hack time changed, so recalculate whenUpdate for all methods
- for (var i = 0; i < methods.length; i++) {
- whenUpdate[i] = getWhenUpdate(methods[i][1], intervals[i], methods[i][3], lastUpdate[i], values[indexTH]);
- }
- }
- }
- }
- /** @version 0.47.0 */
- :::po-example-client.js:::
- import { getMoneyAvail, getSecLevel, getTimeHack } from "po-methods.js";
- // This is a minimal client demonstrating method calls. Your client should not be a monitor.
- /** @param {NS} ns NS2 namespace */
- export async function main(ns) {
- if (ns.args.length < 1) { ns.exit(); }
- var server = ns.args[0]; // server being polled by service
- ns.disableLog("ALL");
- while (true) {
- var timeHack = getTimeHack(ns, 1);
- ns.tprint(server + ": moneyAvail=$" + Math.round(getMoneyAvail(ns, 1))
- + " secLevel=" + ns.formatNumber(getSecLevel(ns, 1))
- + " timeHack=" + ns.formatNumber(timeHack / 1000) + "s");
- await ns.sleep(timeHack / 25);
- }
- }
- /** @version 0.47.0 */
- :::po-example.js:::
- // This runs the service and monitor client. Your client should have more than one thread.
- /** @param {NS} ns NS2 namespace */
- export async function main(ns) {
- if (ns.args.length < 1) {
- ns.tprint("Usage: " + ns.getScriptName() + " <server>");
- ns.exit();
- }
- var server = ns.args[0]; // server to poll values for
- // run service
- ns.run("po-service.js", 1, server);
- await ns.sleep(50); // wait for service to come up
- // run client
- ns.run("po-example-client.js", 1, server);
- }
- /** @version 0.47.0 */
Advertisement
Add Comment
Please, Sign In to add comment