4#include <unordered_map>
8 std::vector<std::string_view> tokens =
_split(input,
' ');
10 if (tokens.empty())
return std::nullopt;
13 using ParserFunc = std::optional<Event> (*)(
const std::vector<std::string_view>&);
14 static const std::unordered_map<std::string_view, ParserFunc> parsers = {
26 auto it = parsers.find(tokens[0]);
27 if (it != parsers.end()) {
28 return it->second(tokens);
31 std::cerr <<
"[ProtocolParser] Unknown command: " << tokens[0] <<
"\n";
38 std::vector<std::string_view> tokens;
40 size_t stopPos = str.find(stopAt);
41 if (stopPos == std::string_view::npos) {
46 while (start < stopPos) {
47 size_t end = str.find(delimiter, start);
49 if (end == std::string_view::npos || end >= stopPos) {
54 tokens.emplace_back(str.substr(start, end - start));
66 for (
size_t i = start; i < tokens.size(); ++i) {
67 if (i > start) result +=
" ";
74 [[maybe_unused]]
const std::vector<std::string_view>& tokens)
84 if (tokens.size() != 3)
return std::nullopt;
87 int width = std::stoi(std::string(tokens[1]));
88 int height = std::stoi(std::string(tokens[2]));
99 if (tokens.size() != 10)
return std::nullopt;
102 int x = std::stoi(std::string(tokens[1]));
103 int y = std::stoi(std::string(tokens[2]));
106 for (
size_t i = 0; i < 7; ++i) {
107 resources[i] = std::stoi(std::string(tokens[3 + i]));
120 if (tokens.size() < 2)
return std::nullopt;
129 if (tokens.size() != 7)
return std::nullopt;
132 int id = std::stoi(std::string(tokens[1].substr(1)));
133 int x = std::stoi(std::string(tokens[2]));
134 int y = std::stoi(std::string(tokens[3]));
139 int level = std::stoi(std::string(tokens[5]));
140 std::string team = std::string(tokens[6]);
142 return PlayerNew{id, x, y, orientation, level, team};
152 if (tokens.size() != 5)
return std::nullopt;
155 int id = std::stoi(std::string(tokens[1].substr(1)));
156 int x = std::stoi(std::string(tokens[2]));
157 int y = std::stoi(std::string(tokens[3]));
173 if (tokens.size() != 3)
return std::nullopt;
176 int id = std::stoi(std::string(tokens[1].substr(1)));
177 int level = std::stoi(std::string(tokens[2]));
189 if (tokens.size() != 11)
return std::nullopt;
192 int id = std::stoi(std::string(tokens[1].substr(1)));
193 int x = std::stoi(std::string(tokens[2]));
194 int y = std::stoi(std::string(tokens[3]));
197 for (
size_t i = 0; i < 7; ++i) {
198 inventory[i] = std::stoi(std::string(tokens[4 + i]));
211 if (tokens.size() != 2)
return std::nullopt;
214 int id = std::stoi(std::string(tokens[1].substr(1)));
225 if (tokens.size() < 3)
return std::nullopt;
228 int id = std::stoi(std::string(tokens[1].substr(1)));
239 if (tokens.size() < 5)
return std::nullopt;
242 int x = std::stoi(std::string(tokens[1]));
243 int y = std::stoi(std::string(tokens[2]));
244 int level = std::stoi(std::string(tokens[3]));
246 std::vector<int> playerIds;
247 for (
size_t i = 4; i < tokens.size(); ++i) {
248 playerIds.push_back(std::stoi(std::string(tokens[i].substr(1))));
261 if (tokens.size() != 4)
return std::nullopt;
264 int x = std::stoi(std::string(tokens[1]));
265 int y = std::stoi(std::string(tokens[2]));
268 bool success = (tokens[3] ==
"1");
280 if (tokens.size() != 2)
return std::nullopt;
283 int id = std::stoi(std::string(tokens[1].substr(1)));
294 if (tokens.size() != 3)
return std::nullopt;
297 int playerId = std::stoi(std::string(tokens[1].substr(1)));
298 int resourceId = std::stoi(std::string(tokens[2]));
309 if (tokens.size() != 3)
return std::nullopt;
312 int playerId = std::stoi(std::string(tokens[1].substr(1)));
313 int resourceId = std::stoi(std::string(tokens[2]));
324 if (tokens.size() != 2)
return std::nullopt;
327 int id = std::stoi(std::string(tokens[1].substr(1)));
338 if (tokens.size() != 5)
return std::nullopt;
341 int eggId = std::stoi(std::string(tokens[1].substr(1)));
342 int playerId = std::stoi(std::string(tokens[2].substr(1)));
343 int x = std::stoi(std::string(tokens[3]));
344 int y = std::stoi(std::string(tokens[4]));
346 return EggNew{eggId, playerId, x, y};
356 if (tokens.size() != 2)
return std::nullopt;
359 int id = std::stoi(std::string(tokens[1].substr(1)));
370 if (tokens.size() != 2)
return std::nullopt;
373 int id = std::stoi(std::string(tokens[1].substr(1)));
384 if (tokens.size() != 2)
return std::nullopt;
387 int timeUnit = std::stoi(std::string(tokens[1]));
398 if (tokens.size() != 2)
return std::nullopt;
401 int timeUnit = std::stoi(std::string(tokens[1]));
412 if (tokens.size() < 2)
return std::nullopt;
421 if (tokens.size() < 2)
return std::nullopt;
430 if (tokens.size() != 1)
return std::nullopt;
439 if (tokens.size() != 1)
return std::nullopt;
448 if (tokens.size() != 3)
return std::nullopt;
451 int uptimeSeconds = std::stoi(std::string(tokens[1]));
452 int tickCount = std::stoi(std::string(tokens[2]));
463 if (tokens.size() != 5)
return std::nullopt;
466 int eggId = std::stoi(std::string(tokens[1].substr(1)));
467 std::string team = std::string(tokens[2]);
468 int x = std::stoi(std::string(tokens[3]));
469 int y = std::stoi(std::string(tokens[4]));
480 if (tokens.size() != 4)
return std::nullopt;
483 std::string team = std::string(tokens[1]);
484 int seconds = std::stoi(std::string(tokens[2]));
485 int64_t ticks = std::stoll(std::string(tokens[3]));
static std::optional< Event > _parseSTU(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePFK(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSGT(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSBP(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePPO(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePNW(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseGWT(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseEBO(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePLV(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSEG(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePIE(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseWelcome(const std::vector< std::string_view > &tokens)
static std::optional< Event > parse(std::string_view input)
Parses a raw input string from the server and converts it into an Event variant. This method stops at...
static std::optional< Event > _parsePEX(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSUC(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePBC(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSST(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseMSZ(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePIN(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSMG(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePDI(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseEDI(const std::vector< std::string_view > &tokens)
static std::vector< std::string_view > _split(std::string_view str, char delimiter, char stopAt='\n')
static std::string _joinTokens(const std::vector< std::string_view > &tokens, size_t start)
static std::optional< Event > _parseTNA(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseSSE(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseBCT(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePGT(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePIC(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parseENW(const std::vector< std::string_view > &tokens)
static std::optional< Event > _parsePDR(const std::vector< std::string_view > &tokens)