/// <summary>
 /// NFC message event handler.
 /// </summary>
 public void MessageReceived(Message msg)
 {
     //  MainPage handles the deal cards -message, which starts a new game.
     // Both devices publish a set of cards, UseCards decides which
     // one to use based on the random id (uniqueid).
     App.CardModel.UseCards(msg);
     App.CardModel.InitCardListing();
     NavigationService.Navigate(new Uri("/GamePage.xaml?" + paramkey + "=1", UriKind.Relative));
 }
        /// <summary>
        /// NFC message event handler.
        /// </summary>
        public void MessageReceived(Message msg)
        {
            // GamePage handles only the showcard -message
            if (msg == null || msg.MessageType != Message.TypeEnum.EShowCard)
            {
                return;
            }

            bool needRepublish = false;
            // Both players must select the same property, e.g. "engine"
            if (msg.SelectedCardProperty != App.CardModel.SelectedCardPropertyName)
            {
                if (App.CardModel.SelectedCardPropertyName == null && msg.SelectedCardProperty != null)
                {
                    App.CardModel.SelectedCardPropertyName = msg.SelectedCardProperty;
                }
                else if (App.CardModel.SelectedCardPropertyName != null && msg.SelectedCardProperty == null)
                {
                    msg.SelectedCardProperty = App.CardModel.SelectedCardPropertyName;
                }
                else
                {
                    App.CardModel.StatusMsg = "Your opponent selected " + msg.SelectedCardProperty;
                    needRepublish = true;
                }
            }
            else if (App.CardModel.SelectedCardPropertyName == null && msg.SelectedCardProperty == null)
            {
                needRepublish = true;
            }

            if (needRepublish)
            {
                ClearSelection();
            }
            else
            {
                // Find the card and decide which one wins
                foreach (CarTrumps.Model.Card card in App.CardModel)
                {
                    if (card.CardId == msg.CardId)
                    {
                        App.CardModel.ChallengerCard = card;
                        App.CardModel.SelectWinnerCard();
                        // Show the winner -control and disable the menu bar buttons
                        NavigationService.Navigate(new Uri("/WinnerPage.xaml", UriKind.Relative));
                        break;
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Publish and subscribe a dealcards -message.
        /// </summary>
        public void DealCards()
        {
            state = ProtoState.DealCard;

            if (IsMaster())
            {
                opponentsCards = App.CardModel.SuffleCards();
                Message msg = new Message(Message.TypeEnum.EDealCards);
                msg.CardIds = opponentsCards;

                // Construct and serialize a dealcards -message.
                MemoryStream mstream = _nfcMessage.SerializeMessage(msg);

                var dataWriter = new Windows.Storage.Streams.DataWriter();
                dataWriter.WriteBytes(mstream.GetBuffer());

                // Publish the message
                _publishedMsgId = _proximityDevice.PublishBinaryMessage("Windows.CarTrumps",
                    dataWriter.DetachBuffer(), NfcWriteCallback);
            }
            else
            {
                // subscribe for a reply
                _subscribedMsgId = _proximityDevice.SubscribeForMessage("Windows.CarTrumps",
                    NfcMessageReceived);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Publish and subscribe a showcard -message.
        /// </summary>
        public void ShowCard()
        {
            state = ProtoState.ShowCard;

            StopAll();

            // Construct and serialize a showcard -message.
            Message msg = new Message(Message.TypeEnum.EShowCard);
            msg.CardId = (ushort)App.CardModel.ActiveCard.CardId;
            msg.SelectedCardProperty = App.CardModel.SelectedCardPropertyName;
            MemoryStream mstream = _nfcMessage.SerializeMessage(msg);

            var dataWriter = new Windows.Storage.Streams.DataWriter();
            dataWriter.WriteBytes(mstream.GetBuffer());

            // Publish the message
            _publishedMsgId = _proximityDevice.PublishBinaryMessage("Windows.CarTrumps",
                dataWriter.DetachBuffer(), NfcWriteCallback);
            // and subscribe for a reply
            _subscribedMsgId = _proximityDevice.SubscribeForMessage("Windows.CarTrumps",
                NfcMessageReceived);
        }
Beispiel #5
0
 /// <summary>
 /// Serializes this message into a stream (XML).
 /// </summary>
 public MemoryStream SerializeMessage(Message message)
 {
     MemoryStream stream = new MemoryStream();
     try
     {
         stream.Position = 0;
         DataContractSerializer serializer = new DataContractSerializer(typeof(Message));
         serializer.WriteObject(stream, message);
     }
     catch (SerializationException ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
     return stream;
 }