http://www.jjplusplus.com/projects/drawr and below is the code for the Javascript room module. Thanks for your help!
The code does run through the first part of the setInterval function, firing the first method it runs into (right now that's newWord() ) and then stopping, not running any code after it or firing again after 10 seconds.
-Jj
- Code: Select all
/*
--------------------------------------------------
IMPORT UNION CLASSES FROM SERVER
--------------------------------------------------
*/
importClass(net.user1.union.core.event.RoomEvent);
importClass(net.user1.union.api.Client);
importClass(org.apache.log4j.Logger);
importClass(net.user1.union.core.attribute.Attribute);
importPackage(java.lang);
importPackage(java.io);
importPackage(java.net);
importPackage(org.dom4j.io);
/*------------------------------
Necessary room module variables
--------------------------------
Modules will be passed a ModuleContext by the server which is the starting point of interacting with the server.
Use the ModuleContext to get a reference to the server which gives you access to rooms, clients and much more.
Room modules can also get a direct reference to the room with which they are associated through the getRoom() method.
*/
var moduleContext;
var intervalID;
//gets the context of the object that triggered this module
var log = Logger.getLogger("GreeterBot"); // where "union" should match what's in log4j.properties
//the room has a "wrapper", which we use to register for events
var wrapper;
//An array of users currently in the room
var currentUsers = [];
//WTF IS GOING ON WITH CURRENT USER INDEX?!
var currUserIndex = -1;
//an array of pre-defined words to use in the game
var wordsArray = [
"pirate",
"apple",
"alligator",
"code",
"spaceship",
"ucode",
"shrimp",
"playboy",
"sex",
"iphone"
];
/* OBJECT "PLAYA"
** object representation of a player in the room.
** properties ID, SCORE, and GETSCORE
*/
function Playa (num) {
this.id = num;
this.score = 0;
this.getScore = function getScore() {
return this.id + '|' + this.score;
};
}
// This method must be implemented by every room module script.
function init(ctx, wrap) {
log.debug(" INIT");
moduleContext = ctx;
wrapper = wrap;
// Set the currentDrawr attribute to "" by default
moduleContext.getRoom().setAttribute("currentDrawr",
"",
Attribute.SCOPE_GLOBAL,
Attribute.FLAG_SHARED | Attribute.FLAG_PERSISTENT);
moduleContext.getRoom().setAttribute("currentWerd",
"",
Attribute.SCOPE_GLOBAL,
Attribute.FLAG_SHARED | Attribute.FLAG_PERSISTENT);
// This is a script, so we have to register for
// events through the room's wrapper.
wrapper.addRoomEventListener(RoomEvent.ADD_CLIENT, "onRoomAddClient");
wrapper.addRoomEventListener(RoomEvent.REMOVE_CLIENT, "onRoomRemoveClient");
wrapper.addRoomEventListener(RoomEvent.MODULE_MESSAGE, "onModuleMessage");
/*
######################### GAME LOOP #####################################################
Call a 20 second timer for match setup
Start the word loop
*/
var intervalID = setInterval(setRound, 6000);//END OF SETINTERVAL GAME
}//END OF INIT FUNCTION
function setRound() {
log.debug(" currUserIndex is: " + currUserIndex);
log.debug(" length of currentUsers: " + currentUsers.length);
if (currUserIndex >= currentUsers.length) {
currUserIndex = 0;
}
//whoseTurn(currUserIndex); // set new news item into the ticker
var werdlength = wordsArray.length;
log.debug(" length of the array is" + werdlength);
var whichNum = (Math.random()*10) + 1;
log.debug(" length of the array is" + whichNum);
var floorNum = Math.floor(whichNum);
log.debug(" whichnum is: " + floorNum);
var newWerd = wordsArray[floorNum];
log.debug(" newWerd is: " + newWerd);
newWord(newWerd);
//pick a new word to send to users from the words array
//get the length of the array
wordsLength = wordsArray.length();
log.debug(" " + wordsLength);
//whichWord is the index of the wordsArray of a length between 1 and the length of the array
var whichWord = wordsArray[Math.floor((Math.random()* wordsLength)+1)];
log.debug(whichWord);
var cULength = currentUsers.length;
for ( i = 0; i < currentUsers.length; i++) {
var playerID = currentUsers[i].id;
var playerScore = currentUsers[i].score;
//can i send a whole object and interpret it on the other side?
var playerObj = currentUsers[i];
var serverMessage = playerObj.id + " " + playerObj.score;
log.debug(" playerID: " + playerID);
log.debug(" playerScore: " + playerScore);
log.debug(" playerObj: " + playerObj);
log.debug(" playerObjID: " + playerObj.id);
log.debug(" playerObjScore: " + playerObj.score);
log.debug(" server Message is: " + serverMessage);
moduleContext.getRoom().sendMessage("PLAYER_SCORES", serverMessage);
}
}
function onModuleMessage(e) {
log.debug(" Module message received.");
log.debug(" From client: " + e.getClient().getClientID());
log.debug(" Message name: " + e.getMessage().getMessageName());
log.debug(" testArg1's value: " + e.getMessage().getArg("testArg1"));
log.debug(" testArg2's value: " + e.getMessage().getArg("testArg2"));
log.debug(" e is: " + e);
// Print a list of all module message arguments in the log
log.debug(" All message arguments:");
var args = e.getMessage().getArgs();
var argIterator = args.entrySet().iterator();
var arg;
while (argIterator.hasNext()) {
arg = argIterator.next();
log.debug(" " + arg.getKey() + " = " + arg.getValue());
}
//NOT SURE ABOUT ANY OF THIS
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var msgName = e.getMessage().getMessageName();// iWin
var senderID = e.getClient().getClientID();// 5
var cULength = currentUsers.length;
for ( i = 0; i <= currentUsers.length; i++) {
var playerID = currentUsers[i].id;
log.debug(" playerID: " + playerID);
if (playerID == senderID) {
log.debug(" Made it into the player's object, and the players CU index is " + i);
currentUsers[i].score = currentUsers[i].score + 100;
log.debug(currentUsers[i].score);
} else if (playerID == "lobster") {
log.debug(" lobster");
} else {
log.debug(" else");
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//client 5 is probably currentUsers[1]
//I need to navigate to the index of currentUsers
//that has an object with an id property that equals 5
//var inUserArray = currentUsers[senderID];// undefined
//var thisGuysScore = inUserArray.score;
//log.debug("senderID: " + senderID);
//log.debug("length of CurrentUsers " + currentUsers.length);
// if (msgName == "iWin") {
//thisGuysScore = thisGuysScore + 100;
// }
}
function whoseTurn(currentDrawrIndex) {
log.debug(' in whoseTurn, currentDrawrIndex is: ' + currentDrawrIndex);
if(currentDrawrIndex == -1) {
currentDrawrIndex = 0;
}
var currentDrawrID = currentUsers[currentDrawrIndex].id;
log.debug(' in whoseTurn, currentDrawrID is: ' + currentDrawrID);
moduleContext.getRoom().sendMessage("WHOSE_TURN", currentDrawrID);
//log.debug(' made it past the first sendMessage');
//set room attribute from the room module
moduleContext.getRoom().setAttribute("currentDrawr",
currentDrawrID,
Attribute.SCOPE_GLOBAL,
Attribute.FLAG_SERVER_ONLY | Attribute.FLAG_SHARED);
log.debug(' made it past the setAttribute call');
}
function newWord(newWerd) {
moduleContext.getRoom().setAttribute("currentWerd",
newWerd,
Attribute.SCOPE_GLOBAL,
Attribute.FLAG_SERVER_ONLY | Attribute.FLAG_SHARED);
}
/* FUNCTION ONROOMADDCLIENT
* arg "evt"; the object that evoked the event listener, in this case, the app room
* Sends a welcome message to all clients when a new user joins the room
*
*/
function onRoomAddClient(evt) {
var thisUser = evt.getClient().getClientID();
var args = ["Welcome Guest" + evt.getClient().getClientID()];
//orbiter.getClientManager().sendMessage("CHAT_MESSAGE", [ e.getClientID() ], null, "Private hi to you!");
//chatRoom.sendMessage("CHAT_MESSAGE", true, null, outgoingMessages.text);
//moduleContext.getRoom().sendMessage("CHAT_MESSAGE", thisUser, args);
var newClientID = evt.getClient().getClientID();
var user = new Playa(newClientID);
//newClientID.points = 0;
currentUsers.push(user);
var arrayContents = currentUsers.toString();
var arrayContents = "" + currentUsers;
log.debug(" currentUsers on add : " + arrayContents);
//moduleContext.getRoom().sendMessage("CHAT_MESSAGE", thisUser, currentUsers);
//moduleContext.getRoom().sendMessage("CHAT_MESSAGE", thisUser, "POOOOOOOO0000000000000000000000P!!!");
}
function onRoomRemoveClient(evt) {
//get the ID of the client being removed
var oldClientID = evt.getClient().getClientID();
//search through the currentUsers array for the item with the client's ID in it.
var thisUserIndex = currentUsers.indexOf(oldClientID);
//splice the user from the array
currentUsers.splice(thisUserIndex);
var arrayContents = currentUsers.toString();
var arrayContents = "" + currentUsers;
log.debug(" after user removed, currentUsers is: " + arrayContents);
}
// This method must be implemented by every room module script.
function shutdown() {
wrapper.removeRoomEventListener(RoomEvent.ADD_CLIENT, "onRoomAddClient");
wrapper.removeRoomEventListener(RoomEvent.REMOVE_CLIENT, "onRoomRemoveClient");
//clearInterval(intervalID);
}
//==============================================================================
// setInterval()/clearInterval() IMPLEMENTATION
//==============================================================================
// JavaScript's setInterval() and clearInterval() functions are not included
// with Rhino, so implement custom versions
var timers = new Array();
var setInterval = function (fn, time) {
var num = timers.length;
var isRunning = true;
var timerObj = {
thread: new java.lang.Thread(new java.lang.Runnable({
run: function () {
while (isRunning) {
log.debug(' running running running');
java.lang.Thread.currentThread().sleep(time);
if (isRunning) {
fn();
}
}
}
})),
stop: function () {isRunning = false;}
};
timers[num] = timerObj;
timers[num].thread.start();
return num;
};