#pragma once #include "ICharacter.h" #include "HistoryLog.h" #include #include #include #include // 前向声明 class ClientHandler; /** * @brief 战斗房间 * * 技术点: * 1. 多态 - ICharacter* 和 ISkill* * 2. 模板+链表 - HistoryLog * 3. STL - std::vector, std::mutex */ class BattleRoom { private: int m_roomId; // 战斗双方 std::shared_ptr m_player1; std::shared_ptr m_player2; // 角色 (多态) std::shared_ptr m_char1; std::shared_ptr m_char2; // 战斗日志 (模板+链表) HistoryLog m_historyLog; // 战斗状态 bool m_isRunning; int m_currentTurn; // 0 = player1, 1 = player2 int m_turnCount; std::mutex m_mutex; public: /** * @brief 构造函数 */ BattleRoom(int roomId, std::shared_ptr player1, std::shared_ptr player2, std::shared_ptr char1, std::shared_ptr char2); /** * @brief 析构函数 */ ~BattleRoom(); /** * @brief 启动战斗 */ void start(); /** * @brief 处理玩家行动 */ bool handleAction(const std::string& username, int skillIndex, const std::string& targetName); /** * @brief 检查战斗是否结束 */ bool isFinished() const; /** * @brief 获取胜利者 */ std::string getWinner() const; /** * @brief 获取战斗日志 */ std::vector getBattleLog() const; /** * @brief 获取房间ID */ int getRoomId() const { return m_roomId; } /** * @brief 检查是否轮到某玩家 */ bool isPlayerTurn(const std::string& username) const; private: /** * @brief 执行一回合 */ void executeTurn(); /** * @brief 广播给双方玩家 */ void broadcastToBoth(const std::string& message); /** * @brief 发送给玩家1 */ void sendToPlayer1(const std::string& message); /** * @brief 发送给玩家2 */ void sendToPlayer2(const std::string& message); /** * @brief 添加战斗日志 */ void addLog(const std::string& log); /** * @brief 结束战斗 */ void endBattle(); /** * @brief 发送回合通知 */ void notifyTurn(); /** * @brief 发送战斗状态 */ void sendBattleStatus(); };