Fix web state management: add state metadata to API responses
Issues fixed: - Remove redundant parameter in handleGameInput (line 195) - Add getStateInfo() method to GameSession to return current game state - Return state metadata in both handleGameRender() and handleGameInput() - Add playerInfo (hp, mana, exp) to responses for UI sync - Add state name mapping for debugging Changes: 1. GameSession.php: - Rename getPlayerInfo() to include full stats (mana, exp) - Add getStateInfo() returning state, stateName, playerInfo - Add getStateName() helper to convert state constant to string 2. server.php: - Fix handleGameInput() parameter error - handleGameRender() now returns state metadata - handleGameInput() now returns state metadata 3. index.html (web): - Add console.log for debugging state sync - Add stateName logging to track state transitions - Prepare for renderGame() refresh (commented) These changes allow: - Frontend to verify correct game state after each action - Debugging state sync issues via browser console - Foundation for state validation in UI 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
ac5f57c3ca
commit
10e64d4766
|
|
@ -157,7 +157,42 @@ class GameSession
|
||||||
'level' => $this->game->player->level,
|
'level' => $this->game->player->level,
|
||||||
'hp' => $this->game->player->hp,
|
'hp' => $this->game->player->hp,
|
||||||
'maxHp' => $stats['maxHp'],
|
'maxHp' => $stats['maxHp'],
|
||||||
|
'mana' => $this->game->player->mana,
|
||||||
|
'maxMana' => $stats['maxMana'],
|
||||||
'spiritStones' => $this->game->player->spiritStones,
|
'spiritStones' => $this->game->player->spiritStones,
|
||||||
|
'exp' => $this->game->player->exp,
|
||||||
|
'maxExp' => $this->game->player->maxExp,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前游戏状态信息
|
||||||
|
*/
|
||||||
|
public function getStateInfo(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'state' => $this->game->state,
|
||||||
|
'stateName' => $this->getStateName(),
|
||||||
|
'playerInfo' => $this->getPlayerInfo(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将状态常量转换为名称
|
||||||
|
*/
|
||||||
|
private function getStateName(): string
|
||||||
|
{
|
||||||
|
return match($this->game->state) {
|
||||||
|
Game::MENU => 'MENU',
|
||||||
|
Game::DUNGEON_SELECT => 'DUNGEON_SELECT',
|
||||||
|
Game::BATTLE => 'BATTLE',
|
||||||
|
Game::STATS => 'STATS',
|
||||||
|
Game::INVENTORY => 'INVENTORY',
|
||||||
|
Game::EQUIPMENT_ENHANCE => 'EQUIPMENT_ENHANCE',
|
||||||
|
Game::NPC => 'NPC',
|
||||||
|
Game::TALENT => 'TALENT',
|
||||||
|
Game::EXIT => 'EXIT',
|
||||||
|
default => 'UNKNOWN'
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -472,6 +472,13 @@
|
||||||
const result = await api('game/input', { input });
|
const result = await api('game/input', { input });
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
|
console.log('API响应:', result);
|
||||||
|
|
||||||
|
// 显示调试信息(可选)
|
||||||
|
if (result.stateName) {
|
||||||
|
console.log('当前状态:', result.stateName);
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否是时间戳战斗日志
|
// 检查是否是时间戳战斗日志
|
||||||
if (result.type === 'battle_log' && result.logs) {
|
if (result.type === 'battle_log' && result.logs) {
|
||||||
terminal.clear();
|
terminal.clear();
|
||||||
|
|
@ -483,6 +490,11 @@
|
||||||
lines.forEach(line => {
|
lines.forEach(line => {
|
||||||
terminal.writeln(line);
|
terminal.writeln(line);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 输入后立即刷新当前界面(确保状态同步)
|
||||||
|
// setTimeout(() => {
|
||||||
|
// renderGame();
|
||||||
|
// }, 100);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
terminal.writeln('\x1b[31m' + (result.message || '操作失败') + '\x1b[0m');
|
terminal.writeln('\x1b[31m' + (result.message || '操作失败') + '\x1b[0m');
|
||||||
|
|
|
||||||
|
|
@ -176,10 +176,14 @@ function handleGameRender(): array
|
||||||
|
|
||||||
$session = new GameSession($_SESSION['user_id']);
|
$session = new GameSession($_SESSION['user_id']);
|
||||||
$output = $session->render();
|
$output = $session->render();
|
||||||
|
$stateInfo = $session->getStateInfo();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'output' => $output,
|
'output' => $output,
|
||||||
|
'state' => $stateInfo['state'],
|
||||||
|
'stateName' => $stateInfo['stateName'],
|
||||||
|
'playerInfo' => $stateInfo['playerInfo'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,18 +196,23 @@ function handleGameInput(): array
|
||||||
$data = getInput();
|
$data = getInput();
|
||||||
$input = $data['input'] ?? '';
|
$input = $data['input'] ?? '';
|
||||||
|
|
||||||
$session = new GameSession($_SESSION['user_id'],$input);
|
$session = new GameSession($_SESSION['user_id']);
|
||||||
$output = $session->handleInput($input);
|
$output = $session->handleInput($input);
|
||||||
|
|
||||||
|
// 获取当前游戏状态信息
|
||||||
|
$stateInfo = $session->getStateInfo();
|
||||||
|
|
||||||
// 如果输出是数组(时间戳数据),直接返回
|
// 如果输出是数组(时间戳数据),直接返回
|
||||||
if (is_array($output)) {
|
if (is_array($output)) {
|
||||||
return array_merge(['success' => true], $output);
|
return array_merge(['success' => true], $stateInfo, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 否则返回纯文本输出
|
// 否则返回纯文本输出 + 状态信息
|
||||||
return [
|
return [
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'output' => $output,
|
'output' => $output,
|
||||||
|
'state' => $stateInfo['state'],
|
||||||
|
'playerInfo' => $stateInfo['playerInfo'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user