This commit is contained in:
JWo1F 2015-10-11 07:32:49 +03:00
parent 19b5c9a9cc
commit b51519e138

View file

@ -1,36 +1,33 @@
var Vec3 = require('vec3');
module.exports = inject;
function inject(serv, player, options) {
var hash = {};
class Command {
constructor(params, parent) {
constructor(params, parent, hash) {
this.params = params;
this.parent = parent;
this.child = [];
this.hash = parent ? parent.hash : {};
this.updateHistory();
}
static find(command) {
find(command) {
var res;
for(var key in hash) {
var space = hash[key].space(true);
for(var key in this.hash) {
var space = this.hash[key].space(true);
if(space) space += '?';
var ended = space + '(.*)';
var finded = command.match(new RegExp('^' + key + ended));
if(finded) {
res = [hash[key], finded];
res = [this.hash[key], finded];
}
}
return res;
}
static use(command) {
use(command) {
var res = this.find(command);
if(res) {
@ -39,8 +36,7 @@ function inject(serv, player, options) {
if(typeof parse == 'function') {
res[1] = parse(res[1][1]);
if(res[1] === false) {
player.chat(res[0].params.usage ? 'Usage: ' + res[0].params.usage : 'Bad syntax');
return;
return res[0].params.usage ? 'Usage: ' + res[0].params.usage : 'Bad syntax';
}
} else {
res[1] = res[1][1].match(parse);
@ -50,9 +46,9 @@ function inject(serv, player, options) {
}
res = res[0].params.action(res[1]);
if(res) player.chat('' + res);
if(res) return '' + res;
} else {
player.chat('Command not found');
return 'Command not found';
}
}
@ -69,13 +65,12 @@ function inject(serv, player, options) {
this.path = parentBase + this.space() + (command || all);
if(this.path == all && !this.parent) this.path = '';
if(this.path) hash[this.path] = this;
if(this.path) this.hash[this.path] = this;
});
}
add(params) {
var command = new Command(params, this);
this.child.push(command);
return command;
}
@ -86,7 +81,8 @@ function inject(serv, player, options) {
}
}
var base = new Command({ basic: true });
function inject(serv, player, options) {
var base = new Command({});
base.add({
base: 'help',
@ -94,9 +90,10 @@ function inject(serv, player, options) {
usage: '/help [command]',
action(params) {
var c = params[0];
var hash = base.hash;
if(c) {
var res = Command.find(params[0])[0];
var res = base.find(params[0])[0];
var help = res.params.help && res.params.help(params);
return help ? '' + help : 'Information not found';
@ -340,5 +337,8 @@ function inject(serv, player, options) {
serv.commands = base;
player.handleCommand = Command.use.bind(Command);
player.handleCommand = function(str) {
var res = base.use(str);
if(res) player.chat('' + res);
};
}