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 AnswerConsentMessage OnAskForConsentMsg(int hostId, int connectionId, AskConsentMessage message) { Debug.Log("P2P: OnAskForConsentMsg for: " + message.consentAction + ", parameter0: " + message.parameters[0]); AnswerConsentMessage answerMessage = new AnswerConsentMessage(); answerMessage.consentId = message.consentId; answerMessage.consentAction = message.consentAction; if (message.consentAction == ConsentAction.SpawnRocket) { answerMessage.result = gameController.lanes[message.parameters[1]].spawnManager.GetRandomSpawnerIndex(); answerMessage.parameters = message.parameters; //will I cheat? int randomInt = UnityEngine.Random.Range(0, 100); if (randomInt < gameController.cheaterProfileContainer.cheaterProfile.cheatingRate) { if (Recorder.session != null) { Recorder.session.cheatsTried++; } //sending a wrong spawner index answerMessage.result = 100; } if (hostId != -1 && connectionId != -1) //not imposing consent (see AskForConsent) { P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, answerMessage, MessageTypes.AnswerConsent); } } if (message.consentAction == ConsentAction.JoinGame) { answerMessage.parameters = message.parameters; Lane freeLane = gameController.GetFirstUnoccupiedLane(); answerMessage.result = 10; if (freeLane != null) { answerMessage.result = freeLane.id; } if (hostId != -1 && connectionId != -1) //not imposing consent (see AskForConsent) { P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, answerMessage, MessageTypes.AnswerConsent); } } return(answerMessage); }
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"); } } }