public static void ApplyPendingConsent(P2PPendingConsent pendingConsent) { if (Recorder.session != null) { Recorder.session.AddSentAndAppliedConsent(pendingConsent.pendingSince); } Debug.Log("Received enough votes! (" + (pendingConsent.answerConsentMessages.Count - 1) + ")."); //enough votes received! Pick the most occuring result int mostOccuringAnswerResult = pendingConsent.answerConsentMessages .GroupBy(acm => acm.result) .OrderByDescending(g => g.Count()) .Select(g => g.Key) .FirstOrDefault(); Debug.Log("Most occuring vote result: " + mostOccuringAnswerResult); //apply it AnswerConsentMessage mostOccuringAnswer = pendingConsent.answerConsentMessages[0]; mostOccuringAnswer.result = mostOccuringAnswerResult; ApplyAndSpreadConsentResult(mostOccuringAnswer); pendingConsents.Remove(pendingConsent); }
static void CreateNetworkReader(byte[] data) { //https://docs.unity3d.com/ScriptReference/Networking.NetworkReader.html NetworkReader networkReader = new NetworkReader(data); // The first two bytes in the buffer represent the size of the message. This is equal to the NetworkReader.Length // minus the size of the prefix. networkReader.ReadBytes(2); //short readerMsgSize = (short)((readerMsgSizeData[1] << 8) + readerMsgSizeData[0]); // The message type added in NetworkWriter.StartMessage is to be read now. It is a short and so consists of // two bytes. It is the second two bytes on the buffer. byte[] readerMsgTypeData = networkReader.ReadBytes(2); short readerMsgType = (short)((readerMsgTypeData[1] << 8) + readerMsgTypeData[0]); //Debug.Log("Message of type " + readerMsgType + " received"); if (readerMsgType == MessageTypes.JoinRequest) { JoinRequestMessage message = new JoinRequestMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinRequest(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.JoinAnswer) { JoinAnswerMessage message = new JoinAnswerMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinAnswer(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.JoinAnnounce) { JoinAnnounceMessage message = new JoinAnnounceMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinAnnounce(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.Position) { PositionMessage message = new PositionMessage(); message.Deserialize(networkReader); p2PController.ReceivePositionInformation(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.AskConsent) { AskConsentMessage message = new AskConsentMessage(); message.Deserialize(networkReader); p2PController.OnAskForConsentMsg(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.AnswerConsent) { AnswerConsentMessage message = new AnswerConsentMessage(); message.Deserialize(networkReader); P2PConsentManager.ReceiveAnswerConsent(message); } else if (readerMsgType == MessageTypes.ApplyConsent) { ApplyConsentMessage message = new ApplyConsentMessage(); message.Deserialize(networkReader); p2PController.ApplyConsent(message); } }
public static void ApplyAndSpreadConsentResult(AnswerConsentMessage message) { ApplyConsentMessage applyMessage = new ApplyConsentMessage(); applyMessage.consentAction = message.consentAction; applyMessage.result = message.result; applyMessage.parameters = message.parameters; P2PSender.SendToAll(P2PChannels.ReliableChannelId, applyMessage, MessageTypes.ApplyConsent); p2PController.ApplyConsent(applyMessage, true); }
public P2PPendingConsent(AskConsentMessage message) { pendingSince = Time.time * 1000.0f; askConsentMessage = message; AnswerConsentMessage am = new AnswerConsentMessage(); am.result = askConsentMessage.result; am.parameters = askConsentMessage.parameters; am.consentAction = askConsentMessage.consentAction; am.consentId = askConsentMessage.consentId; answerConsentMessages.Add(am); }
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 static void ReceiveAnswerConsent(AnswerConsentMessage message) { Debug.Log("ReceiveAnswerConsent for " + message.consentAction + ", id: " + message.consentId); int consentId = message.consentId; P2PPendingConsent pendingConsent = pendingConsents.FirstOrDefault(p => p.askConsentMessage.consentId == consentId); if (pendingConsent != null) { pendingConsent.answerConsentMessages.Add(message); if (pendingConsent.answerConsentMessages.Count - 1 >= P2PConnectionManager.connections.Count) { ApplyPendingConsent(pendingConsent); } } else { Debug.Log("Error: receive answer consent for unsent consent request"); } }