QuickJoinGameRequest - Working Example Project?

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

QuickJoinGameRequest - Working Example Project?

Postby Rashomon » 27 Mar 2012, 19:02

Is there a working example project (preferably Flash) that uses QuickJoinGameRequest? I can't find one in the sample games.

I want to let the user click a button to join an existing game, or create a new game if one does not exist. I've been unable to get this to work from reading the docs. I'd rather dig through a working example than keep bothering the forums.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: QuickJoinGameRequest - Working Example Project?

Postby Bax » 28 Mar 2012, 08:29

What is not clear in the documentation? If you check the ASDoc of the QuickJoinGameRequest class you will also find an example.
Paolo Bax
The SmartFoxServer Team
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Re: QuickJoinGameRequest - Working Example Project?

Postby Rashomon » 28 Mar 2012, 20:29

From the docs:

Quick Join Games

Another feature offered by the Game API (both client and server side) is the QuickJoinGame feature. By providing a MatchExpression and a list of Rooms (of type SFSGame) or a Room Group the system can search for matching Rooms and immediately teleport the player in the game action.
As usual an example will clarify the concept:


Code: Select all

// Prepare a match expresison
var expr:MatchExpression = new MatchExpression("rank", NumberMatch.GREATER_THAN, 3).and("rank", NumberMatch.LESS_THAN, 8)
 
// An array of Room Groups where we want the search to take place
var whereToSearch:Array = ["poker", "blackJack"]
 
// Fire the request and jump into the game!
sfs.send( new QuickJoinGameRequest(expr, whereToSearch) )


In this example is "rank" a room variable?

What if I don't want to check anything besides a non-started game? How can I check for that?

By providing a MatchExpression and a list of Rooms (of type SFSGame) or a Room Group the system can search for matching Rooms and immediately teleport the player in the game action.


In this example, where/how are the "poker" and "blackjack" Room Groups being created? I found the "groupId" property for SFSRoom, but I don't see how/where to set that in the QuickJoinGameRequest.

Do I need to create a Room Group, or can I just create an SFSGame?

Do I need to do anything on the server side?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: QuickJoinGameRequest - Working Example Project?

Postby rjgtav » 28 Mar 2012, 20:37

Hi.
Yes rank is a room variable. If you just want to check if the game has started or not, you can do something like:

Code: Select all

var expr:MatchExpression = new MatchExpression(ReservedRoomVariables.RV_GAME_STARTED, BoolMatch.EQUALS, false);


Note: Yes, when you do game.isStarted() it simply returns the value of a room variable, which is specifically reserved for this purpose.

Regarding the Room Groups, by default, when you create a game, it is created on the "default" room group. And you don't need to specify the room group when searching for rooms, that's optional :) (it is only used for example if you have a "lobby" and a "games" groups or groups for different games - like in the example in the docs).

And finally, no, you don't need to do anything on the server-side. The server does everything for you and returns you a list of the available rooms.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Re: QuickJoinGameRequest - Working Example Project?

Postby Rashomon » 30 Mar 2012, 00:54

Thanks, rjgtav. I made a few changes, based on your advice. Here's what I have:

Code: Select all

import com.smartfoxserver.v2.entities.variables.ReservedRoomVariables;
import com.smartfoxserver.v2.entities.match.BoolMatch;

function onQuickJoin(Event:MouseEvent):void {   
   sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
   sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError);
   
   var expr:MatchExpression = new MatchExpression(ReservedRoomVariables.RV_GAME_STARTED, BoolMatch.EQUALS, false);
   var whereToSearch:Array = ["default"];
   sfs.send(new QuickJoinGameRequest(expr, whereToSearch));
}
function onRoomJoin(evt:SFSEvent):void
{
   trace("Room joined");
}
function onRoomJoinError(evt:SFSEvent):void
{
   trace("Room join failed: " + evt.params.errorMessage);
}


Unfortunately, I get the following message:
Room join failed: QuickJoinGame failed: no matching Rooms were found


I thought QuickJoinGameRequest was supposed to create and join the user to a room if the request couldn't find a matching game. What am I missing?
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: QuickJoinGameRequest - Working Example Project?

Postby Bax » 30 Mar 2012, 08:36

No, the QuickJoinGameRequest doesn't create a Room if one to join was not found. Otherwise the constructor would ask you to pass the Room creation parameters, or it would be impossible for the server to know what kind of Room it should create.
Paolo Bax
The SmartFoxServer Team
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Re: QuickJoinGameRequest - Working Example Project?

Postby Rashomon » 31 Mar 2012, 01:43

Ok. That makes sense. However, now I'm still getting the "no matching Rooms were found" error. SFSAdmin shows me creating a room, but I still can't get a match.

Here's what I'm trying this time:

Code: Select all

btn_createGame1.addEventListener(MouseEvent.CLICK,onCreateGame1Click);
function onCreateGame1Click(Event:MouseEvent):void {
   setAvatarUserVars();
    sfs.addEventListener(SFSEvent.ROOM_ADD, onRoomCreated);
   sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
    sfs.addEventListener(SFSEvent.ROOM_CREATION_ERROR, onRoomCreationError);
   var roomName:String = "game1"
   var roomPwd:String = "";
   var roomMaxS:int = 0;   
   
   if (roomName.length > 0)
   {
      var settings:RoomSettings = new RoomSettings(roomName)
      settings.groupId = "game"
      settings.password = roomPwd
      settings.isGame = true
      settings.maxUsers = 3
      settings.maxSpectators = roomMaxS
      settings.extension = new RoomExtension(EXTENSION_ID, EXTENSIONS_CLASS)
      
      sfs.send( new CreateRoomRequest(settings, true, sfs.lastJoinedRoom) )
      
   }   
}

btn_quickJoin.addEventListener(MouseEvent.CLICK,onQuickJoin);
function onQuickJoin(Event:MouseEvent):void {
   
   sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
   sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError);
   
       // I tried just matching "true", but no luck.  I'm just trying to get a hit on ANYTHING, at this point
   var expr:MatchExpression = new MatchExpression(ReservedRoomVariables.RV_GAME_STARTED, BoolMatch.EQUALS, false).or(ReservedRoomVariables.RV_GAME_STARTED, BoolMatch.EQUALS, true);
   var whereToSearch:Array = ["default","game"];
   sfs.send(new QuickJoinGameRequest(expr, whereToSearch));
}


What am I doing wrong?

(BTW, I don't see any sort of "game started" variable for the room in the SFSAdmin utility. Maybe it doesn't show all of the reserved room vars?)
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: QuickJoinGameRequest - Working Example Project?

Postby rjgtav » 31 Mar 2012, 11:01

Hello.
In order to that RoomVariable be available, you need to create a SFSGame Room, not a common SFSRoom. That said, you need to use the CreateSFSGameRequest instead of the CreateRoomRequest.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 16 guests