Skip to content

Commit

Permalink
format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
1330Petrel committed Jan 14, 2025
1 parent 7a3fcc0 commit d53cf4e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 36 deletions.
File renamed without changes.
20 changes: 10 additions & 10 deletions sdk/cpp/src/agent/agent.cc → sdk/cpp/src/agent/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <string>
#include <string_view>
#include <utility>

#include "available_buffs.hpp"
#include "message.hpp"
Expand All @@ -18,9 +19,9 @@ constexpr unsigned int kMinDelayMs{10};

namespace thuai8_agent {

Agent::Agent(std::string_view token, const hv::EventLoopPtr& event_loop,
Agent::Agent(std::string token, const hv::EventLoopPtr& event_loop,
int loop_interval_ms)
: token_(token),
: token_(std::move(token)),
event_loop_(event_loop),
ws_client_(std::make_unique<hv::WebSocketClient>(event_loop)),
timer_id_(event_loop->setInterval(loop_interval_ms,
Expand Down Expand Up @@ -88,21 +89,20 @@ void Agent::Loop() const {
void Agent::OnMessage(std::string_view message) {
if (auto msg_type{Message::ReadMessageType(message)};
msg_type == "PLAYER_INFO") {
if (auto received_token{Message::ReadToken(message)};
received_token == token_) {
Message::ReadInfo(self_info_, message);
if (token_ == Message::ReadToken(message)) {
Message::Read(self_info_, message);
} else {
Message::ReadInfo(opponent_info_, message);
Message::Read(opponent_info_, message);
}
} else if (msg_type == "ENVIRONMENT_INFO") {
Message::ReadInfo(environment_info_, message);
Message::Read(environment_info_, message);
} else if (msg_type == "GAME_STATISTICS") {
Message::ReadInfo(game_statistics_, message);
Message::Read(game_statistics_, message);
} else if (msg_type == "AVAILABLE_BUFFS") {
Message::ReadInfo(available_buffs_, message);
Message::Read(available_buffs_, message);
} else if (msg_type == "ERROR") {
auto [error_code, error_message]{Message::ReadError(message)};
spdlog::error("{} got an error from server: [{}] {}]", *this, error_code,
spdlog::error("{} got an error from server: [{}] {}", *this, error_code,
error_message);
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/cpp/src/agent/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace thuai8_agent {

class Agent {
public:
Agent(std::string_view token, const hv::EventLoopPtr& event_loop,
Agent(std::string token, const hv::EventLoopPtr& event_loop,
int loop_interval_ms);

Agent(const Agent&) = delete;
Expand Down
2 changes: 1 addition & 1 deletion sdk/cpp/src/agent/available_buffs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template <typename T, typename U>
requires(std::is_same_v<T, BuffKind> && std::is_same_v<U, SkillKind>) ||
(std::is_same_v<T, SkillKind> && std::is_same_v<U, BuffKind>)
constexpr auto operator==(T lhs, U rhs) -> bool {
return static_cast<std::uint8_t>(lhs) == static_cast<std::uint8_t>(rhs);
return static_cast<unsigned char>(lhs) == static_cast<unsigned char>(rhs);
}

using AvailableBuffs = std::vector<BuffKind>;
Expand Down
10 changes: 5 additions & 5 deletions sdk/cpp/src/agent/environment_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct fmt::formatter<thuai8_agent::Wall> : fmt::formatter<std::string> {
template <>
struct fmt::formatter<thuai8_agent::Fence> : fmt::formatter<std::string> {
static auto format(const thuai8_agent::Fence& obj, format_context& ctx) {
return fmt::format_to(ctx.out(), "Fence: {{{}, health: {}}}", obj.position,
return fmt::format_to(ctx.out(), "Fence: {{{}, Health: {}}}", obj.position,
obj.health);
}
};
Expand All @@ -61,7 +61,7 @@ struct fmt::formatter<thuai8_agent::Bullet> : fmt::formatter<std::string> {
static auto format(const thuai8_agent::Bullet& obj, format_context& ctx) {
return fmt::format_to(
ctx.out(),
"Bullet: {{{}, speed: {}, damage: {}, traveledDistance: {}}}",
"Bullet: {{{}, Speed: {}, Damage: {}, TraveledDistance: {}}}",
obj.position, obj.speed, obj.damage, obj.traveledDistance);
}
};
Expand All @@ -71,9 +71,9 @@ struct fmt::formatter<thuai8_agent::EnvironmentInfo>
: fmt::formatter<std::string> {
static auto format(const thuai8_agent::EnvironmentInfo& obj,
format_context& ctx) {
return fmt::format_to(ctx.out(),
"EnvironmentInfo[Walls: {}, Fences: {}, Bullets: {}]",
obj.walls, obj.fences, obj.bullets);
return fmt::format_to(
ctx.out(), "EnvironmentInfo: {{Walls: {}, Fences: {}, Bullets: {}}}",
obj.walls, obj.fences, obj.bullets);
};
};

Expand Down
14 changes: 7 additions & 7 deletions sdk/cpp/src/agent/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Message {
~Message() = delete;

template <class T>
static void ReadInfo(T& value, std::string_view message) {
static void Read(T& value, std::string_view message) {
#ifdef NDEBUG
std::ignore = glz::read<readopts>(value, message);
#else
Expand All @@ -82,14 +82,14 @@ class Message {
}

template <class T>
[[nodiscard]] static auto ReadInfo(std::string_view message) -> T {
[[nodiscard]] static auto Read(std::string_view message) -> T {
T value{};
ReadInfo(value, message);
Read(value, message);
return value;
}

template <class T>
[[nodiscard, maybe_unused]] static auto Write(T&& value) -> std::string {
[[nodiscard]] static auto Write(T&& value) -> std::string {
return glz::write<writeopts>(std::forward<T>(value)).value();
}

Expand All @@ -102,16 +102,16 @@ class Message {

[[nodiscard]] static auto ReadMessageType(std::string_view message)
-> std::string {
return ReadInfo<MessageType>(message).messageType;
return Read<MessageType>(message).messageType;
}

[[nodiscard]] static auto ReadToken(std::string_view message) -> std::string {
return ReadInfo<Token>(message).token;
return Read<Token>(message).token;
}

[[nodiscard]] static auto ReadError(std::string_view message)
-> std::pair<int, std::string> {
Error value{ReadInfo<Error>(message)};
Error value{Read<Error>(message)};
return {value.errorCode, value.message};
}

Expand Down
File renamed without changes.
25 changes: 14 additions & 11 deletions sdk/cpp/src/main.cc → sdk/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ constexpr int kDefaultIntervalMs{200};
namespace {
#ifndef NDEBUG
void SetLogLevel(const std::string& level) {
if (level == "0" || level == "trace") {
spdlog::set_level(spdlog::level::trace);
return;
}
if (level == "1" || level == "debug") {
spdlog::set_level(spdlog::level::debug);
return;
Expand Down Expand Up @@ -69,9 +73,9 @@ auto ParseOptions(int argc, char** argv)
"t,token", "Set token",
cxxopts::value<std::string>()->default_value(token));
#ifndef NDEBUG
options.add_options()("l,log",
"Set log level: 1=debug, 2=info, 3=warn, 4=error",
cxxopts::value<std::string>()->default_value("1"));
options.add_options()(
"l,log", "Set log level: 0=trace, 1=debug, 2=info, 3=warn, 4=error",
cxxopts::value<std::string>()->default_value("0"));
#endif

try {
Expand All @@ -93,7 +97,7 @@ auto ParseOptions(int argc, char** argv)
}
#ifndef NDEBUG
else {
spdlog::set_level(spdlog::level::debug);
spdlog::set_level(spdlog::level::trace);
}
#endif

Expand All @@ -107,15 +111,14 @@ auto main(int argc, char* argv[]) -> int {
if (!options.has_value()) {
return 0;
}
auto [server, token]{options.value()};

hv::EventLoopPtr event_loop{std::make_shared<hv::EventLoop>()};

thuai8_agent::Agent agent{token, event_loop, kDefaultIntervalMs};
thuai8_agent::Agent agent{options->second, event_loop, kDefaultIntervalMs};

spdlog::info("{} is starting with server {}", agent, server);
event_loop->runInLoop([&] { agent.Connect(options->first); });

event_loop->runInLoop([&] { agent.Connect(server); });
spdlog::info("{} is starting with server {}", agent, options->first);

bool is_previous_connected{false};
bool is_previous_game_ready{false};
Expand All @@ -127,7 +130,7 @@ auto main(int argc, char* argv[]) -> int {
spdlog::error("{} disconnected from server", agent);
is_previous_connected = false;
}
spdlog::debug("{} is waiting for connection", agent);
spdlog::trace("{} is waiting for connection", agent);
return;
}

Expand All @@ -141,7 +144,7 @@ auto main(int argc, char* argv[]) -> int {
spdlog::error("{} is no longer in a ready game", agent);
is_previous_game_ready = false;
}
spdlog::debug("{} is waiting for the game to be ready", agent);
spdlog::trace("{} is waiting for the game to be ready", agent);
return;
}

Expand All @@ -163,7 +166,7 @@ auto main(int argc, char* argv[]) -> int {
}
is_buff_selected = true;
}
spdlog::debug("{} is waiting for next battle", agent);
spdlog::trace("{} is waiting for next battle", agent);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/cpp/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ target("agent")
set_kind("binary")
add_packages("cxxopts", "glaze", "libhv", "magic_enum", "spdlog")
add_includedirs("src")
add_files("src/**.cc")
add_files("src/**.cpp")
set_languages("cxx23")
set_exceptions("cxx")
set_warnings("allextra")
Expand Down
File renamed without changes.

0 comments on commit d53cf4e

Please sign in to comment.