Пример #1
0
        public void MakeAGuess(int pos)
        {
            var game = mathGames
                       .FirstOrDefault(g => g.Player1.ConnectionId == Context.ConnectionId || g.Player2.ConnectionId == Context.ConnectionId);

            if (game == null || game.IsOver)
            {
                //// No such game exist!
                return;
            }

            if (game.CheckAnswer(Context.ConnectionId, pos))
            {
                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.CorrectAnswer, pos);
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.CorrectAnswer, pos);
                game.Round++;

                if (game.CheckForWinner())
                {
                    var winner = game.Player1Score > game.Player2Score ? game.Player1.Name : game.Player2.Name;

                    mathGames = Remove <MathGame>(mathGames, game);
                    var match = new Match();
                    match.Draw    = false;
                    match.Player1 = game.Player1.Name;
                    match.Player2 = game.Player2.Name;
                    match.Winner  = winner;
                    match.Game    = "MathGame";
                    statsRep.ReportMatch(match);

                    Clients.Client(game.Player1.ConnectionId).InvokeAsync("winner", winner);
                    Clients.Client(game.Player2.ConnectionId).InvokeAsync("winner", winner);
                }
                else
                {
                    Clients.Client(game.Player1.ConnectionId).InvokeAsync("nextQuestion", game);
                    Clients.Client(game.Player2.ConnectionId).InvokeAsync("nextQuestion", game);
                }
            }
            else
            {
                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.WrongAnswer, pos);
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.WrongAnswer, pos);
            }
        }
Пример #2
0
        /// <summary>
        /// Invoked by the player to make a move on the board.
        /// </summary>
        /// <param name="position">The position to place the player</param>
        public void MakeAMoveMemory(int[] positions, string userName)
        {
            //// Lets find a game from our list of games where one of the player has the same connection Id as the current connection has.
            var game = games?.FirstOrDefault(x => x.Player1.ConnectionId == Context.ConnectionId || x.Player2.ConnectionId == Context.ConnectionId);

            if (game == null || game.IsOver)
            {
                //// No such game exist!
                return;
            }

            //// Designate 0 for player 1
            int symbol = 0;

            if (game.Player2.ConnectionId == Context.ConnectionId)

            {
                //// Designate 1 for player 2.
                symbol = 1;
            }

            var player = symbol == 0 ? game.Player1 : game.Player2;

            if (player.WaitingForMove)
            {
                return;
            }


            //// Place the symbol=player and look for a winner after every move.
            //if true end of game
            if (game.Play(symbol, positions))
            {
                var match = new Match();
                match.Draw    = false;
                match.Player1 = game.Player1.Name;
                match.Player2 = game.Player2.Name;
                match.Winner  = player.Name;
                match.Game    = "Memory";

                statsRep.ReportMatch(match);


                Remove <GameMemory>(games, game);
                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.GameOverMemory, $"The winner is {player.Name}");
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.GameOverMemory, $"The winner is {player.Name}");
                player.IsPlaying          = false;
                player.Opponent.IsPlaying = false;
                this.Clients.Client(player.ConnectionId).InvokeAsync(Constants.RegistrationCompleteMemory);
                this.Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.RegistrationCompleteMemory);
            }

            //// If no one won and its a tame draw, update the players that the game is over and let them look for new game to play.
            if (game.IsOver && game.IsDraw)
            {
                Remove <GameMemory>(games, game);
                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.GameOverMemory, "Its a tame draw!!!");
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.GameOverMemory, "Its a tame draw!!!");
                player.IsPlaying          = false;
                player.Opponent.IsPlaying = false;
                this.Clients.Client(player.ConnectionId).InvokeAsync(Constants.RegistrationCompleteMemory);
                this.Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.RegistrationCompleteMemory);
            }

            if (!game.IsOver)
            {
                //måste nog ha in två olika pos för att kunna jämnföra
                int matchOrNot = game.AreThisPairAMatch(symbol, positions);
                //matchOrNot>0 =match
                if (matchOrNot > 0)
                {
                    Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.MoveMadeMemory, new MoveInformationMemory {
                        OpponentName = player.Name, ImagePosition = positions[0], Image = player.Image
                    }, userName);
                    Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.MoveMadeMemory, new MoveInformationMemory {
                        OpponentName = player.Name, ImagePosition = positions[1], Image = player.Image
                    }, userName);

                    //player.WaitingForMove = !player.WaitingForMove;
                    //player.Opponent.WaitingForMove = !player.Opponent.WaitingForMove;

                    Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.MoveMadeMemory, new MoveInformationMemory {
                        OpponentName = player.Name, ImagePosition = positions[0], Image = player.Image
                    }, userName);
                    Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.MoveMadeMemory, new MoveInformationMemory {
                        OpponentName = player.Name, ImagePosition = positions[1], Image = player.Image
                    }, userName);

                    //player.WaitingForMove = !player.WaitingForMove;
                    //player.Opponent.WaitingForMove = !player.Opponent.WaitingForMove;
                }
                else
                {
                    //players shift "sides"
                    player.WaitingForMove          = !player.WaitingForMove;
                    player.Opponent.WaitingForMove = !player.Opponent.WaitingForMove;

                    Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.WaitingForOpponentMemory, player.Opponent.Name);
                    Clients.Client(player.ConnectionId).InvokeAsync(Constants.WaitingForOpponentMemory, player.Opponent.Name);

                    Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.WaitingforMoveCheck, player.Opponent.Name);
                    Clients.Client(player.ConnectionId).InvokeAsync(Constants.WaitingforMoveCheck, player.Opponent.Name);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Invoked by the player to make a move on the board.
        /// </summary>
        /// <param name="position">The position to place the player</param>
        public void MakeAMove(int position)
        {
            //// Lets find a game from our list of games where one of the player has the same connection Id as the current connection has.
            var game = games?.FirstOrDefault(x => x.Player1.ConnectionId == Context.ConnectionId || x.Player2.ConnectionId == Context.ConnectionId);

            if (game == null || game.IsOver)
            {
                //// No such game exist!
                return;
            }

            //// Designate 0 for player 1
            int symbol = 0;

            if (game.Player2.ConnectionId == Context.ConnectionId)
            {
                //// Designate 1 for player 2.
                symbol = 1;
            }

            var player = symbol == 0 ? game.Player1 : game.Player2;

            if (player.WaitingForMove)
            {
                return;
            }

            //// Update both the players that
            Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.MoveMade, new MoveInformation {
                OpponentName = player.Name, ImagePosition = position, Image = player.Image
            });
            Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.MoveMade, new MoveInformation {
                OpponentName = player.Name, ImagePosition = position, Image = player.Image
            });

            //// Place the symbol and look for a winner after every move.
            if (game.Play(symbol, position))
            {
                var match = new Match();
                match.Player1 = game.Player1.Name;
                match.Player2 = game.Player2.Name;
                match.Draw    = false;
                match.Winner  = player.Name;
                match.Game    = "Tic-Tac-Toe";
                statsRep.ReportMatch(match);

                Remove <Game>(games, game);

                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.GameOver, $"The winner is {player.Name}");
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.GameOver, $"The winner is {player.Name}");
                player.IsPlaying          = false;
                player.Opponent.IsPlaying = false;

                this.Clients.Client(player.ConnectionId).InvokeAsync(Constants.RegistrationComplete);
                this.Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.RegistrationComplete);
            }

            //// If no one won and its a tame draw, update the players that the game is over and let them look for new game to play.
            if (game.IsOver && game.IsDraw)
            {
                Remove <Game>(games, game);

                var match = new Match();
                match.Player1 = game.Player1.Name;
                match.Player2 = game.Player2.Name;
                match.Draw    = true;
                match.Winner  = "none";
                match.Game    = "Tic-Tac-Toe";
                statsRep.ReportMatch(match);

                Clients.Client(game.Player1.ConnectionId).InvokeAsync(Constants.GameOver, "Its a tame draw!!!");
                Clients.Client(game.Player2.ConnectionId).InvokeAsync(Constants.GameOver, "Its a tame draw!!!");
                player.IsPlaying          = false;
                player.Opponent.IsPlaying = false;
                this.Clients.Client(player.ConnectionId).InvokeAsync(Constants.RegistrationComplete);
                this.Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.RegistrationComplete);
            }

            if (!game.IsOver)
            {
                player.WaitingForMove          = !player.WaitingForMove;
                player.Opponent.WaitingForMove = !player.Opponent.WaitingForMove;

                Clients.Client(player.Opponent.ConnectionId).InvokeAsync(Constants.WaitingForOpponent, player.Opponent.Name);
                Clients.Client(player.ConnectionId).InvokeAsync(Constants.WaitingForOpponent, player.Opponent.Name);
            }
        }