示例#1
0
        private void HandleTurnInfoMessage(TurnInfoMessage msg)
        {
            _logger.Log("Received turn info message from other player: " + JsonSerializer.Serialize(msg));

            StateUpdateInfo receivedInfo = _GameState.Update(msg.TurnInfo);

            bool approved = receivedInfo.Result == StateUpdateResult.Ok;

            if (approved)
            {
                _logger.Log("Received turn info message approved");
            }
            else
            {
                _logger.Log("Error processing turn info: " + receivedInfo.ErrorString);
                Debug.WriteLine("Error processing turn info: " + receivedInfo.ErrorString);
            }

            ResponseMessage response = new ResponseMessage()
            {
                Sender = _UniqueID, Receiver = msg.Sender, Approve = approved, ErrorString = receivedInfo.ErrorString, ReceivedMsgID = msg.MsgID, MsgID = _ResponseMessageID
            };

            _NetworkComms.SendMessage(JsonSerializer.SerializeToUtf8Bytes(response));
            _ResponseMessageNumber++;
        }
示例#2
0
        public void EndTurn()
        {
            _logger.Log("Ending turn...");
            TurnInfo turnInfo = _GameState.GetTurn();

            if (turnInfo == null)
            {
                Debug.WriteLine("Received null turnInfo from GameState");
                return;
            }

            //Use same ID for all turnInfo messages for this turn
            string msgID = _TurnInfoMessageID;

            _TurnInfoMessageNumber++;
            TurnInfoMessage turnInfoMsg = new TurnInfoMessage {
                TurnInfo = turnInfo, Sender = _UniqueID, MsgID = msgID
            };

            byte[] turnInfoMsgBytes = JsonSerializer.SerializeToUtf8Bytes(turnInfoMsg);

            bool       turnApproved    = false;
            List <int> ApprovedPlayers = new List <int>();                             //Players who have approved this turn
            int        requiredNumberOfPlayerApprovals = _LobbyInfo.Players.Count - 1; //-1 because player does not approve local turn

            //TODO, implement some time here? now sends turn max 10 times then fails
            for (int i = 0; i < 10; i++)
            {
                _NetworkComms.SendMessage(turnInfoMsgBytes);

                //Sleep a bit to give others time to check turn and response
                Thread.Sleep(500);

                bool responseAvailable;
                do
                {
                    responseAvailable = ResponseMessages.TryDequeue(out ResponseMessage response);

                    if (responseAvailable == false)
                    {
                        continue;
                    }

                    //Check if response is to our message
                    if (response.Receiver != _UniqueID)
                    {
                        Debug.WriteLine("Receive msg error: response was to some other message"); continue;
                    }

                    //Check if response is to same message as we sent
                    if (response.ReceivedMsgID != msgID)
                    {
                        Debug.WriteLine("Receive msg error: message ID did not match"); continue;
                    }

                    //Log message
                    _logger.Log("Received response to turn info message from " + response.Sender);

                    //Check if this player is in lobby
                    if (IsThisPlayerInLobby(response.Sender) == false)
                    {
                        Debug.WriteLine("Receive msg error: player not in lobby"); continue;
                    }

                    //Check if turn was approved
                    if (response.Approve == false)
                    {
                        Debug.WriteLine("Receive msg error: player did not approve turn, reason: " + response.ErrorString); continue;
                    }

                    //Check if this player has already approved turn
                    if (ApprovedPlayers.Contains(response.Sender) == true)
                    {
                        Debug.WriteLine("Error: player has already approved turn"); continue;
                    }

                    _logger.Log("Other player approved turn, PlayerID: " + response.Sender);
                    ApprovedPlayers.Add(response.Sender);

                    //Check if it was last required approvement
                    if (ApprovedPlayers.Count == requiredNumberOfPlayerApprovals)
                    {
                        break;
                    }
                } while (responseAvailable);

                if (ApprovedPlayers.Count == requiredNumberOfPlayerApprovals)
                {
                    turnApproved = true;
                    break;
                }
            }

            if (turnApproved)
            {
                //All good
                _logger.Log("Turn ended successfully");
                //Clear unnessary responses
                ResponseMessages.Clear();
            }
            else
            {
                _logger.Log("Ending turn FAILED, did not receive approvement from other players");
            }
        }