From 94382b8ffddff2369dfa21ec67ebbef8ea787f7a Mon Sep 17 00:00:00 2001 From: LightCat Date: Sun, 19 Aug 2018 22:53:59 +0200 Subject: [PATCH] how needs backpacktf anyways --- include/CMakeLists.txt | 1 - include/backpacktf.hpp | 36 ------- include/common.hpp | 1 - src/CMakeLists.txt | 1 - src/backpacktf.cpp | 238 ----------------------------------------- src/hack.cpp | 2 - 6 files changed, 279 deletions(-) delete mode 100644 include/backpacktf.hpp delete mode 100644 src/backpacktf.cpp diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 1d2601d5..e8dd5d4b 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,7 +1,6 @@ target_sources(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}/angles.hpp" "${CMAKE_CURRENT_LIST_DIR}/averager.hpp" - "${CMAKE_CURRENT_LIST_DIR}/backpacktf.hpp" "${CMAKE_CURRENT_LIST_DIR}/base64.hpp" "${CMAKE_CURRENT_LIST_DIR}/chatlog.hpp" "${CMAKE_CURRENT_LIST_DIR}/chatstack.hpp" diff --git a/include/backpacktf.hpp b/include/backpacktf.hpp deleted file mode 100644 index 7b230d36..00000000 --- a/include/backpacktf.hpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * backpacktf.hpp - * - * Created on: Jul 23, 2017 - * Author: nullifiedcat - */ - -#pragma once -#include "config.h" - -namespace backpacktf -{ - -constexpr float REFINED_METAL_PRICE = 0.075f; // $ -constexpr unsigned REQUEST_INTERVAL = - 10; // Make a backpack.tf request every 30 seconds -constexpr unsigned MAX_CACHE_AGE = 60 * 30; -constexpr unsigned OUTDATED_AGE = - 60 * 60 * 24 * 3; // After how many seconds backpack is marked "outdated" - // (possibly private) - -struct backpack_data_s -{ - bool pending{ false }; - bool bad{ true }; - bool no_value{ false }; // No recorded value - bool outdated_value{ false }; // Outdated value. Private inventory? - unsigned last_request{ 0 }; - float value{ 0 }; - unsigned id{ 0 }; -}; - -const backpack_data_s &get_data(unsigned id); -void init(); -bool enabled(); -} // namespace backpacktf diff --git a/include/common.hpp b/include/common.hpp index 928b1e85..f6514b37 100755 --- a/include/common.hpp +++ b/include/common.hpp @@ -93,7 +93,6 @@ #include "votelogger.hpp" #include "crits.hpp" #include "textmode.hpp" -#include "backpacktf.hpp" #include "core/sharedobj.hpp" #include "init.hpp" #include "reclasses/reclasses.hpp" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 97b8202a..963d3daa 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,5 @@ target_sources(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}/angles.cpp" - "${CMAKE_CURRENT_LIST_DIR}/backpacktf.cpp" "${CMAKE_CURRENT_LIST_DIR}/chatlog.cpp" "${CMAKE_CURRENT_LIST_DIR}/chatstack.cpp" "${CMAKE_CURRENT_LIST_DIR}/conditions.cpp" diff --git a/src/backpacktf.cpp b/src/backpacktf.cpp deleted file mode 100644 index 4f1cf2f6..00000000 --- a/src/backpacktf.cpp +++ /dev/null @@ -1,238 +0,0 @@ -/* - * backpacktf.cpp - * - * Created on: Jul 23, 2017 - * Author: nullifiedcat - */ -#include "config.h" -#include "common.hpp" -#include "backpacktf.hpp" -#include "json.hpp" -#include "https_request.hpp" - -#include -#include -#include - -static settings::Bool bptf_enable{ "backpack-tf.enable", "false" }; - -namespace backpacktf -{ - -std::unordered_map cache{}; -std::queue pending_queue{}; -std::mutex queue_mutex{}; -std::mutex cache_mutex{}; -bool thread_running{ true }; - -std::string api_key_s = ""; -bool valid_api_key = false; - -CatCommand api_key("bptf_key", "Set API Key", [](const CCommand &args) { - api_key_s = args.ArgS(); - logging::Info("API key changed!"); - valid_api_key = false; - if (api_key_s.length() != 24) - { - logging::Info("API key must be exactly 24 characters long"); - valid_api_key = false; - } - else - { - valid_api_key = true; - } -}); - -void store_data(unsigned id, float value, bool no_value, bool outdated_value); - -void processing_thread() -{ - logging::Info("[bp.tf] Starting the thread"); - while (thread_running) - { - if (enabled()) - { - try - { - std::vector batch{}; - int count = 0; - { - std::lock_guard lock(queue_mutex); - while (not pending_queue.empty() && ++count < 100) - { - batch.push_back(pending_queue.front()); - pending_queue.pop(); - } - } - if (count) - { - logging::Info("[bp.tf] Requesting data for %d users", - count); - std::string id_list = ""; - for (const auto &x : batch) - { - x->pending = false; - id_list += format("[U:1:", x->id, "],"); - } - // Remove trailing ',' - id_list = id_list.substr(0, id_list.length() - 1); - std::string query = - format("steamids=", id_list, "&key=", api_key_s); - try - { - auto sock = https::RAII_HTTPS_Socket("backpack.tf"); - std::string response = - sock.get("/api/users/info/v1?" + query); - if (response.compare("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)); - } - - std::string body = - response.substr(response.find("\r\n\r\n") + 4); - - try - { - 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(); - if (not v.is_object()) - { - logging::Info("Data for %u (%s) is not " - "an object!", - userid, it.key().c_str()); - continue; - } - 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); - continue; - } - const auto &inv = - v.at("inventory").at("440"); - if (inv.find("value") == inv.end()) - { - store_data(userid, 0, true, false); - } - else - { - float value = float(inv["value"]); - unsigned updated = - unsigned(inv["updated"]); - 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: %s", - it.key().c_str(), ex.what()); - } - } - } - catch (std::exception &e) - { - logging::Info( - "[bp.tf] Exception while parsing response: %s", - e.what()); - } - } - catch (std::exception &e) - { - logging::Info("[bp.tf] HTTPS exception: %s", e.what()); - } - } - } - catch (std::exception &e) - { - logging::Info("[bp.tf] Thread exception: %s", e.what()); - } - } - sleep(REQUEST_INTERVAL); - } -} - -void request_data(unsigned id) -{ - if (cache[id].pending) - return; - cache[id].pending = true; - { - std::lock_guard lock(queue_mutex); - pending_queue.push(&cache[id]); - } -} - -bool enabled() -{ - return bptf_enable && valid_api_key; -} - -backpack_data_s &access_data(unsigned id) -{ - try - { - return cache.at(id); - } - catch (std::out_of_range &oor) - { - cache.emplace(id, backpack_data_s{}); - cache.at(id).id = id; - return cache.at(id); - } -} - -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; - d.value = value; - d.no_value = none; - d.outdated_value = outdated; - d.pending = false; -} - -const backpack_data_s &get_data(unsigned id) -{ - auto &d = access_data(id); - if (d.bad || ((unsigned) time(0) - MAX_CACHE_AGE > cache[id].last_request)) - { - request_data(id); - } - return d; -} - -std::thread &GetBackpackTFThread() -{ - static std::thread thread(processing_thread); - return thread; -} - -void init() -{ - GetBackpackTFThread(); -} -} // namespace backpacktf diff --git a/src/hack.cpp b/src/hack.cpp index b683a3a5..988edbc2 100644 --- a/src/hack.cpp +++ b/src/hack.cpp @@ -380,8 +380,6 @@ free(logname);*/ logging::Info("Initialized Fidget Spinner"); #endif hacks::shared::spam::init(); - backpacktf::init(); - logging::Info("Initialized Backpack.TF integration"); #endif #if not LAGBOT_MODE hacks::shared::walkbot::Initialize();