Example #1
0
void Client::handleData(boost::system::error_code ec)
{
    if (!ec)
    {
        std::istream is(&data);
        std::string str;
        std::getline(is, str);

        boost::erase_all(str,"\r");

        std::vector<std::string> tokens;


        boost::split(tokens, str, boost::is_any_of(" "));

        if (!tokens.empty())
        {
            if ((tokens[0] == "<12>")&&(tokens[27]!="none"))
            {
                int row1;
                int col1;
                int row2;
                int col2;

                if (tokens[27]=="o-o")
                {
                    if (tokens[9]=="W")
                    {
                        row1 = 7;
                        col1 = 4;
                        row2 = 7;
                        col2 = 6;
                    }else{
                        row1 = 0;
                        col1 = 4;
                        row2 = 0;
                        col2 = 6;
                    }
                }else if (tokens[27]=="o-o-o"){
                    if (tokens[9]=="W")
                    {
                        row1 = 7;
                        col1 = 4;
                        row2 = 7;
                        col2 = 2;
                    }else{
                        row1 = 0;
                        col1 = 4;
                        row2 = 0;
                        col2 = 2;
                    }

                }else{
                    row1 = std::stoi(tokens[27].substr(3,1)) - 1;
                    col1 = stringToCol(tokens[27].substr(2,1));
                    row2 = std::stoi(tokens[27].substr(6,1)) - 1;
                    col2 = stringToCol(tokens[27].substr(5,1));
                }

                int whiteTime = std::stoi(tokens[24]);
                int blackTime = std::stoi(tokens[25]);

                Piece promotionPiece;

                if (tokens[27].size()==9){ //means promotion
                    promotionPiece = symbolToPiece(tokens[27].substr(8,1));
                }else promotionPiece = Piece::None;


                positionReady({{row1,col1},{row2,col2}}, whiteTime, blackTime, promotionPiece);
            }else if (tokens[0] == "Creating:"){
                int time = 60*std::stoi(tokens[7]);
                if (tokens[1] == nickname) startGame(Color::White, time, tokens[1], tokens[3]);
                else startGame(Color::Black, time, tokens[1], tokens[3]);
            }else if (tokens[0] == "{Game"){
                std::string toCheck = tokens.back();
                if (toCheck == "1-0") gameEnd(Color::White);
                else if (toCheck == "0-1") gameEnd(Color::Black);
                else if (toCheck == "1/2-1/2") gameEnd(Color::Both);
            }else if ((tokens[0]=="****")&&(tokens[1]=="Starting")&&(tokens[2]=="FICS")){
                //succesfully logged in
                boost::erase_all(tokens[5],"(U)");
                boost::erase_all(tokens[5],"(R)");
                nickname = tokens[5];
                toClient("set style 12");
            }else if (tokens[0] != "fics%"){
                //not processed, so let's print it
                textReady(str);
            }
        }

        boost::asio::async_read_until(socket, data, "\n\r",
                boost::bind(&Client::handleData, this, _1));

    }
}
Example #2
0
void Client::handleData(boost::system::error_code ec)
{
    if (!ec)
    {
        std::istream is(&data);
        std::string str;
        while(std::getline(is, str)){

            boost::erase_all(str,"\r");

            std::vector<std::string> tokens;

            boost::split(tokens, str, boost::is_any_of(" "));

//            namespace qi = boost::spirit::qi;
//            using qi::phrase_parse;
//            using qi::int_;
//            using qi::space;


//            phrase_parse(str.begin(), str.end(),
//                         '<' >> int_[&print] >> '>' >> *boost::spirit::qi::char_[&print_string],
//                         space);

//            <12> rnbqk--r ppp--ppp -----n-- ---p--N- --P-p--- B-P---P- P--PPP-P R--QKB-R B -1 1 1 1 1 1
//            197 chessbadboy SevenChecks 0 60 0 36 36 3511 3086 7 B/c1-a3 (0:45) Ba3 0 1 0

            if (!tokens.empty())
            {
                if ((tokens[0] == "<12>")&&(tokens[27]!="none"))
                {
                    int row1;
                    int col1;
                    int row2;
                    int col2;

                    if (tokens[27]=="o-o")
                    {
                        if (tokens[9]=="W")
                        {
                            row1 = 7;
                            col1 = 4;
                            row2 = 7;
                            col2 = 6;
                        }else{
                            row1 = 0;
                            col1 = 4;
                            row2 = 0;
                            col2 = 6;
                        }
                    }else if (tokens[27]=="o-o-o"){
                        if (tokens[9]=="W")
                        {
                            row1 = 7;
                            col1 = 4;
                            row2 = 7;
                            col2 = 2;
                        }else{
                            row1 = 0;
                            col1 = 4;
                            row2 = 0;
                            col2 = 2;
                        }

                    }else{
                        row1 = std::stoi(tokens[27].substr(3,1)) - 1;
                        col1 = stringToCol(tokens[27].substr(2,1));
                        row2 = std::stoi(tokens[27].substr(6,1)) - 1;
                        col2 = stringToCol(tokens[27].substr(5,1));
                    }

                    int whiteTime = std::stoi(tokens[24]);
                    int blackTime = std::stoi(tokens[25]);

                    Piece promotionPiece;

                    if (tokens[27].size()==9){ //means promotion
                        promotionPiece = symbolToPiece(tokens[27].substr(8,1));
                    }else promotionPiece = Piece::None;


                    positionReady({{row1,col1},{row2,col2}}, whiteTime, blackTime, promotionPiece);
                }else if (tokens[0] == "Creating:"){
                    int time = 60*std::stoi(tokens[7]);
                    if (tokens[1] == nickname) startGame(Color::White, time, tokens[1], tokens[3]);
                    else startGame(Color::Black, time, tokens[1], tokens[3]);
                }else if (tokens[0] == "{Game"){
                    std::string toCheck = tokens.back();
                    if (toCheck == "1-0")
                        messages.triggerEvent(EndGameMessage(Color::White));
                    else if (toCheck == "0-1")
                        messages.triggerEvent(EndGameMessage(Color::Black));
                    else if (toCheck == "1/2-1/2")
                        messages.triggerEvent(EndGameMessage(Color::Both));
                }else if ((tokens[0]=="****")&&(tokens[1]=="Starting")&&(tokens[2]=="FICS")){
                    //succesfully logged in
                    boost::erase_all(tokens[5],"(U)");
                    boost::erase_all(tokens[5],"(R)");
                    nickname = tokens[5];
                    toClient("set style 12");
                }else if (tokens[0] != "fics%"){
                    //not processed, so let's print it
                    textReady(str);
                }
            }
        }

        boost::asio::async_read_until(socket, data, "\n\r",
                                      boost::bind(&Client::handleData, this, _1));

    }
}