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