2014-01-02 19:46:46 +01:00
|
|
|
'use strict';
|
2013-12-30 02:07:41 +01:00
|
|
|
/*
|
|
|
|
command management - allow for non-ops to execute approved javascript code.
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _commands = {},
|
|
|
|
_cmdInterceptors = [];
|
2013-12-30 02:07:41 +01:00
|
|
|
/*
|
|
|
|
execute a JSP command.
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
var executeCmd = function( args, player ) {
|
|
|
|
var name,
|
|
|
|
cmd,
|
|
|
|
intercepted,
|
|
|
|
result = null;
|
|
|
|
|
|
|
|
if ( args.length === 0 ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
throw new Error('Usage: jsp command-name command-parameters');
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
name = args[0];
|
|
|
|
cmd = _commands[name];
|
|
|
|
if ( typeof cmd === 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
// it's not a global command - pass it on to interceptors
|
2014-01-29 20:49:15 +01:00
|
|
|
intercepted = false;
|
|
|
|
for ( var i = 0; i < _cmdInterceptors.length; i++ ) {
|
|
|
|
if ( _cmdInterceptors[i]( args, player ) )
|
2014-01-25 00:38:56 +01:00
|
|
|
intercepted = true;
|
2013-12-30 02:07:41 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( !intercepted ) {
|
|
|
|
console.warn( 'Command %s is not recognised', name );
|
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
}else{
|
|
|
|
try {
|
2014-01-29 20:49:15 +01:00
|
|
|
result = cmd.callback( args.slice(1), player );
|
|
|
|
} catch ( e ) {
|
|
|
|
console.error( 'Error while trying to execute command: ' + JSON.stringify( args ) );
|
2014-01-25 00:38:56 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
return result;
|
2013-12-30 02:07:41 +01:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
define a new JSP command.
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
var defineCmd = function( name, func, options, intercepts ) {
|
2014-06-14 16:38:41 +02:00
|
|
|
|
|
|
|
if ( typeof name == 'function'){
|
|
|
|
intercepts = options;
|
|
|
|
options = func;
|
|
|
|
func = name;
|
|
|
|
name = func.name;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( typeof options == 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
options = [];
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
_commands[name] = { callback: func, options: options };
|
|
|
|
if ( intercepts ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
_cmdInterceptors.push(func);
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
return func;
|
2013-12-30 02:07:41 +01:00
|
|
|
};
|
2014-01-23 00:57:27 +01:00
|
|
|
exports.command = defineCmd;
|
2013-12-30 02:07:41 +01:00
|
|
|
exports.commands = _commands;
|
2014-01-23 00:57:27 +01:00
|
|
|
exports.exec = executeCmd;
|