public static bool SubscribeForLobby(string lobbyUrl)
        {
            string username = Connection.LocalPlayer.Name;
            int    user_id  = (int)Connection.LocalPlayer.Id;

            RemoteSpace lobbySpace = new RemoteSpace(lobbyUrl);

            lobbySpace.Get("lobby_lock");
            ITuple playerTuple = lobbySpace.GetP("player", 0, "No user");

            //Means that that there is no slots left in the lobby
            if (playerTuple == null)
            {
                lobbySpace.Put("lobby_lock");
                return(false);
            }

            // Get lobby info (wasn't given with responsee)
            var lobbyTuple = Connection.GlobalSpace.Query("existingLobby", typeof(string), typeof(string), lobbyUrl);

            var ownerName = (string)lobbyTuple[2];
            var ownerId   = GetPlayerId(ownerName);

            Connection.Lobby       = new Lobby();
            Connection.Lobby.Space = lobbySpace;
            Connection.Lobby.Url   = lobbyUrl;
            Connection.Lobby.Owner = new Player(ownerId, ownerName);
            Connection.Lobby.Id    = (string)lobbyTuple[1];

            lobbySpace.Put("player", user_id, username);
            lobbySpace.Put("lobby_lock");

            return(true);
        }
Пример #2
0
        public void AddUser(string name)
        {
            // checar se há tupla com usuario
            if (!this.CheckUser(name))
            {
                // se não houver, então colocar
                _remotespace.Put("User", name);
                // comecar com a thread para que o servidor possa checar se o usuario esta online
                ContinueToCheckOnline = true;
                var threadBeCheck = new Thread(() => this.BeCheckedOnline(name));
                threadBeCheck.Start();
            }
            else
            {
                // a tupla com o usuario existe
                // checar se o usuario dono da tupla está online colocando uma tupla de ping
                _remotespace.Put("User", name, "ping");
                // caso a resposta demore mais que 2s, assumir que esta offline e pegar a tupla
                pingCompleted = false;
                var threadCheck = new Thread(() => this.checkOnline(name));
                threadCheck.Start();
                if (!WaitUntil(2000, () => pingCompleted))
                {
                    // offline
                    // se está offline entao pode-se adicionar o usuario

                    // Remover velha tupla
                    _remotespace.GetP("User", name);
                    // Adicionar nova tupla com o nome
                    _remotespace.Put("User", name);
                }
                else
                {
                    // online
                    // se está online, emitir erro!
                    throw new Exception("Usuario ja existe!");
                }
            }
        }