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
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 increment
number <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 str
string 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 amt
number Amount to add. Returns:
if data was able to be added.- Type
- boolean
-
removeData(amt)
-
Remove data
Parameters:
Name Type Description amt
number Amount to remove. Returns:
if data was able to be removed.- Type
- boolean
-
addMoney(amt)
-
Add money
Parameters:
Name Type Description amt
number Amount to add. -
removeMoney(amt)
-
Remove money
Parameters:
Name Type Description amt
number 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 size
number 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 increment
number <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 str
string 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 amt
number Amount to add. Returns:
if data was able to be added.- Type
- boolean
-
removeData(amt)
-
Remove data
Parameters:
Name Type Description amt
number Amount to remove. Returns:
if data was able to be removed.- Type
- boolean
-
addMoney(amt)
-
Add money
Parameters:
Name Type Description amt
number Amount to add. -
removeMoney(amt)
-
Remove money
Parameters:
Name Type Description amt
number 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 size
number 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 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. 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 cmdObj
CMD 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 err
Error 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 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. 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 cmdObj
CMD 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 err
Error Error thrown.