Beispiel #1
0
        /// <summary>
        /// Finalize player joining to server room
        /// </summary>
        /// <param name="conn"></param>
        protected virtual void FinalizePlayerJoining(NetworkConnection conn)
        {
            if (roomPlayersByMirrorPeerId.ContainsKey(conn.connectionId))
            {
                RoomPlayer player = roomPlayersByMirrorPeerId[conn.connectionId];

                logger.Debug($"Client {conn.connectionId} has become a player of this room. Congratulations to {player.Username}");

                conn.Send(new ValidateRoomAccessResultMessage()
                {
                    Error  = string.Empty,
                    Status = ResponseStatus.Success
                });

                // Inform subscribers about this player
                OnPlayerJoinedRoomEvent?.Invoke(player);
            }
        }
        /// <summary>
        /// Fires when client that wants to connect to this room made request to validate the access token
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="msg"></param>
        private void ValidateRoomAccessRequestHandler(NetworkConnection conn, ValidateRoomAccessRequestMessage msg)
        {
            logger.Debug($"Room client {conn.connectionId} asked to validate access token [{msg.Token}]");

            // Triying to validate given token
            Msf.Server.Rooms.ValidateAccess(CurrentRoomController.RoomId, msg.Token, (usernameAndPeerId, error) =>
            {
                // If token is not valid
                if (usernameAndPeerId == null)
                {
                    logger.Error(error);

                    conn.Send(new RoomAccessValidationResultMessage()
                    {
                        Error  = error,
                        Status = ResponseStatus.Failed
                    });

                    MsfTimer.WaitForSeconds(1f, () => conn.Disconnect());

                    return;
                }

                logger.Debug($"Client {conn.connectionId} is successfully validated");
                logger.Debug("Getting his account info...");

                Msf.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (accountInfo, accountError) =>
                {
                    if (accountInfo == null)
                    {
                        logger.Error(accountError);

                        conn.Send(new RoomAccessValidationResultMessage()
                        {
                            Error  = accountError,
                            Status = ResponseStatus.Error
                        });

                        MsfTimer.WaitForSeconds(1f, () => conn.Disconnect());

                        return;
                    }

                    logger.Debug($"Client {conn.connectionId} has become a player of this room. Congratulations to {accountInfo.Username}");

                    conn.Send(new RoomAccessValidationResultMessage()
                    {
                        Error  = string.Empty,
                        Status = ResponseStatus.Success
                    });

                    // Create new room player
                    var player = new MirrorRoomPlayer(usernameAndPeerId.PeerId, conn, accountInfo.Username, accountInfo.CustomOptions);

                    // Add this player to filtered lists
                    roomPlayersByMsfPeerId.Add(usernameAndPeerId.PeerId, player);
                    roomPlayersByMirrorPeerId.Add(conn.connectionId, player);
                    roomPlayersByUsername.Add(accountInfo.Username, player);

                    // Inform subscribers about this player
                    OnPlayerJoinedRoomEvent?.Invoke(player);
                });
            });
        }