Added aliases

This commit is contained in:
Daniel Xie 2017-05-23 10:12:09 -05:00
parent a6e1fb6cfb
commit a3c104ca4e
4 changed files with 53 additions and 1 deletions

44
src/Alias.js Normal file

@ -0,0 +1,44 @@
/* Alias.js */
Aliases = {};
//Print all aliases to terminal
function printAliases() {
for (var name in Aliases) {
if (Aliases.hasOwnProperty(name)) {
post("alias " + name + "=" + Aliases[name]);
}
}
}
//True if successful, false otherwise
function parseAliasDeclaration(dec) {
var re = /([^=]+)="(.+)"/;
var matches = dec.match(re);
if (matches == null || matches.length != 3) {return false;}
addAlias(matches[1], matches[2]);
return true;
}
function addAlias(name, value) {
Aliases[name] = value;
}
function getAlias(name) {
if (Aliases.hasOwnProperty(name)) {
return Aliases[name];
}
return null;
}
//Returns the original string with any aliases substituted in
//Aliases only applied to "whole words", one level deep
function substituteAliases(origCommand) {
var commandArray = origCommand.split(" ");
for (var i = 0; i < commandArray.length; ++i) {
var alias = getAlias(commandArray[i]);
if (alias != null) {
commandArray[i] = alias;
}
}
return commandArray.join(" ");
}

@ -128,7 +128,8 @@ CONSTANTS = {
CrimeHeist: "pull off the ultimate heist",
//Text that is displayed when the 'help' command is ran in Terminal
HelpText: "analyze Get statistics and information about current machine <br>" +
HelpText: 'alias [name="value"] Create aliases for Terminal commands, or list existing aliases<br>' +
"analyze Get statistics and information about current machine <br>" +
"clear Clear all text on the terminal <br>" +
"cls See 'clear' command <br>" +
"connect [ip/hostname] Connects to the machine given by its IP or hostname <br>" +

@ -10,6 +10,7 @@ function BitburnerSaveObject() {
this.FactionsSave = "";
this.SpecialServerIpsSave = "";
this.AugmentationsSave = "";
this.AliasesSave = "";
}
BitburnerSaveObject.prototype.saveGame = function() {
@ -19,6 +20,7 @@ BitburnerSaveObject.prototype.saveGame = function() {
this.FactionsSave = JSON.stringify(Factions);
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
this.AugmentationsSave = JSON.stringify(Augmentations);
this.AliasesSave = JSON.stringify(Aliases);
var saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this))));
window.localStorage.setItem("bitburnerSave", saveString);
@ -41,6 +43,7 @@ loadGame = function(saveObj) {
Factions = JSON.parse(saveObj.FactionsSave, Reviver);
SpecialServerIps = JSON.parse(saveObj.SpecialServerIpsSave, Reviver);
Augmentations = JSON.parse(saveObj.AugmentationsSave, Reviver);
Aliases = JSON.parse(saveObj.AliasesSave, Reviver);
return true;
}

@ -360,6 +360,10 @@ var Terminal = {
}
Terminal.commandHistoryIndex = Terminal.commandHistory.length;
//Process any aliases
command = substituteAliases(command);
console.log("command after alises: " + command);
//Only split the first space
var commandArray = command.split(" ");
if (commandArray.length > 1) {