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. |
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.
-
respond(txt)
-
Send response to respond function from constructor
Parameters:
Name Type Argument Description txt* <repeatable>
Strings to be sent to respond function. -
checkStorage(increment)
-
Check if storage is full.
Parameters:
Name Type Argument Description incrementnumber <nullable>
Increment to check against. If undefined, equal to CMD#increment. Returns:
If storage has enough space.- Type
- boolean
-
command(str)
-
Run command
Parameters:
Name Type Description strstring Command to be ran. -
update()
-
Run update function from constructor to update game values
-
save()
-
Save game progress
-
load()
-
Load game progress
-
addData(amt)
-
Add data
Parameters:
Name Type Description amtnumber Amount to add. Returns:
if data was able to be added.- Type
- boolean
-
removeData(amt)
-
Remove data
Parameters:
Name Type Description amtnumber Amount to remove. Returns:
if data was able to be removed.- Type
- boolean
-
addMoney(amt)
-
Add money
Parameters:
Name Type Description amtnumber Amount to add. -
removeMoney(amt)
-
Remove money
Parameters:
Name Type Description amtnumber Amount to remove. Returns:
if money was able to be removed.- Type
- boolean
-
formatBytes()
-
Format bytes into a human-readable format
Returns:
CMD#data in human-readable format- Type
- string
-
formatter(size)
-
Format number into a human-readable format
Parameters:
Name Type Description sizenumber Number to be formatted. Returns:
size in human-readable format- Type
- string
-
reset()
-
Reset game progress
-
gameLoop()
-
Start the game loop.
-
respond(txt)
-
Send response to respond function from constructor
Parameters:
Name Type Argument Description txt* <repeatable>
Strings to be sent to respond function. -
checkStorage(increment)
-
Check if storage is full.
Parameters:
Name Type Argument Description incrementnumber <nullable>
Increment to check against. If undefined, equal to CMD#increment. Returns:
If storage has enough space.- Type
- boolean
-
command(str)
-
Run command
Parameters:
Name Type Description strstring Command to be ran. -
update()
-
Run update function from constructor to update game values
-
save()
-
Save game progress
-
load()
-
Load game progress
-
addData(amt)
-
Add data
Parameters:
Name Type Description amtnumber Amount to add. Returns:
if data was able to be added.- Type
- boolean
-
removeData(amt)
-
Remove data
Parameters:
Name Type Description amtnumber Amount to remove. Returns:
if data was able to be removed.- Type
- boolean
-
addMoney(amt)
-
Add money
Parameters:
Name Type Description amtnumber Amount to add. -
removeMoney(amt)
-
Remove money
Parameters:
Name Type Description amtnumber Amount to remove. Returns:
if money was able to be removed.- Type
- boolean
-
formatBytes()
-
Format bytes into a human-readable format
Returns:
CMD#data in human-readable format- Type
- string
-
formatter(size)
-
Format number into a human-readable format
Parameters:
Name Type Description sizenumber Number to be formatted. Returns:
size in human-readable format- Type
- string
-
reset()
-
Reset game progress
Type Definitions
-
respondCallback(txt)
-
Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description txt* <repeatable>
Responses -
saveCallback(cmdData)
-
Function to handle saving progress.
Parameters:
Name Type Description cmdDataObject Game progress to be saved. Properties
Name Type Description datanumber Data collected. moneynumber Money collected. incrementnumber Increment value for mineData. autoIncrementnumber Increment value for autoMine. storagestring Current storage value. unlockedArray.<string> Commands bought with buyCommand. Returns:
An error if encountered.- Type
- Error
-
loadCallback()
-
Function to handle saving progress.
Returns:
Game progress loaded from save.- Type
- Object
-
updateCallback(cmdObj)
-
Function to handle updating game values.
Parameters:
Name Type Description cmdObjCMD CMD object. -
Command
-
An object representing a command.
Type:
- Object
-
commandProviderCallback()
-
Function to provide custom commands.
Returns:
Object of custom commands.- Type
- CMD~Command
-
errorHandlerCallback(err)
-
Function to handle thrown errors.
Parameters:
Name Type Description errError Error thrown. -
respondCallback(txt)
-
Function to handle responses from the CMD object.
Parameters:
Name Type Argument Description txt* <repeatable>
Responses -
saveCallback(cmdData)
-
Function to handle saving progress.
Parameters:
Name Type Description cmdDataObject Game progress to be saved. Properties
Name Type Description datanumber Data collected. moneynumber Money collected. incrementnumber Increment value for mineData. autoIncrementnumber Increment value for autoMine. storagestring Current storage value. unlockedArray.<string> Commands bought with buyCommand. Returns:
An error if encountered.- Type
- Error
-
loadCallback()
-
Function to handle saving progress.
Returns:
Game progress loaded from save.- Type
- Object
-
updateCallback(cmdObj)
-
Function to handle updating game values.
Parameters:
Name Type Description cmdObjCMD CMD object. -
Command
-
An object representing a command.
Type:
- Object
-
commandProviderCallback()
-
Function to provide custom commands.
Returns:
Object of custom commands.- Type
- CMD~Command
-
errorHandlerCallback(err)
-
Function to handle thrown errors.
Parameters:
Name Type Description errError Error thrown.