예제 #1
0
 private async void OnTimedEvent(object sender, ElapsedEventArgs e)
 {
     if (isPlayer1)
     {
         if (timeCount > 0)
         {
             GameMessage newMessage = new GameMessage()
             {
                 Type = "time", Content = 1
             };
             await SignalRService.SendMessage(newMessage);
         }
         else
         {
             GameTimer.Stop();
         }
     }
 }
예제 #2
0
        public static async void Player2Datapass()
        {
            GameMessage newMessage = new GameMessage()
            {
                Type = "round_p2", RoomId = gameInfo.RoomId
            };
            await SignalRService.SendMessage(newMessage);

            newMessage = new GameMessage()
            {
                Type = "guess_p2", RoomId = gameInfo.RoomId
            };
            await SignalRService.SendMessage(newMessage);

            newMessage = new GameMessage()
            {
                Type = "point_p2", RoomId = gameInfo.RoomId
            };
            await SignalRService.SendMessage(newMessage);
        }
예제 #3
0
 public async void StopGameWaiting()
 {
     await SignalRService.MultiplayerStopWaiting(me);
 }
예제 #4
0
 public async void SetupGame()
 {
     await SignalRService.MultiplayerSetup(me);
 }
예제 #5
0
        public async void GameConnect()
        {
            if (string.IsNullOrEmpty(UserData.GetUserLabel()))
            {
                return;
            }
            try
            {
                me.UserName = UserData.GetUserLabel()?.Trim();
                me.UserId   = Guid.NewGuid().ToString();

                // spinner.IsVisible = true;
                //    spinner.IsRunning = true;
                //    lblMessages.Text = "Getting connection info...";

                SignalRConnectionInfo connectionInfo = await SignalRService.GetSignalRConnectionInfo();

                var url = connectionInfo.Url;
                //   lblMessages.Text = $"Connecting...";

                connection = new HubConnectionBuilder()
                             .WithUrl(url, options =>
                {
                    options.AccessTokenProvider = () => Task.FromResult(connectionInfo.AccessToken);
                })
                             .Build();

                //connection.Closed += async (error) =>
                //{
                //    //Optional code: only used for retrying to connect
                //    //await Task.Delay(new Random().Next(0, 5) * 1000);
                //    //await connection.StartAsync();
                //};
                #region Connection
                //Waiting for other player
                connection.On <GamePlayer>("waiting", (player) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        if (player.UserId == me.UserId)
                        {
                            //     lblStatus.Text = "Waiting for players...";
                            //      waitingSpinner.IsVisible = true;
                            //          waitingSpinner.IsRunning = true;

                            //          btnSetup.IsVisible = false;
                            //         btnStopWaiting.IsEnabled = true;
                            //           btnStopWaiting.IsVisible = true;
                        }
                    });
                });

                //2 player game successfully created (method name MUST be 'newgame'
                connection.On <GameInfo>("newgame", (info) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        gameInfo          = info;
                        var currentPlayer = info.Players.Find(x => x.UserId == me.UserId);
                        //  multiplayerGameContent.IsVisible = true;
                        //    lblStatus.Text = $"Game setup successful, you are player {currentPlayer.PlayerNumber}";
                        if (currentPlayer.PlayerNumber == 1)
                        {
                            isPlayer1 = true;
                            ///      btnStartTimer.IsEnabled = true;
                        }
                        else
                        {
                            isPlayer1 = false;
                            //         btnStartTimer.IsEnabled = false;
                        }

                        //Fill label with game content
                        //      lblGameContent.Text = $"Room Id: {info.RoomId}{Environment.NewLine}Player 1: {info.Players[0].UserName}{Environment.NewLine}Player 2: {info.Players[1].UserName}{Environment.NewLine}Toks:{Environment.NewLine}";

                        for (int i = 0; i < info.Toks.Count; ++i)
                        {
                        }
                        //            lblGameContent.Text += $"- {info.Toks[i].PrimaryFieldText}{Environment.NewLine}";

                        //           btnStopWaiting.IsEnabled = false;
                        //            btnStopWaiting.IsVisible = false;
                        //           waitingSpinner.IsVisible = false;
                        //            waitingSpinner.IsRunning = true;
                    });
                });
                #endregion
                #region Rounds and guesses

                connection.On <GameMessage>("log", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var newMessage = $"{message.Player.UserName}: {message.Type}";
                            //      lblMessages.Text += Environment.NewLine + newMessage;
                        }
                    });
                });

                connection.On <GameMessage>("round_p1", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var num = ++roundNumP1;
                            //  lblRoundP1.Text = (num).ToString();
                        }
                    });
                });
                connection.On <GameMessage>("guess_p1", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var num = ++guessNumP1;
                            //      lblGuessP1.Text = (num).ToString();
                        }
                    });
                });
                connection.On <GameMessage>("round_p2", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var num = ++roundNumP2;
                            //          lblRoundP2.Text = (num).ToString();
                        }
                    });
                });
                connection.On <GameMessage>("guess_p2", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var num = ++guessNumP2;
                            //        lblGuessP2.Text = (num).ToString();
                        }
                    });
                });
                #endregion

                //2 player "Elapsed" function
                connection.On <GameMessage>("time", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var num    = (int)message.Content;
                            timeCount += num;
                            //         lblTimer.Text = (timeCount).ToString();
                        }
                    });
                });

                //2 player "Start/Stop" function
                connection.On <GameMessage>("startstoptimer", (message) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        bool allowMessage = true;
                        if (onlyAllowRoomId == true && gameInfo.RoomId != message.RoomId && !string.IsNullOrEmpty(gameInfo.RoomId))
                        {
                            allowMessage = false;
                        }

                        if (allowMessage)
                        {
                            var timerIsRunning = Convert.ToBoolean(message.Content);

                            if (timerIsRunning)
                            {
                                if (isPlayer1)
                                {
                                    GameTimer.Stop();
                                }

                                TimerIsRunning = false;
                                //            btnStartTimer.Text = "Start Timer";
                            }
                            else
                            {
                                if (isPlayer1)
                                {
                                    GameTimer.Start();
                                }

                                TimerIsRunning = true;
                                //           btnStartTimer.Text = "Stop Timer";
                            }
                        }
                    });
                });

                await connection.StartAsync();

                //   lblMessages.Text = $"Connected.{Environment.NewLine}Id: {me.UserId}{Environment.NewLine}Name: {me.UserName}";
                //btnConnect.Text = connectionInfo.AccessToken;
                //     btnConnect.IsEnabled = false;

                //      gridRoundGuess.IsVisible = true;
                //       btnGuessP1.IsEnabled = true;
                //       btnGuessP2.IsEnabled = true;
                //          btnRoundP1.IsEnabled = true;
                //         btnRoundP2.IsEnabled = true;

                //            lblStatus.Text = "Click below to start a game:";
                //           btnSetup.IsEnabled = true;

                //             spinner.IsVisible = false;
                //           spinner.IsEnabled = false;
            }
            catch (Exception ex)
            {
                //        lblMessages.Text = ex.Message;
            }
        }