void Client::readyRead() { while(socket->canReadLine()) { QString line = QString::fromUtf8(socket->readLine()).trimmed(); QRegExp usersRegex("^/usernames:(.*)$"); QRegExp commandRegex("^/command:(.*)$"); if(usersRegex.indexIn(line) != -1) ///az auth rész { players = usersRegex.cap(1).split(","); emit refreshPlayers(); } else if(commandRegex.indexIn(line) != -1) ///command jön a servertõl { uchar id,type; int msg; QStringList command = commandRegex.cap(1).split(' '); id=(uchar)command.at(0).toInt(); type=(uchar)command.at(1).toInt(); msg=command.at(2).toInt(); emit CommandReceivedFromServer(Command(id,type,msg)); } else ///egyéb hülyeség jön a servertõl { qDebug() << "hülyeség jött"; } } }
std::pair<bool, std::string> Config::parseConfigFile(CommandHandler& commandHandler, const std::string& configFilename) { std::ifstream configFile(configFilename); std::regex commandRegex("([a-zA-z0-9-]+)(?:=((?: ?[^\\s^ ]+)+))?"); std::regex argsRegex("([^\\s]+)"); std::string errorString; size_t lineNumber = 1; std::string line; while (std::getline(configFile, line)) { std::smatch commandMatch; std::string command(line); if (std::regex_match(command, commandMatch, commandRegex)) { std::string argsStr = commandMatch.str(2); std::vector<std::string> args(std::sregex_token_iterator(argsStr.begin(), argsStr.end(), argsRegex, 1), std::sregex_token_iterator()); auto executeResult = commandHandler.executeCommand(commandMatch.str(1), args); if (!executeResult.first) { errorString = executeResult.second; break; } } else if (line != "") { errorString = "wrong syntax. Syntax is command=<value>+."; break; } lineNumber++; } if (errorString.size() > 0) { std::stringstream resultSS; resultSS << "Error in config file line " << lineNumber << " '" << line << "': " << errorString; return std::make_pair(false, resultSS.str()); } else { return std::make_pair(true, ""); } }
void Console::runLoop() { char *buf; currentCommands = &(commandHandler.getCommands()); rl_attempted_completion_function = completion; std::regex commandRegex("([a-zA-z0-9-]+)((?: [^\\s]+)*)"); std::regex argsRegex("([^\\s]+)"); while ((buf = readline(" >> ")) != NULL) { //enable auto-complete rl_bind_key('\t', rl_complete); std::smatch commandMatch; std::string command(buf); if (std::regex_match(command, commandMatch, commandRegex)) { std::string argsStr = commandMatch.str(2); std::vector<std::string> args(std::sregex_token_iterator(argsStr.begin(), argsStr.end(), argsRegex, 1), std::sregex_token_iterator()); auto executeResult = commandHandler.executeCommand(commandMatch.str(1), args); if (!executeResult.first) { std::cout << executeResult.second << "Type 'help' for more info." << std::endl; } } else if (command != "") { std::cout << "Wrong syntax. Syntax is command <value>*." << std::endl; std::cout << "Type 'help' for more info." << std::endl; } if (command != "") { add_history(buf); } } free(buf); }
void Servernet::readyRead() { QTcpSocket *client =(QTcpSocket*)sender(); while(client->canReadLine()) { QString line = QString::fromUtf8(client->readLine()).trimmed(); QRegExp meRegex("^/me:(.*)$"); QRegExp commandRegex("^/command:(.*)$"); if(meRegex.indexIn(line) != -1 && players.size()<=this->playernumber) ///az auth rész { QString user = meRegex.cap(1); players.insert(client,user); Command c((uchar)clients.size(),(uchar)255,(int)survive?0:1); client->write(c.ToString().toUtf8()); client->flush(); emit NewPlayerConnected(); sendusernames(); if(players.size()==this->playernumber)emit AllPlayersConnected(); } else if(commandRegex.indexIn(line) != -1) ///command jön a klienstől { uchar id,type; int msg; QStringList command = commandRegex.cap(1).split(' '); id=(uchar)command.at(0).toInt(); type=(uchar)command.at(1).toInt(); msg=command.at(2).toInt(); emit CommandReceivedFromClients(Command(id,type,msg)); } else ///egyéb hülyeség jön a klienstől { qDebug() << "hülyeség jött"; } } }