diff --git a/include/Common.h b/include/Common.h index afc935f..d7454f2 100644 --- a/include/Common.h +++ b/include/Common.h @@ -216,6 +216,7 @@ void RegisterThread(const std::string& str); #define beammp_errorf(...) beammp_error(fmt::format(__VA_ARGS__)) #define beammp_infof(...) beammp_info(fmt::format(__VA_ARGS__)) #define beammp_warnf(...) beammp_warn(fmt::format(__VA_ARGS__)) +#define beammp_debugf(...) beammp_debug(fmt::format(__VA_ARGS__)) #define beammp_tracef(...) beammp_trace(fmt::format(__VA_ARGS__)) #define beammp_lua_errorf(...) beammp_lua_error(fmt::format(__VA_ARGS__)) #define beammp_lua_warnf(...) beammp_lua_warn(fmt::format(__VA_ARGS__)) diff --git a/include/VehicleData.h b/include/VehicleData.h index f8a6d3d..baba597 100644 --- a/include/VehicleData.h +++ b/include/VehicleData.h @@ -1,10 +1,13 @@ #pragma once +#include #include +using json = nlohmann::json; + class TVehicleData final { public: - TVehicleData(int ID, std::string Data); + TVehicleData(int ID, const std::string& Data); ~TVehicleData(); // We cannot delete this, since vector needs to be able to copy when it resizes. // Deleting this causes some wacky template errors which are hard to decipher, @@ -19,9 +22,12 @@ public: bool operator==(const TVehicleData& v) const { return mID == v.mID; } + const json& Json() const; + private: int mID { -1 }; std::string mData; + json mJson; }; // TODO: unused now, remove? diff --git a/src/Http.cpp b/src/Http.cpp index 12af53f..37d2125 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -169,6 +169,35 @@ void Http::Server::THttpServerInstance::operator()() try { } std::unique_ptr HttpLibServerInstance = std::make_unique(); + HttpLibServerInstance->Get(API_V1 R"(/player/(.+)/vehicles)", [this](const httplib::Request& Req, httplib::Response& Res) { + if (Req.matches.empty()) { + Res.status = 404; + } else { + const std::string PlayerName = Req.matches[1]; + bool Found = false; + json Result = json::array(); + mServer.ForEachClient([&](std::weak_ptr ClientPtr) { + if (!ClientPtr.expired()) { + const auto Client = ClientPtr.lock(); + if (Client->GetName() == PlayerName) { + const auto [Cars, Lock] = Client->GetAllCars(); + for (const auto& Car : *Cars) { + Result.emplace_back().at("jbm") = Car.Json().at("jbm"); + } + Found = true; + } + } + return true; + }); + if (!Found) { + Res.status = 404; + } else { + Res.status = 200; + Res.set_content(Result.dump(), "application/json"); + } + } + }); + HttpLibServerInstance->Get(API_V1 "/players", [this](const httplib::Request&, httplib::Response& res) { res.status = 200; json Players = json::array(); diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 5d3d437..a4f5ca5 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -7,7 +7,6 @@ #include #include -namespace json = rapidjson; void THeartbeatThread::operator()() { RegisterThread("Heartbeat"); @@ -57,7 +56,7 @@ void THeartbeatThread::operator()() { auto Target = "/heartbeat"; unsigned int ResponseCode = 0; - json::Document Doc; + rapidjson::Document Doc; bool Ok = false; for (const auto& Url : Application::GetBackendUrlsInOrder()) { T = Http::POST(Url, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } }); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index dfa8776..c39b049 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -194,7 +194,6 @@ void TNetwork::TCPServerMain() { #undef GetObject // Fixes Windows #include "Json.h" -namespace json = rapidjson; void TNetwork::Identify(const TConnection& client) { RegisterThreadAuto(); @@ -281,7 +280,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) { Rc = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, RequestString, "application/json", &ResponseCode); } - json::Document AuthResponse; + rapidjson::Document AuthResponse; AuthResponse.Parse(Rc.c_str()); if (Rc == Http::ErrorString || AuthResponse.HasParseError()) { ClientKick(*Client, "Invalid key! Please restart your game."); diff --git a/src/VehicleData.cpp b/src/VehicleData.cpp index 00905fa..86b33d5 100644 --- a/src/VehicleData.cpp +++ b/src/VehicleData.cpp @@ -1,14 +1,30 @@ #include "VehicleData.h" #include "Common.h" +#include #include -TVehicleData::TVehicleData(int ID, std::string Data) +TVehicleData::TVehicleData(int ID, const std::string& Data) : mID(ID) - , mData(std::move(Data)) { + , mData(Data) { + try { + std::regex Reg(R"(^[a-zA-Z0-9_\-.]+:[a-zA-Z0-9_\-.]+:[a-zA-Z0-9_\-.]+:\d+\-\d+:(\{.+\}))", std::regex::ECMAScript); + std::smatch Match; + std::string Result; + if (std::regex_search(Data, Match, Reg) && Match.size() > 1) { + Result = Match.str(1); + mJson = json::parse(Result); + } + } catch (const std::exception& e) { + beammp_debugf("Failed to parse vehicle data for vehicle {} as json, this isn't expected: {}", ID, e.what()); + } beammp_trace("vehicle " + std::to_string(mID) + " constructed"); } TVehicleData::~TVehicleData() { beammp_trace("vehicle " + std::to_string(mID) + " destroyed"); } + +const json& TVehicleData::Json() const { + return mJson; +}