Exemple #1
0
        /// <summary>
        /// Checks if access token is valid
        /// </summary>
        /// <param name="token"></param>
        /// <param name="peer"></param>
        /// <returns></returns>
        public bool ValidateAccess(string token, out IPeer peer)
        {
            unconfirmedAccesses.TryGetValue(token, out RoomAccessData data);

            peer = null;

            // If there's no data
            if (data == null)
            {
                return false;
            }

            // Remove unconfirmed
            unconfirmedAccesses.Remove(token);

            // If player is no longer connected
            if (!data.Peer.IsConnected)
            {
                return false;
            }

            // Set access as used
            accessesInUse.Add(data.Peer.Id, data.Access);

            peer = data.Peer;

            // Invoke the event
            if (OnPlayerJoinedEvent != null)
            {
                OnPlayerJoinedEvent.Invoke(peer);
            }

            return true;
        }
        /// <summary>
        /// Fired when player requested access to this room
        /// </summary>
        /// <param name="message"></param>
        private void ValidateRoomAccessRequestHandler(IIncommingMessage message)
        {
            var token      = message.AsString();
            var roomServer = Server as RoomServerBehaviour;

            logger.Debug($"Client {message.Peer.Id} requested access validation");

            // Trying to validate room client connection access
            Msf.Server.Rooms.ValidateAccess(roomServer.CurrentRoomController.RoomId, token, (usernameAndPeerId, error) =>
            {
                if (usernameAndPeerId == null)
                {
                    logger.Error(error);
                    message.Peer.Disconnect("Invalid room access token");
                    return;
                }

                // Let's get account info of the connected peer from master server
                Msf.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (info, infoError) =>
                {
                    if (info == null)
                    {
                        logger.Error(infoError);
                        return;
                    }

                    logger.Debug($"Room got peer account info. {info}");

                    // Create new room player info
                    var roomUserExtension = new RoomUserPeerExtension(info.PeerId, info.Username, message.Peer, info.CustomOptions);

                    // Add extension to peer
                    message.Peer.AddExtension(roomUserExtension);

                    // Inform all listeners
                    OnPlayerJoinedEvent?.Invoke(roomUserExtension);
                });

                message.Respond(ResponseStatus.Success);
            });
        }