mirror of
https://github.com/ChuXunYu/OnlineRpg.git
synced 2026-01-30 23:41:26 +00:00
3
This commit is contained in:
@@ -60,19 +60,25 @@ void GameClient::run() {
|
||||
return;
|
||||
}
|
||||
|
||||
while (m_isRunning && m_isConnected) {
|
||||
if (!m_isAuthenticated) {
|
||||
showMainMenu();
|
||||
} else if (m_inBattle && m_waitingForTurn) {
|
||||
// 只在战斗中且轮到玩家时才显示战斗菜单
|
||||
showBattleMenu();
|
||||
} else if (!m_inBattle) {
|
||||
// 不在战斗中,显示大厅菜单
|
||||
showLobbyMenu();
|
||||
} else {
|
||||
// 在战斗中但不是玩家回合,等待
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
try {
|
||||
while (m_isRunning && m_isConnected) {
|
||||
if (!m_isAuthenticated) {
|
||||
showMainMenu();
|
||||
} else if (m_inBattle && m_waitingForTurn) {
|
||||
// 只在战斗中且轮到玩家时才显示战斗菜单
|
||||
showBattleMenu();
|
||||
} else if (!m_inBattle) {
|
||||
// 不在战斗中,显示大厅菜单
|
||||
showLobbyMenu();
|
||||
} else {
|
||||
// 在战斗中但不是玩家回合,等待
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "主循环异常: " << e.what() << std::endl;
|
||||
} catch (...) {
|
||||
std::cerr << "主循环未知异常" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,18 +87,28 @@ bool GameClient::sendMessage(const std::string& message) {
|
||||
}
|
||||
|
||||
void GameClient::recvLoop() {
|
||||
while (m_isRunning) {
|
||||
std::string message = m_socket.recvLine();
|
||||
if (message.empty()) {
|
||||
if (m_isRunning) {
|
||||
std::cerr << "\n连接已断开" << std::endl;
|
||||
m_isConnected = false;
|
||||
m_isRunning = false;
|
||||
try {
|
||||
while (m_isRunning) {
|
||||
std::string message = m_socket.recvLine();
|
||||
if (message.empty()) {
|
||||
if (m_isRunning) {
|
||||
std::cerr << "\n连接已断开" << std::endl;
|
||||
m_isConnected = false;
|
||||
m_isRunning = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
handleServerMessage(message);
|
||||
}
|
||||
|
||||
handleServerMessage(message);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "\n接收线程异常: " << e.what() << std::endl;
|
||||
m_isConnected = false;
|
||||
m_isRunning = false;
|
||||
} catch (...) {
|
||||
std::cerr << "\n接收线程未知异常" << std::endl;
|
||||
m_isConnected = false;
|
||||
m_isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +239,12 @@ void GameClient::showLobbyMenu() {
|
||||
std::string input;
|
||||
showPrompt();
|
||||
std::getline(std::cin, input);
|
||||
|
||||
// 检查状态是否在输入期间改变(例如收到战斗邀请被接受)
|
||||
if (m_inBattle) {
|
||||
return; // 已进入战斗,不处理大厅输入
|
||||
}
|
||||
|
||||
handleLobbyInput(input);
|
||||
}
|
||||
|
||||
@@ -234,6 +256,12 @@ void GameClient::showBattleMenu() {
|
||||
std::string input;
|
||||
showPrompt();
|
||||
std::getline(std::cin, input);
|
||||
|
||||
// 检查状态是否在输入期间改变
|
||||
if (!m_inBattle || !m_waitingForTurn) {
|
||||
return; // 状态已改变,不处理输入
|
||||
}
|
||||
|
||||
handleBattleInput(input);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ static std::mt19937 gen(rd());
|
||||
// ===== NormalAttack 实现 =====
|
||||
|
||||
NormalAttack::NormalAttack() {
|
||||
m_name = "Normal Attack";
|
||||
m_name = "NormalAttack";
|
||||
m_description = "Basic attack";
|
||||
m_manaCost = 0;
|
||||
m_cooldown = 0;
|
||||
@@ -29,7 +29,7 @@ std::string NormalAttack::execute(ICharacter& caster, ICharacter& target) {
|
||||
// ===== HeavyStrike 实现 =====
|
||||
|
||||
HeavyStrike::HeavyStrike() {
|
||||
m_name = "Heavy Strike";
|
||||
m_name = "HeavyStrike";
|
||||
m_description = "Powerful strike consuming rage";
|
||||
m_manaCost = 10;
|
||||
m_cooldown = 0;
|
||||
@@ -54,7 +54,7 @@ std::string HeavyStrike::execute(ICharacter& caster, ICharacter& target) {
|
||||
// ===== DefensiveStance 实现 =====
|
||||
|
||||
DefensiveStance::DefensiveStance() {
|
||||
m_name = "Defensive Stance";
|
||||
m_name = "DefensiveStance";
|
||||
m_description = "Heal and boost defense";
|
||||
m_manaCost = 15;
|
||||
m_cooldown = 0;
|
||||
@@ -101,8 +101,8 @@ std::string Fireball::execute(ICharacter& caster, ICharacter& target) {
|
||||
// ===== FrostNova 实现 =====
|
||||
|
||||
FrostNova::FrostNova() {
|
||||
m_name = "Frost Nova";
|
||||
m_description = "Freeze and damage enemy";
|
||||
m_name = "FrostNova";
|
||||
m_description = "Freeze enemies in an area";
|
||||
m_manaCost = 25;
|
||||
m_cooldown = 0;
|
||||
}
|
||||
@@ -124,8 +124,8 @@ std::string FrostNova::execute(ICharacter& caster, ICharacter& target) {
|
||||
// ===== ArcaneShield 实现 =====
|
||||
|
||||
ArcaneShield::ArcaneShield() {
|
||||
m_name = "Arcane Shield";
|
||||
m_description = "Create magical shield";
|
||||
m_name = "ArcaneShield";
|
||||
m_description = "Create a protective shield";
|
||||
m_manaCost = 30;
|
||||
m_cooldown = 0;
|
||||
}
|
||||
|
||||
@@ -64,15 +64,23 @@ bool BattleRoom::handleAction(const std::string& username,
|
||||
const std::string& targetName) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
std::cout << "[DEBUG] handleAction called: user=" << username
|
||||
<< " skillIndex=" << skillIndex
|
||||
<< " target=" << targetName << std::endl;
|
||||
|
||||
if (!m_isRunning) {
|
||||
std::cout << "[DEBUG] Battle not running!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否轮到该玩家
|
||||
if (!isPlayerTurn(username)) {
|
||||
std::cout << "[DEBUG] Not player's turn! Current turn: " << m_currentTurn << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "[DEBUG] Executing action..." << std::endl;
|
||||
|
||||
// 确定攻击者和防守者
|
||||
ICharacter* attacker = (m_currentTurn == 0) ? m_char1.get() : m_char2.get();
|
||||
ICharacter* defender = (m_currentTurn == 0) ? m_char2.get() : m_char1.get();
|
||||
@@ -89,7 +97,19 @@ bool BattleRoom::handleAction(const std::string& username,
|
||||
}
|
||||
|
||||
if (!skill) {
|
||||
skill = attacker->getSkill("NormalAttack"); // fallback
|
||||
std::cerr << "[ERROR] Skill not found for skillIndex " << skillIndex
|
||||
<< " for character " << attacker->getName()
|
||||
<< ". Falling back to NormalAttack." << std::endl;
|
||||
skill = attacker->getSkill("NormalAttack");
|
||||
}
|
||||
|
||||
// 最终检查,如果连普通攻击都没有,就无法继续
|
||||
if (!skill) {
|
||||
std::cerr << "[FATAL] Critical error: NormalAttack skill is missing for character "
|
||||
<< attacker->getName() << ". Cannot proceed with action." << std::endl;
|
||||
// 在这种严重错误下,也许应该结束战斗或通知玩家
|
||||
broadcastToBoth(Protocol::buildBattleLog("错误:攻击失败,找不到技能!"));
|
||||
return false; // 提前退出,防止崩溃
|
||||
}
|
||||
|
||||
// 执行技能
|
||||
|
||||
Reference in New Issue
Block a user