Forum C# UserJoined problem

UserJoined problem

Postby gnollik » February 14th, 2012, 5:23 pm

Hello I`m create a new game and I`m have a problem in this code:
Code: Select all
public override void UserJoined(Player player)
{
            //get and set parameters
            foreach (KeyValuePair<string, string> info in player.JoinData)
            {
                if (info.Key == "user_name")
                { boss_names[PlayerCount - 1] = info.Value; }
                if (info.Key == "hx")
                { boss_playerInfo[PlayerCount - 1, 0] = Convert.ToInt32(info.Value); }
                if (info.Key == "hy")
                { boss_playerInfo[PlayerCount - 1, 1] = Convert.ToInt32(info.Value); }
                if (info.Key == "health")
                { boss_playerInfo[PlayerCount - 1, 2] = Convert.ToInt32(info.Value); }
                if (info.Key == "damage")
                { boss_playerInfo[PlayerCount - 1, 3] = Convert.ToInt32(info.Value); }
            }

            //start timer if need
            if (PlayerCount == 1)
            {
                this.boss_timer = AddTimer(delegate
                {
                    bla-bla-bla...
                }, 300000);
            }
}

The problem is if 2 players connect to the room simultaneously "if (PlayerCount == 1)" doesnt work because for example first player connects in 0.0 sec and his information saves in an array. In this time the second player enter in this room for example in 0.01sec. So when the 1st player arive to the "if (PlayerCount == 1)" point PlayerCount is already 2, not 1. So the timer doesnt start. I had such problem in the past, and I created a timer on the client side to resolve this problem. But now it will not work. So how I can fix this issue on the server side?
gnollik
Paid Member
 
Posts: 24
Joined: March 12th, 2011, 10:08 pm

Re: UserJoined problem

Postby gnollik » February 14th, 2012, 5:37 pm

Ok, it looks like this problem can be fixed if I replace "if (PlayerCount == 1)" with "if (timer_start == 0) { timer_start = 1; bla-bla-bla...". In this case it doesnt matter how much players in the room. The code will be executed 1 time while timer_start = 0, after that timer_start will become 1 and all will be fine, right? But the same problem can come in this part too:
Code: Select all
if (info.Key == "damage")
{ boss_playerInfo[PlayerCount - 1, 3] = Convert.ToInt32(info.Value); }

Because PlayerCount can be changed while the code is being executed.
gnollik
Paid Member
 
Posts: 24
Joined: March 12th, 2011, 10:08 pm

Re: UserJoined problem

Postby Henrik » February 15th, 2012, 1:40 pm

Ok, so you have some sort of array that contains info about each player, and you index into that by what the value of PlayerCount is at the moment the code is running... That won't ever work. What if a player leaves? Doesn't that shift every remaining player down a step, which means the array with info is wrong?

So you need a better way of storing global info. You could for example give each joining player a unique index and use that. Or you could store the info in a Dictionary<string, Something>, and key it by the connectuserid of each player.

As for your timer problem, just check if you've created it already, and only create if you haven't.

Code: Select all
if (this.boss_timer == null) {
    this.boss_timer = AddTimer(...);
}


That should work pretty well.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: UserJoined problem

Postby gnollik » February 16th, 2012, 11:54 am

Thanks for your advice with timer, it works fine. Yes, sure I`m use a unique index for each player in my game. I`m use PlayerCount only in UserJoined section. When the game start all players have unique index and all work fine even if some one leave the game. I`m already test this system in my 2 previous games and I dont have any issues with it.
gnollik
Paid Member
 
Posts: 24
Joined: March 12th, 2011, 10:08 pm


Return to C#