Compare commits

...

9 Commits

Author SHA1 Message Date
jay
72c4622091 feat(informer): various improvements and fixes
Feat:
- Letter aliases for subcommands.
- Most recent command when no input.
- Start player relative info stub.

Fix:
- Player username not shown.
2021-03-23 15:14:13 +05:00
jay
0757776d8b feat(command): 🎨 make botaddress prefix and regex more flexible and configurable 2021-03-23 14:39:07 +05:00
jay
a0ffaf1654 build(typescript): 🚨 add @types dep to satisfy typescript errors
Typescript linter had an error that `require` was undefined.
This happened after updating Nodejs from 14.15 to 14.16.
Not sure if this should be a global dep or local
2021-03-23 13:25:26 +05:00
jay
1597acca72 fix(eater): 🥅 catch async error on full food
Attempt to fix async error returned by `bot.consume`.
Fixed by wrapping in a `try{}` block and using `.catch`.
Still don't know why or how this works 🤷.
2021-03-22 17:32:56 +05:00
jay
1e82045221 build: ⬆️ update deps 2021-03-22 15:59:22 +05:00
jay
33c4fc0c77 ci: 🔧 add vscode conventional commits scopes 2021-03-22 14:51:02 +05:00
jay
33c4233223 feat(statemachine): 🚧 first draft of new xstate based statemachine implementation
Replaces the old statemachine.
Done so far:
- Basic command interface
- Machine saving and loading
- Sample dummy machine
2021-03-22 14:32:08 +05:00
jay
3f3ebbae10 build: ⬆️ update deps 2021-03-07 12:10:14 +05:00
jay
fd0e1e1347 build(statemachine): add xstate for state machines 2021-01-30 23:08:20 +05:00
8 changed files with 554 additions and 157 deletions

1
.gitignore vendored
View File

@@ -10,6 +10,7 @@
# production # production
/build /build
/data
# misc # misc
.DS_Store .DS_Store

View File

@@ -3,6 +3,8 @@
"command", "command",
"mover", "mover",
"sleeper", "sleeper",
"informer" "informer",
"statemachine",
"builder"
] ]
} }

View File

@@ -86,7 +86,9 @@ reloadplugin = (event, filename, pluginpath) => {
fs.watch('./lib/plugins', reloadplugin) fs.watch('./lib/plugins', reloadplugin)
cfg.bot = bot cfg.bot = bot
cfg.botAddress = new RegExp(`^${bot.username} (!.+)`) // TODO better name, or switch to array
cfg.botAddressPrefix = '!'
cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`)
cfg.quiet = true cfg.quiet = true
@@ -104,7 +106,7 @@ bot.once("spawn", () => {
mover: require('./plugins/mover'), mover: require('./plugins/mover'),
guard: require('./plugins/guard'), guard: require('./plugins/guard'),
// miner: require('./plugins/miner.js'), // miner: require('./plugins/miner.js'),
// statemachine: require('./plugins/statemachine'), statemachine: require('./plugins/statemachine'),
} }
cfg.plugins = plugins cfg.plugins = plugins

View File

@@ -62,6 +62,9 @@ const events = {
const events_registered = [] const events_registered = []
function command(username, message) { function command(username, message) {
// TODO better name, maybe an array?
cfg.botAddressPrefix = cfg.botAddressPrefix || "!"
function fuzzyRespond(responses, probability = 1, timeout = 1) { function fuzzyRespond(responses, probability = 1, timeout = 1) {
if (Math.random() < probability) { if (Math.random() < probability) {
const response = responses[Math.floor(Math.random() * responses.length)] const response = responses[Math.floor(Math.random() * responses.length)]
@@ -76,7 +79,7 @@ function command(username, message) {
} }
} }
if (username === bot.username && !message.startsWith("!")) return if (username === bot.username && !message.startsWith(cfg.botAddressPrefix)) return
const player = bot.players[username] ? bot.players[username].entity : null const player = bot.players[username] ? bot.players[username].entity : null
@@ -88,11 +91,12 @@ function command(username, message) {
} }
if (message.startsWith("!") || cfg.botAddress.test(message)) { if (message.startsWith(cfg.botAddressPrefix) || cfg.botAddressRegex.test(message)) {
message = cfg.botAddress.test(message) ? cfg.botAddress.exec(message)[1] : message message = cfg.botAddressRegex.test(message) ? cfg.botAddressRegex.exec(message)[1] : message
console.log(message) console.log(message)
message = message.slice(1) // remove `!` // remove `!`
message = message.startsWith(cfg.botAddressPrefix) ? message.slice(cfg.botAddressPrefix.length) : message
// TODO command dispatchEvent, for aliases // TODO command dispatchEvent, for aliases
function subcommand(message) { function subcommand(message) {
const message_parts = message.split(/\s+/) const message_parts = message.split(/\s+/)
@@ -327,7 +331,7 @@ function command(username, message) {
} }
break break
case "info": case "info":
cfg.plugins.informer.command(message_parts.splice(1)) cfg.plugins.informer.command(message_parts.splice(1), player)
break break
// case "use": // case "use":
// bot.useOn(bot.nearestEntity()) // bot.useOn(bot.nearestEntity())
@@ -404,6 +408,16 @@ function command(username, message) {
break break
} }
break; break;
case "sm":
case "step":
cfg.plugins.statemachine?.command?.(
message_parts[0] == "sm" ? message_parts.slice(1) : message_parts, player
)
// TODO refactor into plugin detection and command exec function
// safecommand(plugin_name, message_parts, player)
// message_parts includes command?
|| bot.chat("statemachine plugin not loaded")
break
case "location": case "location":
// TODO put in /lib/location // TODO put in /lib/location
switch (message_parts[1]) { switch (message_parts[1]) {

View File

@@ -2,11 +2,7 @@ let cfg = {}
let bot = {} let bot = {}
let isEating = false let isEating = false
function callbackHandle(err) { function eat(callback = e => e && console.error(e)) {
if (err) console.error(err)
}
function eat(callback) {
isEating = true isEating = true
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name)
@@ -37,23 +33,23 @@ function eat(callback) {
bot.equip(best_food, 'hand', function (error) { bot.equip(best_food, 'hand', function (error) {
if (error) { if (error) {
console.error(error) console.warn(error, best_food)
isEating = false isEating = false
bot.emit('eat_stop') bot.emit('eat_stop')
} else { } else {
bot.consume(function (err) { try {
if (err) { bot.consume().catch(error => {
console.error(err) if (error.message === "Food is full") {
console.warn(error, best_food)
} else {
return callback({ error, best_food })
}
}).finally(() => {
isEating = false isEating = false
bot.emit('eat_stop') bot.emit('eat_stop')
return callback(err) })
} else { } catch { }
isEating = false if (bot.food !== 20) eat(callback)
bot.emit('eat_stop')
callback(null)
if (!bot.food === 20) eat(callbackHandle)
}
})
} }
}) })
} }
@@ -76,7 +72,7 @@ function checkFood() {
// TODO implement better idle state // TODO implement better idle state
) || true // idle most likely ) || true // idle most likely
) { ) {
eat(callbackHandle) eat()
} }
} }
} }

View File

@@ -1,6 +1,7 @@
let cfg let cfg
let bot let bot
let mcData let mcData
// import v from 'vec3'
const v = require('vec3') const v = require('vec3')
function block(pos) { function block(pos) {
@@ -51,34 +52,47 @@ function item(
} }
function entity(name) { function entity(name) {
const entity = bot.nearestEntity((entity) => { const entity = typeof name === "string" ? bot.nearestEntity((entity) => {
const ename = entity.name || entity.username const ename = entity.name || entity.username
return name && ename ? ename == name : true return name && ename ? ename == name : true
}) }) : entity
console.log(entity) console.log(entity)
if (!entity) { if (!entity) {
cfg.quiet || bot.chat("no entity") cfg.quiet || bot.chat("no entity")
return entity return entity
} }
let info = [entity.type, entity.name || entity.username] let info = [entity.type, entity.username || entity.name]
// TODO various info depending on the type of entity; player, villager, etc
if (entity.metadata) info.push("len: " + entity.metadata.length) if (entity.metadata) info.push("len: " + entity.metadata.length)
cfg.quiet || bot.chat(info.join("; ")) cfg.quiet || bot.chat(info.join("; "))
return entity
} }
function command(message_parts) { function command(message_parts, player) {
if (message_parts.length > 0) {
cfg.info.recentCommand = message_parts
}
switch (message_parts.length) { switch (message_parts.length) {
case 0: case 0:
// TODO most recent command? if (cfg.info.recentCommand) {
block() command(cfg.info.recentCommand, player)
} else {
// TODO dispatch on instance of entity, block, etc..
// TODO have the logic inside the function or with a utility function
block(player.position || player?.entity.position || null)
}
break; break;
case 1: case 1:
switch (message_parts[0]) { switch (message_parts[0]) {
case "i":
case "item": case "item":
item() item()
break break
case "e":
case "entity": case "entity":
entity() entity()
break break
case "b":
case "block": case "block":
default: default:
block() block()
@@ -89,9 +103,11 @@ function command(message_parts) {
case 2: case 2:
switch (message_parts[0]) { switch (message_parts[0]) {
case "i":
case "item": case "item":
item(message_parts[1]) item(message_parts[1])
break break
case "e":
case "entity": case "entity":
default: default:
entity(message_parts[1]) entity(message_parts[1])
@@ -101,6 +117,7 @@ function command(message_parts) {
case 4: case 4:
switch (message_parts[0]) { switch (message_parts[0]) {
case "b":
case "block": case "block":
default: default:
block(message_parts.slice(1)) block(message_parts.slice(1))
@@ -110,6 +127,7 @@ function command(message_parts) {
break; break;
default: default:
cfg.quiet || bot.chat("info: unknown command")
break; break;
} }
} }
@@ -119,6 +137,7 @@ const load = (config) => {
bot = cfg.bot bot = cfg.bot
cfg.info = { cfg.info = {
quiet: cfg.quiet, quiet: cfg.quiet,
recentCommand: null,
} }
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version)) mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
} }

View File

@@ -1,115 +1,476 @@
// Load your dependency plugins. // import { createMachine, interpret, InterpreterStatus } from "xstate";
const { createMachine, interpret, InterpreterStatus } = require('xstate');
const {pathfinder} = require('mineflayer-pathfinder') // import { access, mkdir, writeFile, readFile } from "fs";
// bot.loadPlugin(require('prismarine-viewer').mineflayer) const { access, mkdir, writeFile, readFile } = require('fs');
// const mineflayerViewer = require('prismarine-viewer').mineflayer const v = require('vec3'); // for look dummy action, maybe not needed in future
// ANGRAM_PREFIX='MINECRAFT'
// Import required behaviors. const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed;
const { const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/";
StateTransition, // import { createBot } from "mineflayer"
BotStateMachine, // let { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
EntityFilters, // let cfg
BehaviorFollowEntity, // let bot = createBot({ username: 'statebot' })
BehaviorLookAtEntity, let bot;
BehaviorGetClosestEntity, let quiet;
NestedStateMachine, let updateRate = 20;
BehaviorIdle, const machines = {
StateMachineWebserver, list: {},
} = require("mineflayer-statemachine"); running: {},
};
// TODO chat let webserver;
let cfg = {
statemachine: {
webserver: null,
// wait for our bot to login. // quiet: true,
function statemachineInit() { quiet: quiet,
cfg.botAddress = new RegExp(`^${bot.username} (!.+)`) list: {},
// This targets object is used to pass data between different states. It can be left empty. running: {},
const targets = new Object(); draft: null,
recent: null,
// Create our states // debug: null,
const getClosestPlayer = new BehaviorGetClosestEntity( debug: true,
bot, targets, entity => EntityFilters().PlayersOnly(entity) && bot.entity.position.distanceTo(entity.position) <= 50 ); // && a.username !== bot.username); updateRate: updateRate
const followPlayer = new BehaviorFollowEntity(bot, targets); }
const lookAtPlayer = new BehaviorLookAtEntity(bot, targets); // FIXME temp variables to satisfy typescript autocomplete
const stay = new BehaviorIdle(); // , quiet: null
,
// Create our transitions bot: bot,
const transitions = [ plugins: { statemachine: null }
};
// We want to start following the player immediately after finding them. // Edit your machine(s) here
// Since getClosestPlayer finishes instantly, shouldTransition() should always return true. function init(smName = "dummy", webserver) {
new StateTransition({ access(storage_dir, err => {
parent: getClosestPlayer, if (err?.code === 'ENOENT') {
child: followPlayer, mkdir(storage_dir, e => e && console.warn("sm init: create dir", e));
onTransition: (quiet) => quiet || bot.chat(`Hi ${targets.entity.username}!`), }
shouldTransition: () => bot.entity.position.distanceTo(targets.entity.position) <= 50, else if (err) {
// shouldTransition: () => getClosestPlayer.distanceToTarget() < 100 || console.info("player too far!") && false, console.warn("sm init: create dir", err);
}), }
});
// If the distance to the player is less than two blocks, switch from the followPlayer // const machine = newSM(smName)
// state to the lookAtPlayer state. // machine.states.idle.on.TOGGLE = "start"
new StateTransition({ // machine.states.start.on.TOGGLE = "idle"
parent: followPlayer, const machine = createMachine({
child: lookAtPlayer, id: smName,
// onTransition: () => console.log(targets), initial: "idle",
shouldTransition: () => followPlayer.distanceToTarget() < 2, states: {
}), idle: {
on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" }
// If the distance to the player is more than two blocks, switch from the lookAtPlayer },
// state to the followPlayer state. start: {
new StateTransition({ on: { TOGGLE: "look", NEXT: "look", PREV: "idle" },
parent: lookAtPlayer, entry: 'lookAtPlayerOnce',
child: followPlayer, },
shouldTransition: () => lookAtPlayer.distanceToTarget() >= 5, look: {
}), on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" },
new StateTransition({ // entry: ['look', 'blah']
parent: lookAtPlayer, // entry: 'lookAtPlayerOnce',
child: stay, activities: 'lookAtPlayer',
onTransition: () => bot.chat("ok, staying"), meta: { debug: true }
// shouldTransition: () => true, },
}), finish: {
new StateTransition({ type: 'final'
parent: stay, },
child: getClosestPlayer, },
// shouldTransition: () => Math.random() > 0.01, on: { START: '.start', STOP: '.idle' },
// shouldTransition: () => Math.random() > 0.1 && getClosestPlayer.distanceToTarget() < 2, meta: { debug: true },
}), context: { player: null, rate: updateRate },
]; }, {
actions: {
// Now we just wrap our transition list in a nested state machine layer. We want the bot // action implementation
// to start on the getClosestPlayer state, so we'll specify that here. lookAtPlayerOnce: (context, event) => {
const rootLayer = new NestedStateMachine(transitions, getClosestPlayer, stay); const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
if (player.position || player.entity) {
// We can start our state machine simply by creating a new instance. context.player = player;
cfg.stateMachines.follow = new BotStateMachine(bot, rootLayer); bot.lookAt((new v.Vec3(0, 1, 0)).add((player.entity || player).position));
const webserver = new StateMachineWebserver(bot, cfg.stateMachines.follow); }
webserver.startServer(); }
},
// mineflayerViewer(bot, { port: 3000 }) activities: {
// const path = [bot.entity.position.clone()] lookAtPlayer: (context, event) => {
// bot.on('move', () => { const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
// if (path[path.length - 1].distanceTo(bot.entity.position) > 1) { // TODO check every event?
// path.push(bot.entity.position.clone()) if (player.position || player.entity) {
// bot.viewer.drawLine('path', path) context.player = player;
// } function looks() {
// }) bot.lookAt((new v.Vec3(0, 1, 0)).add((player.entity || player).position));
} }
bot.on("time", looks);
const load = (config) => { return () => bot.off("time", looks);
cfg = config }
bot = cfg.bot }
// cfg.inventory = { },
// auto: true, delays: {
// quiet: false /* ... */
// } },
// bot.on('chat', inventory) guards: {
bot.loadPlugin(pathfinder) /* ... */
// statemachineInit() },
} services: {
/* ... */
const unload = () => { }
// bot.off('chat', inventory) });
} console.log("sm init: machine", machine);
const service = runSM(saveSM(machine));
module.exports = { load, unload } if (service?.send) {
setTimeout(service.send, 200, "TOGGLE");
// setTimeout(service.send, 400, "TOGGLE")
}
else {
console.warn("sm init: service", service);
}
}
function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) {
smName = smName.replace(/\s+/, '_');
if (cfg.statemachine.list[smName]) {
console.warn("sm exists", smName);
quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`);
return;
}
const machine = createMachine({
id: smName,
initial: "start",
// TODO use history states for PAUSE and RESUME
states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } }
});
cfg.statemachine.draft = machine;
return machine;
}
function saveSM(machine = cfg.statemachine.draft) {
if (!machine?.id) {
console.warn("sm save: invalid", machine);
quiet || bot.chat("sm: couldn't save, invalid");
return;
}
// TODO do tests and validation
// 1. ensure default states [start, idle]
// 2. ensure closed cycle from start to idle
cfg.statemachine.list[machine.id] = machine;
if (machine.id === cfg.statemachine.draft?.id) {
cfg.statemachine.draft = null;
}
writeFile(
// TODO
// `${storage_dir}/${player}/${machine.id}.json`
`${storage_dir}/${machine.id}.json`,
// TODO decide which data to store
// https://xstate.js.org/docs/guides/states.html#persisting-state
// JSON.stringify(machine.toJSON),
// JSON.stringify(machine.states.toJSON), // + activities, delays, etc
JSON.stringify({ config: machine.config, context: machine.context }), e => e && console.log("sm load sm: write file", e));
// return run ? runSM(machine) : machine
return machine;
}
function loadSM(name, run = true) {
//: StateMachine<any, any, any> {
readFile(
// readFileSync(
// TODO
// `${storage_dir}/${player}/${machine.id}.json`
`${storage_dir}/${name}.json`, afterRead
// JSON.stringify(machine.toJSON),
);
function afterRead(err, jsonString) {
if (err) {
console.warn("sm load sm: read file", err);
return;
}
else {
const machine = createMachine(JSON.parse(jsonString).config);
// TODO do tests and validation
// 1. ensure default states [start, idle]
// 2. ensure closed cycle from start to idle
cfg.statemachine.list[machine.id] = machine;
if (machine.id === cfg.statemachine.draft?.id) {
cfg.statemachine.draft = machine;
}
if (run) {
runSM(machine);
}
}
}
// return run ? runSM(machine) : machine
// return machine
}
// function isInterpreter(SMorService: StateMachine<any, any, any> | Interpreter<any>): SMorService is Interpreter<any> {
// return (SMorService as Interpreter<any>).start !== undefined;
// }
function getSM(name = cfg.statemachine.draft || cfg.statemachine.recent, asService = false, quiet = false) {
const machine = typeof name === "string" ? cfg.statemachine.list[name]
: name;
if (!machine) {
console.warn("sm get: doesn't exist", name);
cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`);
return;
}
if (asService) {
const service = cfg.statemachine.running[machine?.id];
if (!service) {
quiet || console.warn("sm get: already stopped", machine);
quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`);
return interpret(machine);
}
return service;
}
else {
// return machine.machine ? machine.machine : machine
return machine;
}
}
function runSM(name = getSM(undefined, undefined, true), player // or supervisor?
, restart = false) {
if (!name)
return;
const service = getSM(name, true, true);
if (!service)
return;
const machine = service.machine;
if (!machine)
return;
switch (service.status) {
case InterpreterStatus.Running:
if (!restart) {
console.warn("sm run: already running", service.id);
quiet || bot.chat(`sm ${service.id} already running`);
return service;
}
stopSM(machine);
case InterpreterStatus.NotStarted:
case InterpreterStatus.Stopped:
break;
default:
console.warn("sm run: unknown status:", service.status, service);
return;
break;
}
if (cfg.statemachine.debug || machine.meta?.debug) {
service.onTransition((state) => {
quiet || bot.chat(`sm trans: ${machine.id}, ${state.value}`);
quiet || state.meta?.debug && bot.chat(`sm next events: ${machine.id}, ${state.nextEvents}`);
console.log("sm debug: trans", state.value, state);
}).onDone((done) => {
quiet || bot.chat(`sm done: ${machine.id}, ${done}`);
console.log("sm debug: done", done.data, done);
});
}
cfg.statemachine.running[machine.id] = service;
cfg.statemachine.recent = machine;
service.start();
// // TODO check if idle state is different (maybe?)
console.log("sm run", service.state.value === machine.initialState.value, service.state);
console.log("sm run", service.state !== machine.initialState, machine.initialState);
// return machine
return service;
}
function stopSM(name = getSM(), quiet = false) {
let service = getSM(name, true, quiet);
if (!service)
return;
const machine = service.machine;
switch (service.status) {
case InterpreterStatus.NotStarted:
case InterpreterStatus.Stopped:
console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]);
// TODO check if any bugs
case InterpreterStatus.Running:
break;
default:
console.warn("sm stop: unknown status:", service.status);
break;
}
service?.stop?.();
cfg.statemachine.running[machine.id] = null;
delete cfg.statemachine.running[machine.id];
// return machine
return service;
}
function actionSM(action, name = getSM()) {
if (!action) {
return console.warn("sm action", action);
}
let service = getSM(name, true, true);
if (service.status !== InterpreterStatus.Running)
return;
// const machine = service.machine
service.send(action.toLowerCase());
}
function stepSM(command = "", ...message_parts) {
let service = getSM(undefined, true);
if (!service)
return;
if (!service.send) {
console.warn("sm step: can't send", service.machine);
// TODO start a temporary service to interpret
quiet || bot.chat("sm: step doesn't support machines that aren't running yet");
return;
}
if (service?.status !== InterpreterStatus.Running) {
console.warn("sm step: machine not running, attempting start", service);
runSM;
}
// const machine = service.machine
switch (command) {
case "edit":
// maybe `edit <type>`, like `add`?
// where type:
// context
// action
// timeout | all timeout
break;
case "undo":
break;
case "del":
break;
case "p":
case "prev":
service?.send("PREV");
break;
case "add":
// maybe `add <type>`?
// where type:
// context
// action
// timeout
case "new":
break;
// case "with":
// console.log(this)
// stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1))
// break;
case "help":
quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]");
break;
case "n":
case "next":
default:
service?.send("NEXT");
quiet || bot.chat("stepped");
break;
}
}
function tickSM(time = bot.time.timeOfDay, rate = 20) {
if (time % rate !== 0) {
return;
}
console.log("sm tick", rate, time);
}
function debugSM(name = getSM()) {
if (!name)
return;
let service = getSM(name, true);
if (!service)
return;
const machine = service.machine;
machine.meta.debug = !!!machine.meta.debug;
console.info("sm debug", machine.meta, service, machine);
cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id);
}
function command(message_parts) {
const message_parts2 = message_parts.slice(1);
switch (message_parts[0]) {
case "add":
command(["new"].concat(message_parts2));
break;
case "finish":
case "done":
case "end":
command(["save"].concat(message_parts2));
break;
case "do":
case "load":
case "start":
command(["run"].concat(message_parts2));
break;
case "debug":
switch (message_parts[1]) {
case "sm":
case "global":
case "meta":
cfg.statemachine.debug = !!!cfg.statemachine.debug;
quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`);
break;
}
case "new":
case "save":
case "run":
case "step":
case "stop":
// temp
case "action":
switch (message_parts2.length) {
case 0:
console.warn(`sm ${message_parts[0]}: no name, using defaults`);
case 1:
// FIXME `this` doesn't work always
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
break;
default:
if (["action"].includes(message_parts[0])) {
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
}
else {
console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2);
}
break;
}
break;
case "undo":
break;
case "list":
case "status":
// TODO current/recent, updateRate
const { list, running } = cfg.statemachine;
console.log("sm list", running, list);
quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}`
+ (message_parts[1] === "all" ? `${Object.keys(list)}` : ''));
break;
case "quiet":
quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet;
quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`);
break;
default:
// TODO general helper from declarative commands object
quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`);
console.warn("sm unknown command", message_parts);
break;
}
return true;
}
function load(config) {
webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver;
config.statemachine = cfg.statemachine || {
webserver: null,
// quiet: true,
quiet: false,
list: {},
running: {},
draft: null,
recent: null,
// debug: null,
debug: true,
updateRate: updateRate
};
cfg = config;
bot = cfg.bot;
// pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
// mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
init(undefined, webserver);
updateRate = cfg.statemachine.updateRate || updateRate;
bot.on('time', tickSM);
// bot.once('time', tickSM, 5)
console.log("sm load", cfg.statemachine);
}
function unload() {
const { list, running } = cfg.statemachine;
bot.off('time', tickSM);
Object.keys(running).forEach(sm => {
stopSM(sm);
});
// delete cfg.statemachine;
cfg.statemachine = null;
console.log("sm unload: deleted", cfg.statemachine);
}
module.exports = {
load, unload, command, init,
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM
};

View File

@@ -30,24 +30,26 @@
}, },
"homepage": "https://github.com/PrismarineJS/prismarine-template#readme", "homepage": "https://github.com/PrismarineJS/prismarine-template#readme",
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.35",
"jest": "^26.6.3", "jest": "^26.6.3",
"require-self": "^0.2.3" "require-self": "^0.2.3",
"typescript": "^4.2.3"
}, },
"dependencies": { "dependencies": {
"dotenv-packed": "^1.2.1", "dotenv-packed": "^1.2.1",
"minecraft-data": "^2.73.1", "minecraft-data": "^2.80.0",
"mineflayer": "^2.40.1", "mineflayer": "^3.4.0",
"mineflayer-armor-manager": "^1.4.0", "mineflayer-armor-manager": "^1.4.0",
"mineflayer-pathfinder": "^1.3.6", "mineflayer-pathfinder": "^1.6.1",
"mineflayer-pvp": "^1.0.2", "mineflayer-pvp": "^1.0.2",
"prismarine-block": "^1.7.3", "prismarine-block": "^1.8.0",
"prismarine-chat": "^1.0.3", "prismarine-chat": "^1.0.3",
"prismarine-entity": "^1.1.0", "prismarine-entity": "^1.1.0",
"prismarine-item": "^1.5.0", "prismarine-item": "^1.8.0",
"prismarine-nbt": "^1.4.0", "prismarine-nbt": "^1.5.0",
"prismarine-recipe": "^1.1.0", "prismarine-recipe": "^1.1.0",
"typescript": "^4.1.3", "vec3": "^0.1.7",
"vec3": "^0.1.7" "xstate": "^4.17.0"
}, },
"files": [ "files": [
"lib/**/*" "lib/**/*"