Esempio n. 1
0
        /// <summary>
        /// A player has connected and the player's Player Prefab has been instantiated.
        /// Set the player's identity on the prefab and send the player the map JSON data.
        /// </summary>
        public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            base.OnServerAddPlayer(conn, extraMessage);

            // Send the game data to the connecting client
            NetworkServer.SendToClient <GameMessage>(conn.connectionId, _serverGameData);

            // Update all clients with the new player data
            PlayerMessage playerInfo = _serverPlayerInfo[conn.connectionId];
            PlayerListMessage <PlayerMessage> playerListMessage = new PlayerListMessage <PlayerMessage>();

            playerListMessage.command      = 1;
            playerListMessage.connectionId = conn.connectionId;
            playerListMessage.playerData   = playerInfo;
            NetworkServer.SendToAll <PlayerListMessage <PlayerMessage> >(playerListMessage);

            // Update the new player with all player data
            foreach (KeyValuePair <int, NetworkConnection> entry in NetworkServer.connections)
            {
                if (entry.Value.connectionId != conn.connectionId)
                {
                    playerInfo                     = _serverPlayerInfo[entry.Value.connectionId];
                    playerListMessage              = new PlayerListMessage <PlayerMessage>();
                    playerListMessage.command      = 1;
                    playerListMessage.connectionId = entry.Value.connectionId;
                    playerListMessage.playerData   = playerInfo;
                    NetworkServer.SendToClient <PlayerListMessage <PlayerMessage> >(conn.connectionId, playerListMessage);
                }
            }
        }
Esempio n. 2
0
        // use this to implicitly become ready
        // -> extraMessage can contain character selection, etc.
        public static bool AddPlayer(NetworkConnection readyConn, MessageBase extraMessage)
        {
            // ensure valid ready connection
            if (readyConn != null)
            {
                ready = true;
                readyConnection = readyConn;
            }

            if (!ready)
            {
                Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                return false;
            }

            if (readyConnection.playerController != null)
            {
                Debug.LogError("ClientScene::AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
                return false;
            }

            if (LogFilter.Debug) { Debug.Log("ClientScene::AddPlayer() called with connection [" + readyConnection + "]"); }

            AddPlayerMessage msg = new AddPlayerMessage();
            if (extraMessage != null)
            {
                NetworkWriter writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.value = writer.ToArray();
            }
            readyConnection.Send((short)MsgType.AddPlayer, msg);
            return true;
        }
Esempio n. 3
0
        public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            if (!IsPlayerPrefabValid(conn))
            {
                return;
            }

            if (extraMessage?.value == null)
            {
                base.OnServerAddPlayer(conn, null);
                return;
            }

            /*
             * Deserialize character data from byte[]
             * Format controlled in SpawnPlayerWithLoginServer()
             * Current expected format:
             * {
             *     "name" : "SomePlayerName"
             * }
             */
            string rawJson = System.Text.Encoding.UTF8.GetString(extraMessage.value);
            string name    = CharacterResponse.CreateFromJSON(rawJson).name;

            //Spawn player based on their character choices
            Transform  startPos = GetStartPosition();
            GameObject player   = startPos != null
                ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                : Instantiate(playerPrefab);

            player.name = name;

            NetworkServer.AddPlayerForConnection(conn, player);
        }
Esempio n. 4
0
        internal void OnServerAddPlayerMessageInternal(NetworkMessage netMsg)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal");
            }

            AddPlayerMessage msg = netMsg.ReadMessage <AddPlayerMessage>();

            if (msg.value != null && msg.value.Length > 0)
            {
                // convert payload to extra message and call OnServerAddPlayer
                // (usually for character selection information)
                NetworkMessage extraMessage = new NetworkMessage
                {
                    reader = new NetworkReader(msg.value),
                    conn   = netMsg.conn
                };
                OnServerAddPlayer(netMsg.conn, extraMessage);
            }
            else
            {
                OnServerAddPlayer(netMsg.conn);
            }
        }
        public virtual void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            if (LogFilter.Debug) Debug.Log("NetworkManager.OnServerAddPlayer");

            if (playerPrefab == null)
            {
                Debug.LogError("The PlayerPrefab is empty on the NetworkManager. Please setup a PlayerPrefab object.");
                return;
            }

            if (playerPrefab.GetComponent<NetworkIdentity>() == null)
            {
                Debug.LogError("The PlayerPrefab does not have a NetworkIdentity. Please add a NetworkIdentity to the player prefab.");
                return;
            }

            if (conn.playerController != null)
            {
                Debug.LogError("There is already a player for this connections.");
                return;
            }

            Transform startPos = GetStartPosition();
            GameObject player = startPos != null
                ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                : Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);

            NetworkServer.AddPlayerForConnection(conn, player);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="conn">Connection of the client</param>
        /// <param name="extraMessage"></param>
        public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            if (SceneManager.GetActiveScene().name != RoomScene)
            {
                return;
            }

            if (roomSlots.Count == maxConnections)
            {
                return;
            }

            allPlayersReady = false;

            if (LogFilter.Debug)
            {
                Debug.LogFormat("NetworkRoomManager.OnServerAddPlayer playerPrefab:{0}", roomPlayerPrefab.name);
            }

            GameObject newRoomGameObject = OnRoomServerCreateRoomPlayer(conn);

            if (newRoomGameObject == null)
            {
                newRoomGameObject = (GameObject)Instantiate(roomPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
            }

            NetworkRoomPlayer newRoomPlayer = newRoomGameObject.GetComponent <NetworkRoomPlayer>();

            roomSlots.Add(newRoomPlayer);

            RecalculateRoomPlayerIndices();

            NetworkServer.AddPlayerForConnection(conn, newRoomGameObject);
        }
Esempio n. 7
0
        // use this to implicitly become ready
        // -> extraMessage can contain character selection, etc.
        public static bool AddPlayer(NetworkConnection readyConn, byte[] extraData)
        {
            // ensure valid ready connection
            if (readyConn != null)
            {
                ready           = true;
                readyConnection = readyConn;
            }

            if (!ready)
            {
                Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                return(false);
            }

            if (readyConnection.playerController != null)
            {
                Debug.LogError("ClientScene.AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
                return(false);
            }

            if (LogFilter.Debug)
            {
                Debug.Log("ClientScene.AddPlayer() called with connection [" + readyConnection + "]");
            }

            AddPlayerMessage msg = new AddPlayerMessage()
            {
                value = extraData
            };

            readyConnection.Send(msg);
            return(true);
        }
Esempio n. 8
0
        void OnServerAddPlayerInternal(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager.OnServerAddPlayer");
            }

            if (autoCreatePlayer && playerPrefab == null)
            {
                Debug.LogError("The PlayerPrefab is empty on the NetworkManager. Please setup a PlayerPrefab object.");
                return;
            }

            if (autoCreatePlayer && playerPrefab.GetComponent <NetworkIdentity>() == null)
            {
                Debug.LogError("The PlayerPrefab does not have a NetworkIdentity. Please add a NetworkIdentity to the player prefab.");
                return;
            }

            if (conn.playerController != null)
            {
                Debug.LogError("There is already a player for this connections.");
                return;
            }

            OnServerAddPlayer(conn, extraMessage);
        }
Esempio n. 9
0
        internal void OnServerAddPlayerMessageInternal(NetworkConnection conn, AddPlayerMessage msg)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager.OnServerAddPlayerMessageInternal");
            }

            OnServerAddPlayer(conn, msg);
        }
Esempio n. 10
0
        /// <summary>
        /// Called on the server when a client adds a new player with ClientScene.AddPlayer.
        /// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
        /// </summary>
        /// <param name="conn">Connection from client.</param>
        /// <param name="extraMessage">An extra message object passed for the new player.</param>
        public virtual void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            Transform  startPos = GetStartPosition();
            GameObject player   = startPos != null
                ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                : Instantiate(playerPrefab);

            NetworkServer.AddPlayerForConnection(conn, player);
        }
Esempio n. 11
0
        void OnServerAddPlayerInternal(INetworkConnection conn, AddPlayerMessage msg)
        {
            logger.Log("PlayerSpawner.OnServerAddPlayer");

            if (conn.Identity != null)
            {
                throw new InvalidOperationException("There is already a player for this connection.");
            }

            OnServerAddPlayer(conn);
        }
Esempio n. 12
0
        void OnServerAddPlayerInternal(INetworkConnection conn, AddPlayerMessage msg)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager.OnServerAddPlayer");
            }

            if (conn.Identity != null)
            {
                throw new InvalidOperationException("There is already a player for this connection.");
            }

            OnServerAddPlayer(conn);
        }
Esempio n. 13
0
        void OnServerAddPlayerInternal(NetworkConnection conn, AddPlayerMessage msg)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager.OnServerAddPlayer");
            }


            if (conn.Identity != null)
            {
                Debug.LogError("There is already a player for this connection.");
                return;
            }

            OnServerAddPlayer(conn);
        }
Esempio n. 14
0
        internal void OnServerAddPlayerMessageInternal(NetworkMessage netMsg)
        {
            if (LogFilter.Debug)
            {
                Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal");
            }

            AddPlayerMessage msg = netMsg.ReadMessage <AddPlayerMessage>();

            if (msg.msgData != null && msg.msgData.Length > 0)
            {
                NetworkReader reader = new NetworkReader(msg.msgData);
                OnServerAddPlayer(netMsg.conn, reader);
            }
            else
            {
                OnServerAddPlayer(netMsg.conn);
            }
        }
Esempio n. 15
0
        internal void OnServerAddPlayerMessageInternal(NetworkMessage netMsg)
        {
            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal");
            }

            AddPlayerMessage msg = new AddPlayerMessage();

            netMsg.ReadMessage(msg);

            if (msg.msgData != null && msg.msgData.Length > 0)
            {
                var reader = new NetworkReader(msg.msgData);
                OnServerAddPlayer(netMsg.conn, msg.playerControllerId, reader);
            }
            else
            {
                OnServerAddPlayer(netMsg.conn, msg.playerControllerId);
            }
        }
Esempio n. 16
0
        /// <summary>
        /// This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.
        /// <para>extraMessage can contain character selection, etc.</para>
        /// </summary>
        /// <param name="readyConn">The connection to become ready for this client.</param>
        /// <param name="extraData">An extra message object that can be passed to the server for this player.</param>
        /// <returns>True if player was added.</returns>
        public static bool AddPlayer(NetworkConnection readyConn, byte[] extraData)
        {
            // ensure valid ready connection
            if (readyConn != null)
            {
                ready           = true;
                readyConnection = readyConn;
            }

            if (!ready)
            {
                Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                return(false);
            }

            if (readyConnection.identity != null)
            {
                Debug.LogError(
                    "ClientScene.AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
                return(false);
            }

            if (LogFilter.Debug)
            {
                Debug.Log("ClientScene.AddPlayer() called with connection [" + readyConnection + "]");
            }

            var message = new AddPlayerMessage
            {
#pragma warning disable CS0618 // Type or member is obsolete
                value = extraData
#pragma warning restore CS0618 // Type or member is obsolete
            };

            readyConnection.Send(message);
            return(true);
        }
Esempio n. 17
0
        // use this to implicitly become ready
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + PlayerController.MaxPlayersPerClient);
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient / 2)
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                }
            }

            // fill out local players array
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }

            // ensure valid ready connection
            if (readyConn != null)
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            if (!s_IsReady)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                }
                return(false);
            }

            PlayerController existingPlayerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out existingPlayerController))
            {
                if (existingPlayerController.IsValid && existingPlayerController.gameObject != null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                    }
                    return(false);
                }
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }

            var msg = new AddPlayerMessage();

            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
            }
            s_ReadyConnection.Send((short)MsgType.AddPlayer, msg);
            return(true);
        }
Esempio n. 18
0
 public virtual void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
 {
     OnServerAddPlayerInternal(conn);
 }