public void SetParams(int playerCount, int timesToTick, int playerID)
 {
     // Should only be called if the app is a client.
     this.playerCount = playerCount;
     this.timesToTick = timesToTick;
     this.playerID    = playerID;
     InitImages();
     socket.WriteMessage(Message.Ready);
 }
Exemplo n.º 2
0
 public void SendScores()
 {
     try
     {
         if (PeerFinder.Role == PeerRole.Client || singlePairRole == "client")
         {
             socket.WriteMessage(string.Format("{0} {1} {2}", Constants.OpCodeUpdateScore, PeerFinder.DisplayName, gameScore));
         }
     }
     catch (Exception err)
     {
         infoText.Text += "\nError sending score to host: \n" + err.ToString();
     }
 }
Exemplo n.º 3
0
        // Used to display state of the connection (i.e. connecting, connected, disconnected) when two PCs are tapped.
        private async void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e)
        {
            await messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                bool tapIsProcessed = false;
                switch (e.State)
                {
                case TriggeredConnectState.PeerFound:
                    directions.Visibility    = Visibility.Collapsed;
                    nfcStatusText.Visibility = Visibility.Visible;
                    foundPeers.IsEnabled     = false;

                    progressBar.Visibility = Visibility.Visible;
                    backButton.Visibility  = Visibility.Collapsed;

                    break;

                case TriggeredConnectState.Completed:
                    // Associate socket handed back by TriggeredConnectionStateChanged event with player connectedPeers.
                    socket = new SocketReaderWriter(e.Socket, this);
                    socket.ReadMessage();
                    socket.WriteMessage(string.Format("{0} {1}", Constants.OpCodeSendDisplayName, PeerFinder.DisplayName));
                    startGameButton.Visibility = Visibility.Visible;
                    tapIsProcessed             = true;

                    break;

                case TriggeredConnectState.Failed:
                    tapIsProcessed = true;

                    break;
                }

                // Reset UI
                if (tapIsProcessed)
                {
                    nfcStatusText.Visibility = Visibility.Collapsed;
                    foundPeers.IsEnabled     = true;

                    lock (availablePeers)
                    {
                        if (availablePeers.Count > 0)
                        {
                            directions.Visibility = Visibility.Visible;
                        }
                    }

                    // Progress bar should be visible as long as PeerWatcher is running
                    progressBar.Visibility = Visibility.Collapsed;
                    if (peerWatcher != null)
                    {
                        PeerWatcherStatus status = peerWatcher.Status;
                        if (status == PeerWatcherStatus.EnumerationCompleted || status == PeerWatcherStatus.Started)
                        {
                            progressBar.Visibility = Visibility.Visible;
                        }
                    }
                }
            });
        }
 public void SendParams(SocketReaderWriter socket, int id)
 {
     // Should only be called if the app is a host.
     // Otherwise it has no idea which socket is being used.
     if (PeerFinder.Role == PeerRole.Host)
     {
         // Write the op code, number of players, time limit.
         socket.WriteMessage(string.Format("{0} {1} {2} {3}", Constants.OpCodeParams, playerCount, timesToTick, id));
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        private async void LoadState(Object navigationParameter, Dictionary <String, Object> pageState)
        {
            // Users wait (i.e. watch progress) on this page until the connection has succeeded/failed.
            backButton.Visibility = Visibility.Collapsed;
            if (navigationParameter != null)
            {
                WaitingForHostParameters parameters = (WaitingForHostParameters)navigationParameter;
                if (parameters.Peer != null)
                {
                    pageTitle.Text = "Connecting to " + parameters.Peer.DisplayName.ToString() + "...";
                    try
                    {
                        StreamSocket socket = await PeerFinder.ConnectAsync(parameters.Peer);

                        pageTitle.Text = "Connected! Waiting for Host...";
                        this.socket    = new SocketReaderWriter(socket, this);
                        ConnectedPeer tempPeer = new ConnectedPeer(parameters.Peer.DisplayName);
                        connectedPeers[tempPeer] = this.socket;

                        PeerFinder.Stop();
                        StartReading();
                    }
                    catch (Exception)
                    {
                        pageTitle.Text = "Cannot connect to " + parameters.Peer.DisplayName;
                        PeerFinder.Stop();
                    }
                    progressBar.Visibility = Visibility.Collapsed;
                    backButton.Visibility  = Visibility.Visible;
                }
                else if (parameters.Socket != null)
                {
                    try
                    {
                        this.socket    = new SocketReaderWriter(parameters.Socket, this);
                        pageTitle.Text = "Waiting for Host...";
                        socket.WriteMessage(string.Format("{0} {1}", Constants.OpCodeSendDisplayName, PeerFinder.DisplayName));

                        PeerFinder.Stop();
                        StartReading();
                    }
                    catch (Exception)
                    {
                        pageTitle.Text = "Cannot connect";
                        PeerFinder.Stop();
                    }
                    backButton.Visibility  = Visibility.Visible;
                    progressBar.Visibility = Visibility.Collapsed;
                }
            }
        }
 public void SendParams(SocketReaderWriter socket, int id)
 {
     // Should only be called if the app is a host.
     // Otherwise it has no idea which socket is being used.
     if (PeerFinder.Role == PeerRole.Host)
     {
         // Write the op code, number of players, time limit.
         socket.WriteMessage(string.Format("{0} {1} {2} {3}", Constants.OpCodeParams, playerCount, timesToTick, id));
     }
 }
Exemplo n.º 7
0
        // Used to display state of the connection (i.e. connecting, connected, disconnected) when two PCs are tapped.
        private async void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e)
        {
            await messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                bool tapIsProcessed = false;
                switch (e.State)
                {
                    case TriggeredConnectState.PeerFound:
                        directions.Visibility = Visibility.Collapsed;
                        nfcStatusText.Visibility = Visibility.Visible;
                        foundPeers.IsEnabled = false;

                        progressBar.Visibility = Visibility.Visible;
                        backButton.Visibility = Visibility.Collapsed;

                        break;
                    case TriggeredConnectState.Completed:
                        // Associate socket handed back by TriggeredConnectionStateChanged event with player connectedPeers.
                        socket = new SocketReaderWriter(e.Socket, this);
                        socket.ReadMessage();
                        socket.WriteMessage(string.Format("{0} {1}", Constants.OpCodeSendDisplayName, PeerFinder.DisplayName));
                        startGameButton.Visibility = Visibility.Visible;
                        tapIsProcessed = true;

                        break;
                    case TriggeredConnectState.Failed:
                        tapIsProcessed = true;
                        
                        break;
                }

                // Reset UI
                if (tapIsProcessed)
                {
                    nfcStatusText.Visibility = Visibility.Collapsed;
                    foundPeers.IsEnabled = true;

                    lock (availablePeers)
                    {
                        if (availablePeers.Count > 0)
                        {
                            directions.Visibility = Visibility.Visible;
                        }
                    }

                    // Progress bar should be visible as long as PeerWatcher is running
                    progressBar.Visibility = Visibility.Collapsed;
                    if (peerWatcher != null)
                    {
                        PeerWatcherStatus status = peerWatcher.Status;
                        if (status == PeerWatcherStatus.EnumerationCompleted || status == PeerWatcherStatus.Started)
                        {
                            progressBar.Visibility = Visibility.Visible;
                        }
                    }
                }
            });
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        private async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // Users wait (i.e. watch progress) on this page until the connection has succeeded/failed.
            backButton.Visibility = Visibility.Collapsed;
            if (navigationParameter != null)
            {
                WaitingForHostParameters parameters = (WaitingForHostParameters)navigationParameter;
                if (parameters.Peer != null)
                {
                    pageTitle.Text = "Connecting to " + parameters.Peer.DisplayName.ToString() + "...";
                    try
                    {
                        StreamSocket socket = await PeerFinder.ConnectAsync(parameters.Peer);
                        pageTitle.Text = "Connected! Waiting for Host...";
                        this.socket = new SocketReaderWriter(socket, this);
                        ConnectedPeer tempPeer = new ConnectedPeer(parameters.Peer.DisplayName);
                        connectedPeers[tempPeer] = this.socket;

                        PeerFinder.Stop();
                        StartReading();
                    }
                    catch (Exception)
                    {
                        pageTitle.Text = "Cannot connect to " + parameters.Peer.DisplayName;
                        PeerFinder.Stop();
                    }
                    progressBar.Visibility = Visibility.Collapsed;
                    backButton.Visibility = Visibility.Visible;
                }
                else if (parameters.Socket != null)
                {
                    try
                    {
                        this.socket = new SocketReaderWriter(parameters.Socket, this);
                        pageTitle.Text = "Waiting for Host...";
                        socket.WriteMessage(string.Format("{0} {1}", Constants.OpCodeSendDisplayName, PeerFinder.DisplayName));

                        PeerFinder.Stop();
                        StartReading();
                    }
                    catch (Exception)
                    {
                        pageTitle.Text = "Cannot connect";
                        PeerFinder.Stop();
                    }
                    backButton.Visibility = Visibility.Visible;
                    progressBar.Visibility = Visibility.Collapsed;
                }
            }
        }