Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NITWORK/SERVER-NETWORK: Fix wrong implementation of tick and receiveClientEnemyDeath #117

Merged
merged 7 commits into from
Oct 20, 2023
Merged
2 changes: 1 addition & 1 deletion src/Nitwork/ANitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace Nitwork {
_clockThread = std::thread([this, tick]() {
try {
while (_isRunning) {
std::this_thread::sleep_for(std::chrono::milliseconds(tick));
std::this_thread::sleep_for(std::chrono::milliseconds(TICKS_PER_MILLISECOND(tick)));
_tickMutex.lock();
_tickConvVar.notify_all();
_tickMutex.unlock();
Expand Down
5 changes: 3 additions & 2 deletions src/Nitwork/Nitwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#include "MessageTypes.h"

#define HEADER_SIZE sizeof(struct header_s)
#define TICKS_PER_SECOND 60
#define ONE_SECOND 1000
#define TICKS 128
#define TICKS_PER_SECOND(t) (t / TICKS)
#define TICKS_PER_MILLISECOND(t) (TICKS_PER_SECOND(t) / 1000)
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TICKS_PER_SECOND and TICKS_PER_MILLISECOND macros are now dependent on the TICKS macro. This introduces a coupling between these macros. If TICKS is changed in the future, it will affect the other two macros. Consider if this coupling is desired or if it would be better to keep these values independent.

#define DEFAULT_THREAD_NB 4
#define MAX_NB_ACTION 16
#define HEADER_CODE1 '\x01'
Expand Down
2 changes: 1 addition & 1 deletion src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Nitwork {
int port,
const std::string &ip,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);
int tick = TICKS);

// Messages creation methods
void addInitMsg();
Expand Down
6 changes: 1 addition & 5 deletions src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ namespace Nitwork {

static NitworkServer &getInstance();

bool startServer(
int port,
int nbPlayer,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);
bool startServer(int port, int nbPlayer, int threadNb = DEFAULT_THREAD_NB, int tick = TICKS);

/* Messages creation methods */
void addStarWaveMessage(boost::asio::ip::udp::endpoint &endpoint, n_id_t enemyId);
Expand Down
37 changes: 18 additions & 19 deletions src/Server/Systems/Network/ServerNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,25 @@ namespace Systems {
auto &arrEnemies = registry.getComponents<Types::Enemy>();
auto arrHealth = registry.getComponents<struct health_s>();
auto arrPos = registry.getComponents<Types::Position>();
auto it = std::find_if(arrEnemies.begin(), arrEnemies.end(), [&msgEnemyDeath](auto &enemy) {
return enemy.getConstId().id == msgEnemyDeath.enemyId.id;
});
if (it == arrEnemies.end()) {
return;
}
auto index = std::distance(arrEnemies.begin(), it);
if (!arrEnemies.exist(index) || !arrHealth.exist(index) || !arrPos.exist(index)) {
return;
auto ids = arrEnemies.getExistingsId();

for (auto &id : ids) {
if (arrEnemies[id].getConstId().id == msgEnemyDeath.enemyId.id) {
if (arrHealth.exist(id) && arrPos.exist(id)) {
Nitwork::NitworkServer::getInstance().addNewEnemyMessage(
endpoint,
{
.id = arrEnemies[id].getConstId(),
.life = arrHealth[id],
.pos =
{static_cast<char>(Maths::removeIntDecimals(arrPos[id].x)),
static_cast<char>(Maths::removeIntDecimals(arrPos[id].y))},
.type = arrEnemies[id].type,
});
}
return;
}
}
Nitwork::NitworkServer::getInstance().addNewEnemyMessage(
endpoint,
{
.id = arrEnemies[index].getConstId(),
.life = arrHealth[index],
.pos =
{static_cast<char>(Maths::removeIntDecimals(arrPos[index].x)),
static_cast<char>(Maths::removeIntDecimals(arrPos[index].y))},
.type = arrEnemies[index].type,
});
}

void receiveNewBulletMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint)
Expand Down
2 changes: 1 addition & 1 deletion src/main_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main(int ac, const char **av)
}
auto &sceneManager = Scene::SceneManager::getInstance();
if (!Nitwork::NitworkClient::getInstance()
.startClient(std::stoi(av[2]), av[1], DEFAULT_THREAD_NB, TICKS_PER_SECOND)) {
.startClient(std::stoi(av[2]), av[1], DEFAULT_THREAD_NB, TICKS)) {
return EXIT_EPITECH;
}
Nitwork::NitworkClient::getInstance().addInitMsg();
Expand Down
3 changes: 2 additions & 1 deletion src/main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ int main(int ac, const char **av)
return EXIT_EPITECH;
}
Logger::info("Starting Server...");
if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]))) {
if (!Nitwork::NitworkServer::getInstance()
.startServer(std::stoi(av[1]), std::stoi(av[2]), DEFAULT_THREAD_NB, TICKS)) {
return EXIT_EPITECH;
}
auto &director = Systems::SystemManagersDirector::getInstance();
Expand Down