Exemplo n.º 1
0
        /* Construtor padrão para a classe RequestMessage, processa a mensagem
         *   de requisão XML recebida de um cliente e armazena seu conteúdo
         *   nos atributos globais da classe.
         * Parâmetros:
         *   request: mensagem XML de requisição (enviada por uma aplicação
         *            cliente)
         *   address: endereço IP e porta da aplicação cliente que enviou
         *            a mensagem ao servidor.
         */
        public RequestMessage(String request, AddressPort address)
        {
            REQUEST = request;
            OK      = false;
            Regex           is_request = new Regex(@"<REQUEST>(.+)</REQUEST>", RegexOptions.IgnoreCase);
            MatchCollection m_class    = is_request.Matches(request);

            if (m_class.Count > 0)
            {
                request = m_class[0].Groups[1].ToString();
                Regex kind_of_request = new Regex(@"(.+)<(.+)>(.+)<.+>", RegexOptions.IgnoreCase);
                m_class = kind_of_request.Matches(request);
                Console.WriteLine(request);
                while (m_class.Count > 0)
                {
                    String type  = m_class[0].Groups[2].ToString();
                    String value = m_class[0].Groups[3].ToString();
                    request = m_class[0].Groups[1].ToString();
                    m_class = kind_of_request.Matches(request);
                    if (!(update_data(type, value)))
                    {
                        Console.Error.WriteLine("Field not known: " + type + " => " + value);
                    }
                }
                kind_of_request = new Regex(@"<(.+)>(.+)<(.+)>", RegexOptions.IgnoreCase);
                m_class         = kind_of_request.Matches(request);
                if (m_class.Count > 0)
                {
                    String type  = m_class[0].Groups[1].ToString();
                    String value = m_class[0].Groups[2].ToString();

                    if (!(update_data(type, value)))
                    {
                        Console.Error.WriteLine("Field not known: " + type + " => " + value);
                    }
                    OK = true;
                }
            }
        }
        private static AnswerMessage newGame(RequestMessage message)
        {
            AddressPort ClientAddress = message.REQUESTER_ADDRESS;

            manager.Game NEW_GAME  = null;
            String       game_type = message.GT;
            String       game_name = message.GN;
            int          player1   = message.PLAYER;

            Console.WriteLine("NEW GAME: game type " + game_type + ", game name: " + game_name + ", player id:" + player1);
            if (game_type.Equals("checkers", StringComparison.CurrentCultureIgnoreCase))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.CheckersGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.CheckersGame(game_name, player1);
                }
            }
            else if (game_type.Equals("novojogo", StringComparison.CurrentCultureIgnoreCase))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.NovoJogoGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.NovoJogoGame(game_name, player1);
                }
            }

            else if ((game_type.Equals("tictactoe", StringComparison.CurrentCultureIgnoreCase)) || (game_type.Equals("tic tac toe", StringComparison.CurrentCultureIgnoreCase)))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.TictactoeGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.TictactoeGame(game_name, player1);
                }
            }
            else if (game_type.Equals("deflexion", StringComparison.CurrentCultureIgnoreCase))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.DeflexionGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.DeflexionGame(game_name, player1);
                }
            }
            else if (game_type.Equals("chess", StringComparison.CurrentCultureIgnoreCase))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.ChessGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.ChessGame(game_name, player1);
                }
            }
            else if (game_type.Equals("checkers", StringComparison.CurrentCultureIgnoreCase))
            {
                if (player1 < 0)
                {
                    NEW_GAME = new manager.CheckersGame(game_name);
                }
                else
                {
                    NEW_GAME = new manager.CheckersGame(game_name, player1);
                }
            }


            if (NEW_GAME != null)
            {
                game_last_id        = 1 + game_last_id;
                NEW_GAME.GAME_ENDED = false;
                //if (GAMES.ContainsKey(game_last_id))
                GAMES.Add(game_last_id, NEW_GAME);

                Console.WriteLine("   => NEW GAME CREATED " + game_last_id + " players:  " + ((manager.Game)(GAMES[game_last_id])).getPlayers().Count);
                GAMES_PLAYERS.Add(game_last_id, new ArrayList());
                if (player1 >= 0)
                {
                    ((ArrayList)(GAMES_PLAYERS[game_last_id])).Add(ClientAddress);
                    Console.WriteLine("SIZE: " + ((ArrayList)(GAMES_PLAYERS[game_last_id])).Count);
                }

                return(new AnswerMessage(true, AnswerMessage.NEW_GAME, "<GID>" + game_last_id + "</GID><LINES>" + ((manager.Game)GAMES[game_last_id]).getLines() + "</LINES><COLUMNS>" + ((manager.Game)GAMES[game_last_id]).getColumns() + "</COLUMNS>", game_last_id));
            }
            return(new AnswerMessage(false, AnswerMessage.NEW_GAME, "", -1));
        }
        private static AnswerMessage joinGame(RequestMessage message)
        {
            int         gameID        = message.GID;
            AddressPort ClientAddress = message.REQUESTER_ADDRESS;

            Console.Write("JOIN GAME steps: 1");
            if (GAMES.ContainsKey(gameID))
            {
                Console.Write(" 2");
                if ((manager.Game)GAMES[gameID] != null)
                {
                    Console.Write(" 3");

                    if (((manager.Game)GAMES[gameID]).getPlayers().Count < ((manager.Game)GAMES[gameID]).getMaxPlayers())
                    {
                        Console.Write(" 4");
                        if (((manager.Game)GAMES[gameID]).WAITING_PLAYERS)
                        {
                            Console.Write(" 5");
                            if (((manager.Game)GAMES[gameID]).joinGame(message.PLAYER))
                            {
                                ((ArrayList)GAMES_PLAYERS[gameID]).Add(ClientAddress);

                                ArrayList TempPlayers   = ((manager.Game)GAMES[gameID]).getPlayers();
                                String    StringPlayers = "";
                                int       PlayerNumber  = TempPlayers.Count;
                                String    PlayerTag     = "";
                                if (PlayerNumber == 1)
                                {
                                    PlayerTag = AnswerMessage.PLAYER1;
                                }
                                if (PlayerNumber == 2)
                                {
                                    PlayerTag = AnswerMessage.PLAYER2;
                                }
                                if (TempPlayers != null)
                                {
                                    for (int cont = 0; cont < TempPlayers.Count; cont++)
                                    {
                                        StringPlayers += (int)TempPlayers[cont] + ";";
                                    }
                                }
                                Console.WriteLine("");
                                Console.WriteLine("Number of players: " + ((ArrayList)GAMES_PLAYERS[gameID]).Count);
                                return(new AnswerMessage(true, AnswerMessage.JOIN_GAME, "<GID>" + gameID + "</GID><GN>" + ((manager.Game)GAMES[gameID]).getGameName() + "</GN><GT>" + ((manager.Game)GAMES[gameID]).getGameType() + "</GT><LINES>" + ((manager.Game)GAMES[gameID]).getLines() + "</LINES><COLUMNS>" + ((manager.Game)GAMES[gameID]).getColumns() + "</COLUMNS><PLAYERS>" + StringPlayers + "</PLAYERS>" + PlayerTag, gameID));
                            }
                            else
                            {
                                return(new AnswerMessage(false, AnswerMessage.CANNOT_JOIN_GAME, "<GID>" + gameID + "</GID><REASON>THE GAME IS NOT WAITING FOR PLAYERS</REASON>", gameID));
                            }
                        }
                        else
                        {
                            return(new AnswerMessage(false, AnswerMessage.CANNOT_JOIN_GAME, "<GID>" + gameID + "</GID><REASON>THE GAME IS NOT WAITING FOR PLAYERS</REASON>", gameID));
                        }
                    }
                    else
                    {
                        return(new AnswerMessage(false, AnswerMessage.CANNOT_JOIN_GAME, "<GID>" + gameID + "</GID><REASON>THE GAME IS FULL OF PLAYERS</REASON>", gameID));
                    }
                }
                else
                {
                    return(new AnswerMessage(false, AnswerMessage.GAME_DOES_NOT_EXIST, "<GID>" + gameID + "</GID>", gameID));
                }
            }
            else
            {
                return(new AnswerMessage(false, AnswerMessage.GAME_DOES_NOT_EXIST, "<GID>" + gameID + "</GID>", gameID));
            }
        }