mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 10:41:01 +00:00
http: add /player/{name}/vehicles
This commit is contained in:
@@ -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__))
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
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?
|
||||
|
||||
29
src/Http.cpp
29
src/Http.cpp
@@ -169,6 +169,35 @@ void Http::Server::THttpServerInstance::operator()() try {
|
||||
}
|
||||
std::unique_ptr<httplib::Server> HttpLibServerInstance = std::make_unique<httplib::Server>();
|
||||
|
||||
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<TClient> 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();
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <rapidjson/rapidjson.h>
|
||||
#include <sstream>
|
||||
|
||||
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" } });
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
#include "VehicleData.h"
|
||||
|
||||
#include "Common.h"
|
||||
#include <regex>
|
||||
#include <utility>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user