Skip to content

Commit

Permalink
clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
qchateau committed Jun 8, 2022
1 parent f3ef7f4 commit a3be948
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Checks: "bugprone-*,clang-analyzer-*,cppcoreguidelines-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-modernize-use-trailing-return-type,-readability-identifier-length"
CheckOptions:
3 changes: 1 addition & 2 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ conan_cmake_run(
BASIC_SETUP
CMAKE_TARGETS
REQUIRES
fmt/7.0.1
spdlog/1.8.0
spdlog/1.10.0
boost/1.74.0
nlohmann_json/3.9.1
OPTIONS
Expand Down
14 changes: 7 additions & 7 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ ENV TZ=Europe/Paris
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install -y \
cmake \
clang-10 \
libc++-10-dev \
libc++abi-10-dev \
lld-10 \
python3 \
python3-pip \
cmake \
clang-10 \
libc++-10-dev \
libc++abi-10-dev \
lld-10 \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*

RUN python3 -m pip install -U pip && python3 -m pip install conan
Expand Down
8 changes: 4 additions & 4 deletions server/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace sd {

namespace beast = boost::beast;
namespace net = boost::asio;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace beast = boost::beast; // NOLINT
namespace net = boost::asio; // NOLINT
namespace http = beast::http; // NOLINT
namespace websocket = beast::websocket; // NOLINT
using tcp = net::ip::tcp;

template <typename T>
Expand Down
6 changes: 2 additions & 4 deletions server/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace sd {
listener_t::listener_t(
net::io_context& ioc,
std::vector<std::shared_ptr<world_t>> worlds,
tcp::endpoint endpoint)
const tcp::endpoint& endpoint)
: ioc_{ioc}, acceptor_{ioc}, worlds_{std::move(worlds)}
{
acceptor_.open(endpoint.protocol());
Expand Down Expand Up @@ -36,17 +36,15 @@ net::awaitable<void> listener_t::on_run()

while (true) {
auto socket = co_await acceptor_.async_accept(net::use_awaitable);
bool found_world = false;
for (auto& world_ptr : worlds_) {
if (world_ptr->available_places() == 0) {
continue;
}

std::make_shared<session_t>(world_ptr, std::move(socket))->run();
found_world = true;
break;
}
}
}

} // sd
} // sd
4 changes: 2 additions & 2 deletions server/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class listener_t : public std::enable_shared_from_this<listener_t> {
listener_t(
net::io_context& ioc,
std::vector<std::shared_ptr<world_t>> worlds,
tcp::endpoint endpoint);
const tcp::endpoint& endpoint);

void run();

Expand All @@ -28,4 +28,4 @@ class listener_t : public std::enable_shared_from_this<listener_t> {
std::vector<std::shared_ptr<world_t>> worlds_;
};

} // sd
} // sd
16 changes: 8 additions & 8 deletions server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ constexpr auto addr_envvar = "ADDR";
constexpr auto port_envvar = "PORT";
constexpr auto nworlds_envvar = "NWORLDS";

int main(int argc, char* argv[])
int main(int /*argc*/, char* /*argv*/[])
{
const auto mb_address = std::getenv(addr_envvar);
if (!mb_address) {
const auto* mb_address = std::getenv(addr_envvar);
if (mb_address == nullptr) {
std::cerr << "Environment variable " << addr_envvar << " is not defined"
<< std::endl;
return EXIT_FAILURE;
}
const auto mb_port = std::getenv(port_envvar);
if (!mb_address) {
const auto* mb_port = std::getenv(port_envvar);
if (mb_address == nullptr) {
std::cerr << "Environment variable " << port_envvar << " is not defined"
<< std::endl;
return EXIT_FAILURE;
}
const auto mb_nworlds = std::getenv(nworlds_envvar);
if (!mb_address) {
const auto* mb_nworlds = std::getenv(nworlds_envvar);
if (mb_address == nullptr) {
std::cerr << "Environment variable " << nworlds_envvar
<< " is not defined" << std::endl;
return EXIT_FAILURE;
Expand Down Expand Up @@ -53,4 +53,4 @@ int main(int argc, char* argv[])
ioc.run();

return EXIT_SUCCESS;
}
}
23 changes: 15 additions & 8 deletions server/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
#include <algorithm>

namespace sd {
namespace {
constexpr auto score_multiplier = 1000;
}

player_t::player_t(world_t& world, id_t id, std::string_view name, bool fake)
: id_{id},
name_{name},
score_{0},
best_score_{0},
fake_{fake},
state_{.x = 0.5, .y = 0.5, .dx = 0, .dy = 0, .ddx = 0, .ddy = 0},
acc_{0.02},
world_{world}
state_{.x = 0.5, .y = 0.5, .dx = 0, .dy = 0, .ddx = 0, .ddy = 0}, // NOLINT(*-magic-numbers)
acc_{0.02}, // NOLINT(*-magic-numbers)
world_{world},
alive_{false}
{
respawn();
}
Expand All @@ -27,7 +32,7 @@ bool player_t::operator!=(const player_t& other) const
return id_ != other.id_;
}

void player_t::set_pos(double x, double y)
void player_t::set_pos(double x, double y) // NOLINT(bugprone-*)
{
state_.x = x;
state_.y = y;
Expand All @@ -46,7 +51,9 @@ void player_t::set_dd(double ddx, double ddy)

void player_t::respawn()
{
std::uniform_real_distribution<> rnd(0.1, 0.9);
constexpr auto low_bound = 0.1;
constexpr auto high_bound = 0.9;
std::uniform_real_distribution<> rnd(low_bound, high_bound);

state_.dx = state_.dy = state_.ddx = state_.ddy = 0;
state_.x = rnd(rnd_gen_);
Expand All @@ -71,7 +78,7 @@ void player_t::update_pos(std::chrono::nanoseconds dt)
const auto yinc = state_.dy * seconds;
state_.x += xinc;
state_.y += yinc;
add_score((std::abs(xinc) + std::abs(yinc)) * 1000);
add_score((std::abs(xinc) + std::abs(yinc)) * score_multiplier);
}

double player_t::speed() const
Expand All @@ -94,7 +101,7 @@ bool player_t::is_in_world() const

bool player_t::collides(const player_t& other) const
{
const auto size = state_.size / 2 + other.state_.size / 2;
const auto size = player_t::state_t::size;
const auto dist = std::sqrt(std::norm(
std::complex{other.state_.x - state_.x, other.state_.y - state_.y}));
return dist < size;
Expand All @@ -105,4 +112,4 @@ void player_t::kill()
alive_ = false;
}

} // sd
} // sd
25 changes: 13 additions & 12 deletions server/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class player_t {
using id_t = player_id_t;
using clock_t = std::chrono::steady_clock;
struct state_t {
const double size{0.01};
static constexpr double size{0.01};

double x, y, dx, dy, ddx, ddy;
};
static constexpr double max_dd = 5;

player_t(world_t& world, id_t id, std::string_view name, bool fake);
~player_t() = default;

player_t(const player_t&) = delete;
player_t(player_t&&) = delete;
Expand All @@ -39,18 +40,18 @@ class player_t {
void add_score(double v);
void update_pos(std::chrono::nanoseconds dt);

const auto& state() const { return state_; };
id_t id() const { return id_; }
const std::string& name() const { return name_; }
bool alive() const { return alive_; }
bool fake() const { return fake_; }
double score() const { return score_; }
double best_score() const { return best_score_; }
double speed() const;
double distance_to(const player_t& other) const;
[[nodiscard]] const auto& state() const { return state_; };
[[nodiscard]] id_t id() const { return id_; }
[[nodiscard]] const std::string& name() const { return name_; }
[[nodiscard]] bool alive() const { return alive_; }
[[nodiscard]] bool fake() const { return fake_; }
[[nodiscard]] double score() const { return score_; }
[[nodiscard]] double best_score() const { return best_score_; }
[[nodiscard]] double speed() const;
[[nodiscard]] double distance_to(const player_t& other) const;

bool is_in_world() const;
bool collides(const player_t& other) const;
[[nodiscard]] bool is_in_world() const;
[[nodiscard]] bool collides(const player_t& other) const;
void kill();

private:
Expand Down
6 changes: 4 additions & 2 deletions server/session.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "session.h"
#include "world.h"

#include <optional>
#include <boost/uuid/string_generator.hpp>
#include <spdlog/spdlog.h>

Expand All @@ -9,10 +10,11 @@ namespace sd {
namespace {

constexpr auto keepalive_period = std::chrono::seconds{60};
constexpr auto player_name_max_length = 30;

bool player_name_is_valid(std::string_view name)
{
return name.size() >= 3 && name.size() <= 30;
return name.size() >= 3 && name.size() <= player_name_max_length;
}

}
Expand Down Expand Up @@ -202,4 +204,4 @@ void session_t::handle_input(const nlohmann::json& input)
}
}

} // sd
} // sd
3 changes: 2 additions & 1 deletion server/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace sd {
class session_t : public std::enable_shared_from_this<session_t> {
public:
session_t(std::shared_ptr<world_t> world, tcp::socket&& socket);
~session_t() = default;

session_t(const session_t&) = delete;
session_t(session_t&&) = delete;
Expand All @@ -39,4 +40,4 @@ class session_t : public std::enable_shared_from_this<session_t> {
net::steady_timer timer_;
};

} // sd
} // sd
29 changes: 15 additions & 14 deletions server/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,35 @@ std::string get_fake_player_name(
"Spock",
};
int idx = 0;
for (; idx < names.size() - 1; ++idx) {
const auto& name = names[idx];
for (const auto& name : names) {
auto it = find_if(
begin(current_players), end(current_players), [&](const auto& p) {
return name == p->name();
});
if (it == end(current_players)) {
break;
return name;
}
}
return names[idx];
return names.back();
}

}

world_t::world_t(net::io_context& ioc) : ioc_{ioc} {}
world_t::world_t(net::io_context& ioc) : ioc_{ioc}, uuid_generator_{} {}

world_t::~world_t() = default;

int world_t::real_players() const
std::size_t world_t::real_players() const
{
return active_real_players() + idle_players_.size();
}

int world_t::active_real_players() const
std::size_t world_t::active_real_players() const
{
return players_.size() - fake_players_.size();
}

int world_t::available_places() const
std::size_t world_t::available_places() const
{
return max_players - real_players();
}
Expand Down Expand Up @@ -145,7 +144,8 @@ void world_t::adjust_players()
return;
}

int missing = max_players - players_.size();
auto missing = static_cast<ssize_t>(max_players)
- static_cast<ssize_t>(players_.size());
if (missing > 0) {
spdlog::debug("adding {} fake players", missing);
for (int i = 0; i < missing; ++i) {
Expand All @@ -154,7 +154,8 @@ void world_t::adjust_players()
}
}
else if (missing < 0) {
int nr_to_remove = std::min<int>(-missing, fake_players_.size());
auto nr_to_remove =
std::min(-missing, static_cast<ssize_t>(fake_players_.size()));
spdlog::debug("removing {} fake players", nr_to_remove);
for (int i = 0; i < nr_to_remove; ++i) {
fake_players_.pop_back();
Expand Down Expand Up @@ -198,7 +199,7 @@ nlohmann::json world_t::game_state_for_player(const player_handle_t& player)
{"dy", p.state().dy},
{"ddx", p.state().ddx},
{"ddy", p.state().ddy},
{"size", p.state().size},
{"size", player_t::state_t::size},
{"score", p.score()},
{"best_score", p.best_score()},
{"is_me", is_me},
Expand Down Expand Up @@ -299,8 +300,8 @@ void world_t::update_fake_player_dd(player_t& p)

// initial closest target is the center of the map
// this way fake players will more likely stay close to the middle
double closest_x = 0.5;
double closest_y = 0.5;
double closest_x = 0.5; // NOLINT(*-magic-numbers)
double closest_y = 0.5; // NOLINT(*-magic-numbers)
double closest_distance = l1_dist_to(closest_x, closest_y);

for (const auto& other_ptr : players_) {
Expand Down Expand Up @@ -357,4 +358,4 @@ void world_t::check_idle_players()
idle_players_.erase(end_it, end(idle_players_));
}

} // sd
} // sd
Loading

0 comments on commit a3be948

Please sign in to comment.