Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/gui implement parsing for pbc pic pie and smg commands zappy gui #21

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions gui/include/Parsing/ServerParser.hpp
mathieurobert1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Gui::ServerParser {
enum ParseType {
INT,
STRING,
HASHTAG
MESSAGE,
LIST_INT
};

private:
Expand All @@ -67,25 +68,27 @@ class Gui::ServerParser {
{"msz", std::vector<ParseType>{INT, INT}},
{"bct", std::vector<ParseType>{INT, INT, INT, INT, INT, INT, INT, INT, INT}},
{"tna", std::vector<ParseType>{STRING}},
{"pnw", std::vector<ParseType>{HASHTAG, INT, INT, INT, INT, STRING}},
{"ppo", std::vector<ParseType>{HASHTAG, INT, INT, INT}},
{"plv", std::vector<ParseType>{HASHTAG, INT}},
{"pin", std::vector<ParseType>{HASHTAG, INT, INT, INT, INT, INT, INT, INT, INT, INT}},
{"pex", std::vector<ParseType>{HASHTAG}},
{"pfk", std::vector<ParseType>{HASHTAG}},
{"pdr", std::vector<ParseType>{HASHTAG, INT}},
{"pgt", std::vector<ParseType>{HASHTAG, INT}},
{"pdi", std::vector<ParseType>{HASHTAG}},
{"enw", std::vector<ParseType>{HASHTAG, HASHTAG, INT, INT}},
{"ebo", std::vector<ParseType>{HASHTAG}},
{"edi", std::vector<ParseType>{HASHTAG}},
{"pnw", std::vector<ParseType>{INT, INT, INT, INT, INT, STRING}},
{"ppo", std::vector<ParseType>{INT, INT, INT, INT}},
{"plv", std::vector<ParseType>{INT, INT}},
{"pin", std::vector<ParseType>{INT, INT, INT, INT, INT, INT, INT, INT, INT, INT}},
{"pex", std::vector<ParseType>{INT}},
{"pbc", std::vector<ParseType>{INT, MESSAGE}},
{"pic", std::vector<ParseType>{INT, INT, INT, LIST_INT}},
{"pie", std::vector<ParseType>{INT, INT, INT}},
{"pfk", std::vector<ParseType>{INT}},
{"pdr", std::vector<ParseType>{INT, INT}},
{"pgt", std::vector<ParseType>{INT, INT}},
{"pdi", std::vector<ParseType>{INT}},
{"enw", std::vector<ParseType>{INT, INT, INT, INT}},
{"ebo", std::vector<ParseType>{INT}},
{"edi", std::vector<ParseType>{INT}},
mathieurobert1 marked this conversation as resolved.
Show resolved Hide resolved
{"sgt", std::vector<ParseType>{INT}},
{"sst", std::vector<ParseType>{INT}},
{"seg", std::vector<ParseType>{STRING}},
{"smg", std::vector<ParseType>{MESSAGE}},
{"suc", std::vector<ParseType>{}},
{"sbp", std::vector<ParseType>{}}
// TODO : pbc, pic, pie, smg
// #15 (https://github.com/FppEpitech/Zappy/issues/15))
};

/**
Expand Down Expand Up @@ -116,11 +119,22 @@ class Gui::ServerParser {
std::vector<std::string> parseString(std::istringstream& stream, std::vector<std::string> arguments);

/**
* @brief Parse an hashtag in the command stream.
* @brief Parse a message in the command stream.
*
* @param stream Stream to parse.
* @param arguments List of arguments parsed.
* @param commandName Name of the server command.
* @return std::vector<std::string> - arguments parsed
*/
std::vector<std::string> parseHashtag(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName);
std::vector<std::string> parseMessage(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName);

/**
* @brief Parse a list of int in the command stream.
*
* @param stream Stream to parse.
* @param arguments List of arguments parsed.
* @param commandName Name of the server command.
* @return std::vector<std::string> - arguments parsed
*/
std::vector<std::string> parseListInt(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName);
};
4 changes: 1 addition & 3 deletions gui/src/Parsing/ParseCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ void Gui::ParseCommandLine::parseFlags(int argc, char **argv)
} else
throw Errors::ParseCommandLineException(GUI_USAGE);
}
if (!isPort || !isHostname) {
std::cout << isPort << isHostname << std::endl;
if (!isPort || !isHostname)
throw Errors::ParseCommandLineException(GUI_USAGE);
}
}

int Gui::ParseCommandLine::getPort(void)
Expand Down
43 changes: 33 additions & 10 deletions gui/src/Parsing/ServerParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ std::vector<std::string> Gui::ServerParser::parseCommand(const std::string& comm
arguments = parseString(stream, arguments);
break;
}
case Gui::ServerParser::ParseType::HASHTAG:
case Gui::ServerParser::ParseType::MESSAGE:
{
arguments = parseHashtag(stream, arguments, commandName);
break;
arguments = parseMessage(stream, arguments, commandName);
return arguments;
}
case Gui::ServerParser::ParseType::LIST_INT:
{
arguments = parseListInt(stream, arguments, commandName);
return arguments;
mathieurobert1 marked this conversation as resolved.
Show resolved Hide resolved
}
default:
break;
Expand Down Expand Up @@ -75,14 +80,32 @@ std::vector<std::string> Gui::ServerParser::parseString(std::istringstream& stre
return arguments;
}

std::vector<std::string> Gui::ServerParser::parseHashtag(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName)
std::vector<std::string> Gui::ServerParser::parseMessage(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName)
{
char hashtag;
stream >> hashtag;
if (hashtag != '#')
if (stream.fail())
throw Errors::ServerParserException("Wrong parameters for '" + commandName + "' command.");

std::string start;
stream >> start;
if (stream.fail())
throw Errors::ServerParserException("Wrong parameters for '" + commandName + "' command.");
int nb;
stream >> nb;
arguments.push_back(std::to_string(nb));
std::string end;
std::getline(stream, end, '\0');
arguments.push_back(start + end);
return arguments;
}

std::vector<std::string> Gui::ServerParser::parseListInt(std::istringstream& stream, std::vector<std::string> arguments, std::string commandName)
{
if (stream.fail())
throw Errors::ServerParserException("Wrong parameters for '" + commandName + "' command.");
while (1) {
int player;
stream >> player;
if (stream.fail())
throw Errors::ServerParserException("Wrong parameters for '" + commandName + "' command.");
arguments.push_back(std::to_string(player));
if((stream.eof()))
return arguments;
}
}
Loading