From 1dc25a193e687f5fe9bef5dd0b5b706dbf5d238c Mon Sep 17 00:00:00 2001 From: alexsandulescu Date: Wed, 14 Jul 2021 02:03:47 +0300 Subject: [PATCH] Added integration with server browser api --- r5dev/src/overlay.cpp | 55 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/r5dev/src/overlay.cpp b/r5dev/src/overlay.cpp index e133e352..92ed611d 100644 --- a/r5dev/src/overlay.cpp +++ b/r5dev/src/overlay.cpp @@ -520,14 +520,55 @@ public: void RefreshServerList() { ServerList.clear(); + + static bool bThreadLocked = false; + + if (!bThreadLocked) { + std::thread t([this]() { + std::cout << "Refreshing server list..." << std::endl; + bThreadLocked = true; + httplib::Client client(MatchmakingServerStringBuffer); + client.set_connection_timeout(10); + auto res = client.Get("/servers"); + if (res) + { + json root = json::parse(res->body); + for (auto obj : root["servers"]) + { + ServerList.push_back( + new ServerListing(obj["name"], obj["map"], obj["ip"], obj["version"]) + ); + } + } + bThreadLocked = false; + }); + + t.detach(); + } + + + + + } + + void SendHostingPostRequest() + { httplib::Client client(MatchmakingServerStringBuffer); - auto res = client.Get("/browse"); - json root = json::parse(res->body); - for (auto obj : root["servers"]) + client.set_connection_timeout(10); + // send a post request to "/servers/add" with a json body + json body = json::object(); + body["name"] = ServerNameBuffer; + body["map"] = *SelectedMap; + body["version"] = "1.0"; + + std::string body_str = body.dump(); + + std::cout << body_str << "\n"; + + auto res = client.Post("/servers/add", body_str.c_str(), body_str.length(), "application/json"); + if (res) { - //ServerList.push_back( - // new ServerListing(obj["token"], obj["name"], obj["version"]) - //); + std::cout << "Hosting Request Result: " << res->body << "\n"; } } @@ -537,6 +578,7 @@ public: CurrentSection = section; } + void CompMenu() { ImGui::BeginTabBar("CompMenu"); @@ -660,6 +702,7 @@ public: { ToggleDevCommands(); } + SendHostingPostRequest(); } if (StartAsDedi)