Friends challenging each other

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

Moderators: Lapo, Bax

wilprim
Posts: 6
Joined: 19 Jan 2018, 18:37

Friends challenging each other

Postby wilprim » 02 Feb 2018, 02:39

Hey everyone! I am creating a game where users can either 1) Challenge friends to a match or 2) search for a random opponent. Each game will consist of a max of 2 users. I am wondering the best way to approach this...

For the challenge friend method I have this approach:

1) User sends a invitation request to server:

Code: Select all

//Challenge a user! (Client Side.. Unity)
void ChallengeUser(int userId){
   //Then invite the user of your choice
   ISFSObject invitation = new SFSObject ();
   invitation.PutInt ("userId", userId);
   sfs.Send (new ExtensionRequest ("challengeUser", invitation, sfs.LastJoinedRoom));
}


2) Server handles the request and sends a game challenge to a specific user (invitation)

Request Handler

Code: Select all

//Server side request handler
public class ChallengeUserHandler extends BaseClientRequestHandler {
   @Override
   public void handleClientRequest(User sender, ISFSObject params) {
      User invitee = this.getApi().getUserById(params.getInt("userId"));
      Invitation invitation = new SFSInvitation(sender, invitee, 50);
      
      //send this invitation
      ((CuberLobbyExtension) this.getParentExtension()).sendGameChallenge(invitation);
   }
}

Lobby Extension that Sends the invitation and handles the callback

Code: Select all

//Invitations
public void sendGameChallenge(Invitation inv) {
   this.getGameApi().sendInvitation(inv, new InvitationCallback() {

      @Override
      public void onAccepted(Invitation invitation, ISFSObject params) {
         //user accepted invitation
         CreateRoomSettings roomSettings= new CreateRoomSettings();
         roomSettings.isGame();
         roomSettings.setName("HAHAHROOM");
         roomSettings.setMaxUsers(2);
         Zone zone = getParentZone();
            
         try {
            getApi().createRoom(zone, roomSettings, inv.getInviter());
         } catch (SFSCreateRoomException e) {
            // TODO Auto-generated catch block
            trace("Error creating room...");
         }
      }

      @Override
      public void onExpired(Invitation invitation) {
            
      }

      @Override
      public void onRefused(Invitation invitation, ISFSObject params) {
            
      }
         
   });
}


This is all I have so far.. I don't really know where to go from here. I am thinking after the room is created I send both the inviter and invitee a response that makes them join a room... Is this correct? Also, would like to know how to create rooms with random unique names (maybe hash ids...) that get deleted when game is done...

As for the random opponent matching, I have not even begun on that... Maybe save for later discussion... :)

Thanks in advance!
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Friends challenging each other

Postby Lapo » 02 Feb 2018, 08:30

Hi,
I don't really know where to go from here. I am thinking after the room is created I send both the inviter and invitee a response that makes them join a room... Is this correct?

Yes indeed. It's a good way to do it.

Also, would like to know how to create rooms with random unique names (maybe hash ids...) that get deleted when game is done...

Users have unique IDs. You could combine their IDS with the Room name to make sure you have no duplicates.
Example:
User A, id = 35
User B, id = 80
Room Name will be GameRoom_35_80
Otherwise you can just use a shared counter (e.g. an AtomicLong) that keeps increasing on every new room created.

Deleting the Room when the game finishes is done automatically by Rooms whose isGame() property is set to true.
(i.e. when both players leave the Room is auto-removed)

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 41 guests