Forum C# Setting limit of players in room

Setting limit of players in room

Postby Pawel Wozniak » November 7th, 2013, 4:49 pm

I want to set a player limit for a room (i.e 4/6/8)

Here is my C# code

Code: Select all
public override void GameStarted(){
   if(RoomData.ContainsKey("maxPlayers")){
      maxPlayers = int.Parse(RoomData["maxPlayers"]);
   }else{
      maxPlayers = 4;
   }
   playersOnline = 0;
}


Code: Select all
public override bool AllowUserJoin(Player player){
   lock (letJoinLock)
   {
      Console.WriteLine("players: "+playersOnline+"/" + maxPlayers);
      if (playersOnline >= maxPlayers) return false;
      if (!base.AllowUserJoin(player)) return false;

      playersOnline++;
      return true;
   }
}


Code: Select all
public override void UserLeft(Player player){
   playersOnline--;
}



Any idea why it doesnt work? (I can see rooms containing more players that are allowed)
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Setting limit of players in room

Postby Benjaminsen » November 8th, 2013, 5:51 pm

Pawel Wozniak wrote:I want to set a player limit for a room (i.e 4/6/8)

Here is my C# code

Code: Select all
public override void GameStarted(){
   if(RoomData.ContainsKey("maxPlayers")){
      maxPlayers = int.Parse(RoomData["maxPlayers"]);
   }else{
      maxPlayers = 4;
   }
   playersOnline = 0;
}


Code: Select all
public override bool AllowUserJoin(Player player){
   lock (letJoinLock)
   {
      Console.WriteLine("players: "+playersOnline+"/" + maxPlayers);
      if (playersOnline >= maxPlayers) return false;
      if (!base.AllowUserJoin(player)) return false;

      playersOnline++;
      return true;
   }
}


Code: Select all
public override void UserLeft(Player player){
   playersOnline--;
}



Any idea why it doesnt work? (I can see rooms containing more players that are allowed)


That's funky, can you provide a small example that results in this error behavior?
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Setting limit of players in room

Postby Pawel Wozniak » November 10th, 2013, 4:51 pm

What kind of example?

there is no error thrown (reported in error log). All i know is that from time to time there are rooms with number of players bigger than limit.
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Setting limit of players in room

Postby Pawel Wozniak » November 12th, 2013, 5:16 pm

I`m not able to reproduce this error behaviour on dev server. I tried to enter simultaunously 4 players room with over 10 players but everything was working as it should so its not about enetring very fast or development server behave in other way.
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Setting limit of players in room

Postby Pawel Wozniak » November 24th, 2013, 10:25 pm

I'm still facing this bug. I'm almost sure that something is wrong with playerio server backend. Even if there is static limit (8 players) more players can enter the room. How that can be possible?
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm

Re: Setting limit of players in room

Postby archipdev » September 7th, 2014, 10:39 pm

I have the same problem.

I made a room limited at one player for testing purpose :
Code: Select all
public override bool AllowUserJoin(Player player)
        {
            if (PlayerCount + 1 > 1)
            {
                Console.WriteLine("Too many players !!");
                return false;
            }
            return true;
        }


When the client join one after the other, it works. However, when two clients join at exactly the same time, they can both enter...
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Setting limit of players in room

Postby Henrik » September 8th, 2014, 6:09 pm

It's a concurrency issue, if two people join at the same time, that code is going to run in parallele, which means that both people check the variable, decide it's ok to join, joins, and then both of them increases the variable independently.

The simplest solution is to put a lock around the variable you're checking to make sure that only one client at a time runs the code. Something like this:

Code: Select all
lock(PlayerCount) {
    if (PlayerCount > 7) { //8 players max
        PlayerCount++;
        return true;
    }
}
return false;

You could also use the Players array that contains already connected players and check the Length property of that, so you don't have to maintain an independent variables, but then you would have to lock on that, which might cause other problems if you're using it extensively in other places.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Setting limit of players in room

Postby archipdev » September 8th, 2014, 10:10 pm

Thank you for your reply, I didn't know the lock instruction.
I will try it tomorrow
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Setting limit of players in room

Postby gfoot » September 10th, 2014, 2:42 pm

Couldn't the library perform the locking itself, maintaining a lock from the point that AllowUserJoin is called until the end of the join notification? This sounds a lot more robust to me.

It feels weird to have AllowUserJoin count the players itself. Is it OK to use Players.Count instead?
gfoot
 
Posts: 21
Joined: July 31st, 2014, 11:44 pm


Return to C#