Ejemplo n.º 1
0
        public Player PlayerConnected(NitroxConnection connection, string reservationKey, out bool wasBrandNewPlayer)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage  = assetsByConnection[connection];
                PlayerContext    playerContext = reservations[reservationKey];
                Validate.NotNull(playerContext);

                wasBrandNewPlayer = playerContext.WasBrandNewPlayer;

                Player player;

                lock (allPlayersByName)
                {
                    if (!allPlayersByName.TryGetValue(playerContext.PlayerName, out player))
                    {
                        player = new Player(playerContext.PlayerId, playerContext.PlayerName, playerContext, connection, NitroxVector3.Zero, new NitroxId(), Optional <NitroxId> .Empty(), Perms.PLAYER, defaultPlayerStats, new List <EquippedItemData>(), new List <EquippedItemData>());
                        allPlayersByName[playerContext.PlayerName] = player;
                    }
                }

                // TODO: make a ConnectedPlayer wrapper so this is not stateful
                player.PlayerContext = playerContext;
                player.connection    = connection;

                assetPackage.Player         = player;
                assetPackage.ReservationKey = null;
                reservations.Remove(reservationKey);

                return(player);
            }
        }
Ejemplo n.º 2
0
        public void PlayerDisconnected(Connection connection)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage = null;
                assetsByConnection.TryGetValue(connection, out assetPackage);

                if (assetPackage == null)
                {
                    return;
                }

                if (assetPackage.ReservationKey != null)
                {
                    PlayerContext playerContext = reservations[assetPackage.ReservationKey];
                    reservedPlayerNames.Remove(playerContext.PlayerName);
                    reservations.Remove(assetPackage.ReservationKey);
                }

                if (assetPackage.Player != null)
                {
                    Player player = assetPackage.Player;
                    reservedPlayerNames.Remove(player.Name);
                }

                assetsByConnection.Remove(connection);
            }
        }
Ejemplo n.º 3
0
        public Player CreatePlayer(NitroxConnection connection, string reservationKey, out bool wasBrandNewPlayer)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage  = assetsByConnection[connection];
                PlayerContext    playerContext = reservations[reservationKey];
                Validate.NotNull(playerContext);

                wasBrandNewPlayer = playerContext.WasBrandNewPlayer;

                // Load previously persisted data for this player.
                Vector3             position  = playerData.GetPosition(playerContext.PlayerName);
                Optional <NitroxId> subRootId = playerData.GetSubRootId(playerContext.PlayerName);

                // Load a NitroxID for the newly connected Player
                NitroxId id = playerData.GetNitroxId(playerContext.PlayerName);

                Player player = new Player(playerContext, connection, position, id, subRootId);

                assetPackage.Player         = player;
                assetPackage.ReservationKey = null;
                reservations.Remove(reservationKey);

                return(player);
            }
        }
Ejemplo n.º 4
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.IsHardcore)
            {
                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;

            return(new MultiplayerSessionReservation(correlationId, playerId, reservationKey));
        }
Ejemplo n.º 5
0
 public Player GetPlayer(Connection connection)
 {
     lock (assetsByConnection)
     {
         ConnectionAssets assetPackage = null;
         assetsByConnection.TryGetValue(connection, out assetPackage);
         return(assetPackage?.Player);
     }
 }
Ejemplo n.º 6
0
        public MultiplayerSessionReservation ReservePlayerContext(
            NitroxConnection connection,
            PlayerSettings playerSettings,
            AuthenticationContext authenticationContext,
            string correlationId)
        {
            lock (assetsByConnection)
            {
                // TODO: ServerPassword in NitroxClient

                if (!string.IsNullOrEmpty(serverConfig.ServerPassword) && (authenticationContext.ServerPassword.IsEmpty() || (authenticationContext.ServerPassword.Get() != serverConfig.ServerPassword)))
                {
                    MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.Rejected | MultiplayerSessionReservationState.AuthenticationFailed;
                    return(new MultiplayerSessionReservation(correlationId, rejectedState));
                }

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

                string playerName = authenticationContext.Username;
                if (reservedPlayerNames.Contains(playerName))
                {
                    MultiplayerSessionReservationState rejectedState = MultiplayerSessionReservationState.Rejected | MultiplayerSessionReservationState.UniquePlayerNameConstraintViolated;
                    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);
                }

                Player player;
                allPlayersByName.TryGetValue(playerName, out player);

                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;

                return(new MultiplayerSessionReservation(correlationId, playerId, reservationKey));
            }
        }
Ejemplo n.º 7
0
        public Player CreatePlayer(Connection connection, string reservationKey)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage  = assetsByConnection[connection];
                PlayerContext    playerContext = reservations[reservationKey];
                Validate.NotNull(playerContext);

                Player player = new Player(playerContext, connection);
                assetPackage.Player         = player;
                assetPackage.ReservationKey = null;
                reservations.Remove(reservationKey);

                return(player);
            }
        }
Ejemplo n.º 8
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));
            }
        }
Ejemplo n.º 9
0
        public Player CreatePlayer(Connection connection, string reservationKey)
        {
            lock (assetsByConnection)
            {
                ConnectionAssets assetPackage  = assetsByConnection[connection];
                PlayerContext    playerContext = reservations[reservationKey];
                Validate.NotNull(playerContext);

                // Load previously persisted data for this player.
                Vector3           position    = playerData.GetPosition(playerContext.PlayerName);
                Optional <string> subRootGuid = playerData.GetSubRootGuid(playerContext.PlayerName);

                Player player = new Player(playerContext, connection, position, subRootGuid);
                assetPackage.Player         = player;
                assetPackage.ReservationKey = null;
                reservations.Remove(reservationKey);

                return(player);
            }
        }
Ejemplo n.º 10
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));
            }
        }