Skip to content

Commit 7052f94

Browse files
fix InventorySystem
1 parent 31b87a7 commit 7052f94

3 files changed

Lines changed: 116 additions & 253 deletions

File tree

include/database/DbManager.hpp

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -106,156 +106,3 @@ class DbManager {
106106
DatabaseType ParseDatabaseType(const std::string& typeStr) const;
107107
std::string DatabaseTypeToString(DatabaseType type) const;
108108
};
109-
110-
/**
111-
* @brief PostgreSQL Client Implementation
112-
*
113-
* Implements the DatabaseBackend interface for standard PostgreSQL.
114-
*/
115-
class PostgreSqlCli : public DatabaseBackend {
116-
public:
117-
PostgreSqlCli(const nlohmann::json& config);
118-
virtual ~PostgreSqlCli();
119-
120-
// Connection Management
121-
bool Connect() override;
122-
bool Reconnect() override;
123-
void Disconnect() override;
124-
bool IsConnected() const override;
125-
bool CheckHealth() override;
126-
void ReconnectAll() override;
127-
128-
// Player Data Operations
129-
bool SavePlayerData(uint64_t playerId, const nlohmann::json& data) override;
130-
nlohmann::json LoadPlayerData(uint64_t playerId) override;
131-
bool UpdatePlayer(uint64_t playerId, const nlohmann::json& updates) override;
132-
bool DeletePlayer(uint64_t playerId) override;
133-
bool UpdatePlayerPosition(uint64_t playerId, float x, float y, float z) override;
134-
bool PlayerExists(uint64_t playerId) override;
135-
nlohmann::json GetPlayerStats(uint64_t playerId) override;
136-
bool UpdatePlayerStats(uint64_t playerId, const nlohmann::json& stats) override;
137-
138-
// Game State Operations
139-
bool SaveGameState(const std::string& key, const nlohmann::json& state) override;
140-
nlohmann::json LoadGameState(const std::string& key) override;
141-
bool DeleteGameState(const std::string& key) override;
142-
std::vector<std::string> ListGameStates() override;
143-
144-
// World Data Operations
145-
bool SaveChunkData(int chunkX, int chunkZ, const nlohmann::json& chunkData) override;
146-
nlohmann::json LoadChunkData(int chunkX, int chunkZ) override;
147-
bool DeleteChunkData(int chunkX, int chunkZ) override;
148-
std::vector<std::pair<int, int>> ListChunksInRange(int centerX, int centerZ, int radius) override;
149-
150-
// Inventory Operations
151-
bool SaveInventory(uint64_t playerId, const nlohmann::json& inventory) override;
152-
nlohmann::json LoadInventory(uint64_t playerId) override;
153-
154-
// Quest Operations
155-
bool SaveQuestProgress(uint64_t playerId, const std::string& questId, const nlohmann::json& progress) override;
156-
nlohmann::json LoadQuestProgress(uint64_t playerId, const std::string& questId) override;
157-
std::vector<std::string> ListActiveQuests(uint64_t playerId) override;
158-
159-
// Transaction Operations
160-
bool BeginTransaction() override;
161-
bool CommitTransaction() override;
162-
bool RollbackTransaction() override;
163-
bool ExecuteTransaction(const std::function<bool()>& operation) override;
164-
165-
// Query Operations
166-
nlohmann::json Query(const std::string& sql) override;
167-
nlohmann::json QueryWithParams(const std::string& sql, const std::vector<std::string>& params) override;
168-
bool Execute(const std::string& sql) override;
169-
bool ExecuteWithParams(const std::string& sql, const std::vector<std::string>& params) override;
170-
171-
// Shard Operations (simulated for compatibility)
172-
nlohmann::json QueryShard(int shardId, const std::string& sql) override;
173-
nlohmann::json QueryShardWithParams(int shardId, const std::string& sql, const std::vector<std::string>& params) override;
174-
bool ExecuteShard(int shardId, const std::string& sql) override;
175-
bool ExecuteShardWithParams(int shardId, const std::string& sql, const std::vector<std::string>& params) override;
176-
177-
// Utility Methods
178-
std::string EscapeString(const std::string& str) override;
179-
int GetShardId(uint64_t entityId) const override;
180-
int GetTotalShards() const override;
181-
std::string GetConnectionInfo() const override;
182-
int64_t GetLastInsertId() override;
183-
int GetAffectedRows() override;
184-
185-
// Statistics
186-
nlohmann::json GetDatabaseStats() override;
187-
void ResetStats() override;
188-
189-
// Connection Pool Management
190-
bool InitializeConnectionPool(size_t minConnections, size_t maxConnections) override;
191-
void ReleaseConnectionPool() override;
192-
size_t GetActiveConnections() const override;
193-
size_t GetIdleConnections() const override;
194-
195-
private:
196-
// PIMPL pattern to hide PostgreSQL implementation details
197-
class Impl;
198-
std::unique_ptr<Impl> impl_;
199-
200-
nlohmann::json config_;
201-
std::atomic<bool> connected_;
202-
203-
// Connection pool statistics
204-
struct PoolStats {
205-
size_t activeConnections;
206-
size_t idleConnections;
207-
size_t totalConnections;
208-
};
209-
PoolStats poolStats_;
210-
};
211-
212-
#ifdef USE_CITUS
213-
/**
214-
* @brief Citus Client Implementation
215-
*
216-
* Extends PostgreSQL client with Citus-specific distributed database features.
217-
*/
218-
class CitusCli : public PostgreSqlCli {
219-
public:
220-
static CitusCli& GetInstance();
221-
222-
CitusCli(const nlohmann::json& config);
223-
virtual ~CitusCli();
224-
225-
// Citus-specific operations
226-
bool CreateDistributedTable(const std::string& tableName, const std::string& distributionColumn);
227-
bool CreateReferenceTable(const std::string& tableName);
228-
bool AddWorkerNode(const std::string& host, int port);
229-
bool RemoveWorkerNode(const std::string& host, int port);
230-
nlohmann::json GetWorkerNodes();
231-
bool RebalanceShards();
232-
233-
// Override shard operations for actual Citus distribution
234-
nlohmann::json QueryShard(int shardId, const std::string& sql) override;
235-
nlohmann::json QueryShardWithParams(int shardId, const std::string& sql, const std::vector<std::string>& params) override;
236-
bool ExecuteShard(int shardId, const std::string& sql) override;
237-
bool ExecuteShardWithParams(int shardId, const std::string& sql, const std::vector<std::string>& params) override;
238-
239-
// Enhanced shard management
240-
int GetShardId(uint64_t entityId) const override;
241-
int GetTotalShards() const override;
242-
bool MoveShard(int shardId, const std::string& sourceNode, const std::string& targetNode);
243-
nlohmann::json GetShardPlacements();
244-
245-
// Citus-specific statistics
246-
nlohmann::json GetCitusStats();
247-
nlohmann::json GetQueryStats();
248-
249-
// Backward compatibility methods
250-
bool ExecuteDatabase(const std::string& sql) { return Execute(sql); }
251-
nlohmann::json QueryDatabase(const std::string& sql) { return Query(sql); }
252-
253-
private:
254-
static std::mutex instanceMutex_;
255-
static CitusCli* instance_;
256-
257-
// Citus-specific configuration
258-
int totalShards_;
259-
std::unordered_map<std::string, int> workerNodes_;
260-
};
261-
#endif

include/game/InventorySystem.hpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
11
#pragma once
22

3-
#include <vector>
4-
#include <unordered_map>
3+
#include <algorithm>
54
#include <memory>
65
#include <mutex>
7-
8-
#include "game/LootItem.hpp"
6+
#include <vector>
7+
#include <unordered_map>
98

109
#include "database/DbManager.hpp"
10+
#include "logging/Logger.hpp"
11+
#include "game/LootItem.hpp"
1112

1213
struct InventorySlot {
1314
std::shared_ptr<LootItem> item;
1415
int quantity = 0;
1516
bool equipped = false;
16-
int position = -1; // For equipment slots
17-
17+
int position = -1;
18+
1819
nlohmann::json Serialize() const;
1920
void Deserialize(const nlohmann::json& data);
2021
};
2122

2223
class InventorySystem {
2324
public:
2425
static InventorySystem& GetInstance();
25-
26+
2627
// Inventory management
2728
bool AddItem(uint64_t playerId, const LootItem& item, int quantity = 1);
2829
bool RemoveItem(uint64_t playerId, const std::string& itemId, int quantity = 1);
2930
bool MoveItem(uint64_t playerId, int fromSlot, int toSlot);
3031
bool SwapItems(uint64_t playerId, int slot1, int slot2);
3132
bool SplitStack(uint64_t playerId, int slot, int splitQuantity);
3233
bool MergeStacks(uint64_t playerId, int sourceSlot, int targetSlot);
33-
34+
3435
// Equipment management
3536
bool EquipItem(uint64_t playerId, int inventorySlot);
3637
bool UnequipItem(uint64_t playerId, int equipmentSlot);
3738
bool AutoEquip(uint64_t playerId, const std::string& itemId);
38-
39+
3940
// Query methods
4041
std::shared_ptr<LootItem> GetItem(uint64_t playerId, int slot);
4142
int GetItemCount(uint64_t playerId, const std::string& itemId) const;
4243
bool HasItem(uint64_t playerId, const std::string& itemId, int quantity = 1) const;
4344
std::vector<InventorySlot> GetInventory(uint64_t playerId) const;
4445
std::vector<InventorySlot> GetEquipment(uint64_t playerId) const;
45-
46+
4647
// Space management
4748
int GetFreeSlots(uint64_t playerId) const;
4849
int GetTotalSlots(uint64_t playerId) const;
4950
bool HasSpaceFor(uint64_t playerId, const LootItem& item, int quantity = 1) const;
50-
51+
5152
// Trading
5253
bool CanTradeItem(uint64_t playerId, const std::string& itemId) const;
5354
bool TransferItem(uint64_t fromPlayerId, uint64_t toPlayerId, const std::string& itemId, int quantity);
54-
55+
5556
// Serialization
5657
bool LoadInventory(uint64_t playerId);
5758
bool SaveInventory(uint64_t playerId);
5859
nlohmann::json SerializeInventory(uint64_t playerId) const;
5960
bool DeserializeInventory(uint64_t playerId, const nlohmann::json& data);
60-
61+
6162
// Gold/currency
6263
int64_t GetGold(uint64_t playerId) const;
6364
bool AddGold(uint64_t playerId, int64_t amount);
6465
bool RemoveGold(uint64_t playerId, int64_t amount);
6566
bool TransferGold(uint64_t fromPlayerId, uint64_t toPlayerId, int64_t amount);
66-
67+
6768
private:
6869
InventorySystem() = default;
6970
~InventorySystem() = default;
70-
71+
7172
struct PlayerInventory {
7273
std::vector<InventorySlot> inventorySlots;
7374
std::vector<InventorySlot> equipmentSlots;
7475
int64_t gold = 0;
7576
int maxInventorySize = 40;
7677
int maxEquipmentSlots = 12;
77-
78-
// Equipment slot mapping
78+
7979
enum EquipmentSlot {
8080
HEAD = 0,
8181
CHEST = 1,
@@ -91,11 +91,10 @@ class InventorySystem {
9191
TRINKET2 = 11
9292
};
9393
};
94-
94+
9595
mutable std::mutex mutex_;
9696
std::unordered_map<uint64_t, PlayerInventory> playerInventories_;
97-
CitusClient& dbClient_;
98-
97+
9998
// Helper methods
10099
bool ValidateSlot(uint64_t playerId, int slot) const;
101100
int FindItemSlot(uint64_t playerId, const std::string& itemId) const;
@@ -104,8 +103,8 @@ class InventorySystem {
104103
bool IsEquipmentSlot(int slot) const;
105104
int GetEquipmentSlotForItem(const LootItem& item) const;
106105
bool MeetsRequirements(uint64_t playerId, const LootItem& item) const;
107-
106+
108107
// Database operations
109108
bool LoadFromDatabase(uint64_t playerId);
110109
bool SaveToDatabase(uint64_t playerId);
111-
};
110+
};

0 commit comments

Comments
 (0)