2022-02-04 17:06:44 -05:00
/ * *
* Manages hacknet nodes , purchasing nodes to reach the desired amount .
* Upgrades ( Level , RAM , Cores & Cache ) will be automatically purchased .
* /
2022-02-04 11:49:05 -05:00
export async function main ( ns ) {
2022-02-04 17:06:44 -05:00
/ * *
* How to use this script
* message - optional message to add
* /
2022-02-04 16:17:48 -05:00
function help ( message ) {
2022-02-04 17:06:44 -05:00
ns . tprint ( ` \n \n ${ ! message ? '' : ` ${ message } \n \n ` } Usage: \n run node-manager.js <num> [savings] \n \n \t num - Target number of nodes \n \t savings - Prevent spending bellow this point \n \n ` ) ;
2022-02-04 16:17:48 -05:00
ns . exit ( ) ;
}
2022-02-04 17:06:44 -05:00
/ * *
* Print header with logs
* message - message to append to logs
* /
2022-02-04 16:17:48 -05:00
function log ( message ) {
ns . clearLog ( ) ;
ns . print ( '===================================================' ) ;
ns . print ( ` 🖥️ Node Manager: ${ nodeCount } / ${ LIMIT } Nodes ` ) ;
ns . print ( '===================================================' ) ;
if ( message != null ) MESSAGE _HISTORY . push ( message ) ;
MESSAGE _HISTORY . splice ( 0 , MESSAGE _HISTORY . length - HISTORY _LENGTH ) ;
2022-02-04 18:48:08 -05:00
MESSAGE _HISTORY . forEach ( m => ns . print ( m ) ) ;
2022-02-04 16:17:48 -05:00
}
2022-02-04 17:06:44 -05:00
// Setup
2022-02-04 11:49:05 -05:00
ns . disableLog ( 'ALL' ) ;
2022-02-04 16:17:48 -05:00
if ( ns . args [ 0 ] == 'help' ) help ( ) ;
if ( ns . args [ 0 ] == null ) help ( 'Missing number of nodes' ) ;
if ( isNaN ( ns . args [ 0 ] ) ) help ( 'First argument must be a number' ) ;
const HISTORY _LENGTH = 17 ;
2022-02-04 18:48:08 -05:00
const MESSAGE _HISTORY = Array ( HISTORY _LENGTH ) . fill ( '' ) ;
2022-02-04 16:17:48 -05:00
const LIMIT = ns . args [ 0 ] < ns . hacknet . maxNumNodes ( ) ? ns . args [ 0 ] : ns . hacknet . maxNumNodes ( ) ;
2022-02-04 18:48:08 -05:00
const SAVINGS = ns . args [ 1 ] ? ? 0 ;
2022-02-04 11:49:05 -05:00
let nodeCount = ns . hacknet . numNodes ( ) ;
2022-02-04 16:17:48 -05:00
log ( ) ;
2022-02-04 11:49:05 -05:00
while ( true ) {
2022-02-04 14:29:15 -05:00
const BALANCE = ns . getServerMoneyAvailable ( 'home' ) ;
2022-02-04 11:49:05 -05:00
2022-02-04 17:06:44 -05:00
// Check if we should buy a new node
2022-02-04 16:17:48 -05:00
if ( nodeCount < LIMIT && BALANCE - ns . hacknet . getPurchaseNodeCost ( ) > SAVINGS ) {
2022-02-04 14:29:15 -05:00
nodeCount ++ ;
ns . hacknet . purchaseNode ( ) ;
2022-02-04 16:17:48 -05:00
log ( ` Buying Node ${ nodeCount } ` ) ;
2022-02-04 14:29:15 -05:00
} else {
2022-02-04 17:06:44 -05:00
// Create an ordered list of nodes by their cheapest upgrade
2022-02-04 14:29:15 -05:00
const NODES = Array ( nodeCount ) . fill ( null )
2022-02-04 17:06:44 -05:00
. map ( ( ignore , i ) => ( { // Gather information
2022-02-04 14:29:15 -05:00
index : i ,
cacheCost : ns . hacknet . getCacheUpgradeCost ( i ) ,
coreCost : ns . hacknet . getCoreUpgradeCost ( i ) ,
levelCost : ns . hacknet . getLevelUpgradeCost ( i ) ,
ramCost : ns . hacknet . getRamUpgradeCost ( i ) ,
... ns . hacknet . getNodeStats ( i )
2022-02-04 17:06:44 -05:00
} ) ) . map ( node => { // Figure out cheapest upgrade
2022-02-04 14:29:15 -05:00
if ( node . cacheCost != 0 && node . cacheCost != Infinity && node . cacheCost <= node . coreCost && node . cacheCost <= node . levelCost && node . cacheCost <= node . ramCost ) {
node . bestUpgrade = {
name : 'Cache' ,
cost : node . cacheCost ,
purchase : ( ) => ns . hacknet . upgradeCache ( node . index )
} ;
} else if ( node . coreCost != 0 && node . coreCost != Infinity && node . coreCost <= node . cacheCost && node . coreCost <= node . levelCost && node . coreCost <= node . ramCost ) {
node . bestUpgrade = {
name : 'Core' ,
cost : node . coreCost ,
purchase : ( ) => ns . hacknet . upgradeCore ( node . index )
} ;
} else if ( node . ramCost != 0 && node . ramCost != Infinity && node . ramCost <= node . cacheCost && node . ramCost <= node . levelCost && node . ramCost <= node . coreCost ) {
node . bestUpgrade = {
name : 'RAM' ,
cost : node . ramCost ,
purchase : ( ) => ns . hacknet . upgradeRam ( node . index )
} ;
} else {
node . bestUpgrade = {
name : 'Level' ,
cost : node . levelCost ,
purchase : ( ) => ns . hacknet . upgradeLevel ( node . index )
} ;
}
return node ;
2022-02-04 17:06:44 -05:00
} ) . sort ( ( a , b ) => { // Sort by cheapest upgrade
2022-02-04 14:29:15 -05:00
if ( a . bestUpgrade . cost > b . bestUpgrade . cost ) return 1 ;
if ( a . bestUpgrade . cost < b . bestUpgrade . cost ) return - 1 ;
return 0 ;
} ) ;
2022-02-04 17:06:44 -05:00
// Apply the cheapest upgrade
2022-02-04 14:29:15 -05:00
if ( BALANCE - NODES [ 0 ] . bestUpgrade . cost > SAVINGS ) {
const COST = Math . round ( NODES [ 0 ] . bestUpgrade . cost * 100 ) / 100 ;
2022-02-04 16:17:48 -05:00
log ( ` Upgrading Node ${ NODES [ 0 ] . index } ${ NODES [ 0 ] . bestUpgrade . name } : $ ${ COST } ` ) ;
2022-02-04 14:29:15 -05:00
NODES [ 0 ] . bestUpgrade . purchase ( ) ;
}
}
2022-02-04 11:49:05 -05:00
2022-02-04 17:06:44 -05:00
// Check again in 1s
2022-02-04 14:29:15 -05:00
await ns . sleep ( 1000 ) ;
2022-02-04 11:49:05 -05:00
}
}