Class: CMD

CMD

Class representing the main game logic.

new CMD(respond, save, load, update, commandProvider, errorHandler, debug)

Instantiate the CMD object
Parameters:
Name Type Default Description
respond CMD~respondCallback Function for responding.
save CMD~saveCallback Function for saving.
load CMD~loadCallback Function for loading.
update CMD~updateCallback Function for updating.
commandProvider CMD~commandProviderCallback Function to provide custom commands. Cannot be ES6 arrow function.
errorHandler CMD~errorHandlerCallback Function for error handling.
debug boolean false Debug mode.
Source:
Example

Instantiate a CMD object.

import { CMD } from 'cmdpp-core';
import fs from 'fs';
var cmdContainer = {
  data: 0,
  money: 0
};
var cmd = new CMD({
  (...txt) => console.log(...txt),
  (cmdData) => fs.writeFileSync('save.json', JSON.stringify(cmdData, null, 2)),
  () => return JSON.parse(fs.readFileSync('save.json')),
  (cmdObj) => {
    cmdContainer.data = cmdObj.data;
    cmdContainer.money = cmdObj.money;
  },
  function() {
    return {
      stringDesc: {
        func: () => this.respond("First test run!"),
        desc: "Desc can be a string"
      },
      functionDesc: {
        func: () => this.respond("Second test run!"),
        desc: () => "Desc can also be a function that returns a string or an array of strings."
      },
      buyableCommand: {
        func: () => this.respond("buyable command!"),
        desc: 'This command must be bought with the "buyCommand" command.',
        price: 10
      }
    };
  },
  (err) => console.error(err),
  true
});

Methods


gameLoop()

Start the game loop.
Source:

respond(txt)

Send response to respond function from constructor
Parameters:
Name Type Argument Description
txt * <repeatable>
Strings to be sent to respond function.
Source:

checkStorage(increment)

Check if storage is full.
Parameters:
Name Type Argument Description
increment number <nullable>
Increment to check against. If undefined, equal to CMD#increment.
Source:
Returns:
If storage has enough space.
Type
boolean

command(str)

Run command
Parameters:
Name Type Description
str string Command to be ran.
Source:

update()

Run update function from constructor to update game values
Source:

save()

Save game progress
Source:

load()

Load game progress
Source:

addData(amt)

Add data
Parameters:
Name Type Description
amt number Amount to add.
Source:
Returns:
if data was able to be added.
Type
boolean

removeData(amt)

Remove data
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if data was able to be removed.
Type
boolean

addMoney(amt)

Add money
Parameters:
Name Type Description
amt number Amount to add.
Source:

removeMoney(amt)

Remove money
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if money was able to be removed.
Type
boolean

formatBytes()

Format bytes into a human-readable format
Source:
Returns:
CMD#data in human-readable format
Type
string

formatter(size)

Format number into a human-readable format
Parameters:
Name Type Description
size number Number to be formatted.
Source:
Returns:
size in human-readable format
Type
string

reset()

Reset game progress
Source:

gameLoop()

Start the game loop.
Source:

respond(txt)

Send response to respond function from constructor
Parameters:
Name Type Argument Description
txt * <repeatable>
Strings to be sent to respond function.
Source:

checkStorage(increment)

Check if storage is full.
Parameters:
Name Type Argument Description
increment number <nullable>
Increment to check against. If undefined, equal to CMD#increment.
Source:
Returns:
If storage has enough space.
Type
boolean

command(str)

Run command
Parameters:
Name Type Description
str string Command to be ran.
Source:

update()

Run update function from constructor to update game values
Source:

save()

Save game progress
Source:

load()

Load game progress
Source:

addData(amt)

Add data
Parameters:
Name Type Description
amt number Amount to add.
Source:
Returns:
if data was able to be added.
Type
boolean

removeData(amt)

Remove data
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if data was able to be removed.
Type
boolean

addMoney(amt)

Add money
Parameters:
Name Type Description
amt number Amount to add.
Source:

removeMoney(amt)

Remove money
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if money was able to be removed.
Type
boolean

formatBytes()

Format bytes into a human-readable format
Source:
Returns:
CMD#data in human-readable format
Type
string

formatter(size)

Format number into a human-readable format
Parameters:
Name Type Description
size number Number to be formatted.
Source:
Returns:
size in human-readable format
Type
string

reset()

Reset game progress
Source:

Type Definitions


respondCallback(txt)

Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description
txt * <repeatable>
Responses
Source:

saveCallback(cmdData)

Function to handle saving progress.
Parameters:
Name Type Description
cmdData Object Game progress to be saved.
Properties
Name Type Description
data number Data collected.
money number Money collected.
increment number Increment value for mineData.
autoIncrement number Increment value for autoMine.
storage string Current storage value.
unlocked Array.<string> Commands bought with buyCommand.
Source:
Returns:
An error if encountered.
Type
Error

loadCallback()

Function to handle saving progress.
Source:
Returns:
Game progress loaded from save.
Type
Object

updateCallback(cmdObj)

Function to handle updating game values.
Parameters:
Name Type Description
cmdObj CMD CMD object.
Source:

Command

An object representing a command.
Type:
  • Object
Properties:
Name Type Argument Default Description
func function Function called when command is run.
desc string | function Description for command.
usage string | function <nullable>
How to use the command.
price number <nullable>
0 Price to pay in bytes for command.
Source:

commandProviderCallback()

Function to provide custom commands.
Source:
Returns:
Object of custom commands.
Type
CMD~Command

errorHandlerCallback(err)

Function to handle thrown errors.
Parameters:
Name Type Description
err Error Error thrown.
Source:

respondCallback(txt)

Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description
txt * <repeatable>
Responses
Source:

saveCallback(cmdData)

Function to handle saving progress.
Parameters:
Name Type Description
cmdData Object Game progress to be saved.
Properties
Name Type Description
data number Data collected.
money number Money collected.
increment number Increment value for mineData.
autoIncrement number Increment value for autoMine.
storage string Current storage value.
unlocked Array.<string> Commands bought with buyCommand.
Source:
Returns:
An error if encountered.
Type
Error

loadCallback()

Function to handle saving progress.
Source:
Returns:
Game progress loaded from save.
Type
Object

updateCallback(cmdObj)

Function to handle updating game values.
Parameters:
Name Type Description
cmdObj CMD CMD object.
Source:

Command

An object representing a command.
Type:
  • Object
Properties:
Name Type Argument Default Description
func function Function called when command is run.
desc string | function Description for command.
usage string | function <nullable>
How to use the command.
price number <nullable>
0 Price to pay in bytes for command.
Source:

commandProviderCallback()

Function to provide custom commands.
Source:
Returns:
Object of custom commands.
Type
CMD~Command

errorHandlerCallback(err)

Function to handle thrown errors.
Parameters:
Name Type Description
err Error Error thrown.
Source:

Class: CMD

CMD

Class representing the main game logic.

new CMD(respond, save, load, update, commandProvider, errorHandler, debug)

Instantiate the CMD object
Parameters:
Name Type Default Description
respond CMD~respondCallback Function for responding.
save CMD~saveCallback Function for saving.
load CMD~loadCallback Function for loading.
update CMD~updateCallback Function for updating.
commandProvider CMD~commandProviderCallback Function to provide custom commands. Cannot be ES6 arrow function.
errorHandler CMD~errorHandlerCallback Function for error handling.
debug boolean false Debug mode.
Source:
Example

Instantiate a CMD object.

import { CMD } from 'cmdpp-core';
import fs from 'fs';
var cmdContainer = {
  data: 0,
  money: 0
};
var cmd = new CMD({
  (...txt) => console.log(...txt),
  (cmdData) => fs.writeFileSync('save.json', JSON.stringify(cmdData, null, 2)),
  () => return JSON.parse(fs.readFileSync('save.json')),
  (cmdObj) => {
    cmdContainer.data = cmdObj.data;
    cmdContainer.money = cmdObj.money;
  },
  function() {
    return {
      stringDesc: {
        func: () => this.respond("First test run!"),
        desc: "Desc can be a string"
      },
      functionDesc: {
        func: () => this.respond("Second test run!"),
        desc: () => "Desc can also be a function that returns a string or an array of strings."
      },
      buyableCommand: {
        func: () => this.respond("buyable command!"),
        desc: 'This command must be bought with the "buyCommand" command.',
        price: 10
      }
    };
  },
  (err) => console.error(err),
  true
});

Methods


gameLoop()

Start the game loop.
Source:

respond(txt)

Send response to respond function from constructor
Parameters:
Name Type Argument Description
txt * <repeatable>
Strings to be sent to respond function.
Source:

checkStorage(increment)

Check if storage is full.
Parameters:
Name Type Argument Description
increment number <nullable>
Increment to check against. If undefined, equal to CMD#increment.
Source:
Returns:
If storage has enough space.
Type
boolean

command(str)

Run command
Parameters:
Name Type Description
str string Command to be ran.
Source:

update()

Run update function from constructor to update game values
Source:

save()

Save game progress
Source:

load()

Load game progress
Source:

addData(amt)

Add data
Parameters:
Name Type Description
amt number Amount to add.
Source:
Returns:
if data was able to be added.
Type
boolean

removeData(amt)

Remove data
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if data was able to be removed.
Type
boolean

addMoney(amt)

Add money
Parameters:
Name Type Description
amt number Amount to add.
Source:

removeMoney(amt)

Remove money
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if money was able to be removed.
Type
boolean

formatBytes()

Format bytes into a human-readable format
Source:
Returns:
CMD#data in human-readable format
Type
string

formatter(size)

Format number into a human-readable format
Parameters:
Name Type Description
size number Number to be formatted.
Source:
Returns:
size in human-readable format
Type
string

reset()

Reset game progress
Source:

gameLoop()

Start the game loop.
Source:

respond(txt)

Send response to respond function from constructor
Parameters:
Name Type Argument Description
txt * <repeatable>
Strings to be sent to respond function.
Source:

checkStorage(increment)

Check if storage is full.
Parameters:
Name Type Argument Description
increment number <nullable>
Increment to check against. If undefined, equal to CMD#increment.
Source:
Returns:
If storage has enough space.
Type
boolean

command(str)

Run command
Parameters:
Name Type Description
str string Command to be ran.
Source:

update()

Run update function from constructor to update game values
Source:

save()

Save game progress
Source:

load()

Load game progress
Source:

addData(amt)

Add data
Parameters:
Name Type Description
amt number Amount to add.
Source:
Returns:
if data was able to be added.
Type
boolean

removeData(amt)

Remove data
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if data was able to be removed.
Type
boolean

addMoney(amt)

Add money
Parameters:
Name Type Description
amt number Amount to add.
Source:

removeMoney(amt)

Remove money
Parameters:
Name Type Description
amt number Amount to remove.
Source:
Returns:
if money was able to be removed.
Type
boolean

formatBytes()

Format bytes into a human-readable format
Source:
Returns:
CMD#data in human-readable format
Type
string

formatter(size)

Format number into a human-readable format
Parameters:
Name Type Description
size number Number to be formatted.
Source:
Returns:
size in human-readable format
Type
string

reset()

Reset game progress
Source:

Type Definitions


respondCallback(txt)

Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description
txt * <repeatable>
Responses
Source:

saveCallback(cmdData)

Function to handle saving progress.
Parameters:
Name Type Description
cmdData Object Game progress to be saved.
Properties
Name Type Description
data number Data collected.
money number Money collected.
increment number Increment value for mineData.
autoIncrement number Increment value for autoMine.
storage string Current storage value.
unlocked Array.<string> Commands bought with buyCommand.
Source:
Returns:
An error if encountered.
Type
Error

loadCallback()

Function to handle saving progress.
Source:
Returns:
Game progress loaded from save.
Type
Object

updateCallback(cmdObj)

Function to handle updating game values.
Parameters:
Name Type Description
cmdObj CMD CMD object.
Source:

Command

An object representing a command.
Type:
  • Object
Properties:
Name Type Argument Default Description
func function Function called when command is run.
desc string | function Description for command.
usage string | function <nullable>
How to use the command.
price number <nullable>
0 Price to pay in bytes for command.
Source:

commandProviderCallback()

Function to provide custom commands.
Source:
Returns:
Object of custom commands.
Type
CMD~Command

errorHandlerCallback(err)

Function to handle thrown errors.
Parameters:
Name Type Description
err Error Error thrown.
Source:

respondCallback(txt)

Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description
txt * <repeatable>
Responses
Source:

saveCallback(cmdData)

Function to handle saving progress.
Parameters:
Name Type Description
cmdData Object Game progress to be saved.
Properties
Name Type Description
data number Data collected.
money number Money collected.
increment number Increment value for mineData.
autoIncrement number Increment value for autoMine.
storage string Current storage value.
unlocked Array.<string> Commands bought with buyCommand.
Source:
Returns:
An error if encountered.
Type
Error

loadCallback()

Function to handle saving progress.
Source:
Returns:
Game progress loaded from save.
Type
Object

updateCallback(cmdObj)

Function to handle updating game values.
Parameters:
Name Type Description
cmdObj CMD CMD object.
Source:

Command

An object representing a command.
Type:
  • Object
Properties:
Name Type Argument Default Description
func function Function called when command is run.
desc string | function Description for command.
usage string | function <nullable>
How to use the command.
price number <nullable>
0 Price to pay in bytes for command.
Source:

commandProviderCallback()

Function to provide custom commands.
Source:
Returns:
Object of custom commands.
Type
CMD~Command

errorHandlerCallback(err)

Function to handle thrown errors.
Parameters:
Name Type Description
err Error Error thrown.
Source: