Exemplo n.º 1
0
        private void SessionConnectionStateChangedHandler(IMultiplayerSessionConnectionState state)
        {
            switch (state.CurrentStage)
            {
            case MultiplayerSessionConnectionStage.EstablishingServerPolicy:
                Log.InGame("Requesting session policy information...");
                break;

            case MultiplayerSessionConnectionStage.AwaitingReservationCredentials:
                Log.InGame("Waiting for User Input...");

                RightSideMainMenu.OpenGroup("Join Server");
                FocusPlayerNameTextbox();

                break;

            case MultiplayerSessionConnectionStage.SessionReserved:
                Log.InGame("Launching game...");

                multiplayerSession.ConnectionStateChanged -= SessionConnectionStateChangedHandler;
                preferencesManager.Save();
                StartCoroutine(LaunchSession());

                break;

            case MultiplayerSessionConnectionStage.SessionReservationRejected:
                Log.InGame("Reservation rejected...");

                MultiplayerSessionReservationState reservationState = multiplayerSession.Reservation.ReservationState;

                string reservationRejectionNotification = reservationState.Describe();

                NotifyUser(
                    reservationRejectionNotification,
                    () =>
                {
                    multiplayerSession.Disconnect();
                    multiplayerSession.Connect(ServerIp);
                });

                break;

            case MultiplayerSessionConnectionStage.Disconnected:
                Log.Info("Disconnected from server");
                break;
            }
        }
Exemplo n.º 2
0
        public MultiplayerSessionReservation ReservePlayerContext(
            Connection connection,
            PlayerSettings playerSettings,
            AuthenticationContext authenticationContext,
            string correlationId)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage;
                assetsByConnection.TryGetValue(connection, out assetPackage);

                if (assetPackage == null)
                {
                    assetPackage = new ConnectionAssets();
                    assetsByConnection.Add(connection, assetPackage);
                }

                string playerName = authenticationContext.Username;

                if (reservedPlayerNames.Contains(playerName))
                {
                    MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.Rejected | MultiplayerSessionReservationState.UniquePlayerNameConstraintViolated;
                    return(new MultiplayerSessionReservation(correlationId, rejectedState));
                }

                ulong playId;
                if (ResolveDuplicateSteamIds(authenticationContext, correlationId, out playId))
                {
                    MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.Rejected | MultiplayerSessionReservationState.UniqueSteamIdConstraintViolated;
                    return(new MultiplayerSessionReservation(correlationId, rejectedState));
                }

                reservedPlayerNames.Add(playerName);

                PlayerContext playerContext  = new PlayerContext(playerName, playId, playerSettings);
                ulong         playerId       = playerContext.PlayerId;
                string        reservationKey = playerContext.PlayerId.ToString();

                reservations.Add(reservationKey, playerContext);
                assetPackage.ReservationKey = reservationKey;

                return(new MultiplayerSessionReservation(correlationId, playerId, reservationKey));
            }
        }
        public static string Describe(this MultiplayerSessionReservationState currentState)
        {
            StringBuilder descriptionBuilder = new StringBuilder();

            foreach (string reservationStateName in Enum.GetNames(typeof(MultiplayerSessionReservationState)))
            {
                MultiplayerSessionReservationState reservationState = (MultiplayerSessionReservationState)Enum.Parse(typeof(MultiplayerSessionReservationState), reservationStateName);
                if (currentState.HasStateFlag(reservationState))
                {
                    DescriptionAttribute descriptionAttribute = reservationState.GetAttribute <DescriptionAttribute>();

                    if (!string.IsNullOrEmpty(descriptionAttribute?.Description))
                    {
                        descriptionBuilder.AppendLine(descriptionAttribute.Description);
                    }
                }
            }

            return(descriptionBuilder.ToString());
        }
Exemplo n.º 4
0
        private void RenderUnableToJoinDialog(int windowId)
        {
            Event e = Event.current;

            if (e.isKey)
            {
                switch (e.keyCode)
                {
                case KeyCode.Return:
                    joiningServer         = true;
                    notifyingUnableToJoin = false;
                    break;

                case KeyCode.Escape:
                    joiningServer         = true;
                    notifyingUnableToJoin = false;
                    break;
                }
            }

            GUISkinUtils.RenderWithSkin(GetGUISkin("dialogs.server.rejected", 490), () =>
            {
                using (new GUILayout.VerticalScope("Box"))
                {
                    using (new GUILayout.HorizontalScope())
                    {
                        MultiplayerSessionReservationState reservationState = multiplayerSession.Reservation.ReservationState;
                        string reservationStateDescription = reservationState.Describe();

                        GUILayout.Label(reservationStateDescription);
                    }

                    if (GUILayout.Button("OK"))
                    {
                        notifyingUnableToJoin = false;
                        multiplayerSession.Disconnect();
                        multiplayerSession.Connect(ServerIp);
                    }
                }
            });
        }
Exemplo n.º 5
0
        public MultiplayerSessionReservation ReservePlayerContext(
            Connection connection,
            PlayerSettings playerSettings,
            AuthenticationContext authenticationContext,
            string correlationId)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage;
                assetsByConnection.TryGetValue(connection, out assetPackage);

                if (assetPackage == null)
                {
                    assetPackage = new ConnectionAssets();
                    assetsByConnection.Add(connection, assetPackage);
                }

                string playerName = authenticationContext.Username;

                if (reservedPlayerNames.Contains(playerName))
                {
                    MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.Rejected | MultiplayerSessionReservationState.UniquePlayerNameConstraintViolated;
                    return(new MultiplayerSessionReservation(correlationId, rejectedState));
                }

                reservedPlayerNames.Add(playerName);

                bool          hasSeenPlayerBefore = playerData.hasSeenPlayerBefore(playerName);
                PlayerContext playerContext       = new PlayerContext(playerName, playerData.GetPlayerId(playerName), !hasSeenPlayerBefore, playerSettings);
                ushort        playerId            = playerContext.PlayerId;
                string        reservationKey      = Guid.NewGuid().ToString();

                reservations.Add(reservationKey, playerContext);
                assetPackage.ReservationKey = reservationKey;

                return(new MultiplayerSessionReservation(correlationId, playerId, reservationKey));
            }
        }
 public MultiplayerSessionReservation(string correlationId, MultiplayerSessionReservationState reservationState) : base(correlationId)
 {
     ReservationState = reservationState;
 }
 public static bool HasStateFlag(this MultiplayerSessionReservationState currentState, MultiplayerSessionReservationState checkedState)
 {
     return((currentState & checkedState) == checkedState);
 }
Exemplo n.º 8
0
        public MultiplayerSessionReservation ReservePlayerContext(
            NitroxConnection connection,
            PlayerSettings playerSettings,
            AuthenticationContext authenticationContext,
            string correlationId)
        {
            // TODO: ServerPassword in NitroxClient

            if (!string.IsNullOrEmpty(serverConfig.ServerPassword) && (!authenticationContext.ServerPassword.HasValue || authenticationContext.ServerPassword.Value != serverConfig.ServerPassword))
            {
                MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.REJECTED | MultiplayerSessionReservationState.AUTHENTICATION_FAILED;
                return(new MultiplayerSessionReservation(correlationId, rejectedState));
            }

            if (reservedPlayerNames.Count >= serverConfig.MaxConnections)
            {
                MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.REJECTED | MultiplayerSessionReservationState.SERVER_PLAYER_CAPACITY_REACHED;
                return(new MultiplayerSessionReservation(correlationId, rejectedState));
            }

            string playerName = authenticationContext.Username;
            Player player;

            allPlayersByName.TryGetValue(playerName, out player);
            if ((player?.IsPermaDeath == true) && serverConfig.IsGameMode("Hardcore"))
            {
                MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.REJECTED | MultiplayerSessionReservationState.HARDCORE_PLAYER_DEAD;
                return(new MultiplayerSessionReservation(correlationId, rejectedState));
            }

            if (reservedPlayerNames.Contains(playerName))
            {
                MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.REJECTED | MultiplayerSessionReservationState.UNIQUE_PLAYER_NAME_CONSTRAINT_VIOLATED;
                return(new MultiplayerSessionReservation(correlationId, rejectedState));
            }

            ConnectionAssets assetPackage;

            assetsByConnection.TryGetValue(connection, out assetPackage);
            if (assetPackage == null)
            {
                assetPackage = new ConnectionAssets();
                assetsByConnection.Add(connection, assetPackage);
                reservedPlayerNames.Add(playerName);
            }


            bool   hasSeenPlayerBefore = player != null;
            ushort playerId            = hasSeenPlayerBefore ? player.Id : ++currentPlayerId;

            PlayerContext playerContext  = new PlayerContext(playerName, playerId, !hasSeenPlayerBefore, playerSettings);
            string        reservationKey = Guid.NewGuid().ToString();

            reservations.Add(reservationKey, playerContext);
            assetPackage.ReservationKey = reservationKey;

            if (ConnectedPlayers().Count() == 1)
            {
                Server.Instance.EnablePeriodicSaving();
            }

            return(new MultiplayerSessionReservation(correlationId, playerId, reservationKey));
        }