Timer for each room created

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Devops
Posts: 23
Joined: 27 May 2020, 15:23

Timer for each room created

Postby Devops » 04 Jun 2020, 14:39

Hi,
Players can join the room only for 60 seconds and post which I will be adding bots to the room if there are insufficient players in the room and game will start.
I want to run a timer here for each room created and track the timer for each room created to check end of timer.

I looked into taskscheduler

Code: Select all

        taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new TimeRunner(), 0, 1, TimeUnit.SECONDS);


But I will have to run a task scheduler for each room created and I believe it is not a efficient approach.
So how do I deal with this scenario and run a timer for each room? I would need room references as well to add bots to the room
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Timer for each room created

Postby Lapo » 04 Jun 2020, 15:15

Hi,
you don't need to create Scheduler for each Task, it would be a big waste. Instead you can just run one Scheduler (which already exists) and add all the Tasks there.

A single Scheduler can handle 100s or even 1000s of tasks, especially if the code in these task is not particularly CPU intensive or it stops the current thread waiting on some slow I/O.

You can configure the Scheduler's thread to a higher value, like 20 under AdminTool > Server Configurator > Server task scheduler thread pool size

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Timer for each room created

Postby Devops » 04 Jun 2020, 15:44

Consider I will be running just 1 scheduler and it will run every 1 second.

So my task for every second, is to get all the rooms in the zone and and check the room variable of every room which has the time when joining into room should be stopped.
In my game, rooms will keep on creating dynamically with every new game and the room list is going to be huge and might be more than a 100000+ rooms.
So at such times this might create a problem as all these rooms will be fetched and looped one by one to check the time. Isn't it? Is there any other way other than looping through all these rooms?
All these 100000+ rooms should be processed within 1 second because if time taken is more than a second then a room that is supposed to be closed in 60th second might get closed at 65th second.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Timer for each room created

Postby Lapo » 05 Jun 2020, 06:51

Isn't 100K Rooms a bit exaggerated?
Considering that every Room should have at least 1 player are you planning to run 100-200K players on the same instance?

I think you're trying to predict scalability too far away in the future. Sure you may end up with 100 of 1000s of Rooms one day but that's a "problem" you will have to adapt to gradually as the traffic grows. You will probably need multiple SFS instances at that point.

For now concentrate on a reasonable scale. As I mentioned you can use one Scheduler to deal with 1000s of tasks at once, unless they are very CPU intensive. Additionally if you need to ramp up the number of Tasks you can simply add more threads to the main Scheduler, even hundreds (or thousands) of threads, if it's necessary and your hardware can support it.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Timer for each room created

Postby Devops » 05 Jun 2020, 07:20

Okay thanks for the update.

I was considering that rooms which are created dynamically will exist forever in the smartfox even if no players are there in the room.
Considering this scenario I meant 100k+ rooms.

- Does the rooms created dynamically at runtime get deleted automatically after all players leave the room?

Code: Select all

            taskHandle = SmartFoxServer.getInstance().getTaskScheduler().scheduleAtFixedRate(new TaskRunner(),

- I have a doubt here, If the above line is executed 5 times for 5 rooms created, does it mean it is running 5 task scheduler?

Code: Select all

getRoomList

- Will this API get all the active rooms in zone where there are players inside the room?
Or will it be giving list of rooms that does not have any players in room too?
I want to get list of rooms which is active(has players inside the room).
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Timer for each room created

Postby Lapo » 06 Jun 2020, 07:37

Devops wrote:- Does the rooms created dynamically at runtime get deleted automatically after all players leave the room?

By default yes, but you can change the behavior if you wish.
Read the details here:
http://docs2x.smartfoxserver.com/Develo ... chitecture
(see at the bottom)

Code: Select all

            taskHandle = SmartFoxServer.getInstance().getTaskScheduler().scheduleAtFixedRate(new TaskRunner(),

- I have a doubt here, If the above line is executed 5 times for 5 rooms created, does it mean it is running 5 task scheduler?

No you're submitting 5 Tasks to the same scheduler.
Every time you call this -> SmartFoxServer.getInstance().getTaskScheduler() you get the same Scheduler.

Code: Select all

getRoomList

- Will this API get all the active rooms in zone where there are players inside the room?

No, as per the method documentation, this call returns the list of Rooms that exist in a Zone.

I want to get list of rooms which is active(has players inside the room).

Loop through the list returned by the method and check which Rooms are empty and which aren't. ( Room.isEmpty() )

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 130 guests