public static void OnJoinAnnounce(int hostId, int connectionId, JoinAnnounceMessage message) { Debug.Log("OnJoinAnnounce received"); P2PConnection connection = P2PConnectionManager.GetConnection(hostId, connectionId); connection.SuccessfullyConnect(); }
public void ReceivePositionInformation(int hostId, int connectionId, PositionMessage message) { if (!GameStarted()) { return; } int lane = System.Convert.ToInt32(message.lane); Player player = players.FirstOrDefault(p => p.lane != null && p.lane.id == lane); if (player != null) { player.transform.position = message.position; player.SetHealth(System.Convert.ToInt32(message.hp)); } else { SpawnPlayer(lane); } P2PConnection connection = P2PConnectionManager.GetConnection(hostId, connectionId); if (connection != null) { connection.lane = lane; } }
public static void ConnectEvent(int hostId, int connectionId) { int port; string ip; UnityEngine.Networking.Types.NetworkID netId; UnityEngine.Networking.Types.NodeID nodeId; NetworkTransport.GetConnectionInfo(hostId, connectionId, out ip, out port, out netId, out nodeId, out P2PController.error); P2PConnection connection = P2PConnectionManager.GetConnection(ip, port); if (connection == null) { //new connection from targeted ip or new player connection = new P2PConnection(hostId, connectionId); connection.ip = ip; connection.port = port; connections.Add(connection); Debug.Log("New connection with " + connection); if (!JoinRequestSend) //I'm wanting to join { Debug.Log("Sending Join Request"); JoinRequestSend = true; JoinRequestMessage message = new JoinRequestMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, message, MessageTypes.JoinRequest); } else if (JoinAnswerReceived && !p2PController.GameStarted()) { connection.SuccessfullyConnect(); } } else if (!connection.ConnectionSuccessful()) { //successfully connect to an existing player. Connection requested previously connection.hostId = hostId; connection.connectionId = connectionId; connection.SuccessfullyConnect(); JoinAnnounceMessage announceMessage = new JoinAnnounceMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, announceMessage, MessageTypes.JoinAnnounce); } if (!p2PController.GameStarted()) { if (JoinAnswerReceived) { CheckConnectionsStatus(); } } }
public static void RemoveConnection(int hostId, int connectionId) { P2PConnection connection = P2PConnectionManager.GetConnection(hostId, connectionId); if (connection == null) { Debug.Log("Warning! Connection with " + connection + " doesn't exist"); } else { p2PController.DespawnPlayer(connection.lane); connections.Remove(connection); Debug.Log("Remove connection with " + connection); } }
public void ApplyConsent(ConsentMessage message) { Debug.Log("P2P: Applying consent for: " + message.consentAction); if (message.consentAction == ConsentAction.SpawnRocket) { bool cheating = !gameController.lanes[message.parameters[1]].spawnManager.ValidIndex(message.result); if (!cheating) { gameController.lanes[message.parameters[1]].spawnManager.Spawn(message.result); } else { Debug.Log("Cheat!"); if (Recorder.session != null) { Recorder.session.cheatsPassed++; } } } else if (message.consentAction == ConsentAction.JoinGame) { P2PConnection connection = P2PConnectionManager.GetConnection(message.parameters[0], message.parameters[1]); if (connection != null) { JoinAnswerMessage answerMessage = new JoinAnswerMessage(); answerMessage.lane = message.result; answerMessage.successfulConnections = P2PConnectionManager.GetSuccessfulConnections(); answerMessage.r = Convert.ToInt32(gameColor.r * 255); answerMessage.g = Convert.ToInt32(gameColor.g * 255); answerMessage.b = Convert.ToInt32(gameColor.b * 255); P2PSender.Send(message.parameters[0], message.parameters[1], P2PChannels.ReliableChannelId, answerMessage, MessageTypes.JoinAnswer); connection.SuccessfullyConnect(); Debug.Log("Sending JoinAnswer with lane: " + answerMessage.lane + "and " + answerMessage.successfulConnections.Count + " connections"); } } }
public static void OnJoinAnswer(int hostId, int connectionId, JoinAnswerMessage message) { if (p2PController.GameStarted() || JoinAnswerReceived) { return; } Debug.Log("JoinAnswer received, lane: " + message.lane + ", playersCount: " + message.successfulConnections.Count); if (!(message.lane >= 0 && message.lane < 4)) { Debug.Log("Game is full"); p2PController.myLane = -1; p2PController.DisplayError("Game is full"); return; } else { Debug.Log("allowed to join the game! Now need to connect to all players"); JoinAnswerReceived = true; p2PController.myLane = message.lane; P2PController.gameColor = new Color(message.r / 255.0f, message.g / 255.0f, message.b / 255.0f); GameObject.FindObjectOfType <UIController>().UpdateGameColor(P2PController.gameColor); foreach (P2PConnection connection in message.successfulConnections) { connections.Add(connection); NetworkTransport.Connect(myHostId, connection.ip, connection.port, 0, out P2PController.error); P2PController.CheckError("Connect"); } P2PConnectionManager.GetConnection(hostId, connectionId).SuccessfullyConnect(); CheckConnectionsStatus(); } }