Skip to content

Commit 99e4c6a

Browse files
fix build bugs
1 parent e696f2b commit 99e4c6a

9 files changed

Lines changed: 73 additions & 70 deletions

File tree

include/game/GameLogic.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class GameLogic : public LogicCore
8787
void HandleCollisionCheck(uint64_t sessionId, const nlohmann::json& data);
8888
void HandleEntitySpawnRequest(uint64_t sessionId, const nlohmann::json& data);
8989
void HandleFamiliarCommand(uint64_t sessionId, const nlohmann::json& data);
90+
void HandlePlayerState(uint64_t sessionId, const std::vector<uint8_t>& data);
9091

9192
// Message handling
9293
void HandleMessage(uint64_t sessionId, const nlohmann::json& message);
@@ -96,9 +97,10 @@ class GameLogic : public LogicCore
9697
void OnPlayerDisconnected(uint64_t sessionId);
9798

9899
// Broadcasting
99-
void BroadcastBinaryToNearbyPlayers(const glm::vec3& position, uint16_t messageType,
100+
void BroadcastToNearbyPlayers(const glm::vec3& position, uint16_t messageType,
101+
const std::vector<uint8_t>& data, float radius = 50.0f);
102+
void BroadcastToNearbyOnlinePlayers(const glm::vec3& position, uint16_t messageType,
100103
const std::vector<uint8_t>& data, float radius = 50.0f);
101-
void BroadcastToNearbyPlayers(const glm::vec3& position, const nlohmann::json& message, float radius = 50.0f);
102104
void SyncNearbyEntitiesToPlayer(uint64_t sessionId, const glm::vec3& position);
103105

104106
// Helper broadcast methods

include/game/Player.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ class Player : public GameEntity {
337337
uint64_t id_;
338338
std::string username_;
339339

340+
bool onGround_{true};
341+
340342
//struct Position {float x, y, z;} position_;
341343
std::chrono::system_clock::time_point last_movement_;
342344

@@ -366,8 +368,6 @@ class Player : public GameEntity {
366368
std::unordered_map<std::string, nlohmann::json> completed_quests_;
367369
std::vector<std::string> achievements_;
368370

369-
bool onGround_{true};
370-
371371
// Active buffs/debuffs
372372
struct ActiveBuff {
373373
std::string buff_id;

include/network/GameServer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "network/WebSocketProtocol.hpp"
1818
#include "network/WebSocketSession.hpp"
1919

20+
#include "game/GameLogic.hpp"
21+
2022
class GameServer {
2123
public:
2224
GameServer(const WorkerGroupConfig& groupConfig, const ConfigManager& globalConfig);

include/network/GameSession.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,6 @@ class GameSession : public IConnection, public std::enable_shared_from_this<Game
433433
asio::ip::tcp::socket& GetSocket();
434434
const asio::ip::tcp::socket& GetSocket() const;
435435
void HandleNetworkError(std::error_code ec);
436+
void SetupDefaultHandlers();
436437
};
437438

include/network/PredictionSystem.hpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct ClientInput {
1414
bool jumping{false};
1515
bool crouching{false};
1616
bool sprinting{false};
17-
17+
1818
// Serialization
1919
std::vector<uint8_t> Serialize() const;
2020
static ClientInput Deserialize(const uint8_t* data, size_t length);
21-
21+
2222
// Validity check
2323
bool IsValid() const { return input_id > 0; }
2424
};
@@ -30,85 +30,85 @@ struct ServerState {
3030
glm::vec3 velocity;
3131
glm::vec3 rotation;
3232
bool on_ground{true};
33-
33+
3434
// Serialization
3535
std::vector<uint8_t> Serialize() const;
3636
static ServerState Deserialize(const uint8_t* data, size_t length);
37-
37+
3838
// Interpolation
3939
static ServerState Interpolate(const ServerState& a, const ServerState& b, float t);
4040
};
4141

4242
class PredictionSystem {
4343
public:
4444
PredictionSystem();
45-
45+
4646
// Client-side methods
4747
void StoreClientInput(const ClientInput& input);
4848
ServerState PredictPosition(uint64_t current_time) const;
49-
49+
5050
// Server-side methods
5151
void StoreServerState(const ServerState& state);
5252
std::vector<ClientInput> GetUnprocessedInputs(uint32_t last_processed) const;
53-
53+
5454
// Reconciliation
5555
struct ReconciliationResult {
5656
bool needs_correction{false};
5757
ServerState corrected_state;
5858
std::vector<uint32_t> processed_inputs;
5959
};
60-
60+
6161
ReconciliationResult ReconcileWithServer(const ServerState& server_state);
62-
62+
6363
// State management
6464
ServerState GetLastConfirmedState() const { return last_confirmed_state_; }
6565
ServerState GetLatestPredictedState() const { return latest_predicted_state_; }
66-
66+
6767
// Input history
6868
const std::deque<ClientInput>& GetInputHistory() const { return input_history_; }
69-
69+
7070
// Clear history
7171
void Clear();
72-
72+
7373
// Statistics
7474
struct PredictionStats {
7575
uint32_t total_predictions{0};
7676
uint32_t corrections_sent{0};
7777
uint32_t corrections_received{0};
7878
float average_correction_distance{0.0f};
7979
uint64_t last_correction_time{0};
80-
80+
8181
void Reset();
8282
std::string ToString() const;
8383
};
84-
84+
8585
const PredictionStats& GetStats() const { return stats_; }
86-
86+
87+
// Helper methods
88+
ServerState SimulateMovement(const ServerState& start_state,
89+
const std::vector<ClientInput>& inputs,
90+
float delta_time) const;
91+
92+
ServerState ApplyInput(const ServerState& state, const ClientInput& input, float delta_time) const;
93+
8794
private:
8895
mutable std::mutex mutex_;
89-
96+
9097
// Input history (client-side)
9198
std::deque<ClientInput> input_history_;
9299
static constexpr size_t MAX_INPUT_HISTORY = 1000;
93-
100+
94101
// Server state history
95102
std::deque<ServerState> server_state_history_;
96103
static constexpr size_t MAX_STATE_HISTORY = 100;
97-
104+
98105
// Current states
99106
ServerState last_confirmed_state_;
100107
mutable ServerState latest_predicted_state_;
101-
108+
102109
// Statistics
103110
PredictionStats stats_;
104-
105-
// Helper methods
106-
ServerState SimulateMovement(const ServerState& start_state,
107-
const std::vector<ClientInput>& inputs,
108-
float delta_time) const;
109-
110-
ServerState ApplyInput(const ServerState& state, const ClientInput& input, float delta_time) const;
111-
111+
112112
// Physics constants
113113
static constexpr float GRAVITY = -9.81f;
114114
static constexpr float MAX_SPEED = 10.0f;
@@ -121,18 +121,18 @@ class PredictionSystem {
121121
class InputBuffer {
122122
public:
123123
InputBuffer(size_t max_size = 1000);
124-
124+
125125
void AddInput(const ClientInput& input);
126126
std::vector<ClientInput> GetOrderedInputs() const;
127127
ClientInput GetNextInput() const;
128128
bool HasInputs() const { return !inputs_.empty(); }
129129
void Clear();
130130
size_t Size() const { return inputs_.size(); }
131-
131+
132132
private:
133133
mutable std::mutex mutex_;
134134
std::vector<ClientInput> inputs_;
135135
size_t max_size_;
136-
136+
137137
void SortAndTrim();
138138
};

include/network/WebSocketSession.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "logging/Logger.hpp"
88

99
#include "network/IConnection.hpp"
10+
#include "network/BinaryProtocol.hpp"
1011
#include "network/WebSocketProtocol.hpp"
1112

1213
class WebSocketSession : public IConnection, public std::enable_shared_from_this<WebSocketSession> {

src/game/GameLogic.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ void GameLogic::HandlePlayerPositionUpdate(uint64_t sessionId, const nlohmann::j
592592
positionWriter.WriteVector3(glm::vec3(0, 0, 0));
593593
positionWriter.WriteUInt64(GetCurrentTimestamp());
594594

595-
BroadcastBinaryToNearbyPlayers(position, BinaryProtocol::MESSAGE_TYPE_PLAYER_POSITION,
595+
BroadcastToNearbyPlayers(position, BinaryProtocol::MESSAGE_TYPE_PLAYER_POSITION,
596596
positionWriter.GetBuffer(), 100.0f);
597597

598598
FirePythonEvent("player_move_3d", {
@@ -862,55 +862,52 @@ void GameLogic::HandleEntitySpawnRequest(uint64_t sessionId, const nlohmann::jso
862862
}
863863

864864
// =============== Broadcasting ===============
865-
void GameLogic::BroadcastEntitySpawn(uint64_t entityId, EntityType type, const glm::vec3& position,
866-
float yaw, const std::string& name) {
867-
BinaryProtocol::BinaryWriter writer;
868-
writer.WriteUInt64(entityId);
869-
writer.WriteUInt8(static_cast<uint8_t>(type));
870-
writer.WriteString(name);
871-
writer.WriteVector3(position);
872-
writer.WriteFloat(yaw);
873-
writer.WriteUInt64(GetCurrentTimestamp());
874-
875-
BroadcastToNearbyPlayers(position, BinaryProtocol::MESSAGE_TYPE_ENTITY_SPAWN, writer.GetBuffer(), 100.0f);
876-
}
877-
878-
void GameLogic::BroadcastBinaryToNearbyPlayers(const glm::vec3& position, uint16_t messageType,
879-
const std::vector<uint8_t>& data, float radius) {
865+
void GameLogic::BroadcastToNearbyPlayers(const glm::vec3& position, uint16_t messageType,
866+
const std::vector<uint8_t>& data, float radius) {
880867
if (!connectionManager_) return;
881868
auto& pm = PlayerManager::GetInstance();
882-
auto onlinePlayers = pm.GetOnlinePlayers(); // returns vector of shared_ptr<Player>
883-
for (const auto& player : onlinePlayers) {
884-
if (glm::distance(player->GetPosition(), position) <= radius) {
885-
uint64_t sessionId = GetSessionIdByPlayer(player->GetId());
886-
if (sessionId != 0) {
887-
auto session = connectionManager_->GetSession(sessionId);
888-
if (session && session->IsConnected()) {
889-
session->SendBinary(messageType, data);
890-
}
869+
auto nearby = pm.GetPlayersInRadius(position, radius);
870+
for (auto& player : nearby) {
871+
uint64_t sessionId = pm.GetSessionIdByPlayerId(player->GetId());
872+
if (sessionId != 0) {
873+
auto session = connectionManager_->GetSession(sessionId);
874+
if (session && session->IsConnected()) {
875+
session->SendBinary(messageType, data);
891876
}
892877
}
893878
}
894879
}
895880

896-
void GameLogic::BroadcastToNearbyPlayers(const glm::vec3& position, const nlohmann::json& message, float radius) {
881+
void GameLogic::BroadcastToNearbyOnlinePlayers(const glm::vec3& position, uint16_t messageType,
882+
const std::vector<uint8_t>& data, float radius) {
897883
if (!connectionManager_) return;
898884
auto& pm = PlayerManager::GetInstance();
899885
auto onlinePlayers = pm.GetOnlinePlayers();
900-
std::string serialized = message.dump();
901886
for (const auto& player : onlinePlayers) {
902887
if (glm::distance(player->GetPosition(), position) <= radius) {
903888
uint64_t sessionId = GetSessionIdByPlayer(player->GetId());
904889
if (sessionId != 0) {
905890
auto session = connectionManager_->GetSession(sessionId);
906891
if (session && session->IsConnected()) {
907-
session->SendRaw(serialized);
892+
session->SendBinary(messageType, data);
908893
}
909894
}
910895
}
911896
}
912897
}
913898

899+
void GameLogic::BroadcastEntitySpawn(uint64_t entityId, EntityType type, const glm::vec3& position,
900+
float yaw, const std::string& name) {
901+
BinaryProtocol::BinaryWriter writer;
902+
writer.WriteUInt64(entityId);
903+
writer.WriteUInt8(static_cast<uint8_t>(type));
904+
writer.WriteString(name);
905+
writer.WriteVector3(position);
906+
writer.WriteFloat(yaw);
907+
writer.WriteUInt64(GetCurrentTimestamp());
908+
BroadcastToNearbyPlayers(position, BinaryProtocol::MESSAGE_TYPE_ENTITY_SPAWN, writer.GetBuffer(), 100.0f);
909+
}
910+
914911
void GameLogic::SyncNearbyEntitiesToPlayer(uint64_t sessionId, const glm::vec3& position) {
915912
// Send a batch of entity data (positions, types, etc.) to the player
916913
auto nearbyEntities = EntityManager::GetInstance().GetEntitiesInRadius(position, 100.0f);

src/game/Player.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ Player::Player(uint64_t id, const std::string& username)
225225
: GameEntity(EntityType::PLAYER, glm::vec3(0.0f, 0.0f, 0.0f)),
226226
id_(id),
227227
username_(username),
228+
onGround_(true),
228229
last_movement_(std::chrono::system_clock::now()),
229230
player_class_(PlayerClass::WARRIOR),
230231
race_(PlayerRace::HUMAN),
231232
status_(PlayerStatus::IDLE),
232233
inventory_system_(InventorySystem::GetInstance()),
233234
skill_system_(SkillSystem::GetInstance()),
234-
quest_manager_(QuestManager::GetInstance()),
235-
onGround_(true)
235+
quest_manager_(QuestManager::GetInstance())
236236
{
237237
ApplyRaceBonuses();
238238
ApplyClassBonuses();
@@ -245,14 +245,14 @@ Player::Player(const glm::vec3& position)
245245
: GameEntity(EntityType::PLAYER, position),
246246
id_(0),
247247
username_(""),
248+
onGround_(true),
248249
last_movement_(std::chrono::system_clock::now()),
249250
player_class_(PlayerClass::WARRIOR),
250251
race_(PlayerRace::HUMAN),
251252
status_(PlayerStatus::IDLE),
252253
inventory_system_(InventorySystem::GetInstance()),
253254
skill_system_(SkillSystem::GetInstance()),
254-
quest_manager_(QuestManager::GetInstance()),
255-
onGround_(true)
255+
quest_manager_(QuestManager::GetInstance())
256256
{
257257
ApplyRaceBonuses();
258258
ApplyClassBonuses();
@@ -266,14 +266,14 @@ Player::Player(const glm::vec3& position, PlayerClass player_class, PlayerRace r
266266
: GameEntity(EntityType::PLAYER, position),
267267
id_(0),
268268
username_(""),
269+
onGround_(true),
269270
last_movement_(std::chrono::system_clock::now()),
270271
player_class_(player_class),
271272
race_(race),
272273
status_(PlayerStatus::IDLE),
273274
inventory_system_(InventorySystem::GetInstance()),
274275
skill_system_(SkillSystem::GetInstance()),
275-
quest_manager_(QuestManager::GetInstance()),
276-
onGround_(true)
276+
quest_manager_(QuestManager::GetInstance())
277277
{
278278
ApplyRaceBonuses();
279279
ApplyClassBonuses();

src/network/GameServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ void GameServer::DoAccept() {
102102
if (webSocketFactory_) {
103103
auto wsConn = webSocketFactory_(std::move(socket), sslContext_);
104104
auto session = std::make_shared<WebSocketSession>(wsConn);
105-
session->SetBinaryMessageHandler([world](uint16_t type, const std::vector<uint8_t>& data) {
106-
world->HandleBinaryMessage(type, data); // Your world's binary handler
105+
session->SetBinaryMessageHandler([session](uint16_t type, const std::vector<uint8_t>& data) {
106+
GameLogic::GetInstance().HandleBinaryMessage(session->GetSessionId(), type, data);
107107
});
108108
ConnectionManager::GetInstance().Start(session);
109109
session->Start();

0 commit comments

Comments
 (0)