Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ int main() {
text.setString("Connexion KO");
}

if (tickTimeAccumulator >= TICK_TIME) {
//if (tickTimeAccumulator >= TICK_TIME) {
if (networking.m_isConnected.load()) {
gf::Packet packet;
while (networking.queue.poll(packet)) {
switch (packet.getType()) {
case ServerHello::type: {
ServerHello serverHello;
serverHello = packet.as<ServerHello>();
gf::Log::info("Received ServerHello packet from server my name is %s", serverHello.name.c_str());
// gf::Log::info("Received ServerHello packet from server my name is %s", serverHello.name.c_str());
break;
}
case ServerTankUpdate::type: {
Expand All @@ -120,7 +120,7 @@ int main() {
player.setX(serverTankUpdate.x);
player.setY(serverTankUpdate.y);
player.setOrientation(serverTankUpdate.rot);
gf::Log::info("Received ServerTankUpdate packet X=%f Y=%f ROT=%f \n", serverTankUpdate.x, serverTankUpdate.y, serverTankUpdate.rot);
// gf::Log::info("Received ServerTankUpdate packet X=%f Y=%f ROT=%f \n", serverTankUpdate.x, serverTankUpdate.y, serverTankUpdate.rot);
break;
}
default:
Expand All @@ -129,16 +129,16 @@ int main() {
}
}


if (tickTimeAccumulator >= TICK_TIME) {
TankControls controls;
controls.forward = moveForward.isActive();
controls.backward = moveBackward.isActive();
controls.left = rotateLeft.isActive();
controls.right = rotateRight.isActive();
controls.action = shoot.isActive() ? Action::Shoot : Action::Neutral;
networking.send(ClientMove{controls});
tickTimeAccumulator = 0.0f;
}
tickTimeAccumulator = 0.0f;
}

actions.reset();
Expand Down
60 changes: 41 additions & 19 deletions src/common/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,26 @@ Archive& operator|(Archive& ar, ClientDisconnect& data) {
return ar;
}

struct ClientPlayerDisconnect {
static constexpr gf::Id type = "ClientDisconnect"_id;
gf::Id id;
};

template <typename Archive>
Archive& operator|(Archive& ar, ClientPlayerDisconnect& data) {
return ar | data.id;
}

struct ClientPlayerConnect {
static constexpr gf::Id type = "ClientDisconnect"_id;
gf::Id id;
};

template <typename Archive>
Archive& operator|(Archive& ar, ClientPlayerConnect& data) {
return ar | data.id;
}
// struct ClientPlayerDisconnect { ClientPlayerDisconnect --> ServerPlayerDisconnect
// static constexpr gf::Id type = "ClientPlayerDisconnect"_id;
// gf::Id id;
// };
//
// template <typename Archive>
// Archive& operator|(Archive& ar, ClientPlayerDisconnect& data) {
// return ar | data.id;
// }
//
// struct ClientPlayerConnect { ClientPlayerConnect --> ServerPlayerConnect
// static constexpr gf::Id type = "ClientPlayerConnect"_id;
// gf::Id id;
// std::string name;
// };
//
// template <typename Archive>
// Archive& operator|(Archive& ar, ClientPlayerConnect& data) {
// return ar | data.id | data.name;
// }


/*
Expand Down Expand Up @@ -95,4 +96,25 @@ Archive& operator|(Archive& ar, ServerTankUpdate& data) {
return ar | data.id | data.x | data.y | data.rot;
}

struct ServerPlayerDisconnect {
static constexpr gf::Id type = "ServerPlayerDisconnect"_id;
gf::Id id;
};

template <typename Archive>
Archive& operator|(Archive& ar, ServerPlayerDisconnect& data) {
return ar | data.id;
}

struct ServerPlayerConnect {
static constexpr gf::Id type = "ServerPlayerConnect"_id;
gf::Id id;
std::string name;
};

template <typename Archive>
Archive& operator|(Archive& ar, ServerPlayerConnect& data) {
return ar;
}

#endif //STANK_PROTOCOL_H
15 changes: 10 additions & 5 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ int gameLoop(ServerNetworking &networking){
//Second: start a 30 TPS loop
while (true) {
float dt = clock.restart().asSeconds();

std::pair<gf::Id, gf::Packet> pocketPair;
while (networking.queue.poll(pocketPair)) {
auto& [id, packet] = pocketPair;
ServerPlayer &player = networking.m_players.at(id);
switch (packet.getType()) {
case ClientHello::type:
gf::Log::info(("Player " + player.name + " say hello!\n").c_str());
// gf::Log::info(("Player " + player.name + " say hello!\n").c_str());
break;
case ClientMove::type: {
auto clientMove = packet.as<ClientMove>();
TankControls ctrl = clientMove.controls;
// TODO: do something with the clientMove data
gf::Log::info(("Player " + player.name + " sent a ClientMove packet\n").c_str());
// gf::Log::info(("Player " + player.name + " sent a ClientMove packet\n").c_str());
player.move(ctrl.forward, ctrl.backward,ctrl.right, ctrl.left, TICK_TIME);

//send update ici (temporaire)
Expand All @@ -50,7 +49,10 @@ int gameLoop(ServerNetworking &networking){
update.x = player.getX();
update.y = player.getY();
update.rot = player.getRotation();
player.send(update);

for (auto &players: networking.m_players) {
players.second.send(update);
}

break;
}
Expand All @@ -59,7 +61,10 @@ int gameLoop(ServerNetworking &networking){
break;
}
}
usleep((unsigned int) ((TICK_TIME - clock.getElapsedTime().asSeconds()) * 1000.0f));

//usleep((unsigned int) ((TICK_TIME - clock.getElapsedTime().asSeconds()) * 1000.0f));
int time = (int) (TICK_TIME - clock.getElapsedTime().asSeconds()) * 1000.0f;
std::this_thread::sleep_for (std::chrono::milliseconds(time));
}
//Third: wait for inputs from network threads
//Fourth: modify gameState according to the inputs
Expand Down
7 changes: 7 additions & 0 deletions src/server/ServerNetworking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void ServerNetworking::run() {

playerInstance.send(ServerHello{playerInstance.id, playerInstance.name});

for (auto &players : m_players) {
players.second.send(ServerPlayerConnect{players.second.id, players.second.name});
}

auto res = m_players.emplace(id, std::move(playerInstance));
assert(res.second);
auto& player = res.first->second;//TODO à comprendre
Expand All @@ -103,6 +107,9 @@ void ServerNetworking::run() {
m_selector.removeSocket(playerPurgatory.socket);
//TODO retirer le joueur du ServerLobby
m_players.erase(it);
for (auto &player : m_players) {
player.second.send(ServerPlayerDisconnect{playerPurgatory.id});
}
}
}
}
Expand Down