New Room (SOLVED)

How can I create new room (a new instance) depending on a condition?
I've seen, for example, that if the maximum number of players is reached, a room is automatically created.
But I would like to create the room according to a pre-established condition.

When I connect to the room, I saw that I can send from client to server options that can interpret on requestJoin or in onIne function. Depending on those options, I would like to create a new room. How can I do that? Thanks

Hey @rscata, the method you're looking for is requestJoin(options). To create a new room, this method should return false for every room already spawned. The new room will then be created if requestJoin(options) returns true on it.

More info: http://colyseus.io/docs/concept-matchmaking/

Cheers!

I tried this method, but if it's false, I get the message "server error: join_request_fail" and do not create a new room.

@rscata can you provide an example of when you want to create a new room? Maybe you should register a new room name instead - having a different condition for requestJoin().

Cheers!

Let's say I have a poker game what contains some game rooms. At this point I want to offer two posibilities:

  1. To join one of the rooms that is already created and has not reached max player limit.
  2. To reate a new room and join it.

@rscata I see. Thanks for the explanation. I've tested the code below and it works as you described.

Server-side

maxClients = 4;

onInit (options) {
  // identify when a new room is being requested
  this.create = options.create || false;
}

requestJoin (options) {
  if (options.create) {
    // creating a new room
    let allowed = (options.create == this.create);

    // this room is not being "created" anymore.
    this.create = false;

    return allowed;

  } else {
    // joining an existing room
    return this.clients.length > 0;
  }
}

Client-side

// creating a new room
let room = client.join("poker", { create: true })
room.onJoin.add(() => { /* created! */ })

// joining an existing room
let room = client.join("poker")
room.onJoin.add(() => { /* joined! */ })
room.onError.add(() => { /* no rooms available? */ })

I feel it should be easier to achieve this, though.

For next versions of Colyseus, I think it's important to know when a room is being created or is an existing one during onInit / requestJoin (like we're doing ourselves on this.create)

Let me know if you have any question. Cheers!

Thank you for explanation.This solution works great, but I still got a problem when I'm trying to join a room by roomId. I read in documentation about that.

// connect the client directly into a specific room id
let room = client.join('H1gbitW3Pf');

And I got this error:

server error: Error: no available handler for "H1gbitW3Pf"

@rscata thanks for reporting. This was actually a bug. It has been fixed now on version 0.8.12.

Release: https://github.com/gamestdio/colyseus/releases/tag/0.8.12

Thanks for support
If we still have some problems or have questions, can we contact you?

@rscata sure, feel free to post here if you need any help!

Hey @rscata, I've just released a new version (colyseus@0.8.13, see release) which adds the isNew parameter on requestJoin. With this version you'd be able to achieve the same result by having this implementation:

// ...
maxClients = 4;

requestJoin (options, isNew) {
  return (options.create && isNew) || this.clients.length > 0;
}
// ...
This post is deleted!