Skip to content

Commit

Permalink
Fix for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezePhoenix committed Apr 2, 2024
1 parent e4f1973 commit 6dde8c7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(USE_TLS TRUE)
set(USE_OPEN_SSL TRUE)

set(SPDLOG_USE_STD_FORMAT ON)
set(SPDLOG_USE_STD_FORMAT OFF)
add_subdirectory(lib/libuv EXCLUDE_FROM_ALL)
add_subdirectory(lib/spdlog lib/spdlog EXCLUDE_FROM_ALL)
# add_subdirectory(lib/spdlog lib/spdlog EXCLUDE_FROM_ALL)
add_subdirectory(lib/IXWebSocket lib/IXWebSocket EXCLUDE_FROM_ALL)
add_library(spdlog_HEADERS ALIAS spdlog_header_only)
# add_library(spdlog_HEADERS ALIAS spdlog_header_only)
add_subdirectory(lib/TriangleManipulator lib/TriangleManipulator EXCLUDE_FROM_ALL)


Expand Down Expand Up @@ -73,7 +73,7 @@ target_link_libraries(
target_link_libraries(
Bot
PUBLIC
spdlog_header_only
spdlog
ixwebsocket
uv
)
Expand Down
4 changes: 2 additions & 2 deletions CODE/BScorpion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(USE_TLS TRUE)
set(USE_OPEN_SSL TRUE)

set(SPDLOG_USE_STD_FORMAT ON)
set(SPDLOG_USE_STD_FORMAT OFF)

add_subdirectory(../../lib/TriangleManipulator ./build/TriangleManipulator EXCLUDE_FROM_ALL)
add_subdirectory(../../lib/spdlog ./build/spdlog EXCLUDE_FROM_ALL)
Expand All @@ -22,7 +22,7 @@ if(NOT DEFINED CHARACTER_NAME_STRING)
set(CHARACTER_NAME_STRING "DEFAULT")
endif()

add_library(spdlog_HEADERS ALIAS spdlog_header_only)
# add_library(spdlog_HEADERS ALIAS spdlog_header_only)

get_filename_component(prjName "${CMAKE_SOURCE_DIR}" NAME_WLE)

Expand Down
3 changes: 2 additions & 1 deletion include/albot/HttpWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define ALBOT_HTTPWRAPPER_HPP_

#include <Poco/Net/HTTPCookie.h>
#include <Poco/Net/HTMLForm.h>

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -88,7 +89,7 @@ class HttpWrapper {
* @return true
* @return false
*/
bool static do_post(const std::string& url, const std::string& args, std::optional<std::reference_wrapper<std::string>> out = std::nullopt, std::optional<std::reference_wrapper<std::vector<Poco::Net::HTTPCookie>>> cookies = std::nullopt);
bool static do_post(const std::string& url, const std::string& args, const std::string& method, std::optional<std::reference_wrapper<std::string>> out = std::nullopt, std::optional<std::reference_wrapper<std::vector<Poco::Net::HTTPCookie>>> cookies = std::nullopt);

/**
* @brief Send a GET request.
Expand Down
23 changes: 14 additions & 9 deletions src/HttpWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ bool HttpWrapper::get_config(nlohmann::json& config) {
return false;
}
}
bool HttpWrapper::do_post(const std::string& url, const std::string& args, std::optional<std::reference_wrapper<std::string>> out, std::optional<std::reference_wrapper<std::vector<Poco::Net::HTTPCookie>>> cookies) {
bool HttpWrapper::do_post(const std::string& url, const std::string& args, const std::string& method, std::optional<std::reference_wrapper<std::string>> out, std::optional<std::reference_wrapper<std::vector<Poco::Net::HTTPCookie>>> cookies) {
Poco::URI uri(url);
std::string path(uri.getPathAndQuery());
if (path.empty()) {
Expand All @@ -174,7 +174,11 @@ bool HttpWrapper::do_post(const std::string& url, const std::string& args, std::
if (!session_cookie.empty()) {
request.setCookies(HttpWrapper::cookie);
}
session.sendRequest(request) << args;
Poco::Net::HTMLForm form;
form.add("method", method);
form.add("arguments", args);
form.prepareSubmit(request);
form.write(session.sendRequest(request));

std::istream &rs = session.receiveResponse(response);

Expand Down Expand Up @@ -249,12 +253,13 @@ bool HttpWrapper::login() {
// Attempt to connect to the server. Since we don't need to copy the output,
// We pass a nullptr for the output. TODO: Get support for HTTP HEADERS verb.
if (HttpWrapper::get_game_version(HttpWrapper::online_version)) {
mLogger->info("Successfully connected to server!");
std::string args = std::format("{{\"email\":\"{}\",\"password\":\"{}\",\"only_login\":true}}", email, password);
mLogger->info("Successfully connected to server! {}", HttpWrapper::online_version);
std::string args = fmt::format("{{\"email\":\"{}\",\"password\":\"{}\",\"only_login\":true}}", email, password);
std::vector<Poco::Net::HTTPCookie> cookies;
// Again, we don't *really* care about the output the server sends us...
// We just want the cookies.
if (HttpWrapper::api_method("signup_or_login", args, std::nullopt, cookies)) {
std::string out;
if (HttpWrapper::api_method("signup_or_login", args, out, cookies)) {
for (size_t i = 0; i < cookies.size(); i++) {
Poco::Net::HTTPCookie _cookie = cookies[i];
if (_cookie.getName() == "auth") {
Expand Down Expand Up @@ -352,7 +357,7 @@ bool HttpWrapper::process_characters(const nlohmann::json& char_jsons) {
for (size_t i = 0; i < char_jsons.size(); i++) {
Character& character = HttpWrapper::characters.emplace_back(char_jsons[i]);
HttpWrapper::NAME_TO_NUMBER.emplace(character.name, i);
std::string macro_section = std::format("{}={}", character.name, i);
std::string macro_section = fmt::format("{}={}", character.name, i);
std::transform(character.name.begin(), character.name.end(), macro_section.begin(), ::toupper);
HttpWrapper::NAME_MACROS += macro_section;
if (i < char_jsons.size() - 1) {
Expand Down Expand Up @@ -426,10 +431,10 @@ void from_json(const nlohmann::json& server_json, Server& server) {
server.region = server_json.at("region").get<std::string>();
server.port = server_json.at("port").get<int>();
server.ip = server_json.at("addr").get<std::string>();
server.url = std::format("{}:{}", server.ip, server.port);
server.fullName = std::format("{} {}", server.region, server.identifier);
server.url = fmt::format("{}:{}", server.ip, server.port);
server.fullName = fmt::format("{} {}", server.region, server.identifier);
}

bool HttpWrapper::api_method(const std::string& method, const std::string& args, std::optional<std::reference_wrapper<std::string>> out, std::optional<std::reference_wrapper<std::vector<Poco::Net::HTTPCookie>>> cookies) {
return HttpWrapper::do_post(std::format("https://adventure.land/api/{}", method), std::format("arguments={}&method={}", args, method), out, cookies);
return HttpWrapper::do_post(fmt::format("https://adventure.land/api/{}", method), args, method, out, cookies);
}
2 changes: 1 addition & 1 deletion src/MapProcessing/MapProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MapProcessing {
std::shared_ptr<MapInfo> parse_map(const nlohmann::json& json) {

// Create a new info smart pointer.
std::shared_ptr<MapInfo> info = std::shared_ptr<MapInfo>(new MapInfo());
std::shared_ptr<MapInfo> info = std::make_shared<MapInfo>();

auto& info_x_lines = info->x_lines;
auto& info_y_lines = info->y_lines;
Expand Down
5 changes: 3 additions & 2 deletions src/SocketWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/async.h>
#include <fmt/core.h>

template <typename T, typename K>
inline T getOrElse(const nlohmann::json& n, K key, T defaultValue) {
Expand Down Expand Up @@ -258,15 +259,15 @@ void SocketWrapper::login(const CharacterGameInfo& info) {

void SocketWrapper::emit(const std::string& event, const nlohmann::json& json) {
if (this->webSocket.getReadyState() == ix::ReadyState::Open) {
this->webSocket.send(std::format("42[\"{}\",{}]", event, json.dump()));
this->webSocket.send(fmt::format("42[\"{}\",{}]", event, json.dump()));
} else {
this->mLogger->error("{} attempting to call emit on a socket that hasn't opened yet.", this->characterId);
}
}

void SocketWrapper::emitRawJsonString(std::string event, std::string json) {
if (this->webSocket.getReadyState() == ix::ReadyState::Open) {
this->webSocket.send(std::format("42[\"{}\",{}]", event, json));
this->webSocket.send(fmt::format("42[\"{}\",{}]", event, json));
} else {
this->mLogger->error("{} attempting to call emit on a socket that hasn't opened yet.", this->characterId);
}
Expand Down
17 changes: 9 additions & 8 deletions src/albot-cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "albot/albot-cpp.hpp"
#include "albot/HttpWrapper.hpp"
#include <functional>
#include <fmt/core.h>

std::shared_ptr<spdlog::logger> mLogger = spdlog::stdout_color_mt("ALBotC++");
namespace ALBot {
Expand Down Expand Up @@ -33,7 +34,7 @@ namespace ALBot {
}

void build_service_code(const std::string& name) {
std::string FOLDER = std::format("SERVICES/{}", name);
std::string FOLDER = fmt::format("SERVICES/{}", name);
std::string build_type = "Release";
if (HttpWrapper::config->contains("debug")) {
if (HttpWrapper::config->at("debug").get<bool>()) {
Expand All @@ -42,9 +43,9 @@ namespace ALBot {
} else {
mLogger->warn("config file does not contain a debug flag, assuming false");
}
std::string CMAKE = std::format("cmake -DCMAKE_BUILD_TYPE={} -DSERVICE_NAME_STRING={} -S {}/. -B {}/.", build_type, name, FOLDER, FOLDER);
std::string MAKE = std::format("make --quiet -C {}/. -j8", FOLDER);
std::string CP = std::format("cp {}/lib{}.so SERVICES/{}.so {}", FOLDER, name, name, NULL_PIPE_OUT);
std::string CMAKE = fmt::format("cmake -DCMAKE_BUILD_TYPE={} -DSERVICE_NAME_STRING={} -S {}/. -B {}/.", build_type, name, FOLDER, FOLDER);
std::string MAKE = fmt::format("make --quiet -C {}/. -j8", FOLDER);
std::string CP = fmt::format("cp {}/lib{}.so SERVICES/{}.so {}", FOLDER, name, name, NULL_PIPE_OUT);
mLogger->info("Running CMake on: SERVICES/{}", name);
system(CMAKE.c_str());
mLogger->info("Finished. Compiling...");
Expand Down Expand Up @@ -85,15 +86,15 @@ namespace ALBot {
} else {
mLogger->warn("config file does not contain a debug flag, assuming false");
}
std::string CMAKE = std::format("cmake -DCMAKE_BUILD_TYPE={} -DNAME_DEFINITIONS=\"{}\" -DCHARACTER_NAME_STRING={} -DCHARACTER_NAME={} -DCHARACTER_CLASS={} -S {}/. -B {}/.", build_type, HttpWrapper::NAME_MACROS, char_names, char_name_ints, char_klasses, FOLDER, FOLDER);
std::string MAKE = std::format("make --quiet -C {}/. -j8", FOLDER);
std::string CMAKE = fmt::format("cmake -DCMAKE_BUILD_TYPE={} -DNAME_DEFINITIONS=\"{}\" -DCHARACTER_NAME_STRING={} -DCHARACTER_NAME={} -DCHARACTER_CLASS={} -S {}/. -B {}/.", build_type, HttpWrapper::NAME_MACROS, char_names, char_name_ints, char_klasses, FOLDER, FOLDER);
std::string MAKE = fmt::format("make --quiet -C {}/. -j8", FOLDER);
mLogger->info("Running CMake on: CODE/{} with character names {}", name, pretty_names);
system(CMAKE.c_str());
mLogger->info("Finished. Compiling...");
system(MAKE.c_str());
mLogger->info("Finished. Copying...");
for (const std::string& char_name : names) {
std::string CP = std::format("cp {}/lib{}_{}.so CODE/{}.so {}", FOLDER, name, char_name, char_name, NULL_PIPE_OUT);
std::string CP = fmt::format("cp {}/lib{}_{}.so CODE/{}.so {}", FOLDER, name, char_name, char_name, NULL_PIPE_OUT);
system(CP.c_str());
}
mLogger->info("Finished.");
Expand All @@ -104,7 +105,7 @@ namespace ALBot {

ServiceInfo& info = SERVICE_HANDLERS.emplace(std::piecewise_construct, std::forward_as_tuple(service.name), std::forward_as_tuple(HttpWrapper::data)).first->second;
build_service_code(service.name);
std::string file = std::format("SERVICES/{}.so", service.name);
std::string file = fmt::format("SERVICES/{}.so", service.name);
void* handle = dlopen(file.c_str(), RTLD_LAZY);
if (!handle) {
mLogger->error("Cannot open library: {}", dlerror());
Expand Down

0 comments on commit 6dde8c7

Please sign in to comment.