style: 🎨 fix crlf -> lf for real this time, using .gitattributes
Add a .gitattributes file to permanentyl fix crlf issues. For explanantion: https://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/#the-new-system
This commit is contained in:
parent
60394e38eb
commit
aded1e4193
16
.gitattributes
vendored
Normal file
16
.gitattributes
vendored
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# See this article for reference: https://help.github.com/articles/dealing-with-line-endings/
|
||||||
|
# Refreshing repo after line ending change:
|
||||||
|
# https://help.github.com/articles/dealing-with-line-endings/#refreshing-a-repository-after-changing-line-endings
|
||||||
|
|
||||||
|
# Handle line endings automatically for files detected as text
|
||||||
|
# and leave all files detected as binary untouched.
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
#
|
||||||
|
# The above will handle all files NOT found below
|
||||||
|
#
|
||||||
|
# These files are text and should be normalized (Convert crlf => lf)
|
||||||
|
# Use lf as eol for these files
|
||||||
|
*.js text eol=lf
|
||||||
|
*.ts text eol=lf
|
||||||
|
*.json text eol=lf
|
60
.gitignore
vendored
60
.gitignore
vendored
|
@ -1,31 +1,31 @@
|
||||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
/node_modules
|
/node_modules
|
||||||
/.pnp
|
/.pnp
|
||||||
.pnp.js
|
.pnp.js
|
||||||
|
|
||||||
# testing
|
# testing
|
||||||
/coverage
|
/coverage
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/build
|
/build
|
||||||
/data
|
/data
|
||||||
|
|
||||||
# misc
|
# misc
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.env
|
.env
|
||||||
.env.local
|
.env.local
|
||||||
.env.development.local
|
.env.development.local
|
||||||
.env.test.local
|
.env.test.local
|
||||||
.env.production.local
|
.env.production.local
|
||||||
/lib/**/*.old
|
/lib/**/*.old
|
||||||
/lib/**/*.bak
|
/lib/**/*.bak
|
||||||
|
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
|
|
||||||
# Editor
|
# Editor
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
|
@ -1,475 +1,475 @@
|
||||||
// import { createMachine, interpret, InterpreterStatus } from "xstate";
|
// import { createMachine, interpret, InterpreterStatus } from "xstate";
|
||||||
const { createMachine, interpret, InterpreterStatus } = require('xstate');
|
const { createMachine, interpret, InterpreterStatus } = require('xstate');
|
||||||
// import { access, mkdir, writeFile, readFile } from "fs";
|
// import { access, mkdir, writeFile, readFile } from "fs";
|
||||||
const { access, mkdir, writeFile, readFile } = require('fs');
|
const { access, mkdir, writeFile, readFile } = require('fs');
|
||||||
// ANGRAM_PREFIX='MINECRAFT'
|
// ANGRAM_PREFIX='MINECRAFT'
|
||||||
const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed;
|
const { MINECRAFT_DATA_FOLDER } = process.env || require("dotenv-packed").parseEnv().parsed;
|
||||||
const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/";
|
const storage_dir = MINECRAFT_DATA_FOLDER || './data/' + "/sm/";
|
||||||
// import { createBot } from "mineflayer"
|
// import { createBot } from "mineflayer"
|
||||||
// let { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
|
// let { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
|
||||||
// let cfg
|
// let cfg
|
||||||
// let bot = createBot({ username: 'statebot' })
|
// let bot = createBot({ username: 'statebot' })
|
||||||
let bot;
|
let bot;
|
||||||
let quiet;
|
let quiet;
|
||||||
let updateRate = 20;
|
let updateRate = 20;
|
||||||
const machines = {
|
const machines = {
|
||||||
list: {},
|
list: {},
|
||||||
running: {},
|
running: {},
|
||||||
};
|
};
|
||||||
let webserver;
|
let webserver;
|
||||||
let cfg = {
|
let cfg = {
|
||||||
statemachine: {
|
statemachine: {
|
||||||
webserver: null,
|
webserver: null,
|
||||||
// quiet: true,
|
// quiet: true,
|
||||||
quiet: quiet,
|
quiet: quiet,
|
||||||
list: {},
|
list: {},
|
||||||
running: {},
|
running: {},
|
||||||
draft: null,
|
draft: null,
|
||||||
recent: null,
|
recent: null,
|
||||||
// debug: null,
|
// debug: null,
|
||||||
debug: true,
|
debug: true,
|
||||||
updateRate: updateRate
|
updateRate: updateRate
|
||||||
}
|
}
|
||||||
// FIXME temp variables to satisfy typescript autocomplete
|
// FIXME temp variables to satisfy typescript autocomplete
|
||||||
// , quiet: null
|
// , quiet: null
|
||||||
,
|
,
|
||||||
bot: bot,
|
bot: bot,
|
||||||
plugins: { statemachine: null }
|
plugins: { statemachine: null }
|
||||||
};
|
};
|
||||||
// Edit your machine(s) here
|
// Edit your machine(s) here
|
||||||
function init(smName = "dummy", webserver) {
|
function init(smName = "dummy", webserver) {
|
||||||
access(storage_dir, err => {
|
access(storage_dir, err => {
|
||||||
if (err?.code === 'ENOENT') {
|
if (err?.code === 'ENOENT') {
|
||||||
mkdir(storage_dir, e => e && console.warn("sm init: create dir", e));
|
mkdir(storage_dir, e => e && console.warn("sm init: create dir", e));
|
||||||
}
|
}
|
||||||
else if (err) {
|
else if (err) {
|
||||||
console.warn("sm init: create dir", err);
|
console.warn("sm init: create dir", err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// const machine = newSM(smName)
|
// const machine = newSM(smName)
|
||||||
// machine.states.idle.on.TOGGLE = "start"
|
// machine.states.idle.on.TOGGLE = "start"
|
||||||
// machine.states.start.on.TOGGLE = "idle"
|
// machine.states.start.on.TOGGLE = "idle"
|
||||||
const machine = createMachine({
|
const machine = createMachine({
|
||||||
id: smName,
|
id: smName,
|
||||||
initial: "idle",
|
initial: "idle",
|
||||||
states: {
|
states: {
|
||||||
idle: {
|
idle: {
|
||||||
on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" }
|
on: { TOGGLE: "start", NEXT: "start", PREV: "look", STOP: "finish" }
|
||||||
},
|
},
|
||||||
start: {
|
start: {
|
||||||
on: { TOGGLE: "look", NEXT: "look", PREV: "idle" },
|
on: { TOGGLE: "look", NEXT: "look", PREV: "idle" },
|
||||||
entry: 'lookAtPlayerOnce',
|
entry: 'lookAtPlayerOnce',
|
||||||
},
|
},
|
||||||
look: {
|
look: {
|
||||||
on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" },
|
on: { TOGGLE: "idle", NEXT: "idle", PREV: "start" },
|
||||||
// entry: ['look', 'blah']
|
// entry: ['look', 'blah']
|
||||||
// entry: 'lookAtPlayerOnce',
|
// entry: 'lookAtPlayerOnce',
|
||||||
activities: 'lookAtPlayer',
|
activities: 'lookAtPlayer',
|
||||||
meta: { debug: true }
|
meta: { debug: true }
|
||||||
},
|
},
|
||||||
finish: {
|
finish: {
|
||||||
type: 'final'
|
type: 'final'
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
on: { START: '.start', STOP: '.idle' },
|
on: { START: '.start', STOP: '.idle' },
|
||||||
meta: { debug: true },
|
meta: { debug: true },
|
||||||
context: { player: null, rate: updateRate },
|
context: { player: null, rate: updateRate },
|
||||||
}, {
|
}, {
|
||||||
actions: {
|
actions: {
|
||||||
// action implementation
|
// action implementation
|
||||||
lookAtPlayerOnce: (context, event) => {
|
lookAtPlayerOnce: (context, event) => {
|
||||||
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
|
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
|
||||||
if (player.position || player.entity) {
|
if (player.position || player.entity) {
|
||||||
context.player = player;
|
context.player = player;
|
||||||
bot.lookAt((player.entity || player).position.offset(0, 1, 0));
|
bot.lookAt((player.entity || player).position.offset(0, 1, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
activities: {
|
activities: {
|
||||||
lookAtPlayer: (context, event) => {
|
lookAtPlayer: (context, event) => {
|
||||||
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
|
const player = context?.player || bot.nearestEntity(entity => entity.type === 'player');
|
||||||
// TODO check every event?
|
// TODO check every event?
|
||||||
if (player.position || player.entity) {
|
if (player.position || player.entity) {
|
||||||
context.player = player;
|
context.player = player;
|
||||||
function looks() {
|
function looks() {
|
||||||
bot.lookAt((player.entity || player).position.offset(0, 1, 0));
|
bot.lookAt((player.entity || player).position.offset(0, 1, 0));
|
||||||
}
|
}
|
||||||
bot.on("time", looks);
|
bot.on("time", looks);
|
||||||
return () => bot.off("time", looks);
|
return () => bot.off("time", looks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
delays: {
|
delays: {
|
||||||
/* ... */
|
/* ... */
|
||||||
},
|
},
|
||||||
guards: {
|
guards: {
|
||||||
/* ... */
|
/* ... */
|
||||||
},
|
},
|
||||||
services: {
|
services: {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log("sm init: machine", machine);
|
console.log("sm init: machine", machine);
|
||||||
const service = runSM(saveSM(machine));
|
const service = runSM(saveSM(machine));
|
||||||
if (service?.send) {
|
if (service?.send) {
|
||||||
setTimeout(service.send, 200, "TOGGLE");
|
setTimeout(service.send, 200, "TOGGLE");
|
||||||
// setTimeout(service.send, 400, "TOGGLE")
|
// setTimeout(service.send, 400, "TOGGLE")
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.warn("sm init: service", service);
|
console.warn("sm init: service", service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) {
|
function newSM(smName = "sm_" + Object.keys(cfg.statemachine.list).length) {
|
||||||
smName = smName.replace(/\s+/, '_');
|
smName = smName.replace(/\s+/, '_');
|
||||||
if (cfg.statemachine.list[smName]) {
|
if (cfg.statemachine.list[smName]) {
|
||||||
console.warn("sm exists", smName);
|
console.warn("sm exists", smName);
|
||||||
quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`);
|
quiet || bot.chat(`sm ${smName} already exists, edit or use another name instead`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const machine = createMachine({
|
const machine = createMachine({
|
||||||
id: smName,
|
id: smName,
|
||||||
initial: "start",
|
initial: "start",
|
||||||
// TODO use history states for PAUSE and RESUME
|
// TODO use history states for PAUSE and RESUME
|
||||||
states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } }
|
states: { idle: { on: { START: "start" } }, start: { on: { STOP: "idle" } } }
|
||||||
});
|
});
|
||||||
cfg.statemachine.draft = machine;
|
cfg.statemachine.draft = machine;
|
||||||
return machine;
|
return machine;
|
||||||
}
|
}
|
||||||
function saveSM(machine = cfg.statemachine.draft) {
|
function saveSM(machine = cfg.statemachine.draft) {
|
||||||
if (!machine?.id) {
|
if (!machine?.id) {
|
||||||
console.warn("sm save: invalid", machine);
|
console.warn("sm save: invalid", machine);
|
||||||
quiet || bot.chat("sm: couldn't save, invalid");
|
quiet || bot.chat("sm: couldn't save, invalid");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO do tests and validation
|
// TODO do tests and validation
|
||||||
// 1. ensure default states [start, idle]
|
// 1. ensure default states [start, idle]
|
||||||
// 2. ensure closed cycle from start to idle
|
// 2. ensure closed cycle from start to idle
|
||||||
cfg.statemachine.list[machine.id] = machine;
|
cfg.statemachine.list[machine.id] = machine;
|
||||||
if (machine.id === cfg.statemachine.draft?.id) {
|
if (machine.id === cfg.statemachine.draft?.id) {
|
||||||
cfg.statemachine.draft = null;
|
cfg.statemachine.draft = null;
|
||||||
}
|
}
|
||||||
writeFile(
|
writeFile(
|
||||||
// TODO
|
// TODO
|
||||||
// `${storage_dir}/${player}/${machine.id}.json`
|
// `${storage_dir}/${player}/${machine.id}.json`
|
||||||
`${storage_dir}/${machine.id}.json`,
|
`${storage_dir}/${machine.id}.json`,
|
||||||
// TODO decide which data to store
|
// TODO decide which data to store
|
||||||
// https://xstate.js.org/docs/guides/states.html#persisting-state
|
// https://xstate.js.org/docs/guides/states.html#persisting-state
|
||||||
// JSON.stringify(machine.toJSON),
|
// JSON.stringify(machine.toJSON),
|
||||||
// JSON.stringify(machine.states.toJSON), // + activities, delays, etc
|
// 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));
|
JSON.stringify({ config: machine.config, context: machine.context }), e => e && console.log("sm load sm: write file", e));
|
||||||
// return run ? runSM(machine) : machine
|
// return run ? runSM(machine) : machine
|
||||||
return machine;
|
return machine;
|
||||||
}
|
}
|
||||||
function loadSM(name, run = true) {
|
function loadSM(name, run = true) {
|
||||||
//: StateMachine<any, any, any> {
|
//: StateMachine<any, any, any> {
|
||||||
readFile(
|
readFile(
|
||||||
// readFileSync(
|
// readFileSync(
|
||||||
// TODO
|
// TODO
|
||||||
// `${storage_dir}/${player}/${machine.id}.json`
|
// `${storage_dir}/${player}/${machine.id}.json`
|
||||||
`${storage_dir}/${name}.json`, afterRead
|
`${storage_dir}/${name}.json`, afterRead
|
||||||
// JSON.stringify(machine.toJSON),
|
// JSON.stringify(machine.toJSON),
|
||||||
);
|
);
|
||||||
function afterRead(err, jsonString) {
|
function afterRead(err, jsonString) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.warn("sm load sm: read file", err);
|
console.warn("sm load sm: read file", err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const machine = createMachine(JSON.parse(jsonString).config);
|
const machine = createMachine(JSON.parse(jsonString).config);
|
||||||
// TODO do tests and validation
|
// TODO do tests and validation
|
||||||
// 1. ensure default states [start, idle]
|
// 1. ensure default states [start, idle]
|
||||||
// 2. ensure closed cycle from start to idle
|
// 2. ensure closed cycle from start to idle
|
||||||
cfg.statemachine.list[machine.id] = machine;
|
cfg.statemachine.list[machine.id] = machine;
|
||||||
if (machine.id === cfg.statemachine.draft?.id) {
|
if (machine.id === cfg.statemachine.draft?.id) {
|
||||||
cfg.statemachine.draft = machine;
|
cfg.statemachine.draft = machine;
|
||||||
}
|
}
|
||||||
if (run) {
|
if (run) {
|
||||||
runSM(machine);
|
runSM(machine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// return run ? runSM(machine) : machine
|
// return run ? runSM(machine) : machine
|
||||||
// return machine
|
// return machine
|
||||||
}
|
}
|
||||||
// function isInterpreter(SMorService: StateMachine<any, any, any> | Interpreter<any>): SMorService is Interpreter<any> {
|
// function isInterpreter(SMorService: StateMachine<any, any, any> | Interpreter<any>): SMorService is Interpreter<any> {
|
||||||
// return (SMorService as Interpreter<any>).start !== undefined;
|
// return (SMorService as Interpreter<any>).start !== undefined;
|
||||||
// }
|
// }
|
||||||
function getSM(name = cfg.statemachine.draft || cfg.statemachine.recent, asService = false, quiet = false) {
|
function getSM(name = cfg.statemachine.draft || cfg.statemachine.recent, asService = false, quiet = false) {
|
||||||
const machine = typeof name === "string" ? cfg.statemachine.list[name]
|
const machine = typeof name === "string" ? cfg.statemachine.list[name]
|
||||||
: name;
|
: name;
|
||||||
if (!machine) {
|
if (!machine) {
|
||||||
console.warn("sm get: doesn't exist", name);
|
console.warn("sm get: doesn't exist", name);
|
||||||
cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`);
|
cfg.statemachine.quiet || bot.chat(`sm ${name} doesn't exist`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (asService) {
|
if (asService) {
|
||||||
const service = cfg.statemachine.running[machine?.id];
|
const service = cfg.statemachine.running[machine?.id];
|
||||||
if (!service) {
|
if (!service) {
|
||||||
quiet || console.warn("sm get: already stopped", machine);
|
quiet || console.warn("sm get: already stopped", machine);
|
||||||
quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`);
|
quiet || cfg.statemachine.quiet || bot.chat(`sm ${machine?.id} isn't running`);
|
||||||
return interpret(machine);
|
return interpret(machine);
|
||||||
}
|
}
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// return machine.machine ? machine.machine : machine
|
// return machine.machine ? machine.machine : machine
|
||||||
return machine;
|
return machine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function runSM(name = getSM(undefined, undefined, true), player // or supervisor?
|
function runSM(name = getSM(undefined, undefined, true), player // or supervisor?
|
||||||
, restart = false) {
|
, restart = false) {
|
||||||
if (!name)
|
if (!name)
|
||||||
return;
|
return;
|
||||||
const service = getSM(name, true, true);
|
const service = getSM(name, true, true);
|
||||||
if (!service)
|
if (!service)
|
||||||
return;
|
return;
|
||||||
const machine = service.machine;
|
const machine = service.machine;
|
||||||
if (!machine)
|
if (!machine)
|
||||||
return;
|
return;
|
||||||
switch (service.status) {
|
switch (service.status) {
|
||||||
case InterpreterStatus.Running:
|
case InterpreterStatus.Running:
|
||||||
if (!restart) {
|
if (!restart) {
|
||||||
console.warn("sm run: already running", service.id);
|
console.warn("sm run: already running", service.id);
|
||||||
quiet || bot.chat(`sm ${service.id} already running`);
|
quiet || bot.chat(`sm ${service.id} already running`);
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
stopSM(machine);
|
stopSM(machine);
|
||||||
case InterpreterStatus.NotStarted:
|
case InterpreterStatus.NotStarted:
|
||||||
case InterpreterStatus.Stopped:
|
case InterpreterStatus.Stopped:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn("sm run: unknown status:", service.status, service);
|
console.warn("sm run: unknown status:", service.status, service);
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (cfg.statemachine.debug || machine.meta?.debug) {
|
if (cfg.statemachine.debug || machine.meta?.debug) {
|
||||||
service.onTransition((state) => {
|
service.onTransition((state) => {
|
||||||
quiet || bot.chat(`sm trans: ${machine.id}, ${state.value}`);
|
quiet || bot.chat(`sm trans: ${machine.id}, ${state.value}`);
|
||||||
quiet || state.meta?.debug && bot.chat(`sm next events: ${machine.id}, ${state.nextEvents}`);
|
quiet || state.meta?.debug && bot.chat(`sm next events: ${machine.id}, ${state.nextEvents}`);
|
||||||
console.log("sm debug: trans", state.value, state);
|
console.log("sm debug: trans", state.value, state);
|
||||||
}).onDone((done) => {
|
}).onDone((done) => {
|
||||||
quiet || bot.chat(`sm done: ${machine.id}, ${done}`);
|
quiet || bot.chat(`sm done: ${machine.id}, ${done}`);
|
||||||
console.log("sm debug: done", done.data, done);
|
console.log("sm debug: done", done.data, done);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
cfg.statemachine.running[machine.id] = service;
|
cfg.statemachine.running[machine.id] = service;
|
||||||
cfg.statemachine.recent = machine;
|
cfg.statemachine.recent = machine;
|
||||||
service.start();
|
service.start();
|
||||||
// // TODO check if idle state is different (maybe?)
|
// // 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.value === machine.initialState.value, service.state);
|
||||||
console.log("sm run", service.state !== machine.initialState, machine.initialState);
|
console.log("sm run", service.state !== machine.initialState, machine.initialState);
|
||||||
// return machine
|
// return machine
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
function stopSM(name = getSM(), quiet = false) {
|
function stopSM(name = getSM(), quiet = false) {
|
||||||
let service = getSM(name, true, quiet);
|
let service = getSM(name, true, quiet);
|
||||||
if (!service)
|
if (!service)
|
||||||
return;
|
return;
|
||||||
const machine = service.machine;
|
const machine = service.machine;
|
||||||
switch (service.status) {
|
switch (service.status) {
|
||||||
case InterpreterStatus.NotStarted:
|
case InterpreterStatus.NotStarted:
|
||||||
case InterpreterStatus.Stopped:
|
case InterpreterStatus.Stopped:
|
||||||
console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]);
|
console.log("sm stop status", service.status, service.id, cfg.statemachine.running[service.id]);
|
||||||
// TODO check if any bugs
|
// TODO check if any bugs
|
||||||
case InterpreterStatus.Running:
|
case InterpreterStatus.Running:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn("sm stop: unknown status:", service.status);
|
console.warn("sm stop: unknown status:", service.status);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
service?.stop?.();
|
service?.stop?.();
|
||||||
cfg.statemachine.running[machine.id] = null;
|
cfg.statemachine.running[machine.id] = null;
|
||||||
delete cfg.statemachine.running[machine.id];
|
delete cfg.statemachine.running[machine.id];
|
||||||
// return machine
|
// return machine
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
function actionSM(action, name = getSM()) {
|
function actionSM(action, name = getSM()) {
|
||||||
if (!action) {
|
if (!action) {
|
||||||
return console.warn("sm action", action);
|
return console.warn("sm action", action);
|
||||||
}
|
}
|
||||||
let service = getSM(name, true, true);
|
let service = getSM(name, true, true);
|
||||||
if (service.status !== InterpreterStatus.Running)
|
if (service.status !== InterpreterStatus.Running)
|
||||||
return;
|
return;
|
||||||
// const machine = service.machine
|
// const machine = service.machine
|
||||||
service.send(action.toLowerCase());
|
service.send(action.toLowerCase());
|
||||||
}
|
}
|
||||||
function stepSM(command = "", ...message_parts) {
|
function stepSM(command = "", ...message_parts) {
|
||||||
let service = getSM(undefined, true);
|
let service = getSM(undefined, true);
|
||||||
if (!service)
|
if (!service)
|
||||||
return;
|
return;
|
||||||
if (!service.send) {
|
if (!service.send) {
|
||||||
console.warn("sm step: can't send", service.machine);
|
console.warn("sm step: can't send", service.machine);
|
||||||
// TODO start a temporary service to interpret
|
// TODO start a temporary service to interpret
|
||||||
quiet || bot.chat("sm: step doesn't support machines that aren't running yet");
|
quiet || bot.chat("sm: step doesn't support machines that aren't running yet");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (service?.status !== InterpreterStatus.Running) {
|
if (service?.status !== InterpreterStatus.Running) {
|
||||||
console.warn("sm step: machine not running, attempting start", service);
|
console.warn("sm step: machine not running, attempting start", service);
|
||||||
runSM;
|
runSM;
|
||||||
}
|
}
|
||||||
// const machine = service.machine
|
// const machine = service.machine
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "edit":
|
case "edit":
|
||||||
// maybe `edit <type>`, like `add`?
|
// maybe `edit <type>`, like `add`?
|
||||||
// where type:
|
// where type:
|
||||||
// context
|
// context
|
||||||
// action
|
// action
|
||||||
// timeout | all timeout
|
// timeout | all timeout
|
||||||
break;
|
break;
|
||||||
case "undo":
|
case "undo":
|
||||||
break;
|
break;
|
||||||
case "del":
|
case "del":
|
||||||
break;
|
break;
|
||||||
case "p":
|
case "p":
|
||||||
case "prev":
|
case "prev":
|
||||||
service?.send("PREV");
|
service?.send("PREV");
|
||||||
break;
|
break;
|
||||||
case "add":
|
case "add":
|
||||||
// maybe `add <type>`?
|
// maybe `add <type>`?
|
||||||
// where type:
|
// where type:
|
||||||
// context
|
// context
|
||||||
// action
|
// action
|
||||||
// timeout
|
// timeout
|
||||||
case "new":
|
case "new":
|
||||||
break;
|
break;
|
||||||
// case "with":
|
// case "with":
|
||||||
// console.log(this)
|
// console.log(this)
|
||||||
// stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1))
|
// stepSM(getSM(message_parts[0], true, true), ...message_parts.slice(1))
|
||||||
// break;
|
// break;
|
||||||
case "help":
|
case "help":
|
||||||
quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]");
|
quiet || bot.chat("![sm ]step [ p(rev) | n(ext) ]");
|
||||||
break;
|
break;
|
||||||
case "n":
|
case "n":
|
||||||
case "next":
|
case "next":
|
||||||
default:
|
default:
|
||||||
service?.send("NEXT");
|
service?.send("NEXT");
|
||||||
quiet || bot.chat("stepped");
|
quiet || bot.chat("stepped");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function tickSM(time = bot.time.timeOfDay, rate = 20) {
|
function tickSM(time = bot.time.timeOfDay, rate = 20) {
|
||||||
if (time % rate !== 0) {
|
if (time % rate !== 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("sm tick", rate, time);
|
console.log("sm tick", rate, time);
|
||||||
}
|
}
|
||||||
function debugSM(name = getSM()) {
|
function debugSM(name = getSM()) {
|
||||||
if (!name)
|
if (!name)
|
||||||
return;
|
return;
|
||||||
let service = getSM(name, true);
|
let service = getSM(name, true);
|
||||||
if (!service)
|
if (!service)
|
||||||
return;
|
return;
|
||||||
const machine = service.machine;
|
const machine = service.machine;
|
||||||
machine.meta.debug = !!!machine.meta.debug;
|
machine.meta.debug = !!!machine.meta.debug;
|
||||||
console.info("sm debug", machine.meta, service, machine);
|
console.info("sm debug", machine.meta, service, machine);
|
||||||
cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id);
|
cfg.statemachine.quiet || machine.meta.debug && bot.chat("sm debug: " + machine.id);
|
||||||
}
|
}
|
||||||
function command(message_parts) {
|
function command(message_parts) {
|
||||||
const message_parts2 = message_parts.slice(1);
|
const message_parts2 = message_parts.slice(1);
|
||||||
switch (message_parts[0]) {
|
switch (message_parts[0]) {
|
||||||
case "add":
|
case "add":
|
||||||
command(["new"].concat(message_parts2));
|
command(["new"].concat(message_parts2));
|
||||||
break;
|
break;
|
||||||
case "finish":
|
case "finish":
|
||||||
case "done":
|
case "done":
|
||||||
case "end":
|
case "end":
|
||||||
command(["save"].concat(message_parts2));
|
command(["save"].concat(message_parts2));
|
||||||
break;
|
break;
|
||||||
case "do":
|
case "do":
|
||||||
case "load":
|
case "load":
|
||||||
case "start":
|
case "start":
|
||||||
command(["run"].concat(message_parts2));
|
command(["run"].concat(message_parts2));
|
||||||
break;
|
break;
|
||||||
case "debug":
|
case "debug":
|
||||||
switch (message_parts[1]) {
|
switch (message_parts[1]) {
|
||||||
case "sm":
|
case "sm":
|
||||||
case "global":
|
case "global":
|
||||||
case "meta":
|
case "meta":
|
||||||
cfg.statemachine.debug = !!!cfg.statemachine.debug;
|
cfg.statemachine.debug = !!!cfg.statemachine.debug;
|
||||||
quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`);
|
quiet || bot.chat(`sm debug: ${cfg.statemachine.debug}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "new":
|
case "new":
|
||||||
case "save":
|
case "save":
|
||||||
case "run":
|
case "run":
|
||||||
case "step":
|
case "step":
|
||||||
case "stop":
|
case "stop":
|
||||||
// temp
|
// temp
|
||||||
case "action":
|
case "action":
|
||||||
switch (message_parts2.length) {
|
switch (message_parts2.length) {
|
||||||
case 0:
|
case 0:
|
||||||
console.warn(`sm ${message_parts[0]}: no name, using defaults`);
|
console.warn(`sm ${message_parts[0]}: no name, using defaults`);
|
||||||
case 1:
|
case 1:
|
||||||
// FIXME `this` doesn't work always
|
// FIXME `this` doesn't work always
|
||||||
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
|
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (["action"].includes(message_parts[0])) {
|
if (["action"].includes(message_parts[0])) {
|
||||||
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
|
(this.runSM && this || cfg.plugins.statemachine)[message_parts[0] + "SM"](...message_parts2);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2);
|
console.warn(`sm ${message_parts[0]}: more than 1 arg passed`, message_parts2);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "undo":
|
case "undo":
|
||||||
break;
|
break;
|
||||||
case "list":
|
case "list":
|
||||||
case "status":
|
case "status":
|
||||||
// TODO current/recent, updateRate
|
// TODO current/recent, updateRate
|
||||||
const { list, running } = cfg.statemachine;
|
const { list, running } = cfg.statemachine;
|
||||||
console.log("sm list", running, list);
|
console.log("sm list", running, list);
|
||||||
quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}`
|
quiet || bot.chat(`${Object.keys(running).length} of ${Object.keys(list).length} active: ${Object.keys(running)}`
|
||||||
+ (message_parts[1] === "all" ? `${Object.keys(list)}` : ''));
|
+ (message_parts[1] === "all" ? `${Object.keys(list)}` : ''));
|
||||||
break;
|
break;
|
||||||
case "quiet":
|
case "quiet":
|
||||||
quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet;
|
quiet = cfg.statemachine.quiet = !!!cfg.statemachine.quiet;
|
||||||
quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`);
|
quiet || bot.chat(`sm: ${cfg.statemachine.quiet ? "" : "not "}being quiet`);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// TODO general helper from declarative commands object
|
// TODO general helper from declarative commands object
|
||||||
quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`);
|
quiet || bot.chat(`sm help: !sm [new | step| save | run | list | quiet | debug]`);
|
||||||
console.warn("sm unknown command", message_parts);
|
console.warn("sm unknown command", message_parts);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
function load(config) {
|
function load(config) {
|
||||||
webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver;
|
webserver = cfg.statemachine.webserver = config.statemachine?.webserver || webserver;
|
||||||
config.statemachine = cfg.statemachine || {
|
config.statemachine = cfg.statemachine || {
|
||||||
webserver: null,
|
webserver: null,
|
||||||
// quiet: true,
|
// quiet: true,
|
||||||
quiet: false,
|
quiet: false,
|
||||||
list: {},
|
list: {},
|
||||||
running: {},
|
running: {},
|
||||||
draft: null,
|
draft: null,
|
||||||
recent: null,
|
recent: null,
|
||||||
// debug: null,
|
// debug: null,
|
||||||
debug: true,
|
debug: true,
|
||||||
updateRate: updateRate
|
updateRate: updateRate
|
||||||
};
|
};
|
||||||
cfg = config;
|
cfg = config;
|
||||||
bot = cfg.bot;
|
bot = cfg.bot;
|
||||||
// pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
// pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
||||||
// mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
|
// mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
|
||||||
init(undefined, webserver);
|
init(undefined, webserver);
|
||||||
updateRate = cfg.statemachine.updateRate || updateRate;
|
updateRate = cfg.statemachine.updateRate || updateRate;
|
||||||
bot.on('time', tickSM);
|
bot.on('time', tickSM);
|
||||||
// bot.once('time', tickSM, 5)
|
// bot.once('time', tickSM, 5)
|
||||||
console.log("sm load", cfg.statemachine);
|
console.log("sm load", cfg.statemachine);
|
||||||
}
|
}
|
||||||
function unload() {
|
function unload() {
|
||||||
const { list, running } = cfg.statemachine;
|
const { list, running } = cfg.statemachine;
|
||||||
bot.off('time', tickSM);
|
bot.off('time', tickSM);
|
||||||
Object.keys(running).forEach(sm => {
|
Object.keys(running).forEach(sm => {
|
||||||
stopSM(sm);
|
stopSM(sm);
|
||||||
});
|
});
|
||||||
// delete cfg.statemachine;
|
// delete cfg.statemachine;
|
||||||
cfg.statemachine = null;
|
cfg.statemachine = null;
|
||||||
console.log("sm unload: deleted", cfg.statemachine);
|
console.log("sm unload: deleted", cfg.statemachine);
|
||||||
}
|
}
|
||||||
module.exports = {
|
module.exports = {
|
||||||
load, unload, command, init,
|
load, unload, command, init,
|
||||||
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM
|
newSM, saveSM, loadSM, runSM, stopSM, actionSM, stepSM, debugSM
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user