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
3 changes: 2 additions & 1 deletion 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_PER_SECOND 128
#define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 19, 2023

Choose a reason for hiding this comment

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

The TICK constant has been introduced, calculated based on TICKS_PER_SECOND. However, the current calculation seems incorrect as it divides ONE_SECOND by the square of TICKS_PER_SECOND. This would result in a much smaller value than expected. The correct calculation should be ONE_SECOND / TICKS_PER_SECOND.

-    #define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
+    #define TICK (ONE_SECOND / TICKS_PER_SECOND)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@coderabbitai its because one second is in milliseconds, and ticks per second is 128 so i need to convert it in miliseconds by doing * 1000 is it correct ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@romainpanno You're correct that ONE_SECOND is defined as 1000, representing one second in milliseconds. However, the current calculation for TICK is still incorrect.

The purpose of TICK is to represent the duration of one tick in milliseconds. If TICKS_PER_SECOND is 128, then there are 128 ticks in one second. Therefore, the duration of one tick should be ONE_SECOND / TICKS_PER_SECOND, which gives the number of milliseconds per tick.

The current calculation ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND) is equivalent to 1 / TICKS_PER_SECOND, which would give a very small value in seconds, not milliseconds.

So, the correct calculation should indeed be ONE_SECOND / TICKS_PER_SECOND. This will give the duration of one tick in milliseconds, which seems to be what you're aiming for. Here's the corrected line:

-    #define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
+    #define TICK (ONE_SECOND / TICKS_PER_SECOND)

This will ensure that TICK correctly represents the duration of one tick in milliseconds.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh ok

Copy link
Contributor

Choose a reason for hiding this comment

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

@romainpanno Great! If you have any more questions or need further clarification, feel free to ask. Happy coding!

#define DEFAULT_THREAD_NB 4
#define MAX_NB_ACTION 16
#define HEADER_CODE1 '\x01'
Expand Down
7 changes: 2 additions & 5 deletions src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ namespace Nitwork {
static NitworkClient &getInstance();

using ANitwork::start;
bool startClient(
int port,
const std::string &ip,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);
bool
startClient(int port, const std::string &ip, int threadNb = DEFAULT_THREAD_NB, int tick = TICK);

// 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 = TICK);

/* 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, TICK)) {
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, TICK)) {
return EXIT_EPITECH;
}
auto &director = Systems::SystemManagersDirector::getInstance();
Expand Down
Loading