From 0b073303d38161d8121d57170d8a153013d11e9b Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Sun, 23 Jul 2017 18:23:43 +0300 Subject: [PATCH] kinda works --- src/backpacktf.cpp | 30 ++++++++++++------------------ src/backpacktf.hpp | 2 +- src/gui/im/Playerlist.cpp | 39 +++++++++++++++++++++------------------ src/https_request.cpp | 8 +++----- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/backpacktf.cpp b/src/backpacktf.cpp index 14fbf154..4583d962 100644 --- a/src/backpacktf.cpp +++ b/src/backpacktf.cpp @@ -40,7 +40,7 @@ CatCommand api_key("bptf_key", "Set API Key", [](const CCommand& args) { }); -void store_data(unsigned id, unsigned value, bool no_value, bool outdated_value); +void store_data(unsigned id, float value, bool no_value, bool outdated_value); void processing_thread() { logging::Info("[bp.tf] Starting the thread"); @@ -56,27 +56,19 @@ void processing_thread() { pending_queue.pop(); } } - logging::Info("[bp.tf] Requesting data for %d users", count); if (count) { + logging::Info("[bp.tf] Requesting data for %d users", count); std::string id_list = ""; - logging::Info("Constructing list"); for (const auto& x : batch) { x->pending = false; - logging::Info("Adding %u to the list", x->id); id_list += format("[U:1:", x->id, "],"); } - logging::Info("List constructed"); // Remove trailing ',' id_list = id_list.substr(0, id_list.length() - 1); std::string query = format("steamids=", id_list, "&key=", api_key_s); - logging::Info("Query constructed"); try { auto sock = https::RAII_HTTPS_Socket("backpack.tf"); - logging::Info("Socket created"); - sock.ssl_connect(); - logging::Info("Socket connected (%d)", sock.sock_); std::string response = sock.get("/api/users/info/v1?" + query); - logging::Info("Request sent"); if (response.find("HTTP/1.1 200 OK\r\n") != 0) { size_t status = response.find("\r\n"); throw std::runtime_error("Response isn't 200 OK! It's " + response.substr(0, status)); @@ -84,13 +76,12 @@ void processing_thread() { std::string body = response.substr(response.find("\r\n\r\n") + 4); - logging::Info("[bp.tf] Response: %s", body.c_str()); - try { - nlohmann::json data(body); + nlohmann::json data = nlohmann::json::parse(body); nlohmann::json users = data["users"]; std::lock_guard lock(cache_mutex); for (auto it = users.begin(); it != users.end(); ++it) { + unsigned userid = strtoul(it.key().substr(5).c_str(), nullptr, 10); try { unsigned userid = strtoul(it.key().substr(5).c_str(), nullptr, 10); const auto& v = it.value(); @@ -98,10 +89,13 @@ void processing_thread() { logging::Info("Data for %u (%s) is not an object!", userid, it.key()); continue; } - logging::Info("Parsing data for user %u (%s)", userid, v["name"]); - const auto& inv = v["inventory"]["440"]; + std::string name = v.at("name"); + logging::Info("Parsing data for user %u (%s)", userid, name.c_str()); + if (v.find("inventory") == v.end()) { + store_data(userid, 0, true, false); + } + const auto& inv = v.at("inventory").at("440"); if (inv.find("value") == inv.end()) { - logging::Info("Private backpack"); store_data(userid, 0, true, false); } else { float value = float(inv["value"]); @@ -109,7 +103,7 @@ void processing_thread() { store_data(userid, value * REFINED_METAL_PRICE, false, (unsigned(time(0)) - updated > OUTDATED_AGE)); } } catch (std::exception& ex) { - logging::Info("Error while parsing user %s", it.key().c_str()); + logging::Info("Error while parsing user %s: %s", it.key().c_str(), ex.what()); } } } catch (std::exception& e) { @@ -151,7 +145,7 @@ backpack_data_s& access_data(unsigned id) { } } -void store_data(unsigned id, unsigned value, bool none, bool outdated) { +void store_data(unsigned id, float value, bool none, bool outdated) { auto& d = access_data(id); d.last_request = unsigned(time(0)); d.bad = false; diff --git a/src/backpacktf.hpp b/src/backpacktf.hpp index bf5a12b7..25793464 100644 --- a/src/backpacktf.hpp +++ b/src/backpacktf.hpp @@ -24,7 +24,7 @@ struct backpack_data_s { bool no_value { false }; // No recorded value bool outdated_value { false }; // Outdated value. Private inventory? unsigned last_request { 0 }; - unsigned value { 0 }; + float value { 0 }; unsigned id { 0 }; }; diff --git a/src/gui/im/Playerlist.cpp b/src/gui/im/Playerlist.cpp index bf434e1c..76a3a322 100644 --- a/src/gui/im/Playerlist.cpp +++ b/src/gui/im/Playerlist.cpp @@ -85,27 +85,30 @@ void RenderPlayer(int eid) { } if (backpacktf::enabled()) { ImGui::SameLine(); - if (!info.friendsID) { + if (info.fakeplayer) { ImGui::Text("[BOT]"); - } - const auto& d = backpacktf::get_data(info.friendsID); - if (d.bad && not d.pending) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); - ImGui::Text("Error"); - } else if (d.pending) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.6f, 0.6f, 0.6f, 1.0f)); - ImGui::Text("Fetching"); - } else if (d.no_value) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.0f, 1.0f)); - ImGui::Text("No value"); - } else if (d.outdated_value) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.6f, 0.6f, 0.6f, 1.0f)); - ImGui::Text("$%u", d.value); + } else if (!info.friendsID) { + ImGui::Text("Unknown"); } else { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.8f, 0.0f, 1.0f)); - ImGui::Text("$%u", d.value); + const auto& d = backpacktf::get_data(info.friendsID); + if (d.bad && not d.pending) { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); + ImGui::Text("Error"); + } else if (d.pending) { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.6f, 0.6f, 0.6f, 1.0f)); + ImGui::Text("Fetching"); + } else if (d.no_value) { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.0f, 1.0f)); + ImGui::Text("Private?"); + } else if (d.outdated_value) { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.6f, 0.6f, 0.6f, 1.0f)); + ImGui::Text("$%.2f", d.value); + } else { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.8f, 0.0f, 1.0f)); + ImGui::Text("$%.2f", d.value); + } + ImGui::PopStyleColor(); } - ImGui::PopStyleColor(); } ImGui::SameLine(610); ImGui::PushItemWidth(200.0f); diff --git a/src/https_request.cpp b/src/https_request.cpp index c0993683..6b3af389 100644 --- a/src/https_request.cpp +++ b/src/https_request.cpp @@ -51,7 +51,6 @@ RAII_HTTPS_Socket::RAII_HTTPS_Socket(const std::string& host) : hostname_(host) } RAII_HTTPS_Socket::~RAII_HTTPS_Socket() { - logging::Info("Cleaning up HTTPS socket"); ssl_die(); if (sock_ >= 0) close(sock_); @@ -60,8 +59,9 @@ RAII_HTTPS_Socket::~RAII_HTTPS_Socket() { bool RAII_HTTPS_Socket::ssl_connect() { connection_ = SSL_new(ssl_context); SSL_set_fd(connection_, sock_); - if (SSL_connect(connection_) != 1) { - printf("SSL connection error\n"); + int ret = SSL_connect(connection_); + if (ret != 1) { + logging::Info("SSL connection error: %d, %d, %x\n", ret, SSL_get_error(connection_, ret), ERR_get_error()); return false; } return true; @@ -86,10 +86,8 @@ std::string RAII_HTTPS_Socket::get(const std::string& path) { int rq_length = snprintf(buffer_rq.get(), rq_size, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", path.c_str(), hostname_.c_str()); int sent = 0; int chunk = 0; - logging::Info("Writing %d bytes to socket %d", rq_length, sock_); do { chunk = SSL_write(connection_, buffer_rq.get() + sent, rq_length - sent); - logging::Info("Written %d bytes", chunk); if (chunk < 0) { throw std::runtime_error("Error writing to Secure Socket: " + std::to_string(ERR_get_error())); } else if (chunk == 0) {