mirror of
https://github.com/ChuXunYu/OnlineRpg.git
synced 2026-01-31 07:01:26 +00:00
3
This commit is contained in:
@@ -60,6 +60,7 @@ void GameClient::run() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
while (m_isRunning && m_isConnected) {
|
while (m_isRunning && m_isConnected) {
|
||||||
if (!m_isAuthenticated) {
|
if (!m_isAuthenticated) {
|
||||||
showMainMenu();
|
showMainMenu();
|
||||||
@@ -74,6 +75,11 @@ void GameClient::run() {
|
|||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GameClient::sendMessage(const std::string& message) {
|
bool GameClient::sendMessage(const std::string& message) {
|
||||||
@@ -81,6 +87,7 @@ bool GameClient::sendMessage(const std::string& message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameClient::recvLoop() {
|
void GameClient::recvLoop() {
|
||||||
|
try {
|
||||||
while (m_isRunning) {
|
while (m_isRunning) {
|
||||||
std::string message = m_socket.recvLine();
|
std::string message = m_socket.recvLine();
|
||||||
if (message.empty()) {
|
if (message.empty()) {
|
||||||
@@ -94,6 +101,15 @@ void GameClient::recvLoop() {
|
|||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameClient::handleServerMessage(const std::string& message) {
|
void GameClient::handleServerMessage(const std::string& message) {
|
||||||
@@ -223,6 +239,12 @@ void GameClient::showLobbyMenu() {
|
|||||||
std::string input;
|
std::string input;
|
||||||
showPrompt();
|
showPrompt();
|
||||||
std::getline(std::cin, input);
|
std::getline(std::cin, input);
|
||||||
|
|
||||||
|
// 检查状态是否在输入期间改变(例如收到战斗邀请被接受)
|
||||||
|
if (m_inBattle) {
|
||||||
|
return; // 已进入战斗,不处理大厅输入
|
||||||
|
}
|
||||||
|
|
||||||
handleLobbyInput(input);
|
handleLobbyInput(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,6 +256,12 @@ void GameClient::showBattleMenu() {
|
|||||||
std::string input;
|
std::string input;
|
||||||
showPrompt();
|
showPrompt();
|
||||||
std::getline(std::cin, input);
|
std::getline(std::cin, input);
|
||||||
|
|
||||||
|
// 检查状态是否在输入期间改变
|
||||||
|
if (!m_inBattle || !m_waitingForTurn) {
|
||||||
|
return; // 状态已改变,不处理输入
|
||||||
|
}
|
||||||
|
|
||||||
handleBattleInput(input);
|
handleBattleInput(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ std::string Fireball::execute(ICharacter& caster, ICharacter& target) {
|
|||||||
|
|
||||||
FrostNova::FrostNova() {
|
FrostNova::FrostNova() {
|
||||||
m_name = "FrostNova";
|
m_name = "FrostNova";
|
||||||
m_description = "Freeze and damage enemy";
|
m_description = "Freeze enemies in an area";
|
||||||
m_manaCost = 25;
|
m_manaCost = 25;
|
||||||
m_cooldown = 0;
|
m_cooldown = 0;
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ std::string FrostNova::execute(ICharacter& caster, ICharacter& target) {
|
|||||||
|
|
||||||
ArcaneShield::ArcaneShield() {
|
ArcaneShield::ArcaneShield() {
|
||||||
m_name = "ArcaneShield";
|
m_name = "ArcaneShield";
|
||||||
m_description = "Create magical shield";
|
m_description = "Create a protective shield";
|
||||||
m_manaCost = 30;
|
m_manaCost = 30;
|
||||||
m_cooldown = 0;
|
m_cooldown = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,15 +64,23 @@ bool BattleRoom::handleAction(const std::string& username,
|
|||||||
const std::string& targetName) {
|
const std::string& targetName) {
|
||||||
std::lock_guard<std::mutex> lock(m_mutex);
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
|
|
||||||
|
std::cout << "[DEBUG] handleAction called: user=" << username
|
||||||
|
<< " skillIndex=" << skillIndex
|
||||||
|
<< " target=" << targetName << std::endl;
|
||||||
|
|
||||||
if (!m_isRunning) {
|
if (!m_isRunning) {
|
||||||
|
std::cout << "[DEBUG] Battle not running!" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否轮到该玩家
|
// 检查是否轮到该玩家
|
||||||
if (!isPlayerTurn(username)) {
|
if (!isPlayerTurn(username)) {
|
||||||
|
std::cout << "[DEBUG] Not player's turn! Current turn: " << m_currentTurn << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "[DEBUG] Executing action..." << std::endl;
|
||||||
|
|
||||||
// 确定攻击者和防守者
|
// 确定攻击者和防守者
|
||||||
ICharacter* attacker = (m_currentTurn == 0) ? m_char1.get() : m_char2.get();
|
ICharacter* attacker = (m_currentTurn == 0) ? m_char1.get() : m_char2.get();
|
||||||
ICharacter* defender = (m_currentTurn == 0) ? m_char2.get() : m_char1.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) {
|
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