Forum Scripting Create new room if player size exceeds to 2

Post your problems and discussions relating to scripting in Unity here.

Create new room if player size exceeds to 2

Postby Ekta » October 6th, 2015, 9:21 am

Hii ,,..

I want to create new room if players reached to maximum number limit.

I have checked Service room but its not creating new room when 3 rd player tries to join same room.

I am limiting player by using below code :

Code: Select all
public override bool AllowUserJoin(Player player)
        {
            int maxplayers = 2; //Default
           
            //Check if there's room for this player.
            if (Players.Count() < maxplayers)
            {
                return true;
            }
            return false;
        }


and connection i have made something like this :

Code: Select all
client.Multiplayer.CreateJoinRoom(
            "$service-room$",               //Room id. If set to null a random roomid is used
            "UnityPoker",                  //The room type started on the server
            true,                        //Should the room be visible in the lobby?
            null,
            null,
            delegate(Connection connection) {
            Debug.Log("Joined Room.");
            
            // We successfully joined a room so set up the message handler
            pioconnection = connection;
            pioconnection.OnMessage += handlemessage;
            joinedroom = true;
         },
         delegate(PlayerIOError error) {
            Debug.Log("Error Joining Room: " + error.ToString());
            
         }
         );


What sholu i do to make this work ?
any other way is there ?

Thanks.
Ekta
 
Posts: 5
Joined: September 29th, 2015, 8:55 am

Re: Create new room if player size exceeds to 2

Postby Guillaume » October 6th, 2015, 1:02 pm

You may try this:

1. You create your room. When the number of players you want is reached, you may add a roomData from your server code, that say that users can't join this room. But that mean that you listRoom on Client side before and check...

2. You may use GameStarted method ? I don't know if it block connection, you have to check.

Also in your snippet of code, can you check in your development server, if the NEW PLAYER is counted when calling Players.Count in AllowUserJoin ? If yes, you may try to change to Players.Count() <= maxplayers.

Have you tried to put a breakpoint to check if your code return false in the AllowUserJoin ?
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Create new room if player size exceeds to 2

Postby Ekta » October 9th, 2015, 8:22 am

I have tried something like :

I have added player in temporary room first and if i get players more than 2 then i will put them in new room of the type i want.

so this solution is good or will create problem ? @Guillaume

Thanks @Guillaume for reply.
Ekta
 
Posts: 5
Joined: September 29th, 2015, 8:55 am

Re: Create new room if player size exceeds to 2

Postby Guillaume » October 9th, 2015, 10:55 am

If it work, it must be ok ;)
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Create new room if player size exceeds to 2

Postby Ekta » October 12th, 2015, 2:33 pm

Guillaume ., that my code is not working when i request to join at the same time. can you please more explain your point 1 which you have listed before. I am not getting that much.
Ekta
 
Posts: 5
Joined: September 29th, 2015, 8:55 am

Re: Create new room if player size exceeds to 2

Postby Guillaume » October 12th, 2015, 2:59 pm

From the serverside, you may try to use AddTimer or ScheduleCallback in order to execute some code later in the time from the server, then disconnect one of the player if there is one more than expected dû to simultaneous connection.

See here:

https://gamesnet.yahoo.net/documentatio ... e#AddTimer

or:

https://gamesnet.yahoo.net/documentatio ... leCallback

See what behavior is better for your game.
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Create new room if player size exceeds to 2

Postby Ekta » October 13th, 2015, 7:17 am

Guillaume i am posting here my code :

public override void UserJoined(Player player)
Code: Select all
        {
            //Notify other players that someone joined,
            //or inform the new player of the game state
            Player opponent = null;
            if (PlayerCount == 2)
            {
                foreach (Player p in Players)
                {
                    if (p != player)
                    {
                        opponent = p;
                        break;
                    }
                }
            }

            if (opponent != null)
            {
                string id = randomString(5);

                opponent.Send("MatchMaking", id, opponent.ConnectUserId);
                player.Send("MatchMaking", id, player.ConnectUserId);
                player.Disconnect();
                opponent.Disconnect();
               
            }
        }


Here i am not able to add timer for connecting another player after some interval .
Ekta
 
Posts: 5
Joined: September 29th, 2015, 8:55 am

Re: Create new room if player size exceeds to 2

Postby Guillaume » October 13th, 2015, 12:38 pm

Use the timer to DISCONNECT the wrong player ;-)

On UserJoined, write in your code a ScheduleCallback call, and write in the delegate a code to disconnect the client if you check there is more than 2 players.

Since this ScheduleCallback will be called some time after each player connection, you can be sure that you will detect if there is more than 2 players, even if there is a simultaneous connection when UserJoined was called some time before.
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Create new room if player size exceeds to 2

Postby Ekta » October 14th, 2015, 6:52 am

Ok Thanks Guillaume. I solved my issue. :)
Ekta
 
Posts: 5
Joined: September 29th, 2015, 8:55 am

Re: Create new room if player size exceeds to 2

Postby Guillaume » October 14th, 2015, 1:13 pm

Congratulation! :D
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Create new room if player size exceeds to 2

Postby ragames » March 11th, 2017, 1:16 pm

This can be usefull for others for preventing simultaneous connection and matching correctly. I did not tested deeply. But when I start 12 clients in exact same time, server decides perfectly and matches players 2 by 2 in the development server.

Code: Select all
[RoomType("matchmaking")]
    public class Matching : Game<Player>
    {
        Random random = new Random(); // For a instance of matchmaking room, 1 random series we need.
        int counter = 0; // this is a counter for console information.
        public override void GameStarted() { }
        public override void GameClosed() { }
        public override void UserJoined(Player player1)
        {
            // Every room, player has a unique id which given by server. Therefore this statements control the id and if remainder of player1.id divided by 2 is zero, hmm this is second player in the room it can match with first player;
            if (player1.Id % 2 == 0)
                foreach (Player player2 in Players)
                {
                    if (player2.Id == (player1.Id) - 1)
                    {
                        string id = (random.Next(1000000, 2000000)) + "";
                        player1.Send("gameFound", id);
                        player2.Send("gameFound", id);

                    Console.WriteLine(player1.Id + " vs " + player2.Id + " on room : " + id + " / match " + (++counter));
                    player1.Disconnect();
                    player2.Disconnect();
                    break;
                    }
                }
            }
        public override void UserLeft(Player player) { }
    }
ragames
 
Posts: 1
Joined: March 5th, 2017, 4:17 pm


Return to Scripting