Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
GuiParser.cpp
Go to the documentation of this file.
1#include "GuiParser.hpp"
2
3#include <stdexcept>
4#include <string>
5
6static int parseInt(std::string_view sv) { return std::stoi(std::string(sv)); }
7
8static int parsePlayerId(std::string_view sv)
9{
10 if (sv.empty() || sv[0] != '#') throw std::invalid_argument("missing #");
11 return std::stoi(std::string(sv.substr(1)));
12}
13
14std::optional<Gui::Request> GuiParser::parse(std::string_view line)
15{
16 if (line == "msz") return Gui::Msz{};
17 if (line == "mct") return Gui::Mct{};
18 if (line == "tna") return Gui::Tna{};
19 if (line == "sgt") return Gui::Sgt{};
20 if (line == "stu") return Gui::Stu{};
21
22 if (line.starts_with("bct ")) {
23 auto rest = line.substr(4);
24 auto sp = rest.find(' ');
25 if (sp == std::string_view::npos) return std::nullopt;
26 try {
27 int x = parseInt(rest.substr(0, sp));
28 int y = parseInt(rest.substr(sp + 1));
29 return Gui::Bct{x, y};
30 } catch (...) {
31 return std::nullopt;
32 }
33 }
34 if (line.starts_with("ppo ")) {
35 try {
36 return Gui::Ppo{parsePlayerId(line.substr(4))};
37 } catch (...) {
38 return std::nullopt;
39 }
40 }
41 if (line.starts_with("plv ")) {
42 try {
43 return Gui::Plv{parsePlayerId(line.substr(4))};
44 } catch (...) {
45 return std::nullopt;
46 }
47 }
48 if (line.starts_with("pin ")) {
49 try {
50 return Gui::Pin{parsePlayerId(line.substr(4))};
51 } catch (...) {
52 return std::nullopt;
53 }
54 }
55 if (line.starts_with("sst ")) {
56 try {
57 return Gui::Sst{parseInt(line.substr(4))};
58 } catch (...) {
59 return std::nullopt;
60 }
61 }
62 return std::nullopt;
63}
static int parsePlayerId(std::string_view sv)
Definition GuiParser.cpp:8
static int parseInt(std::string_view sv)
Definition GuiParser.cpp:6
static std::optional< Gui::Request > parse(std::string_view line)
Parse one line from a GUI client (no trailing newline).
Definition GuiParser.cpp:14