diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 8a6793f..bcb0c4a 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -99,27 +99,18 @@ void trippin::Game::initDbSynchronizer() { m->addScore(s); }; auto scoreDispatchFn = [t = transport.get()](const Score &s) { - auto id = s.id.c_str(); - auto name = s.name.c_str(); - if (t->addScore(s)) { - SDL_Log("added score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score); - return true; - } else { - SDL_Log("failed to add score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score); - return false; - } + auto result = t->addScore(s); + SDL_Log("add score attempted, id=%s, game=%d, name=%s, score=%d, result=%s", + s.id.c_str(), s.game, s.name.c_str(), s.score, toString(result)); + return result == AddResult::success || result == AddResult::clientError; }; scoreDb = std::make_unique>("scores", configuration.version.major, scoreAddCallback, scoreDispatchFn); scoreDb->start(); auto logAddCallback = [](const LogEvent &e) {}; auto logDispatchFn = [t = transport.get()](const LogEvent &e) { - if (t->addLogEvent(e)) { - SDL_Log("sent log event, index=%d", e.index); - return true; - } else { - SDL_Log("failed to send log event, index=%d", e.index); - return false; - } + auto result = t->addLogEvent(e); + SDL_Log("add log event attempted, index=%d, result=%s", e.index, toString(result)); + return result == AddResult::success || result == AddResult::clientError; }; logDb = std::make_unique>("logs", configuration.version.major, logAddCallback, logDispatchFn); logDb->start(); diff --git a/src/net/AddResult.h b/src/net/AddResult.h new file mode 100644 index 0000000..0af810b --- /dev/null +++ b/src/net/AddResult.h @@ -0,0 +1,23 @@ +#ifndef TRIPPIN_ADDRESULT_H +#define TRIPPIN_ADDRESULT_H + +namespace trippin { + enum class AddResult { + success, + clientError, + other + }; + + inline const char *toString(AddResult r) { + switch (r) { + case AddResult::success: + return "success"; + case AddResult::clientError: + return "clientError"; + default: + return "other"; + } + } +} + +#endif diff --git a/src/net/Transport.cpp b/src/net/Transport.cpp index ced9d59..6228530 100644 --- a/src/net/Transport.cpp +++ b/src/net/Transport.cpp @@ -5,7 +5,6 @@ trippin::Transport::Transport(std::string host, int port, int version, int limit) : host(std::move(host)), port(port), version(version), limit(limit) { - } trippin::Transport::Scores trippin::Transport::topScores() const { @@ -23,19 +22,15 @@ trippin::Transport::Scores trippin::Transport::sendRequest(const std::string &ur return {}; } - std::string msg = "GET "; - msg += uri; - msg += "?version="; - msg += std::to_string(version); - msg += "&limit="; - msg += std::to_string(limit); - msg += " HTTP/1.0\r\n"; - msg += "Host: "; - msg += host; - msg += "\r\n"; - msg += "Accept: */*\r\n\r\n"; - - tcp.send(msg); + std::stringstream req; + req << "GET " << uri + << "?version=" << version + << "&limit=" << limit + << " HTTP/1.0\r\n" + << "Host: " << host << "\r\n" + << "Accept: */*\r\n\r\n"; + + tcp.send(req.str()); std::string response = tcp.receive(1'024 * 64); auto f = response.find("\r\n\r\n"); @@ -58,11 +53,11 @@ trippin::Transport::Scores trippin::Transport::sendRequest(const std::string &ur return scores; } -bool trippin::Transport::addScore(const Score &score) const { +trippin::AddResult trippin::Transport::addScore(const Score &score) const { Tcp tcp(host, port); auto sock = tcp.get(); if (sock == nullptr) { - return false; + return AddResult::other; } nlohmann::json j = score.to_json(); @@ -76,29 +71,25 @@ bool trippin::Transport::addScore(const Score &score) const { auto json = j.dump(); - std::string msg = "POST /scores?h="; - msg += std::to_string(hash); - msg += " HTTP/1.0\r\n"; - msg += "Host: "; - msg += host; - msg += "\r\n"; - msg += "Content-Type: application/json\r\n"; - msg += "Content-Length: "; - msg += std::to_string(json.size()); - msg += "\r\n"; - msg += "Accept: */*\r\n\r\n"; - msg += json; - - tcp.send(msg); + std::stringstream req; + req << "POST /scores?h=" << hash + << " HTTP/1.0\r\n" + << "Host: " << host << "\r\n" + << "Content-Type: application/json\r\n" + << "Content-Length: " << json.size() << "\r\n" + << "Accept: */*\r\n\r\n" + << json; + + tcp.send(req.str()); std::string response = tcp.receive(4'096); - return response.find(" 200 OK") != std::string::npos; + return classifyResponse(response); } -bool trippin::Transport::addLogEvent(const LogEvent &event) const { +trippin::AddResult trippin::Transport::addLogEvent(const LogEvent &event) const { Tcp tcp(host, port); auto sock = tcp.get(); if (sock == nullptr) { - return false; + return AddResult::other; } nlohmann::json j; @@ -114,20 +105,29 @@ bool trippin::Transport::addLogEvent(const LogEvent &event) const { auto json = j.dump(); - std::string msg = "POST /logs?h="; - msg += std::to_string(hash); - msg += " HTTP/1.0\r\n"; - msg += "Host: "; - msg += host; - msg += "\r\n"; - msg += "Content-Type: application/json\r\n"; - msg += "Content-Length: "; - msg += std::to_string(json.size()); - msg += "\r\n"; - msg += "Accept: */*\r\n\r\n"; - msg += json; - - tcp.send(msg); + std::stringstream req; + req << "POST /logs?h=" << hash + << " HTTP/1.0\r\n" + << "Host: " << host << "\r\n" + << "Content-Type: application/json\r\n" + << "Content-Length: " << json.size() << "\r\n" + << "Accept: */*\r\n\r\n" + << json; + + tcp.send(req.str()); std::string response = tcp.receive(4'096); - return response.find(" 200 OK") != std::string::npos; + return classifyResponse(response); +} + +trippin::AddResult trippin::Transport::classifyResponse(const std::string &response) { + auto findFn = [&response](const char *str) { + return response.find(str) != std::string::npos; + }; + if (findFn(" 200 OK")) { + return AddResult::success; + } + if (findFn(" 400 Bad Request") || findFn(" 404 Not Found")) { + return AddResult::clientError; + } + return AddResult::other; } \ No newline at end of file diff --git a/src/net/Transport.h b/src/net/Transport.h index e1b4bb8..c21aee5 100644 --- a/src/net/Transport.h +++ b/src/net/Transport.h @@ -5,6 +5,7 @@ #include #include "Score.h" #include "LogEvent.h" +#include "AddResult.h" namespace trippin { class Transport { @@ -15,18 +16,17 @@ namespace trippin { }; Transport(std::string host, int port, int version, int limit); - bool addScore(const Score &score) const; - bool addLogEvent(const LogEvent &event) const; - Scores topScores() const; - Scores todayScores() const; + [[nodiscard]] AddResult addScore(const Score &score) const; + [[nodiscard]] AddResult addLogEvent(const LogEvent &event) const; + [[nodiscard]] Scores topScores() const; + [[nodiscard]] Scores todayScores() const; private: const std::string host; const int port; const int version; const int limit; - static std::vector compress(const std::vector &events); - static std::vector> compress(const std::vector> &vecs); - Scores sendRequest(const std::string &uri) const; + static AddResult classifyResponse(const std::string &response); + [[nodiscard]] Scores sendRequest(const std::string &uri) const; }; }