Exemplo n.º 1
0
        /// <summary>
        /// Checks if the client is in a game.
        /// If so, the model attemps to fetch his opponent.
        ///     if the opponent exists, a reply is sent to him.
        ///     otherwise the function exists.
        /// if the game does not exist the function exists.
        /// </summary>
        /// <param name="from">the client that sent the command.</param>
        /// <param name="commandParsed">The parsed command.</param>
        public override void Execute(object from, string[] commandParsed)
        {
            string     move        = commandParsed[1];
            int        commandType = 4;
            object     otherClient;
            PlayAnswer ans = new PlayAnswer();

            // retrieve game
            MultiplayerGame game = model.IsClientInGame(from);

            if (game == null)
            {
                return;
            }

            // get the second player from the game
            game.RetrieveOtherClient(from, out otherClient);

            // a client tries to play while he's the only one in the game
            if (otherClient == null)
            {
                return;
            }

            ans.Name = game.GetName();
            ans.Move = move;
            string reply = new Answer().GetJSONAnswer(commandType, ans);

            model.CompletedTask(otherClient, new View.MessageEventArgs(reply));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles the server respones.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="ResponseEventArgs"/> instance containing the event data.</param>
        private void ServerResponseHandler(object sender, ResponseEventArgs args)
        {
            // server disconnected
            if (args.Response == null)
            {
                this.isConnected = false;
                return;
            }
            IServerAnswer answer = this.answersFactory.GetJsonAnswer(args.Response);

            switch ((answer as ServerAnswer).Type)
            {
            //  received response with a solved maze
            case 2:
                if (this.multiPlayerMaze != null)
                {
                    this.multiPlayerMaze.PlayerSolution = ((answer as ServerAnswer).Content as SolveAnswer).Maze;
                }
                break;

            // received response with multiplayer data
            case 3:
                this.multiPlayerMaze = new MultiPlayerMaze((answer as ServerAnswer).Content as MultiplayerAnswer, rows, cols, this.server);
                // request solution
                this.server.SendRequest("solve " + this.multiPlayerMaze.PlayerMazeName + " 0");

                // set the data
                this.PlayerMazeName   = this.multiPlayerMaze.PlayerMazeName;
                this.PlayerMazeString = this.multiPlayerMaze.PlayerMaze;
                this.PlayerPosition   = this.multiPlayerMaze.PlayerPosition;

                this.OpponentMazeName   = this.multiPlayerMaze.OpponentMazeName;
                this.OpponentMazeString = this.multiPlayerMaze.OpponentMaze;
                this.OpponentPosition   = this.multiPlayerMaze.OpponentPosition;

                this.PlayerWonGame   = false;
                this.OpponentWonGame = false;
                break;

            // received response with move data
            case 4:
                PlayAnswer playAns = (answer as ServerAnswer).Content as PlayAnswer;
                if (playAns != null)
                {
                    // capitalize first letter of move direction for enum
                    string playMove = char.ToUpper(playAns.Move[0]) + playAns.Move.Substring(1);
                    var    content  = (Move)Enum.Parse(typeof(Move), playMove);
                    // move opponent accordingly
                    switch (content)
                    {
                    case Move.Up:
                        this.multiPlayerMaze.OpponentPosition.Row -= 2;
                        break;

                    case Move.Down:
                        this.multiPlayerMaze.OpponentPosition.Row += 2;
                        break;

                    case Move.Left:
                        this.multiPlayerMaze.OpponentPosition.Col -= 2;
                        break;

                    case Move.Right:
                        this.multiPlayerMaze.OpponentPosition.Col += 2;
                        break;

                    default:
                        break;
                    }
                    // notify of movement
                    this.NotifyPropertyChanged(PlayerType.Opponent, "PlayerPosition");
                    this.NotifyPropertyChanged(PlayerType.Opponent, "WonGame");
                    this.NotifyPropertyChanged(PlayerType.Opponent, "LostGame");
                }
                break;
            }
        }