Skip to content

Commit

Permalink
WIP BlockActionPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
afaure42 authored and SaumonDesMers committed Jun 1, 2024
1 parent c7d2f3e commit d03b708
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 28 deletions.
27 changes: 23 additions & 4 deletions src/app/network/client/ClientPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,37 @@ void ClientPacketHandler::handlePacket(std::shared_ptr<IPacket> packet)
{
switch (packet->GetType())
{
case IPacket::Type::PLAYER_CONNECTED:
handlePlayerConnectedPacket(std::dynamic_pointer_cast<PlayerConnectedPacket>(packet));
case IPacket::Type::CONNECTION:
handleConnectionPacket(std::dynamic_pointer_cast<ConnectionPacket>(packet));
break;
// case IPacket::Type::PLAYER_CONNECTED:
// handlePlayerConnectedPacket(std::dynamic_pointer_cast<PlayerConnectedPacket>(packet));
// break;
case IPacket::Type::PLAYER_MOVE:
handlePlayerMovePacket(std::dynamic_pointer_cast<PlayerMovePacket>(packet));
break;
case IPacket::Type::DISCONNECT:
handleDisconnectPacket(std::dynamic_pointer_cast<DisconnectPacket>(packet));
break;
case IPacket::Type::BLOCK_ACTION:
handleBlockActionPacket(std::dynamic_pointer_cast<BlockActionPacket>(packet));
break;
default:
break;
}
}

void ClientPacketHandler::handlePlayerConnectedPacket(std::shared_ptr<PlayerConnectedPacket> packet)
void ClientPacketHandler::handleConnectionPacket(std::shared_ptr<ConnectionPacket> packet)
{
LOG_INFO("Player connected: " << packet->GetId());
m_world.addPlayer(packet->GetId(), glm::vec3(0));
m_world.addPlayer(packet->GetId(), packet->GetPosition());
}

void ClientPacketHandler::handlePlayerConnectedPacket(std::shared_ptr<PlayerConnectedPacket> packet)
{
(void)packet;
// LOG_INFO("Player connected: " << packet->GetId());
// m_world.addPlayer(packet->GetId());
}

void ClientPacketHandler::handlePlayerMovePacket(std::shared_ptr<PlayerMovePacket> packet)
Expand All @@ -48,3 +61,9 @@ void ClientPacketHandler::handleDisconnectPacket(std::shared_ptr<DisconnectPacke
LOG_INFO("Player disconnected: " << packet->GetPlayerId());
m_world.removePlayer(packet->GetPlayerId());
}

void ClientPacketHandler::handleBlockActionPacket(std::shared_ptr<BlockActionPacket> packet)
{
//idk
(void)packet;
}
2 changes: 2 additions & 0 deletions src/app/network/client/ClientPacketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ClientPacketHandler
Client & m_client;
World & m_world;

void handleConnectionPacket(std::shared_ptr<ConnectionPacket> packet);
void handlePlayerMovePacket(std::shared_ptr<PlayerMovePacket> packet);
void handlePlayerConnectedPacket(std::shared_ptr<PlayerConnectedPacket> packet);
void handleDisconnectPacket(std::shared_ptr<DisconnectPacket> packet);
void handleBlockActionPacket(std::shared_ptr<BlockActionPacket> packet);
};
19 changes: 17 additions & 2 deletions src/app/network/server/ServerPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ void ServerPacketHandler::handlePacket(std::shared_ptr<IPacket> packet)
handleDisconnectPacket(std::dynamic_pointer_cast<DisconnectPacket>(packet));
break;
}
case IPacket::Type::BLOCK_ACTION:
{
mirrorPacket(packet);
break;
}
default:
{
break;
Expand All @@ -39,15 +44,15 @@ void ServerPacketHandler::handleConnectionPacket(std::shared_ptr<ConnectionPacke
{

//send new player to all other players
auto packet_to_send = std::make_shared<PlayerConnectedPacket>(packet->GetId());
auto packet_to_send = std::make_shared<ConnectionPacket>(*packet);
packet_to_send->SetConnectionId(packet->GetConnectionId());
m_server.sendAllExcept(packet_to_send, packet->GetConnectionId());


//send all other players to new player
for(auto & player : m_player_positions)
{
auto packet_to_send = std::make_shared<PlayerConnectedPacket>(player.first);
auto packet_to_send = std::make_shared<ConnectionPacket>(player.first, player.second);
packet_to_send->SetConnectionId(packet->GetConnectionId());
m_server.send(packet_to_send);
}
Expand Down Expand Up @@ -86,3 +91,13 @@ void ServerPacketHandler::handlePlayerMovePacket(std::shared_ptr<PlayerMovePacke
m_server.sendAll(packet_to_send);
m_player_positions[packet->GetId()] = packet->GetPosition();
}

void ServerPacketHandler::mirrorPacket(std::shared_ptr<IPacket> packet)
{
m_server.sendAll(packet);
}

void ServerPacketHandler::relayPacket(std::shared_ptr<IPacket> packet)
{
m_server.sendAllExcept(packet, packet->GetConnectionId());
}
5 changes: 5 additions & 0 deletions src/app/network/server/ServerPacketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ class ServerPacketHandler
void handleConnectionPacket(std::shared_ptr<ConnectionPacket> packet);
void handlePlayerMovePacket(std::shared_ptr<PlayerMovePacket> packet);
void handleDisconnectPacket(std::shared_ptr<DisconnectPacket> packet);
void handleBlockActionPacket(std::shared_ptr<BlockActionPacket> packet);


void mirrorPacket(std::shared_ptr<IPacket> packet);
void relayPacket(std::shared_ptr<IPacket> packet);
};
121 changes: 121 additions & 0 deletions src/app/network/shared/packets/BlockActionPacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "BlockActionPacket.hpp"

BlockActionPacket::BlockActionPacket()
{
}

BlockActionPacket::BlockActionPacket(BlockID block_id, glm::ivec3 position, Action action)
: m_block_id(block_id), m_position(position), m_action(action)
{
}

BlockActionPacket::BlockActionPacket(const BlockActionPacket & other)
: IPacket(other), m_block_id(other.m_block_id), m_position(other.m_position), m_action(other.m_action)
{
}

BlockActionPacket & BlockActionPacket::operator=(const BlockActionPacket & other)
{
if (this != &other)
{
m_block_id = other.m_block_id;
m_position = other.m_position;
m_action = other.m_action;
::IPacket::operator=(other);
}
return *this;
}

BlockActionPacket::BlockActionPacket(BlockActionPacket && other)
: IPacket(other), m_block_id(other.m_block_id), m_position(other.m_position), m_action(other.m_action)
{
}

BlockActionPacket & BlockActionPacket::operator=(BlockActionPacket && other)
{
if (this != &other)
{
m_block_id = other.m_block_id;
m_position = other.m_position;
m_action = other.m_action;
::IPacket::operator=(other);
}
return *this;
}

BlockActionPacket::~BlockActionPacket()
{
}

void BlockActionPacket::Serialize(uint8_t * buffer) const
{
uint32_t type = static_cast<uint32_t>(GetType());
memcpy(buffer, &type, sizeof(uint32_t));
buffer += sizeof(uint32_t);

memcpy(buffer, &m_block_id, sizeof(m_block_id));
buffer += sizeof(m_block_id);

memcpy(buffer, &m_position, sizeof(m_position));
buffer += sizeof(m_position);

memcpy(buffer, &m_action, sizeof(m_action));
buffer += sizeof(m_action);
}

void BlockActionPacket::Deserialize(const uint8_t * buffer)
{
memcpy(&m_block_id, buffer, sizeof(m_block_id));
buffer += sizeof(m_block_id);

memcpy(&m_position, buffer, sizeof(m_position));
buffer += sizeof(m_position);

memcpy(&m_action, buffer, sizeof(m_action));
buffer += sizeof(m_action);
}

uint32_t BlockActionPacket::Size() const
{
return sizeof(IPacket::Type) + sizeof(m_block_id) + sizeof(m_position) + sizeof(m_action);
}

IPacket::Type BlockActionPacket::GetType() const
{
return IPacket::Type::BLOCK_ACTION;
}

std::shared_ptr<IPacket> BlockActionPacket::Clone() const
{
return std::make_shared<BlockActionPacket>();
}

BlockID BlockActionPacket::GetBlockID() const
{
return m_block_id;
}

glm::ivec3 BlockActionPacket::GetPosition() const
{
return m_position;
}

BlockActionPacket::Action BlockActionPacket::GetAction() const
{
return m_action;
}

void BlockActionPacket::SetBlockID(BlockID block_id)
{
m_block_id = block_id;
}

void BlockActionPacket::SetPosition(glm::ivec3 position)
{
m_position = position;
}

void BlockActionPacket::SetAction(Action action)
{
m_action = action;
}
43 changes: 43 additions & 0 deletions src/app/network/shared/packets/BlockActionPacket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "IPacket.hpp"
#include "Block.hpp"

class BlockActionPacket : public IPacket
{
public:
enum class Action : uint8_t
{
PLACE
};

BlockActionPacket();
BlockActionPacket(BlockID block_id, glm::ivec3 position, Action action);
~BlockActionPacket();

BlockActionPacket(const BlockActionPacket& other);
BlockActionPacket& operator=(const BlockActionPacket& other);

BlockActionPacket(BlockActionPacket&& other);
BlockActionPacket& operator=(BlockActionPacket&& other);


virtual void Serialize(uint8_t * buffer) const override;
virtual void Deserialize(const uint8_t * buffer) override;
virtual uint32_t Size() const override;
virtual IPacket::Type GetType() const override;

virtual std::shared_ptr<IPacket> Clone() const override;

BlockID GetBlockID() const;
glm::ivec3 GetPosition() const;
Action GetAction() const;

void SetBlockID(BlockID block_id);
void SetPosition(glm::ivec3 position);
void SetAction(Action action);
private:
BlockID m_block_id;
glm::ivec3 m_position;
Action m_action;
};
11 changes: 6 additions & 5 deletions src/app/network/shared/packets/CtoS/PlayerConnectedPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PlayerConnectedPacket::PlayerConnectedPacket()
{
}

PlayerConnectedPacket::PlayerConnectedPacket(const uint8_t & id)
PlayerConnectedPacket::PlayerConnectedPacket(const uint32_t & id)
: m_id(id)
{
}
Expand All @@ -30,14 +30,15 @@ void PlayerConnectedPacket::Serialize(uint8_t * buffer) const
memcpy(buffer, &type, sizeof(uint32_t));
buffer += sizeof(uint32_t);

buffer[0] = m_id;
memcpy(buffer, &m_id, sizeof(m_id));
}

void PlayerConnectedPacket::Deserialize(const uint8_t * buffer)
{
//skipping type
buffer += sizeof(uint32_t);

m_id = buffer[0];
memcpy(&m_id, buffer, sizeof(m_id));
}

uint32_t PlayerConnectedPacket::Size() const
Expand All @@ -55,12 +56,12 @@ std::shared_ptr<IPacket> PlayerConnectedPacket::Clone() const
return std::make_shared<PlayerConnectedPacket>();
}

uint8_t PlayerConnectedPacket::GetId() const
uint32_t PlayerConnectedPacket::GetId() const
{
return m_id;
}

void PlayerConnectedPacket::setId(uint8_t id)
void PlayerConnectedPacket::setId(uint32_t id)
{
m_id = id;
}
8 changes: 4 additions & 4 deletions src/app/network/shared/packets/CtoS/PlayerConnectedPacket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PlayerConnectedPacket : public IPacket
{
public:
PlayerConnectedPacket();
PlayerConnectedPacket(const uint8_t & id);
PlayerConnectedPacket(const uint32_t & id);
virtual ~PlayerConnectedPacket();

PlayerConnectedPacket(const PlayerConnectedPacket& other) = delete;
Expand All @@ -22,9 +22,9 @@ class PlayerConnectedPacket : public IPacket

std::shared_ptr<IPacket> Clone() const override;

uint8_t GetId() const;
uint32_t GetId() const;

void setId(uint8_t id);
void setId(uint32_t id);
private:
uint8_t m_id;
uint32_t m_id;
};
1 change: 1 addition & 0 deletions src/app/network/shared/packets/IPacket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class IPacket
PLAYER_MOVE = 2,
ENTITY_MOVE = 3,
DISCONNECT = 4,
BLOCK_ACTION = 5,
};
virtual ~IPacket();

Expand Down
1 change: 1 addition & 0 deletions src/app/network/shared/packets/Packets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "PlayerConnectedPacket.hpp"
#include "ConnectionPacket.hpp"
#include "DisconnectPacket.hpp"
#include "BlockActionPacket.hpp"
#include "Client.hpp"
#include "Server.hpp"

Expand Down
19 changes: 17 additions & 2 deletions src/app/network/shared/packets/StoC/ConnectionPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@ ConnectionPacket::ConnectionPacket()
}

ConnectionPacket::ConnectionPacket(uint32_t id, glm::vec3 position)
: m_id(id), m_position(position)
: m_id(id), m_position(position)
{
}

ConnectionPacket::ConnectionPacket(const ConnectionPacket & other)
: m_id(other.m_id), m_position(other.m_position)
{
}

ConnectionPacket & ConnectionPacket::operator=(const ConnectionPacket & other)
{
if (this != &other)
{
m_id = other.m_id;
m_position = other.m_position;
}
return *this;
}

ConnectionPacket::ConnectionPacket(ConnectionPacket && other)
: m_id(other.m_id), m_position(other.m_position)
: m_id(other.m_id), m_position(other.m_position)
{
}

Expand Down
Loading

0 comments on commit d03b708

Please sign in to comment.