2018-05-11 23:30:55 +02:00
import { Engine } from "./engine.js" ;
2017-08-30 19:44:29 +02:00
import { workerScripts ,
addWorkerScript ,
2018-05-06 22:27:47 +02:00
killWorkerScript } from "./NetscriptWorker.js" ;
import { Player } from "./Player.js" ;
import { getServer } from "./Server.js" ;
import { dialogBoxCreate } from "../utils/DialogBox.js" ;
import { printArray , createElement ,
createAccordionElement , removeElement ,
2018-05-07 19:25:44 +02:00
removeChildrenFromElement , exceptionAlert } from "../utils/HelperFunctions.js" ;
2018-05-06 22:27:47 +02:00
import { logBoxCreate } from "../utils/LogBox.js" ;
2018-05-31 04:46:52 +02:00
import numeral from "numeral/min/numeral.min" ;
2018-05-06 22:27:47 +02:00
import { formatNumber } from "../utils/StringHelperFunctions.js" ;
/ * {
* serverName : {
* header : Server Header Element
* panel : Server Panel List ( ul ) element
* scripts : {
* script id : Ref to Script information
* }
* }
* ...
* /
let ActiveScriptsUI = { } ;
2018-05-07 19:25:44 +02:00
let ActiveScriptsTasks = [ ] ; //Sequentially schedule the creation/deletion of UI elements
2017-08-30 19:44:29 +02:00
2018-05-06 22:27:47 +02:00
function createActiveScriptsServerPanel ( server ) {
2018-05-13 03:21:03 +02:00
let hostname = server . hostname ;
2018-05-07 19:25:44 +02:00
2018-05-13 03:21:03 +02:00
var activeScriptsList = document . getElementById ( "active-scripts-list" ) ;
2018-05-07 19:25:44 +02:00
2018-05-13 03:21:03 +02:00
let res = createAccordionElement ( { hdrText : hostname } ) ;
let li = res [ 0 ] ;
var hdr = res [ 1 ] ;
let panel = res [ 2 ] ;
2018-05-07 19:25:44 +02:00
2018-05-13 03:21:03 +02:00
if ( ActiveScriptsUI [ hostname ] != null ) {
console . log ( "WARNING: Tried to create already-existing Active Scripts Server panel. This is most likely fine. It probably means many scripts just got started up on a new server. Aborting" ) ;
return ;
}
2018-05-07 19:25:44 +02:00
2018-05-13 03:21:03 +02:00
var panelScriptList = createElement ( "ul" ) ;
panel . appendChild ( panelScriptList ) ;
activeScriptsList . appendChild ( li ) ;
ActiveScriptsUI [ hostname ] = {
header : hdr ,
panel : panel ,
panelList : panelScriptList ,
scripts : { } , //Holds references to li elements for each active script
scriptHdrs : { } , //Holds references to header elements for each active script
scriptStats : { } //Holds references to the p elements containing text for each active script
} ;
return li ;
2017-06-07 04:33:50 +02:00
}
//Deletes the info for a particular server (Dropdown header + Panel with all info)
//in the Active Scripts page if it exists
function deleteActiveScriptsServerPanel ( server ) {
2018-05-13 03:21:03 +02:00
let hostname = server . hostname ;
if ( ActiveScriptsUI [ hostname ] == null ) {
console . log ( "WARNING: Tried to delete non-existent Active Scripts Server panel. Aborting" ) ;
return ;
}
2017-08-30 19:44:29 +02:00
2018-05-13 03:21:03 +02:00
//Make sure it's empty
if ( Object . keys ( ActiveScriptsUI [ hostname ] . scripts ) . length > 0 ) {
console . log ( "WARNING: Tried to delete Active Scripts Server panel that still has scripts. Aborting" ) ;
return ;
}
2018-05-06 22:27:47 +02:00
2018-05-13 03:21:03 +02:00
removeElement ( ActiveScriptsUI [ hostname ] . panel ) ;
removeElement ( ActiveScriptsUI [ hostname ] . header ) ;
delete ActiveScriptsUI [ hostname ] ;
2017-06-07 04:33:50 +02:00
}
function addActiveScriptsItem ( workerscript ) {
var server = getServer ( workerscript . serverIp ) ;
if ( server == null ) {
2018-05-06 22:27:47 +02:00
console . log ( "ERROR: Invalid server IP for workerscript in addActiveScriptsItem()" ) ;
2017-06-07 04:33:50 +02:00
return ;
}
2018-05-06 22:27:47 +02:00
let hostname = server . hostname ;
2017-08-30 19:44:29 +02:00
2018-05-07 19:25:44 +02:00
ActiveScriptsTasks . push ( function ( workerscript , hostname ) {
2018-05-13 03:21:03 +02:00
if ( ActiveScriptsUI [ hostname ] == null ) {
createActiveScriptsServerPanel ( server ) ;
}
2018-05-07 19:25:44 +02:00
//Create the unique identifier (key) for this script
var itemNameArray = [ "active" , "scripts" , hostname , workerscript . name ] ;
for ( var i = 0 ; i < workerscript . args . length ; ++ i ) {
itemNameArray . push ( String ( workerscript . args [ i ] ) ) ;
2018-05-06 22:27:47 +02:00
}
2018-05-07 19:25:44 +02:00
var itemName = itemNameArray . join ( "-" ) ;
let res = createAccordionElement ( { hdrText : workerscript . name } ) ;
let li = res [ 0 ] ;
let hdr = res [ 1 ] ;
let panel = res [ 2 ] ;
hdr . classList . remove ( "accordion-header" ) ;
hdr . classList . add ( "active-scripts-script-header" ) ;
panel . classList . remove ( "accordion-panel" ) ;
panel . classList . add ( "active-scripts-script-panel" ) ;
//Handle the constant elements on the panel that don't change after creation
//Threads, args, kill/log button
panel . appendChild ( createElement ( "p" , {
innerHTML : "Threads: " + workerscript . scriptRef . threads + "<br>" +
"Args: " + printArray ( workerscript . args )
} ) ) ;
var panelText = createElement ( "p" , {
innerText : "Loading..." , fontSize : "14px" ,
} ) ;
panel . appendChild ( panelText ) ;
panel . appendChild ( createElement ( "br" ) ) ;
panel . appendChild ( createElement ( "span" , {
innerText : "Log" , class : "active-scripts-button" , margin : "4px" , padding : "4px" ,
clickListener : ( ) => {
logBoxCreate ( workerscript . scriptRef ) ;
return false ;
}
} ) ) ;
panel . appendChild ( createElement ( "span" , {
innerText : "Kill Script" , class : "active-scripts-button" , margin : "4px" , padding : "4px" ,
clickListener : ( ) => {
killWorkerScript ( workerscript . scriptRef , workerscript . scriptRef . scriptRef . server ) ;
dialogBoxCreate ( "Killing script, may take a few minutes to complete..." ) ;
return false ;
}
} ) ) ;
//Append element to list
ActiveScriptsUI [ hostname ] [ "panelList" ] . appendChild ( li ) ;
ActiveScriptsUI [ hostname ] . scripts [ itemName ] = li ;
ActiveScriptsUI [ hostname ] . scriptHdrs [ itemName ] = hdr ;
ActiveScriptsUI [ hostname ] . scriptStats [ itemName ] = panelText ;
} . bind ( null , workerscript , hostname ) ) ;
2017-06-07 04:33:50 +02:00
}
function deleteActiveScriptsItem ( workerscript ) {
2018-05-07 19:25:44 +02:00
ActiveScriptsTasks . push ( function ( workerscript ) {
var server = getServer ( workerscript . serverIp ) ;
if ( server == null ) {
2018-05-11 23:30:55 +02:00
throw new Error ( "ERROR: Invalid server IP for workerscript. This most likely occurred because " +
2018-05-17 19:10:12 +02:00
"you tried to delete a large number of scripts and also deleted servers at the " +
2018-05-11 23:30:55 +02:00
"same time. It's not a big deal, just save and refresh the game." ) ;
2018-05-07 19:25:44 +02:00
return ;
}
let hostname = server . hostname ;
if ( ActiveScriptsUI [ hostname ] == null ) {
console . log ( "ERROR: Trying to delete Active Script UI Element with a hostname that cant be found in ActiveScriptsUI: " + hostname ) ;
return ;
}
2018-05-06 22:27:47 +02:00
2018-05-07 19:25:44 +02:00
var itemNameArray = [ "active" , "scripts" , server . hostname , workerscript . name ] ;
for ( var i = 0 ; i < workerscript . args . length ; ++ i ) {
itemNameArray . push ( String ( workerscript . args [ i ] ) ) ;
}
var itemName = itemNameArray . join ( "-" ) ;
2018-05-06 22:27:47 +02:00
2018-05-07 19:25:44 +02:00
let li = ActiveScriptsUI [ hostname ] . scripts [ itemName ] ;
if ( li == null ) {
console . log ( "ERROR: Cannot find Active Script UI element for workerscript: " ) ;
console . log ( workerscript ) ;
return ;
}
removeElement ( li ) ;
delete ActiveScriptsUI [ hostname ] . scripts [ itemName ] ;
delete ActiveScriptsUI [ hostname ] . scriptHdrs [ itemName ] ;
delete ActiveScriptsUI [ hostname ] . scriptStats [ itemName ] ;
if ( Object . keys ( ActiveScriptsUI [ hostname ] . scripts ) . length === 0 ) {
deleteActiveScriptsServerPanel ( server ) ;
}
} . bind ( null , workerscript ) ) ;
2017-06-07 04:33:50 +02:00
}
//Update the ActiveScriptsItems array
2018-05-12 03:54:59 +02:00
function updateActiveScriptsItems ( maxTasks = 100 ) {
2018-05-07 19:25:44 +02:00
//Run tasks that need to be done sequentially (adding items, creating/deleting server panels)
//We'll limit this to 50 at a time in case someone decides to start a bunch of scripts all at once...
2018-05-12 03:54:59 +02:00
let numTasks = Math . min ( maxTasks , ActiveScriptsTasks . length ) ;
2018-05-07 19:25:44 +02:00
for ( let i = 0 ; i < numTasks ; ++ i ) {
let task = ActiveScriptsTasks . shift ( ) ;
try {
task ( ) ;
} catch ( e ) {
exceptionAlert ( e ) ;
console . log ( task ) ;
}
}
2018-05-11 23:30:55 +02:00
if ( Engine . currentPage !== Engine . Page . ActiveScripts ) { return ; }
2017-06-07 04:33:50 +02:00
var total = 0 ;
for ( var i = 0 ; i < workerScripts . length ; ++ i ) {
2018-05-07 19:25:44 +02:00
try {
total += updateActiveScriptsItemContent ( workerScripts [ i ] ) ;
} catch ( e ) {
exceptionAlert ( e ) ;
}
2017-06-07 04:33:50 +02:00
}
document . getElementById ( "active-scripts-total-prod" ) . innerHTML =
2017-09-27 17:13:42 +02:00
"Total online production of Active Scripts: " + numeral ( total ) . format ( '$0.000a' ) + " / sec<br>" +
"Total online production since last Aug installation: " +
numeral ( Player . scriptProdSinceLastAug ) . format ( '$0.000a' ) + " (" +
numeral ( Player . scriptProdSinceLastAug / ( Player . playtimeSinceLastAug / 1000 ) ) . format ( '$0.000a' ) + " / sec)" ;
2017-09-12 01:14:51 +02:00
return total ;
2017-06-07 04:33:50 +02:00
}
//Updates the content of the given item in the Active Scripts list
function updateActiveScriptsItemContent ( workerscript ) {
var server = getServer ( workerscript . serverIp ) ;
if ( server == null ) {
console . log ( "ERROR: Invalid server IP for workerscript." ) ;
return ;
}
2018-05-06 22:27:47 +02:00
let hostname = server . hostname ;
if ( ActiveScriptsUI [ hostname ] == null ) {
2018-05-07 19:25:44 +02:00
return ; //Hasn't been created yet. We'll skip it
2018-05-06 22:27:47 +02:00
}
2017-06-17 04:53:57 +02:00
var itemNameArray = [ "active" , "scripts" , server . hostname , workerscript . name ] ;
for ( var i = 0 ; i < workerscript . args . length ; ++ i ) {
2018-01-09 21:48:06 +01:00
itemNameArray . push ( String ( workerscript . args [ i ] ) ) ;
2017-06-17 04:53:57 +02:00
}
var itemName = itemNameArray . join ( "-" ) ;
2018-05-07 19:25:44 +02:00
if ( ActiveScriptsUI [ hostname ] . scriptStats [ itemName ] == null ) {
return ; //Hasn't been fully added yet. We'll skip it
}
2018-05-06 22:27:47 +02:00
var item = ActiveScriptsUI [ hostname ] . scriptStats [ itemName ] ;
2017-08-30 19:44:29 +02:00
2018-05-06 22:27:47 +02:00
//Update the text if necessary. This fn returns the online $/s production
return updateActiveScriptsText ( workerscript , item , itemName ) ;
2017-06-07 04:33:50 +02:00
}
2018-05-06 22:27:47 +02:00
function updateActiveScriptsText ( workerscript , item , itemName ) {
var server = getServer ( workerscript . serverIp ) ;
if ( server == null ) {
console . log ( "ERROR: Invalid server IP for workerscript." ) ;
return ;
}
let hostname = server . hostname ;
2018-05-07 19:25:44 +02:00
if ( ActiveScriptsUI [ hostname ] == null || ActiveScriptsUI [ hostname ] . scriptHdrs [ itemName ] == null ) {
2018-05-06 22:27:47 +02:00
console . log ( "ERROR: Trying to update Active Script UI Element with a hostname that cant be found in ActiveScriptsUI: " + hostname ) ;
return ;
}
2017-08-30 19:44:29 +02:00
2018-05-06 22:27:47 +02:00
var onlineMps = workerscript . scriptRef . onlineMoneyMade / workerscript . scriptRef . onlineRunningTime ;
2017-08-30 19:44:29 +02:00
2018-05-06 22:27:47 +02:00
//Only update if the item is visible
if ( ActiveScriptsUI [ hostname ] . header . classList . contains ( "active" ) === false ) { return onlineMps ; }
if ( ActiveScriptsUI [ hostname ] . scriptHdrs [ itemName ] . classList . contains ( "active" ) === false ) { return onlineMps ; }
2017-08-30 19:44:29 +02:00
2018-05-06 22:27:47 +02:00
removeChildrenFromElement ( item ) ;
2017-09-21 23:27:31 +02:00
//Online
var onlineTotalMoneyMade = "Total online production: $" + formatNumber ( workerscript . scriptRef . onlineMoneyMade , 2 ) ;
var onlineTotalExpEarned = ( Array ( 26 ) . join ( " " ) + formatNumber ( workerscript . scriptRef . onlineExpGained , 2 ) + " hacking exp" ) . replace ( / /g , " " ) ;
var onlineMpsText = "Online production rate: $" + formatNumber ( onlineMps , 2 ) + "/second" ;
var onlineEps = workerscript . scriptRef . onlineExpGained / workerscript . scriptRef . onlineRunningTime ;
var onlineEpsText = ( Array ( 25 ) . join ( " " ) + formatNumber ( onlineEps , 4 ) + " hacking exp/second" ) . replace ( / /g , " " ) ;
//Offline
var offlineTotalMoneyMade = "Total offline production: $" + formatNumber ( workerscript . scriptRef . offlineMoneyMade , 2 ) ;
var offlineTotalExpEarned = ( Array ( 27 ) . join ( " " ) + formatNumber ( workerscript . scriptRef . offlineExpGained , 2 ) + " hacking exp" ) . replace ( / /g , " " ) ;
var offlineMps = workerscript . scriptRef . offlineMoneyMade / workerscript . scriptRef . offlineRunningTime ;
var offlineMpsText = "Offline production rate: $" + formatNumber ( offlineMps , 2 ) + "/second" ;
var offlineEps = workerscript . scriptRef . offlineExpGained / workerscript . scriptRef . offlineRunningTime ;
var offlineEpsText = ( Array ( 26 ) . join ( " " ) + formatNumber ( offlineEps , 4 ) + " hacking exp/second" ) . replace ( / /g , " " ) ;
2018-05-06 22:27:47 +02:00
item . innerHTML = onlineTotalMoneyMade + "<br>" + onlineTotalExpEarned + "<br>" +
onlineMpsText + "<br>" + onlineEpsText + "<br>" + offlineTotalMoneyMade + "<br>" + offlineTotalExpEarned + "<br>" +
offlineMpsText + "<br>" + offlineEpsText + "<br>" ;
2017-09-21 23:27:31 +02:00
return onlineMps ;
}
2018-05-06 22:27:47 +02:00
export { addActiveScriptsItem , deleteActiveScriptsItem , updateActiveScriptsItems } ;