Added experimental LCDGameClock

This commit is contained in:
walterhiggins 2014-01-24 23:38:56 +00:00
parent f71d1a4e78
commit 8c690452e7
6 changed files with 390 additions and 309 deletions

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
var console = require('./console'); var console = require('./console'),
var File = java.io.File; File = java.io.File,
var FileWriter = java.io.FileWriter; FileWriter = java.io.FileWriter,
var PrintWriter = java.io.PrintWriter; PrintWriter = java.io.PrintWriter;
/* /*
plugin management plugin management
*/ */

View file

@ -174,12 +174,12 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
wph 20131215 Experimental wph 20131215 Experimental
*/ */
var _loadedModules = {}; var _loadedModules = {};
var _format = java.lang.String.format;
var _require = function(parentFile, path) var _require = function(parentFile, path)
{ {
var file = resolveModuleToFile(path, parentFile); var file = resolveModuleToFile(path, parentFile);
if (!file){ if (!file){
var errMsg = '' + java.lang.String.format("require() failed to find matching file for module '%s' " + var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
"in working directory '%s' ", [path, parentFile.canonicalPath]); "in working directory '%s' ", [path, parentFile.canonicalPath]);
if (! ( (''+path).match(/^\./) )){ if (! ( (''+path).match(/^\./) )){
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths); errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************* /*************************************************************************
## sc-mqtt module ## sc-mqtt module
@ -86,11 +87,12 @@ function Client(brokerUrl, clientId){
brokerUrl = 'tcp://localhost:1883'; brokerUrl = 'tcp://localhost:1883';
} }
if (!clientId){ if (!clientId){
clientId = 'scriptcraft'; clientId = 'scriptcraft' + new Date().getTime();
} }
var client = new MqttClient(brokerUrl, clientId, null); var client = new MqttClient(brokerUrl, clientId, null);
client.setCallback(callback); client.setCallback(callback);
return { return {
connect: function(options){ connect: function(options){
if (typeof options === 'undefined'){ if (typeof options === 'undefined'){
client.connect(); client.connect();
@ -99,6 +101,15 @@ function Client(brokerUrl, clientId){
} }
return client; return client;
}, },
disconnect: function(quiesceTimeout){
if (typeof quiesceTimeout == 'undefined')
client.disconnect();
else
client.disconnect(quiesceTimeout);
return client;
},
publish: function(topic, message, qos, retained){ publish: function(topic, message, qos, retained){
if (typeof message == 'string'){ if (typeof message == 'string'){
message = new java.lang.String(message).bytes; message = new java.lang.String(message).bytes;
@ -112,28 +123,33 @@ function Client(brokerUrl, clientId){
client.publish(topic, message,qos, retained); client.publish(topic, message,qos, retained);
return client; return client;
}, },
subscribe: function(topic){ subscribe: function(topic){
client.subscribe(topic); client.subscribe(topic);
return client; return client;
}, },
unsubscribe: function(topic){ unsubscribe: function(topic){
client.unsubscribe(topic); client.unsubscribe(topic);
return client; return client;
}, },
onMessageArrived: function(fn){ onMessageArrived: function(fn){
callback.setMesgArrived(fn); callback.setMesgArrived(fn);
return client; return client;
}, },
onDeliveryComplete: function(fn){ onDeliveryComplete: function(fn){
callback.setDeliveryComplete(fn); callback.setDeliveryComplete(fn);
return client; return client;
}, },
onConnectionLost: function(fn){ onConnectionLost: function(fn){
callback.setConnLost(fn); callback.setConnLost(fn);
return client; return client;
} }
}; };
}; }
exports.client = function(brokerUrl, clientId, options){ exports.client = function(brokerUrl, clientId, options){
if (typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function'){ if (typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function'){

View file

@ -1,3 +1,4 @@
'use strict';
/************************************************************************* /*************************************************************************
## alias Plugin ## alias Plugin
@ -74,11 +75,14 @@ var _store = {
used for the 'set' and 'global' options. used for the 'set' and 'global' options.
*/ */
var _processParams = function(params){ var _processParams = function(params){
var paramStr = params.join(' '); var paramStr = params.join(' '),
var eqPos = paramStr.indexOf('='); eqPos = paramStr.indexOf('='),
var aliasCmd = paramStr.substring(0,eqPos).trim(); aliasCmd = paramStr.substring(0,eqPos).trim(),
var aliasValue = paramStr.substring(eqPos+1).trim(); aliasValue = paramStr.substring(eqPos+1).trim();
return { cmd: aliasCmd, aliases: aliasValue.split(/\s*;\s*/) }; return {
cmd: aliasCmd,
aliases: aliasValue.split(/\s*;\s*/)
};
}; };
var _set = function(params, player){ var _set = function(params, player){
@ -119,8 +123,8 @@ var _global = function(params, player){
}; };
var _list = function(params, player){ var _list = function(params, player){
try {
var alias = 0; var alias = 0;
try {
if (_store.players[player.name]){ if (_store.players[player.name]){
player.sendMessage("Your aliases:"); player.sendMessage("Your aliases:");
for (alias in _store.players[player.name]){ for (alias in _store.players[player.name]){
@ -142,6 +146,7 @@ var _list = function(params, player){
var _help = function(params, player){ var _help = function(params, player){
player.sendMessage('Usage:\n' + _usage); player.sendMessage('Usage:\n' + _usage);
}; };
var alias = plugin('alias', { var alias = plugin('alias', {
store: _store, store: _store,
set: _set, set: _set,
@ -152,7 +157,8 @@ var alias = plugin('alias', {
}, true ); }, true );
var aliasCmd = command('alias', function( params, invoker ) { var aliasCmd = command('alias', function( params, invoker ) {
var operation = params[0], fn; var operation = params[0],
fn;
if (!operation){ if (!operation){
invoker.sendMessage('Usage:\n' + _usage); invoker.sendMessage('Usage:\n' + _usage);
return; return;
@ -162,39 +168,29 @@ var aliasCmd = command('alias', function( params, invoker ) {
accessing object properties by array index notation accessing object properties by array index notation
in JRE8 alias[operation] returns null - definitely a bug in Nashorn. in JRE8 alias[operation] returns null - definitely a bug in Nashorn.
*/ */
if (operation == 'set'){ for (var key in alias){
alias.set(params.slice(1), invoker); if (key == operation){
}else if (operation == 'global'){ fn = alias[key];
alias.global(params.slice(1), invoker); fn(params.slice(1),invoker);
}else if (operation == 'remove'){ return;
alias.remove(params.slice(1), invoker);
}else if (operation == 'list'){
alias.list(params.slice(1), invoker);
}else if (operation == 'help'){
alias.help(params.slice(1), invoker);
}else {
invoker.sendMessage('Usage:\n' + _usage);
} }
}
invoker.sendMessage('Usage:\n' + _usage);
}); });
var _intercept = function( msg, invoker, exec) var _intercept = function( msg, invoker, exec)
{ {
if (msg.trim().length == 0) if (msg.trim().length == 0)
return false; return false;
var msgParts = msg.split(' '); var msgParts = msg.split(' '),
var command = msg.match(/^\/*([^\s]+)/)[1]; command = msg.match(/^\/*([^\s]+)/)[1],
template = [], isAlias = false, cmds = [],
var template = [], isAlias = false, cmds = []; commandObj,
filledinCommand;
if (_store.global[command]){ if (_store.global[command]){
template = _store.global[command]; template = _store.global[command];
isAlias = true; isAlias = true;
}else{
if (config.verbose){
var commandObj = server.commandMap.getCommand(command);
if (!commandObj)
console.info('No global alias found for command: ' + command);
}
} }
/* /*
allows player-specific aliases to override global aliases allows player-specific aliases to override global aliases
@ -204,19 +200,13 @@ var _intercept = function( msg, invoker, exec)
{ {
template = _store.players[invoker][command]; template = _store.players[invoker][command];
isAlias = true; isAlias = true;
}else{
if (config.verbose){
var commandObj = server.commandMap.getCommand(command);
if (!commandObj)
console.info('No player alias found for command: ' + command);
}
} }
for (var i = 0;i < template.length; i++) for (var i = 0;i < template.length; i++)
{ {
var filledinCommand = template[i].replace(/{([0-9]+)}/g, function (match,index){ filledinCommand = template[i].replace(/{([0-9]+)}/g, function (match,index){
index = parseInt(index,10); index = parseInt(index,10);
if (msgParts[index]) if (msgParts[index])
return msgParts[index] return msgParts[index];
else else
return match; return match;
}); });
@ -239,10 +229,10 @@ events.on('player.PlayerCommandPreprocessEvent', function(listener,evt){
var isAlias = _intercept(''+evt.message, ''+invoker.name, exec); var isAlias = _intercept(''+evt.message, ''+invoker.name, exec);
if (isAlias) if (isAlias)
evt.cancelled = true; evt.cancelled = true;
}); });
/* define a 'void' command because ServerCommandEvent can't be canceled */ /* define a 'void' command because ServerCommandEvent can't be canceled */
command('void',function(){}); command('void',function(){});
events.on('server.ServerCommandEvent', function(listener,evt){ events.on('server.ServerCommandEvent', function(listener,evt){
var invoker = evt.sender; var invoker = evt.sender;
var exec = function(cmd){ invoker.server.dispatchCommand(invoker, cmd); }; var exec = function(cmd){ invoker.server.dispatchCommand(invoker, cmd); };

View file

@ -0,0 +1,75 @@
/*
Experimental:
Point at a block and issue the following ...
/js var d = new Drone();
/js var clock = new LCDClock(d);
/js clock.start24();
... start the clock...
/js clock.stop24();
... stops the clock...
*/
var Drone = require('../drone').Drone;
var blocktype = require('../blocktype');
exports.LCDClock = function(drone, fgColor,bgColor,border) {
var lastSecs = [0,0,0,0],
world = drone.world,
intervalId = -1;
if (typeof bgColor == 'undefined')
bgColor = '35:15'; // black wool
if (typeof fgColor == 'undefined')
fgColor = 35 ; // white wool
if (border){
drone.box(border,21,9,1);
drone.up().right();
}
drone.blocktype('00:00',fgColor,bgColor);
return {
start24: function(){
var clock = this;
function tick(){
var rolloverMins = 24*60;
var timeOfDayInMins = Math.floor(((world.time + 6000) % 24000) / 16.6667);
timeOfDayInMins = timeOfDayInMins % rolloverMins;
console.log('Minecraft time: ' + world.time + ' timeOfDayInMins: ' + timeOfDayInMins);
clock.update(timeOfDayInMins);
};
intervalId = setInterval(tick, 800);
},
stop24: function(){
clearInterval(intervalId);
},
update: function(secs){
var digits = [0,0,0,0],
s = secs % 60;
m = (secs - s) / 60;
digits[3] = s%10;
digits[2] = (s-digits[3])/10;
digits[1] = m%10;
digits[0] = (m-digits[1])/10;
//
// updating all 4 digits each time is expensive
// only update digits which have changed (in most cases - just 1)
//
if (digits[3] != lastSecs[3])
drone.right(14).blocktype(''+digits[3],fgColor,bgColor).left(14);
if (digits[2] != lastSecs[2])
drone.right(10).blocktype(''+digits[2],fgColor,bgColor).left(10);
if (digits[1] != lastSecs[1])
drone.right(4).blocktype(''+digits[1], fgColor, bgColor).left(4);
if (digits[0] != lastSecs[0])
drone.blocktype(''+digits[0], fgColor, bgColor);
lastSecs[0] = digits[0];
lastSecs[1] = digits[1];
lastSecs[2] = digits[2];
lastSecs[3] = digits[3];
}
};
};