kinda works

This commit is contained in:
nullifiedcat 2017-07-23 18:23:43 +03:00
parent 381ff27659
commit 0b073303d3
4 changed files with 37 additions and 42 deletions

View File

@ -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() { void processing_thread() {
logging::Info("[bp.tf] Starting the thread"); logging::Info("[bp.tf] Starting the thread");
@ -56,27 +56,19 @@ void processing_thread() {
pending_queue.pop(); pending_queue.pop();
} }
} }
logging::Info("[bp.tf] Requesting data for %d users", count);
if (count) { if (count) {
logging::Info("[bp.tf] Requesting data for %d users", count);
std::string id_list = ""; std::string id_list = "";
logging::Info("Constructing list");
for (const auto& x : batch) { for (const auto& x : batch) {
x->pending = false; x->pending = false;
logging::Info("Adding %u to the list", x->id);
id_list += format("[U:1:", x->id, "],"); id_list += format("[U:1:", x->id, "],");
} }
logging::Info("List constructed");
// Remove trailing ',' // Remove trailing ','
id_list = id_list.substr(0, id_list.length() - 1); id_list = id_list.substr(0, id_list.length() - 1);
std::string query = format("steamids=", id_list, "&key=", api_key_s); std::string query = format("steamids=", id_list, "&key=", api_key_s);
logging::Info("Query constructed");
try { try {
auto sock = https::RAII_HTTPS_Socket("backpack.tf"); 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); 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) { if (response.find("HTTP/1.1 200 OK\r\n") != 0) {
size_t status = response.find("\r\n"); size_t status = response.find("\r\n");
throw std::runtime_error("Response isn't 200 OK! It's " + response.substr(0, status)); 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); std::string body = response.substr(response.find("\r\n\r\n") + 4);
logging::Info("[bp.tf] Response: %s", body.c_str());
try { try {
nlohmann::json data(body); nlohmann::json data = nlohmann::json::parse(body);
nlohmann::json users = data["users"]; nlohmann::json users = data["users"];
std::lock_guard<std::mutex> lock(cache_mutex); std::lock_guard<std::mutex> lock(cache_mutex);
for (auto it = users.begin(); it != users.end(); ++it) { for (auto it = users.begin(); it != users.end(); ++it) {
unsigned userid = strtoul(it.key().substr(5).c_str(), nullptr, 10);
try { try {
unsigned userid = strtoul(it.key().substr(5).c_str(), nullptr, 10); unsigned userid = strtoul(it.key().substr(5).c_str(), nullptr, 10);
const auto& v = it.value(); 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()); logging::Info("Data for %u (%s) is not an object!", userid, it.key());
continue; continue;
} }
logging::Info("Parsing data for user %u (%s)", userid, v["name"]); std::string name = v.at("name");
const auto& inv = v["inventory"]["440"]; 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()) { if (inv.find("value") == inv.end()) {
logging::Info("Private backpack");
store_data(userid, 0, true, false); store_data(userid, 0, true, false);
} else { } else {
float value = float(inv["value"]); 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)); store_data(userid, value * REFINED_METAL_PRICE, false, (unsigned(time(0)) - updated > OUTDATED_AGE));
} }
} catch (std::exception& ex) { } 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) { } 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); auto& d = access_data(id);
d.last_request = unsigned(time(0)); d.last_request = unsigned(time(0));
d.bad = false; d.bad = false;

View File

@ -24,7 +24,7 @@ struct backpack_data_s {
bool no_value { false }; // No recorded value bool no_value { false }; // No recorded value
bool outdated_value { false }; // Outdated value. Private inventory? bool outdated_value { false }; // Outdated value. Private inventory?
unsigned last_request { 0 }; unsigned last_request { 0 };
unsigned value { 0 }; float value { 0 };
unsigned id { 0 }; unsigned id { 0 };
}; };

View File

@ -85,27 +85,30 @@ void RenderPlayer(int eid) {
} }
if (backpacktf::enabled()) { if (backpacktf::enabled()) {
ImGui::SameLine(); ImGui::SameLine();
if (!info.friendsID) { if (info.fakeplayer) {
ImGui::Text("[BOT]"); ImGui::Text("[BOT]");
} } else if (!info.friendsID) {
const auto& d = backpacktf::get_data(info.friendsID); ImGui::Text("Unknown");
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 { } else {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.8f, 0.0f, 1.0f)); const auto& d = backpacktf::get_data(info.friendsID);
ImGui::Text("$%u", d.value); 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::SameLine(610);
ImGui::PushItemWidth(200.0f); ImGui::PushItemWidth(200.0f);

View File

@ -51,7 +51,6 @@ RAII_HTTPS_Socket::RAII_HTTPS_Socket(const std::string& host) : hostname_(host)
} }
RAII_HTTPS_Socket::~RAII_HTTPS_Socket() { RAII_HTTPS_Socket::~RAII_HTTPS_Socket() {
logging::Info("Cleaning up HTTPS socket");
ssl_die(); ssl_die();
if (sock_ >= 0) if (sock_ >= 0)
close(sock_); close(sock_);
@ -60,8 +59,9 @@ RAII_HTTPS_Socket::~RAII_HTTPS_Socket() {
bool RAII_HTTPS_Socket::ssl_connect() { bool RAII_HTTPS_Socket::ssl_connect() {
connection_ = SSL_new(ssl_context); connection_ = SSL_new(ssl_context);
SSL_set_fd(connection_, sock_); SSL_set_fd(connection_, sock_);
if (SSL_connect(connection_) != 1) { int ret = SSL_connect(connection_);
printf("SSL connection error\n"); if (ret != 1) {
logging::Info("SSL connection error: %d, %d, %x\n", ret, SSL_get_error(connection_, ret), ERR_get_error());
return false; return false;
} }
return true; 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 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 sent = 0;
int chunk = 0; int chunk = 0;
logging::Info("Writing %d bytes to socket %d", rq_length, sock_);
do { do {
chunk = SSL_write(connection_, buffer_rq.get() + sent, rq_length - sent); chunk = SSL_write(connection_, buffer_rq.get() + sent, rq_length - sent);
logging::Info("Written %d bytes", chunk);
if (chunk < 0) { if (chunk < 0) {
throw std::runtime_error("Error writing to Secure Socket: " + std::to_string(ERR_get_error())); throw std::runtime_error("Error writing to Secure Socket: " + std::to_string(ERR_get_error()));
} else if (chunk == 0) { } else if (chunk == 0) {