Page 1 of 1

[Solved] Custom room creation

Posted: 27 Oct 2010, 15:21
by jamesr
Hi,

i'm basically an actionscript developer, but need to add extra functionality to my app through an extension. I've spent the last 5 hours, reading the docs, looking at the Tris game example and reading the forum to get a better idea of how extensions work.

So this is what i'm trying to achieve. Nothing very exciting or even that complicated i should imagine, but i just want to clarify that i'm heading in the right direction. At the point of creation of a new room, i need to,

- set the removeMode to 'empty'

- dynamically create the supplied group if it does not exist and add it to the public group list, so that users can subscribe to it's events

- add a global room variable to the room


So firstly, what i'm wondering is, is it possible to intercept a CreateRoomRequest from the client - my first thought was to use addRequestHandler in a Zone extension and listen for the room create request, but this does not appear to be possible? - or do i need to add a request handler for a custom create room event and then use an ExtensionRequest in my client to call this to create my custom room?


Cheers,

James

Posted: 28 Oct 2010, 05:52
by Lapo
It is very simple, instead of calling CreateRoom from client you send a custom request to the Extension which in turn performs the job.

Posted: 28 Oct 2010, 12:35
by jamesr
Thanks Marco, i now have my first extension up and running!

Posted: 28 Oct 2010, 19:48
by sHTiF
Lapo when room is created with the removeMode WHEN_EMPTY does it mean that it will be removed after someone joins and leaves? Since upon room creation there is obviously no one there so it should get removed right after creation.

Other question related to this one, does WHEN_EMPTY room get removed when its creator is null? Because so far all my rooms without specified creator do persist even after the last user left.

Posted: 29 Oct 2010, 06:07
by Lapo
Since upon room creation there is obviously no one there so it should get removed right after creation.
No of course this is not going to happen. Also you can decide to join the creator at the time of the Room creation ( from client )
Other question related to this one, does WHEN_EMPTY room get removed when its creator is null?
Yes.
Make sure that Rooms have the "isDynamic" flag set to true, that's necessary as it indicates that the Room is not static.

Posted: 29 Oct 2010, 08:17
by sHTiF
Thanks.

Please post , example code for the room create extension

Posted: 12 Nov 2010, 00:27
by mmilen
Would you please post example code for the room create extension.

Posted: 12 Nov 2010, 07:39
by Lapo
Check the provided examples source code, for instance see the SFS2X Tris game

Posted: 13 Nov 2010, 18:43
by bart4president.com
You know Lapo I have been looking through the tris2X extension code and to tell you the truth, I find it hardly helpful in this particular case.

For instance.. we have documentation here:
http://docs2x.smartfoxserver.com/Advanc ... s/game-api

yet in the Tris2X extension code.. i have found not too much of the sort that uses this method (thats advertised). Perhaps you can point us in the right direction?

The main confusion is that there seems to be several different ways to connect and match up our users to games and we're getting confused on which is the best way.

As for anyone that has created a successful working extension that does what the OP is suggesting, it would be most useful for us to learn from.

Thanks!

Posted: 15 Nov 2010, 08:01
by y_nk
Hey there,

I'm in the same need :)

So i looked at the Tris server-side example and did not found the good way to create a room.

I tried those 2 ways :

1/ get the server instance, get the zone manager and call myZone.addRoom with a new Room() object.

2/ get the server instance, the zone manager and call myZone.createRoom with a new CreateRoomSettings() object.

Both are throwing an exception (which i catched and logged with a simple trace), so i dont know what to do exactly.


Thanks for helping !



===============================================

UDPATE:

I went through my problem with trying again the addRoom method. Now i can see my brand new rooms in the admin panel... but, i'm unable to join any of them.

Also, when i try to monitor them, the admin tool pops an error message :
"Field not found: flags. Also no public getter found"

The console of SmartFox gives this error :
10:42:22,142 ERROR [com.smartfoxserver.v2.controllers.ExtensionController-1] Extensions - {Admin}: Unable to parse room parameters descriptor; the following error was reported: 'Field not found: flags. Also no public getter found'

I'm gonna look for this "Flags" field in the doc.

Posted: 15 Nov 2010, 09:23
by y_nk
Ok i got it. (i still post because i could be usefull for people like me, newbie in java and in the need to create rooms serverside).

I first tried myRoom.setFlag() with each SFSRoomSettings enum and a boolean, which didnt work. It was throwing the same exception everytime.

So i created a HashSet<SFSRoomSettings> and used the myRoom.setFlags() instead which takes a Set<SFSRoomSettings>.

I can now see my rooms in the admin panel. With a simple myRoom.addUser() i can also see my user in the room. By the way, the client-side "ROOM_JOIN" event doesnt look to be fired properly with this kind of joining... looking forward :)

Posted: 15 Nov 2010, 13:47
by Lapo
Wow... there's a bit of confusion here with everyone responding to my posts as if the original question was theirs. It looks like everyone here is looking for slightly different things.

Let me first say that it is important to follow the docs which in turn will point you immediately to the SFSApi class which is the single most important class for server side development.

SFSApi contains all the main functionalities such as joining, creating Rooms, variables and whatnot.

In fact the SFSExtension class provides a convenient getAPI() method that you can use to immediately access the framework.

Please make sure to read this article:
http://docs2x.smartfoxserver.com/Advanc ... extensions

@y_nk
No that's not very correct. You should pass the Room Flags upon creation of the Room via the SFSApi.createRoom(...) method,

Posted: 15 Nov 2010, 13:51
by y_nk
thanks, i'm gonna check SFSApi right now

Posted: 15 Nov 2010, 15:40
by tpenn
Lapo, you can't be surprised when people think they should be calling SFSRoom.setFlags when it is a publicly accessible function which is in the documentation.

It seems to me by reading on the forums here that one of the most common problems developers are having is not using SFSApi. There is a serious problem that the javadoc is absolutely full of functions that we shouldn't be calling as there are SFSApi functions that we should use. Somehow that has to be made more clear.

As long as the docs contain things like SFSZone.createRoom, SFSRoom.addUser, and SFSUser.setVariable, people are going to mistakenly call them instead of SFSApi.createRoom, SFSApi.joinRoom, and SFSApi.setUserVariables and then wonder why things don't work right.

My opinion is that unless there is a reason for us to call them, functions should not be public to us.

Posted: 15 Nov 2010, 17:21
by Lapo
You are touching on a very important issue here. The javadoc does not allow that :( It's been asked for decades, still it's not possible to exclude methods from the docs.
It all relates to a pretty clumsy and extremely rigid way of the Java language to organize packages.

Another consideration however is that you cannot jump into coding without taking a moment to review the basic documentation. The framework has its own architecture and logic and developers need to take a bit of time and learn how this works.

Would you imagine jumping into Hibernate or any other non trivial java framework without having an overview of the library and taking a look at the docs and examples?

If you just take a look at that one document that we keep linking here you will have more clear where to start and what to do.
Also those public methods can be useful on a few occasions but they are not your starting point.

Of course we'll need to be able to make it clear enough for everyone :)

Thanks