diff --git a/.idea/codeception.xml b/.idea/codeception.xml index 70e3d14..c1859d2 100644 --- a/.idea/codeception.xml +++ b/.idea/codeception.xml @@ -12,6 +12,9 @@ + + diff --git a/.idea/phpspec.xml b/.idea/phpspec.xml index c7cfbc2..41f23c4 100644 --- a/.idea/phpspec.xml +++ b/.idea/phpspec.xml @@ -11,6 +11,9 @@ + + \ No newline at end of file diff --git a/src/Core/Game.php b/src/Core/Game.php index 13557a0..62c1a12 100644 --- a/src/Core/Game.php +++ b/src/Core/Game.php @@ -201,6 +201,7 @@ class Game $this->saveState(); return; } + $this->saveState(); } } } diff --git a/src/Core/GameSession.php b/src/Core/GameSession.php index 2916cc4..a6ae8bc 100644 --- a/src/Core/GameSession.php +++ b/src/Core/GameSession.php @@ -60,12 +60,17 @@ class GameSession // 保存状态 $this->game->saveState(); - // 如果启用了计时模式(战斗中),返回JSON格式的时间戳数据 - if ($this->output->isTimingEnabled()) { - return $this->output->getTimedOutput(); - } + // 获取当前状态信息 + $stateInfo = $this->getStateInfo(); + $output = $this->output->getOutput(); - return $this->output->getOutput(); + // 返回包含状态信息的结构化数据 + return [ + 'output' => $output, + 'state' => $stateInfo['state'], + 'stateName' => $stateInfo['stateName'], + 'playerInfo' => $stateInfo['playerInfo'], + ]; } /** @@ -131,11 +136,6 @@ class GameSession case Game::TALENT: (new \Game\Modules\TalentPanel($this->game))->show(); break; - case Game::EXIT: - exit; - $this->output->writeln("再见!"); - $this->game->saveState(); - break; } } diff --git a/src/Core/GameWebSocketServer.php b/src/Core/GameWebSocketServer.php index 3002fdc..38f6402 100644 --- a/src/Core/GameWebSocketServer.php +++ b/src/Core/GameWebSocketServer.php @@ -146,25 +146,17 @@ class GameWebSocketServer implements MessageComponentInterface try { $sessionData = $this->sessions[$conn->resourceId]; + /** @var GameSession $session */ $session = $sessionData['session']; - // 处理输入 - $output = $session->handleInput($input); - $stateInfo = $session->getStateInfo(); + // 处理输入 - 现在返回结构化数据 + $result = $session->handleInput($input); + + // result是数组,包含:output, state, stateName, playerInfo + $this->sendMessage($conn, array_merge([ + 'type' => 'game-output', + ], $result)); - // 如果是战斗,使用流式输出 - if ($stateInfo['stateName'] === 'BATTLE') { - $this->handleBattleStream($conn, $session); - } else { - // 普通输出 - $this->sendMessage($conn, [ - 'type' => 'game-output', - 'output' => is_array($output) ? '' : $output, - 'state' => $stateInfo['state'], - 'stateName' => $stateInfo['stateName'], - 'playerInfo' => $stateInfo['playerInfo'], - ]); - } } catch (\Exception $e) { $this->sendError($conn, '输入处理失败: ' . $e->getMessage()); } @@ -184,7 +176,6 @@ class GameWebSocketServer implements MessageComponentInterface // 创建SSEOutput替代品 - 收集输出然后发送 // 为了简化,我们直接用WebSocket消息逐行发送 echo "[战斗] 用户 {$this->sessions[$conn->resourceId]['username']} 进入战斗\n"; - // 发送战斗结束信号(实际战斗已经完成) $stateInfo = $session->getStateInfo(); $this->sendMessage($conn, [ diff --git a/web/game-ws.html b/web/game-ws.html index 3f9cda7..37091f4 100644 --- a/web/game-ws.html +++ b/web/game-ws.html @@ -293,17 +293,17 @@ -
- - - - - - - - - -
+ + + + + + + + + + + @@ -420,6 +420,7 @@ break; case 'game-output': + console.log(data) terminal.clear(); displayOutput(data.output); updateGameStatus(data.stateName); diff --git a/web/server.php b/web/server.php index 891d12e..30d300a 100644 --- a/web/server.php +++ b/web/server.php @@ -38,9 +38,9 @@ $requestUri = $_SERVER['REQUEST_URI']; $path = parse_url($requestUri, PHP_URL_PATH); // 静态文件处理 -if ($path === '/' || $path === '/index.html') { +if ($path === '/' || $path === '/game-ws.html') { header('Content-Type: text/html; charset=utf-8'); - readfile(__DIR__ . '/index.html'); + readfile(__DIR__ . '/game-ws.html'); exit; } @@ -197,23 +197,10 @@ function handleGameInput(): array $input = $data['input'] ?? ''; $session = new GameSession($_SESSION['user_id']); - $output = $session->handleInput($input); + $result = $session->handleInput($input); - // 获取当前游戏状态信息 - $stateInfo = $session->getStateInfo(); - - // 如果输出是数组(时间戳数据),直接返回 - if (is_array($output)) { - return array_merge(['success' => true], $stateInfo, $output); - } - - // 否则返回纯文本输出 + 状态信息 - return [ - 'success' => true, - 'output' => $output, - 'state' => $stateInfo['state'], - 'playerInfo' => $stateInfo['playerInfo'], - ]; + // 现在handleInput返回的是数组:output, state, stateName, playerInfo + return array_merge(['success' => true], $result); } /**