Scripts are growing and become confusing. For example the current scripts.js available from here has over 3000 lines of code. While some parts are interesting, others are not. I wish that there is a way to split up scripts. Some ideas: * A directory for scripts, all files inside are loaded * A function to include other scripts / snippets As a long-term quest, there could be a repository with snippets and feature collections.
Nice suggestion. This could be doable by creating sys.import(file, module). Then myengine.globalObject.setProperty(module, myengine.evaluate(readFromFile(file))). Actually, I did it (on branch devel). Code (text): sys.import("tour", "tournament.js"); //loads scripts/tournament.js and set it as the global tour property. tour.doSomething(); The imports can be placed before the ({ }) script object, or inside the code. Anyhow, branch devel, untested yet, and not definitive (I don't know if the order of the arguments should be reversed), but will be there for next version. As for the repository of script snippets, I count on the community of scripters to do that ;O.
I would prefer Code (text): var tour = sys.import("tournaments.js"); actually ^ Don't know if there should be some magic, you could do that import function in JS too.. Code (text): var import = function(filename) { return eval(sys.getFileContents(filename)); } var tour = import('tournaments.js') and I actually used this a long time ago as a proof of separating stuff, but I actually added some pre-defined callbacks to the modules. PO will need them too if we want to make a script snippets library.
I'm just merging my separate files before adding them to PO. Code (makefile): RESULT = result all: cat *.js > $(RESULT)/scripts.js clean: rm -f $(RESULT)/scripts.js :}
When I checked out the po-sever-goodies this week, I had to cry - the scripts become more and more bloated. I still love the idea of a snippets / plugins / script library. For example, I am interested in the checkbot, but not at all in mafia, casino and other games. Here is a small proposal for script plugins (pseudo code of course): Code (text): scripts.js MAIN: importPlugin('common.js'); var mafia = importPlugin('mafia.js'); var tournament = importPlugin('tournament.js'); var checkbot = importPlugin('checkbot.js'); importPlugin(string file) { plugin = load(file); plugin.init(); // http://tetlaw.id.au/view/blog/signals-and-slots-for-prototype-easy-custom-javaplugin-events/ // http://13thparallel.com/archive/sig-slots/ foreach(name in plugin.getHooks()) Signal.connect(script, name, plugin, name); GLOBAL.plugins.add(plugin); return plugin; } commandReceived(command, data) { // Simple and stupid foreach(GLOBAL.plugins) plugin.processCommand(command, data); // Cooler, further research neccessary to check if it is possible? Signal.emit(command + "Command", data); } Displaying Commands and Rules: get them from each plugin and show alltogether (maybe with headings where they come from) *** RULES *** Global * 1 * 2 * 3 Mafia Game (script->name) * 1 * 2 * 3 *** COMMANDS *** Global cmd1 Arguments: Description Mafia Game cmd2 Arguments: Description ================================================= class Plugin { string name; void init() { // Initialize variables, ensure files, etc ... } string[][] getCommands() { return array[ 'user': [ ['command' : 'description'], ['command' : 'description'], ], 'mod': [ ['command' : 'description'], ['command' : 'description'], ] ]; } string[] getHooks() { return array[ 'afterChangeTeam', 'afterLogin' ]; } string[] get Rules() { return array[ 'Be nice', 'Dont cheat', 'Have a nive day' ]; } // note: different plugins should be able to bool processCommand(string command, string data) { switch(command) { case 'sample': return sampleCommandHandler(data); } // Maybe using reflection? var method = command + "CommandHandler"; if( Reflection.methodExists(this, method) ) { return Reflection.invoke(this, method); } return false; // no command handler / command not processed } function sampleCommandHandler(string data) { } function afterChangeTeam() { if(invalid) sys.stopEvent(); // If solution above is not possible, return true (continue event) / false (stop event) and check in main } } What do you think about that?
I did a similar test earlier using pure Javascript, but it becomes a bit bloaty: http://valssi.fixme.fi/~lamperi/pokemononline/plugin_test.zip And I feel like being eavedropped, as we discussed with coyotte yesterday to separate mafia, tournaments and tier checks into their own files x)