From c8b7f750357bee93adc4a71e401be677299b24a5 Mon Sep 17 00:00:00 2001 From: alexsandulescu Date: Sun, 25 Jul 2021 15:26:18 +0300 Subject: [PATCH] Private servers connect dialogue + password field --- r5dev/include/overlay.h | 4 ++ r5dev/include/serverlisting.h | 1 + r5dev/src/overlay.cpp | 108 ++++++++++++++++++++++++++++++---- 3 files changed, 102 insertions(+), 11 deletions(-) diff --git a/r5dev/include/overlay.h b/r5dev/include/overlay.h index 794c5ee5..edc9913c 100644 --- a/r5dev/include/overlay.h +++ b/r5dev/include/overlay.h @@ -181,6 +181,7 @@ public: ServerListing MyServer; std::vector MapsList; std::string HostRequestMessage = ""; + std::string HostToken = ""; ImVec4 HostRequestMessageColor = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); bool StartAsDedi = false; bool BroadCastServer = false; @@ -255,6 +256,7 @@ public: void RefreshServerList(); void UpdateMyServerInfo(); void SendHostingPostRequest(); + const nlohmann::json SendGetServerByTokenRequest(const std::string &token, const std::string &password); void CompMenu(); void ServerBrowserSection(); void SettingsSection(); @@ -264,6 +266,8 @@ public: void ProcessCommand(const char* command_line); void ExecCommand(const char* command_line); + void ConnectToServer(const std::string &ip, const std::string &port); + void ConnectToServer(const std::string &connString); }; extern CCompanion* g_ServerBrowser; \ No newline at end of file diff --git a/r5dev/include/serverlisting.h b/r5dev/include/serverlisting.h index 2979d812..6321a0fd 100644 --- a/r5dev/include/serverlisting.h +++ b/r5dev/include/serverlisting.h @@ -17,5 +17,6 @@ public: std::string map; std::string ip; std::string port; + std::string password; }; diff --git a/r5dev/src/overlay.cpp b/r5dev/src/overlay.cpp index 459084f6..61ace9d9 100644 --- a/r5dev/src/overlay.cpp +++ b/r5dev/src/overlay.cpp @@ -432,6 +432,7 @@ void CCompanion::SendHostingPostRequest() body["name"] = MyServer.name; body["map"] = MyServer.map; body["port"] = MyServer.port; + body["password"] = MyServer.password; std::string body_str = body.dump(); @@ -456,9 +457,11 @@ void CCompanion::SendHostingPostRequest() { std::stringstream msg; msg << "Broadcasting! "; + HostToken = ""; if (res["token"].is_string()) { - msg << "Share this token: " << res["token"] << ", and the password you set"; + msg << "Share the following token for people to connect: "; + HostToken = res["token"]; } HostRequestMessage = msg.str().c_str(); HostRequestMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); @@ -473,6 +476,27 @@ void CCompanion::SendHostingPostRequest() #endif } +const nlohmann::json CCompanion::SendGetServerByTokenRequest(const std::string &token, const std::string &password) +{ + httplib::Client client(MatchmakingServerStringBuffer); + client.set_connection_timeout(10); + + nlohmann::json reqBody = nlohmann::json::object(); + + reqBody["token"] = token; + reqBody["password"] = password; + + std::string reqBody_str = reqBody.dump(); + + auto res = client.Post("/server/byToken", reqBody_str.c_str(), reqBody_str.length(), "application/json"); + if (res && !res->body.empty()) + { + return nlohmann::json::parse(res->body); + } + return nlohmann::json::object(); +} + + void CCompanion::CompMenu() { ImGui::BeginTabBar("CompMenu"); @@ -557,7 +581,10 @@ void CCompanion::ServerBrowserSection() //const char* replace = ""; // For history pos soon std::stringstream cmd; cmd << "connect " << ServerConnStringBuffer; + ConnectToServer(ServerConnStringBuffer); + //strcpy_s(ServerConnStringBuffer, sizeof(replace), replace); // For history pos soon + // wut? } ImGui::SameLine(); @@ -565,12 +592,12 @@ void CCompanion::ServerBrowserSection() if (ImGui::Button("Private Servers##ServerBrowser_PrivateServersButton", ImVec2(ImGui::GetWindowContentRegionWidth() * (1.f / 3.f / 2.f), 19))) ImGui::OpenPopup("Connect to a Private Server##PrivateServersConnectModal"); - bool open = true; - if (ImGui::BeginPopupModal("Connect to a Private Server##PrivateServersConnectModal", &open)) + bool modalOpen = true; + if (ImGui::BeginPopupModal("Connect to a Private Server##PrivateServersConnectModal", &modalOpen)) { // I *WILL* move this in a separate class - ImGui::SetWindowSize(ImVec2(400.f, 200.f)); + ImGui::SetWindowSize(ImVec2(400.f, 200.f), ImGuiCond_Always); int imgWidth = 0; int imgHeight = 0; @@ -597,17 +624,54 @@ void CCompanion::ServerBrowserSection() ImGui::PushItemWidth(ImGui::GetWindowContentRegionWidth()); ImGui::InputTextWithHint("##PrivateServersConnectModal_TokenInput", "Token", &token); - ImGui::InputTextWithHint("##PrivateServersConnectModal_PasswordInput", "Password", &password); + ImGui::InputTextWithHint("##PrivateServersConnectModal_PasswordInput", "Password", &password, ImGuiInputTextFlags_Password); ImGui::PopItemWidth(); - ImGui::Spacing(); + ImGui::Dummy(ImVec2(ImGui::GetWindowContentRegionWidth(), 19.f)); + + + static std::string reqMessage; + static ImVec4 reqMessageColor; + + ImGui::TextColored(reqMessageColor, reqMessage.c_str()); + ImGui::Separator(); - if(ImGui::Button("Connect##PrivateServersConnectModal_ConnectButton")) - ImGui::CloseCurrentPopup(); + if (ImGui::Button("Connect##PrivateServersConnectModal_ConnectButton", ImVec2(ImGui::GetWindowContentRegionWidth() / 2.f, 19))) + { + nlohmann::json response = SendGetServerByTokenRequest(token, password); + if (response["success"]) + { + auto server = response["server"]; + + + if (server["ip"].is_string() && server["port"].is_string()) + { + std::string name = server["name"]; + std::string ip = server["ip"]; + std::string port = server["port"]; + + + ConnectToServer(ip, port); + reqMessage = "Found Server: " + name; + reqMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); + + + } + } + else + { + std::string err = response["err"]; + reqMessage = "Error: " + err; + reqMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f); + } + + } + ImGui::SameLine(); - if (ImGui::Button("Close##PrivateServersConnectModal_CloseButton")) + if (ImGui::Button("Close##PrivateServersConnectModal_CloseButton", ImVec2(ImGui::GetWindowContentRegionWidth() / 2.f, 19))) ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); } @@ -618,7 +682,8 @@ void CCompanion::HostServerSection() { static std::string ServerNameErr = ""; - ImGui::InputTextWithHint("Server Name##ServerHost_ServerName", "Required Field", &MyServer.name); + ImGui::InputTextWithHint("Server Name##ServerHost_ServerName", "Server Name (Required)", &MyServer.name); + ImGui::InputTextWithHint("Server Name##ServerHost_ServerPassword", "Password (Optional)", &MyServer.password); ImGui::Spacing(); if (ImGui::BeginCombo("Map##ServerHost_MapListBox", MyServer.map.c_str())) { @@ -665,6 +730,10 @@ void CCompanion::HostServerSection() ImGui::TextColored(ImVec4(1.00f, 0.00f, 0.00f, 1.00f), ServerNameErr.c_str()); ImGui::TextColored(HostRequestMessageColor, HostRequestMessage.c_str()); + if (!HostToken.empty()) + { + ImGui::InputText("##ServerHost_HostToken", &HostToken, ImGuiInputTextFlags_ReadOnly); + } if (StartAsDedi) { @@ -780,6 +849,23 @@ void Strtrim(char* s) } +void CCompanion::ConnectToServer(const std::string &ip, const std::string &port) +{ + + std::stringstream cmd; + cmd << "connect " << ip << ":" << port; + g_ServerBrowser->ProcessCommand(cmd.str().c_str()); +} + +void CCompanion::ConnectToServer(const std::string &connString) +{ + + std::stringstream cmd; + cmd << "connect " << connString; + g_ServerBrowser->ProcessCommand(cmd.str().c_str()); +} + + //############################################################################# // ENTRYPOINT //############################################################################# @@ -802,4 +888,4 @@ void DrawBrowser() return true; } (); browser.Draw("Companion"); -} \ No newline at end of file +}