Tutorials/helloworldJs
From CubeiaWiki
Contents |
Introduction
This tutorial will replace the server code from the ordinary Hello World tutorial which was written in Java, with a script language instead. It will use JavaScript as this is already in place in Java 6, but will be easy to adapt to other languages such as Ruby or Groovy.
NB: PLease refer to the Hello World tutorial for details, this tutorial will be brief.
Prerequisites
Scripting is somewhat different than working in Java and one of the major differences is that you will need to fully install a Firebase server before you start.
Deploying the Script Support
The script suport is a unified archive (UAR) which you will have to deploy in your Firebase installation. You can find the latest UAR for download here:
At the time of writingt the script support is in version 1.0-RC.1 which you can download here. Simply copy that file to $FIREBASE_HOME/game/deploy and you're ready to go!
Writing the Server Game
First, create a new file called script-game.js in your deploy folder of the Firebase installation (if for example you have installed Firebase at "/usr/local/firebase" then the deploy folder is "/usr/local/firebase/game/deploy").
- The script support will look by default for a script called "script-game", this is configurable.
- The extension "js" will be matched against known script languages, in this case JavaScript. To install other languages, please look here.
Sending Messages
Open "script-game.js" in a text editor and add the following function to handle chat messages:
function handleDataAction(action, table) {
// there is a helper object called "_log"
// which is a log4j logger which we'll use
// for logging via Firebase
_log.debug('Entering handleDataAction');
// the "_support" objects contans some helpful
// methods for scripts to use, here we get the binary
// action data as a string
var data = _support.getActionDataAsUTF8(action);
// player id
var playerId = action.getPlayerId();
// create a new game data action and set the
// message, again using the "_support"
var outAction = _support.newGameDataAction(playerId, table);
_support.setActionDataAsUTF8(outAction, data);
// send action as you would normally
table.getNotifier().notifyAllPlayers(outAction);
// log exit
_log.debug('Exiting handleDataAction');
}
Please have a look at the above code which should be self-explanatory. The "_support" object is rather handy, and documented here.
Greeting Message
In the script support the so called table listener is handled by the same file as before, so open "script-game.js" again and add:
function playerJoined(table, player) {
// log enter
_log.debug('Entering playerJoined');
// compose greeting
var data = 'Greetings ' + player.getName() + '!';
// player id
var playerId = player.getPlayerId();
// create a new game data action and set the
// message, again using the "_support"
var outAction = _support.newGameDataAction(playerId, table);
_support.setActionDataAsUTF8(outAction, data);
// send action as you would normally
table.getNotifier().notifyPlayer(playerId, outAction);
// log exit
_log.debug('Exiting playerJoined');
}
Conclusion
You should now be able to start Firebase and test you game using the Hello World Flex Client! If something went wrong you do not need to restart Firebase, just edit the script file and it will be re-evaluated.
Good Luck!

