So I had a room level extension, and I've switched all of my setInterval in my extension to the scheduler, and now I have a few question, since I'm not sure I've done it in the most officiant way.
In my old extension, I have used the setInterval to time turns, delay phases of the game, and such, and I did make sure not to have more the one setInterval at a time inside the room, but since each setInterval takes a thread, it means that for 500 rooms, I would have 500 threads working only on this issue.
So what I did inside my extension is this:
Code: Select all
var _scheduler = Packages.it.gotoandplay.smartfoxserver.util.scheduling
var scheduler = null
var taskHandler;
var task;
function init(){
scheduler = new _scheduler.Scheduler();
scheduler.startService();
var handlerObj = {};
handlerObj.doTask = function(task){
handle_tasks(task);
}
taskHandler = new _scheduler.ITaskHandler(handlerObj);
}
function handle_tasks(task){
task.active = false;
var task_name = task.id.name;
var task_params = task.id.params;
if(task_name == "xxx"){
do_something(task_params);
}
}
function destroy(){
kill_task();
scheduler.destroy(null);
}
function set_task(name,params,time){
if(task){
task.active = false;
}
task = new _scheduler.Task({name:name, params:params});
scheduler.addScheduledTask(task, time, false, taskHandler);
}
function kill_task(){
if(task){
task.active = false;
}
}
And to explain, since I need only one task running at a time (no general game timer, plus turn time and such), but one task at a time that will activate one function on it's execution, I didn't use a different task for each action, but a single task I can turn off (with kill_task), and when I'm in need of a new task, if there is already a task defined, I kill that and reset the task.
I do this since the bigger part of my tasking need is to wait for a client response, kill the waiting task if the client responded in the allotted time, or execute a function if he didn't, so when a client response, I can kill the task, switch the turn, and start a new waiting task for the next player, and so on.
Now this all works perfectly, no problem, but my fear is that since I'm instancing ("scheduler = new _scheduler.Scheduler();") a new scheduler in the room level, does it not mean that I will have 500 instances taking 500 threads, if I have 500 rooms with this code in them? or does the scheduler object work that is does not instance a new copy of itself when I instance it like that in the room level, but just create a reference to itself in that object, and in the end it is still just one general scheduler, taking just the one thread??
And if it's like I fear it is, what should I do? should I instance my scheduler just once on the zone level, then direct all calls to it from inside the room extension, and then when it fires, I will redirect the event to the specific room that initiated the task?
Thank.
Yuval Lahav.