11#pragma once
22
33#include < algorithm>
4- #include < cmath >
4+ #include < atomic >
55#include < chrono>
6+ #include < cmath>
7+ #include < condition_variable>
68#include < memory>
7- #include < unordered_map>
9+ #include < mutex>
10+ #include < random>
811#include < shared_mutex>
12+ #include < set>
13+ #include < string>
14+ #include < unordered_map>
15+ #include < vector>
916
17+ #include < glm/glm.hpp>
1018#include < nlohmann/json.hpp>
19+ #include " logging/Logger.hpp"
20+ #include " network/ConnectionManager.hpp"
21+ #include " network/GameSession.hpp"
1122
12- // #include "logging/Logger.hpp"
13- // #include "database/DbManager.hpp"
23+ #include " database/DbManager.hpp"
1424#include " game/RAIIThread.hpp"
1525#include " game/Player.hpp"
1626
27+ // Forward declarations
28+ // class Player;
29+
1730struct GlobalPlayerStats {
1831 int total_players = 0 ;
1932 int online_players = 0 ;
2033 int total_connections = 0 ;
21- int total_playtime = 0 ;
22- int average_playtime = 0 ;
34+ long long total_playtime = 0 ;
35+ double average_playtime = 0. 0 ;
2336 int level_1_10 = 0 ;
2437 int level_11_20 = 0 ;
2538 int level_21_30 = 0 ;
@@ -28,23 +41,32 @@ struct GlobalPlayerStats {
2841 int level_50_plus = 0 ;
2942};
3043
44+ struct Party {
45+ int64_t id;
46+ std::string name;
47+ int64_t leader_id;
48+ std::set<int64_t > members;
49+ std::chrono::system_clock::time_point created_at;
50+ };
51+
3152class PlayerManager {
3253public:
3354 static PlayerManager& GetInstance ();
3455
3556 std::shared_ptr<Player> CreatePlayer (const std::string& username);
3657 std::shared_ptr<Player> GetPlayer (int64_t playerId);
3758 std::shared_ptr<Player> GetPlayerBySession (uint64_t sessionId);
59+ std::shared_ptr<Player> GetPlayerByUsername (const std::string& username);
60+ uint64_t GetSessionIdByPlayerId (int64_t playerId) const ;
3861
3962 bool AuthenticatePlayer (const std::string& username, const std::string& password);
4063 void PlayerConnected (uint64_t sessionId, int64_t playerId);
4164 void PlayerDisconnected (uint64_t sessionId);
4265
43- // Session management
4466 void BroadcastToNearbyPlayers (int64_t playerId, const nlohmann::json& message);
4567 std::vector<int64_t > GetNearbyPlayers (int64_t playerId, float radius);
68+ std::vector<std::shared_ptr<Player>> GetPlayersInRadius (const glm::vec3& center, float radius);
4669
47- // Periodic tasks
4870 void SaveAllPlayers ();
4971 void CleanupInactivePlayers ();
5072
@@ -67,16 +89,27 @@ class PlayerManager {
6789 PlayerStats GetPlayerStats (int64_t playerId) const ;
6890 GlobalPlayerStats GetGlobalPlayerStats () const ;
6991 void PrintStats ();
92+
7093 std::vector<int64_t > SearchPlayers (const std::string& query, int limit);
7194 std::vector<int64_t > GetPlayersByLevelRange (int minLevel, int maxLevel);
95+
7296 void CreateParty (int64_t leaderId, const std::string& partyName);
7397 void AddPlayerToParty (int64_t partyId, int64_t playerId);
7498 void RemovePlayerFromParty (int64_t partyId, int64_t playerId);
7599 std::vector<int64_t > GetPartyMembers (int64_t partyId) const ;
76100 int64_t GeneratePartyId ();
77101
102+ void Shutdown (); // Added missing declaration
103+
78104private:
79105 PlayerManager ();
106+ ~PlayerManager ();
107+
108+ void SaveLoop ();
109+ void CleanupLoop ();
110+ void UpdateConnectionStats (int64_t playerId, bool connected);
111+
112+ DbManager& dbManager_;
80113
81114 mutable std::shared_mutex playersMutex_;
82115 std::unordered_map<int64_t , std::shared_ptr<Player>> players_;
@@ -85,22 +118,30 @@ class PlayerManager {
85118 std::unordered_map<uint64_t , int64_t > sessionToPlayer_;
86119 std::unordered_map<int64_t , uint64_t > playerToSession_;
87120
121+ mutable std::shared_mutex usernameMutex_;
122+ std::unordered_map<std::string, int64_t > usernameToId_;
123+
124+ mutable std::mutex statsMutex_;
125+ std::unordered_map<int64_t , PlayerStats> playerStats_;
126+
127+ mutable std::mutex partyMutex_;
128+ std::unordered_map<int64_t , Party> parties_;
129+
130+ std::chrono::system_clock::time_point lastCleanup_;
131+
88132 // Thread management with RAII
89- RAIIThread saveThread_; // Added
90- RAIIThread cleanupThread_; // Added
91-
133+ RAIIThread saveThread_;
134+ RAIIThread cleanupThread_;
135+
92136 std::atomic<bool > running_{true };
93137 std::chrono::minutes saveInterval_{5 };
94138 std::chrono::minutes cleanupInterval_{10 };
95-
139+
96140 std::condition_variable saveCV_;
97141 std::condition_variable cleanupCV_;
98142 std::mutex saveMutex_;
99143 std::mutex cleanupMutex_;
100144
101- CitusClient& dbClient_;
102-
103- // Thread functions
104- void SaveLoop ();
105- void CleanupLoop ();
145+ static constexpr float DEFAULT_BROADCAST_RANGE = 100 .0f ; // Added
146+ static constexpr size_t MAX_PARTY_SIZE = 5 ; // Added
106147};
0 commit comments