void OnServerInitialized() { NetworkPlayerData myPlayerData = NetworkCenter.Instance.GetPlayerData(); string gameName = myPlayerData != null ? myPlayerData.playerName : "guest"; MasterServer.RegisterHost(gameIdentifier, gameName, networkHostAvailable); IsAnouncingServer = true; }
public void ReceiveData(NetworkPlayerData data) { if (!_initialized) { Initialize(); } _lastData = data; _dataBuffer.Add(data); }
// When server is initialized, he's connected void OnServerInitialized() { NetworkPlayerData playerData = GetPlayerData(); playerNumbers[playerData.uniqueId] = FindSlotForPlayer(playerData); if (playerConnectedEvent != null) { playerConnectedEvent(UnityEngine.Network.player.guid); } }
public NetworkChatMessage(string senderGuid, string text, DateTime timeStamp, NetworkChatMessageType type) { this.senderGuid = senderGuid; NetworkPlayerData playerData = NetworkCenter.Instance.GetPlayerData(senderGuid); this.senderName = playerData == null ? string.Empty : playerData.playerName; this.text = text; this.timeStamp = timeStamp; this.type = type; }
// A player left void OnPlayerDisconnectionConfirmed(string guid) { NetworkPlayerData playerData = NetworkCenter.Instance.GetPlayerData(guid); if (playerData != null) { string message = getBotLeaveMessageForPlayerDelegate(playerData); if (message != null) { AddTextMessage(message, guid, NetworkChatMessageType.botMessage); } } }
// A player is not ready void OnPlayerNotReady(string guid) { NetworkPlayerData playerData = NetworkCenter.Instance.GetPlayerData(guid); if (playerData != null && getBotPlayerNotReadyDelegate != null) { string message = getBotPlayerNotReadyDelegate(playerData); if (message != null) { AddTextMessage(message, guid, NetworkChatMessageType.botMessage); } } }
// Get the number of the player given it's network player data public int GetPlayerNumber(NetworkPlayerData playerData) { if (playerData == null) { return(-1); } uint playerNumber; if (playerNumbers.TryGetValue(playerData.uniqueId, out playerNumber)) { return((int)playerNumber); } return(-1); }
private void ClientInputUsernameOnServer(NetworkMessage message) { NetworkPlayerData playerDataMessage = message.ReadMessage <NetworkPlayerData>(); UIManager.CreateMessage("Client said his name is " + playerDataMessage.name, MessageColor.LIGHT_BLUE); int recentJoinedPlayerId = message.conn.connectionId; Room room = roomsManager.PickRoomForPlayer(recentJoinedPlayerId); playerDataMessage.id = recentJoinedPlayerId; playerDataMessage.roomId = room.roomID; connections.Add(recentJoinedPlayerId, message.conn); NetworkServer.SendToAll(MessageConstants.INITIALIZE_PLAYER_IN_ROOM, playerDataMessage); }
void FixedUpdate() { if (!PhotonNetwork.connected) { return; } NetworkPlayerData data = new NetworkPlayerData( new Vector3[] { NVRPlayer.Instance.Head.transform.position, NVRPlayer.Instance.RightHand.transform.position, NVRPlayer.Instance.LeftHand.transform.position }, new Quaternion[] { NVRPlayer.Instance.Head.transform.rotation, NVRPlayer.Instance.RightHand.transform.rotation, NVRPlayer.Instance.LeftHand.transform.rotation }); BinaryFormatter formatter = new BinaryFormatter(); byte[] serializedData = SerializationToolkit.ObjectToByteArray(data); photonView.RPC("UpdateNetworkPlayer", PhotonTargets.Others, serializedData, personalID); }
// Find a suitable player number for the given player uint FindSlotForPlayer(NetworkPlayerData playerData) { // Get all slots in use List <uint> slotsInUse; uint index; if (playerNumbers.TryGetValue(playerData.uniqueId, out index)) { // Theres a slot reserved to this player return(index); } if (playersLocked) { // include those that aren't connected slotsInUse = new List <uint>(playerNumbers.Count); foreach (uint val in playerNumbers.Values) { slotsInUse.Add(val); } } else { // filter those in use only slotsInUse = new List <uint>(players.Count); uint val; foreach (NetworkPlayerData otherPlayerData in players.Values) { if (playerNumbers.TryGetValue(otherPlayerData.uniqueId, out val)) { slotsInUse.Add(val); } } } // Find first slot available slotsInUse.Sort(); index = 0; while (index < slotsInUse.Count && slotsInUse[(int)index] == index) { ++index; } return(index); }
// Connected // When the client connects to the server, we send our player data to it void OnConnectedToServer() { // Send our data to server NetworkPlayerData playerData = GetPlayerData(); if (playerData == null) { // Wops, we can't play without data Debug.LogWarning("No Player Data to anounce to server"); UnityEngine.Network.Disconnect(); return; } byte[] data = serializer.Serialize(playerData); GetComponent <NetworkView>().RPC("OnClientPlayerDataAnounced", RPCMode.Server, data); // wait for server response StartCoroutine(WaitForServerData()); }
private void ClientReadyOnServer(NetworkMessage message) { NetworkPlayerData playerDataMessage = message.ReadMessage <NetworkPlayerData>(); int clientId = playerDataMessage.id; NetworkPlayerData[] networkPlayers = playersManager.players .FindAll(p => p.id != clientId) .Select(pd => new NetworkPlayerData(pd)) .ToArray(); NetworkUpdateRecentJoinedPlayer updateRecentJoinedPlayerMessage = new NetworkUpdateRecentJoinedPlayer(networkPlayers) { isLobbyStillActive = LobbyManager.Instance.isLobbyActive }; NetworkServer.SendToClient(clientId, MessageConstants.RECENT_JOINED_PLAYER, updateRecentJoinedPlayerMessage); }
private void PlayerJoinedInLobbyOnServer(NetworkMessage message) { NetworkPlayerData playerDataMessage = message.ReadMessage <NetworkPlayerData>(); PlayerData playerData = playersManager.FindPlayerById(playerDataMessage.id); if (!LobbyManager.Instance.isLobbyActive) { NetworkServer.SendToClient(playerData.id, MessageConstants.MESSAGE, new NetworkConsoleMessage(0, "Server says: Game already started, have fun!", MessageColor.BLUE)); NetworkServer.SendToClient(playerData.id, MessageConstants.PLAYER_READY, NetworkEmptyMessage.EMPTY); } else { LobbyManager.Instance.PlayersWaiting.Add(playerData); NetworkServer.SendToClient(playerData.id, MessageConstants.JOIN_IN_LOBBY, NetworkEmptyMessage.EMPTY); NetworkServer.SendToClient(playerData.id, MessageConstants.MESSAGE, new NetworkConsoleMessage(0, "Server says: Welcome to EscapeTheRoom lobby. Waiting for others players to start game. When prepared type 'ready' or 'r'.", MessageColor.BLUE)); } }
void FixedUpdate() { if (!PhotonNetwork.inRoom) { return; } NetworkPlayerData data = new NetworkPlayerData( new Vector3[] { NVRPlayer.Instance.transform.position, NVRPlayer.Instance.Head.transform.localPosition, NVRPlayer.Instance.RightHand.transform.localPosition, NVRPlayer.Instance.LeftHand.transform.localPosition, NVRPlayer.Instance.Torso.transform.localPosition }, new Quaternion[] { NVRPlayer.Instance.transform.rotation, NVRPlayer.Instance.Head.transform.localRotation, NVRPlayer.Instance.RightHand.transform.localRotation, NVRPlayer.Instance.LeftHand.transform.localRotation, NVRPlayer.Instance.Torso.transform.localRotation }); byte[] serializedData = SerializationToolkit.ObjectToByteArray(data); photonView.RPC("UpdateNetworkPlayer", PhotonTargets.Others, serializedData, playerID); }
public void Disconnect() { string guid = UnityEngine.Network.player.guid; // Send event notification if (playerDisconnectedEvent != null) { playerDisconnectedEvent(guid); } // disconnect UnityEngine.Network.Disconnect(); // Reset internal state NetworkPlayerData myData = players[UnityEngine.Network.player.guid]; players.Clear(); playerNumbers.Clear(); SetPlayerData(myData); }
void AddPlayerData(int playerNumber, byte[] data, NetworkPlayer sender) { NetworkPlayerData playerData = serializer.Deserialize <NetworkPlayerData>(data); if (playerData == null) { // Woops, invalid player data! Debug.LogWarning("Invalid player data from " + sender.ipAddress); return; } playerNumbers[playerData.uniqueId] = (uint)playerNumber; players[sender.guid] = playerData; // Send notification if (playerConnectedEvent != null) { playerConnectedEvent(sender.guid); } }
/////////////////////////////////// private void InitializePlayerOnClient(NetworkMessage message) { NetworkPlayerData playerDataMessage = message.ReadMessage <NetworkPlayerData>(); Room room = roomsManager.FindRoomById(playerDataMessage.roomId); PlayerData player = playersManager.CreatePlayer(playerDataMessage.id, playerDataMessage.name, room); if (!playersManager.IsActivePlayerSet && isPlayerNameReady) { playersManager.activePlayer = player; playersManager.IsActivePlayerSet = true; } room.playersInRoom.Add(player); if (playersManager.activePlayer.id == playerDataMessage.id) { SendMessageToServer(MessageConstants.RECENT_JOINED_PLAYER, new NetworkPlayerData(player)); } }
private void PlayerReadyOnServer(NetworkMessage message) { NetworkPlayerData playerDataMessage = message.ReadMessage <NetworkPlayerData>(); PlayerData playerData = playersManager.FindPlayerById(playerDataMessage.id); playerData.ready = true; LobbyManager lobbyManager = LobbyManager.Instance; if (lobbyManager.IsEveryoneReady()) { lobbyManager.ClearState(); NetworkServer.SendToAll(MessageConstants.MESSAGE, new NetworkConsoleMessage(0, "Servers says: Enough players in lobby, game will start soon.", MessageColor.BLUE)); StartCoroutine(GameAboutToStartMessage()); } NetworkServer.SendToAll(MessageConstants.MESSAGE, new NetworkConsoleMessage(0, "Servers says: " + playerData.playerName + " is ready.", MessageColor.BLUE)); }
void OnClientPlayerDataAnounced(byte[] data, NetworkMessageInfo info) { // ignore if not server if (!UnityEngine.Network.isServer) { return; } // Deserialize data NetworkPlayerData playerData = serializer.Deserialize <NetworkPlayerData>(data); if (playerData == null) { // Woops, invalid player data! Disconnect it Debug.LogWarning("Invalid player data from " + info.sender.ipAddress); TryCloseConnection(info.sender); return; } // Then see if we have room for this user uint playerNumber = FindSlotForPlayer(playerData); if (playerNumber > UnityEngine.Network.maxConnections) { // No room for it Debug.Log("No room in server for " + playerData.playerName); TryCloseConnection(info.sender); return; } // Everything went well byte[] allPlayersData = serializer.Serialize(players); byte[] allPlayerNumsData = serializer.Serialize(playerNumbers); // send current players to the newcomer GetComponent <NetworkView>().RPC("SetAllPlayersData", info.sender, allPlayersData, allPlayerNumsData); // and notify all players about the newcomer as well GetComponent <NetworkView>().RPC("AddPlayerData", RPCMode.All, (int)playerNumber, data, info.sender); }
public void ReceiveData(NetworkPlayerData data) { _lastData = data; _dataBuffer.Add(data); }
// Default bot not ready message private static string DefaultBotNotReadyMessageForPlayer(NetworkPlayerData playerData) { return(playerData.playerName + " is not ready"); }
// Default bot ready message private static string DefaultBotReadyMessageForPlayer(NetworkPlayerData playerData) { //int playerNum = NetworkCenter.Instance.GetPlayerNumber(playerData); return(playerData.playerName + " is ready"); }
// Default bot leave message private static string DefaultBotLeaveMessageForPlayer(NetworkPlayerData playerData) { return(playerData.playerName + " left"); }
// Default bot enter message private static string DefaultBotEnterMessageForPlayer(NetworkPlayerData playerData) { //int playerNum = NetworkCenter.Instance.GetPlayerNumber(playerData); return(playerData.playerName + " joined"); //with player number: " + NetworkCenter.Instance.GetPlayerNumber(playerData); }
// Set our own player data public void SetPlayerData(NetworkPlayerData data) { players[UnityEngine.Network.player.guid] = data; }
// Get the number of the player given it's network instance public int GetPlayerNumber(NetworkPlayer player) { NetworkPlayerData data = GetPlayerData(player); return(GetPlayerNumber(data)); }
// Get the number of the player given it's network guid public int GetPlayerNumber(string playerGuid) { NetworkPlayerData data = GetPlayerData(playerGuid); return(GetPlayerNumber(data)); }