示例#1
0
        /// <summary>
        ///     Sends a request to room, to retrieve an access to it for a specified peer,
        ///     with some extra properties
        /// </summary>
        public void GetAccess(IClient client, GetAccessCallback callback)
        {
            // If request is already pending
            if (_pendingRequests.ContainsKey(client))
            {
                callback.Invoke(null, "You've already requested an access to this room");
                return;
            }

            // If player is already in the game
            if (_players.ContainsKey(client.ID))
            {
                callback.Invoke(null, "You are already in this room");
                return;
            }

            // If player has already received an access and didn't claim it
            // but is requesting again - send him the old one
            var currentAccess = _unconfirmedAccesses.Values.FirstOrDefault(v => v.Client.ID == client.ID);

            if (currentAccess != null)
            {
                // Restore the timeout //TODO: Timeout-Setting (no fixed value of 20)
                currentAccess.Timeout = DateTime.Now.AddSeconds(20);

                callback.Invoke(currentAccess.Access, null);
                return;
            }

            // If there's a player limit
            if (Options.MaxPlayers != 0)
            {
                var playerSlotsTaken = _pendingRequests.Count
                                       + _accessesInUse.Count
                                       + _unconfirmedAccesses.Count;

                if (playerSlotsTaken >= Options.MaxPlayers)
                {
                    callback.Invoke(null, "Room is already full");
                    return;
                }
            }

            var packet = new RoomAccessProvideCheckPacket
            {
                ClientID = client.ID,
                RoomID   = ID
            };

            // Add to pending list
            _pendingRequests[client] = callback;

            Client.SendMessage(Message.Create(MessageTags.ProvideRoomAccessCheck, packet), SendMode.Reliable);
        }
示例#2
0
        /// <summary>
        /// Sends a request to room, to retrieve an access to it for a specified peer, 
        /// with some extra options
        /// </summary>
        /// <param name="peer"></param>
        /// <param name="customOptions"></param>
        /// <param name="callback"></param>
        public void GetAccess(IPeer peer, MstProperties customOptions, GetAccessCallback callback)
        {
            // If request is already pending
            if (requestsInProgress.Contains(peer.Id))
            {
                callback.Invoke(null, "You've already requested an access to this room");
                return;
            }

            // If player is already in the game
            if (connectedPlayers.ContainsKey(peer.Id))
            {
                callback.Invoke(null, "You are already in this room");
                return;
            }

            // If player has already received an access and didn't claim it
            // but is requesting again - send him the old one
            var currentAccess = unconfirmedAccesses.Values.FirstOrDefault(v => v.Peer == peer);
            if (currentAccess != null)
            {
                // Restore the timeout
                currentAccess.Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod);

                callback.Invoke(currentAccess.Access, null);
                return;
            }

            // If there's a player limit
            if (Options.MaxConnections != 0)
            {
                var playerSlotsTaken = requestsInProgress.Count + accessesInUse.Count + unconfirmedAccesses.Count;

                if (playerSlotsTaken >= Options.MaxConnections)
                {
                    callback.Invoke(null, "Room is already full");
                    return;
                }
            }

            // Create packet to request checking of access
            var provideRoomAccessCheckPacket = new ProvideRoomAccessCheckPacket()
            {
                PeerId = peer.Id,
                RoomId = RoomId,
                CustomOptions = customOptions
            };

            // Try to find out if requester is logged in add the username if available
            // Simetimes we want to check if user is banned
            var userPeerExtension = peer.GetExtension<IUserPeerExtension>();
            if (userPeerExtension != null && !string.IsNullOrEmpty(userPeerExtension.Username))
            {
                provideRoomAccessCheckPacket.Username = userPeerExtension.Username;
            }

            // Add requester peer id to pending list to prevent new access request to this room
            requestsInProgress.Add(peer.Id);

            // Send request to owner of the room to get access token
            Peer.SendMessage((short)MstMessageCodes.ProvideRoomAccessCheck, provideRoomAccessCheckPacket, (status, response) =>
            {
                // Remove requester peer id from pending list
                requestsInProgress.Remove(peer.Id);

                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(null, response.AsString("Unknown Error"));
                    return;
                }

                // Parse access data from message
                var accessData = response.Deserialize(new RoomAccessPacket());

                // Create new access info
                var access = new RoomAccessData()
                {
                    Access = accessData,
                    Peer = peer,
                    Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod)
                };

                // Save the access info to list and wait for confirmation
                unconfirmedAccesses[access.Access.Token] = access;

                callback.Invoke(access.Access, null);
            });
        }
示例#3
0
 /// <summary>
 /// Sends a request to room, to retrieve an access to it for a specified peer
 /// </summary>
 /// <param name="peer"></param>
 /// <param name="callback"></param>
 public void GetAccess(IPeer peer, GetAccessCallback callback)
 {
     GetAccess(peer, new MstProperties(), callback);
 }
示例#4
0
        /// <summary>
        /// Sends a request to room, to retrieve an access to it for a specified peer,
        /// with some extra properties
        /// </summary>
        public void GetAccess(IPeer peer, Dictionary <string, string> properties, GetAccessCallback callback)
        {
            // If request is already pending
            if (_requestsInProgress.Contains(peer.Id))
            {
                callback.Invoke(null, "You've already requested an access to this room");
                return;
            }

            // If player is already in the game
            if (_players.ContainsKey(peer.Id))
            {
                callback.Invoke(null, "You are already in this room");
                return;
            }

            // If player has already received an access and didn't claim it
            // but is requesting again - send him the old one
            var currentAccess = _unconfirmedAccesses.Values.FirstOrDefault(v => v.Peer == peer);

            if (currentAccess != null)
            {
                // Restore the timeout
                currentAccess.Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod);

                callback.Invoke(currentAccess.Access, null);
                return;
            }

            // If there's a player limit
            if (Options.MaxPlayers != 0)
            {
                var playerSlotsTaken = _requestsInProgress.Count
                                       + _accessesInUse.Count
                                       + _unconfirmedAccesses.Count;

                if (playerSlotsTaken >= Options.MaxPlayers)
                {
                    callback.Invoke(null, "Room is already full");
                    return;
                }
            }

            var packet = new RoomAccessProvideCheckPacket()
            {
                PeerId = peer.Id,
                RoomId = RoomId
            };

            // Add the username if available
            var userExt = peer.GetExtension <IUserExtension>();

            if (userExt != null && !string.IsNullOrEmpty(userExt.Username))
            {
                packet.Username = userExt.Username;
            }

            // Add to pending list
            _requestsInProgress.Add(peer.Id);

            Peer.SendMessage((short)OpCodes.ProvideRoomAccessCheck, packet, (status, response) =>
            {
                // Remove from pending list
                _requestsInProgress.Remove(peer.Id);

                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(null, response.AsString("Unknown Error"));
                    return;
                }

                var accessData = response.Deserialize(new RoomAccessPacket());

                var access = new RoomAccessData()
                {
                    Access  = accessData,
                    Peer    = peer,
                    Timeout = DateTime.Now.AddSeconds(Options.AccessTimeoutPeriod)
                };

                // Save the access
                _unconfirmedAccesses[access.Access.Token] = access;

                callback.Invoke(access.Access, null);
            });
        }
示例#5
0
 /// <summary>
 /// Sends a request to room, to retrieve an access to it for a specified peer
 /// </summary>
 /// <param name="peer"></param>
 /// <param name="callback"></param>
 public void GetAccess(IPeer peer, GetAccessCallback callback)
 {
     GetAccess(peer, new Dictionary <string, string>(), callback);
 }
示例#6
0
 /// <summary>
 /// Sends a request to room, to retrieve an access to it for a specified peer
 /// </summary>
 /// <param name="peer"></param>
 /// <param name="callback"></param>
 public void GetAccess(IPeer peer, GetAccessCallback callback)
 {
     GetAccess(peer, new DictionaryOptions(), callback);
 }