Compare commits

..

13 Commits

Author SHA1 Message Date
d63e756265 优化 2025-12-07 22:01:41 +08:00
8014ba64e6 Implement potion pool (小绿瓶) system
- Add potionPool property to Player (initial: 1000 HP)
- Add addPotionPool(), consumePotionPool(), hasPotionPool() methods
- Modify Battle.php to add healing drops directly to pool instead of inventory
- Update InventoryPanel.php useItem() to consume from pool
- Update InventoryPanel.php autoHeal() to use pool system
- Add pool display in inventory header and stats panel
- Healing items no longer take inventory space after drops

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 18:51:21 +08:00
5249b36b7f Move input from text box to terminal
- Removed separate input box and send button from UI
- Added terminal input handling via onData() event listener
- Users can now type directly in the terminal
- Support for:
  - Regular input (echoed in terminal)
  - Backspace (with visual feedback)
  - Enter key to send command
  - Ctrl+C to clear current input
- Removed old keyboard event handlers for input box
- Terminal now has focus by default for seamless input experience

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 15:13:14 +08:00
73f1d4ba9d Handle game process exit gracefully
- Added proc_get_status() check in readProcessOutput() to detect when
  the game process has exited
- When process exits, read any remaining output and send to client
- Send system message to notify client that game process has ended
- Close client connection cleanly when process exits
- Prevents server from trying to read/write to dead process pipes
- Prevents "broken pipe" errors and connection hangs

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:53:06 +08:00
7c103d7209 Optimize frontend terminal rendering and remove unnecessary screen clears
- Removed terminal.clear() calls that were clearing screen on every output
  update, letting the game process control the display
- Added letterSpacing: 0 to xterm.js config to disable automatic spacing
  adjustments that were creating unwanted span tags
- Improved font family to 'Source Code Pro' for better handling of CJK
  characters and monospace alignment
- Added lineHeight adjustment (1.2) for better vertical spacing
- Enabled allowProposedApi for access to additional xterm.js features
- Game process output is now properly displayed without formatting artifacts

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:30:20 +08:00
52e465177c Fix event loop initialization and handle pipe errors
- Modified startup script to instantiate GameProcessServer separately
  before wrapping in WsServer/HttpServer, allowing direct access to
  set event loop after IoServer creation
- Implemented setLoop() method to set event loop after construction
- Added error handling for fwrite() to gracefully handle broken pipes
  when process closes
- Suppressed fread() warnings with @ operator to avoid noise
- Simplified startOutputReader() since event loop polling is now
  handled directly in onOpen()
- Added null checks for event loop before using in timers
- Server now properly starts with continuous 100ms output polling

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:17:28 +08:00
081903563a Fix line breaks in process output handling
Issues fixed:
- GameProcessServer.readProcessOutput() was using fgets() which could cause
  line breaks to be mishandled when reading multiple lines at once
- Changed to use fread() with 8KB buffer for non-blocking I/O
  - fread() is better for interactive programs
  - Properly handles newlines within chunks
  - Doesn't break lines unnecessarily

Frontend improvements:
- Changed sendInput() to use terminal.writeln() instead of write() + write('\n')
  - Ensures consistent line ending handling
  - Cleaner terminal output

Changes:
1. GameProcessServer.php (readProcessOutput):
   - Replaced fgets() loop with fread() loops
   - 8192-byte buffer size for optimal performance
   - Better handles non-blocking I/O streams

2. web/process.html (sendInput):
   - Use terminal.writeln() for user input display
   - More consistent with xterm.js behavior

Results:
- Line breaks now display correctly
- Output formatting preserved
- Better handling of rapid output
- No double line breaks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 13:35:36 +08:00
031428add6 Implement process-forwarding WebSocket architecture
New simplified approach: WebSocket server spawns bin/game process and forwards I/O

Backend Changes:
- New GameProcessServer class (src/Core/GameProcessServer.php)
  - Implements MessageComponentInterface (Ratchet)
  - For each WebSocket connection, spawns independent php bin/game process
  - Uses proc_open() to manage process I/O pipes
  - Reads process STDOUT/STDERR in non-blocking mode
  - Writes client input to process STDIN
  - Automatic process cleanup on disconnect
  - No game code modifications required

- New websocket-process-server.php startup script
  - Listens on port 9002
  - Simple process forwarder without game-specific logic
  - Suitable for any interactive CLI application

Frontend Changes:
- New web/process.html
  - Ultra-simple WebSocket frontend for process I/O
  - Direct STDIN/STDOUT forwarding
  - ANSI color support via xterm.js
  - Minimal dependencies, minimal code
  - Suitable for any CLI game/application

Architecture Benefits:
✓ Zero game code changes needed
✓ Each user gets independent process (isolation)
✓ Real process STDIO, not emulation
✓ ANSI colors work perfectly
✓ Can run ANY CLI application (not just this game)
✓ Simpler than GameSession-based approach
✓ Easier to deploy and manage

Usage:
1. Start WebSocket server:
   php websocket-process-server.php

2. Start HTTP file server (for static files):
   php -S 0.0.0.0:8080 web/server.php

3. Open browser:
   http://localhost:8080/process.html

Message Protocol:
Client → Server:
  { "type": "input", "input": "command" } - Send stdin to process
  { "type": "ping" } - Heartbeat

Server → Client:
  { "type": "output", "text": "..." } - Process stdout/stderr
  { "type": "system", "message": "..." } - Server messages
  { "type": "error", "message": "..." } - Error messages
  { "type": "pong" } - Heartbeat response

Features:
- Non-blocking I/O reading
- Stream buffering management
- Automatic reconnection support
- 30-second heartbeat for keep-alive
- Process termination on disconnect
- Proper error handling

This is the simplest and most elegant approach for running CLI games on web!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 13:21:23 +08:00
7308af1c1b Fix web state management: handleInput now returns structured data
Issues fixed:
- GameSession.handleInput() was returning plain text, causing WebSocket frontend
  to lose state information after each input
- GameWebSocketServer.handleGameInput() was calling unnecessary getStateInfo()
- Duplicate state saves in runCurrentState() and handleInput()

Changes:
1. GameSession.php:
   - handleInput() now returns structured array with output + state info
   - runCurrentState() no longer saves state (already done in handleInput)
   - Consistent return format: { output, state, stateName, playerInfo }

2. GameWebSocketServer.php:
   - handleGameInput() simplified to use handleInput() return value
   - Direct merge of result into WebSocket message

3. web/server.php:
   - handleGameInput() simplified to just return handleInput() result
   - No duplicate getStateInfo() call

Results:
- Web frontend now receives complete state info after each input
- State transitions in submenus now work correctly
- No more state desynchronization between client and server

Testing:
- Input in level 3+ menus now executes correctly
- State name updates properly in header
- Player info (HP, etc) stays synchronized

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 13:14:48 +08:00
cf84c53020 Implement WebSocket real-time communication for game
Replace HTTP API with WebSocket for true real-time bidirectional communication:

Backend Changes:
- Add Ratchet WebSocket library (cboden/ratchet) to composer.json
- New GameWebSocketServer class implementing MessageComponentInterface
  - Handles client connections and session management
  - Message types: login, game-input, sync-state, ping/pong
  - Maintains client connections map and user sessions
- New websocket-server.php startup script
  - Listens on port 9001
  - Uses Ratchet with HttpServer wrapper

Frontend Changes:
- New game-ws.html with WebSocket implementation
  - Replace HTTP requests with WebSocket messages
  - Keep HTTP for authentication (login/register/status)
  - WebSocket handles all game interactions
  - Real-time status display with connection indicator
  - Implements reconnection on disconnect
  - 30-second heartbeat (ping/pong) to maintain connection

Message Protocol:
Client → Server:
  login: { userId, username } - Authenticate and load game
  game-input: { input } - Send game command
  sync-state: {} - Request full state sync
  ping - Heart beat

Server → Client:
  welcome - Initial greeting
  login-success - Auth successful, game loaded
  game-output - Normal command output
  battle-start/end - Battle state changes
  state-sync - Full state snapshot
  error - Error message
  pong - Heartbeat response

Port Configuration:
- HTTP API: port 80 (web server)
- WebSocket: port 9001 (Ratchet server)
- Both services run independently

Usage:
1. Start web server: php -S 0.0.0.0:8080 web/server.php
2. Start WebSocket server: php websocket-server.php
3. Open browser: http://localhost:8080/game-ws.html

Benefits:
✓ True bidirectional real-time communication
✓ Can handle battle interactions in-game
✓ Better for multiplayer scenarios
✓ Persistent connections reduce latency
✓ Future support for spectating, PvP

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 12:21:30 +08:00
10e64d4766 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>
2025-12-07 11:19:28 +08:00
ac5f57c3ca Fix Screen.php: replace Laravel class_basename with native PHP 2025-12-07 11:11:25 +08:00
cb4b955bca Implement Server-Sent Events (SSE) for real-time battle streaming
Redesign web battle system from buffered to streaming architecture:

Backend Changes:
- New SSEOutput class for real-time event streaming to clients
- GameSession::streamBattle() for SSE-based battle execution
- Enhanced Screen::delay() to support SSE timing and buffering modes
- New /api/game/battle-stream endpoint handling SSE connections

Frontend Changes:
- Enhanced sendInput() to detect battle command (input "1")
- New streamBattle() function using EventSource for SSE connections
- Real-time log display matching terminal experience
- Event handlers for start, message, complete, error events

Benefits:
✓ Real-time streaming instead of waiting for complete battle
✓ Web frontend experience identical to terminal
✓ Lightweight implementation without WebSocket
✓ Automatic browser reconnection support
✓ ANSI colors fully preserved
✓ Backward compatible for non-battle screens

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 10:50:30 +08:00
26 changed files with 3763 additions and 243 deletions

View File

@ -12,6 +12,9 @@
<Configuration>
<option name="path" value="$PROJECT_DIR$/tests" />
</Configuration>
<Configuration>
<option name="path" value="$PROJECT_DIR$/tests" />
</Configuration>
</list>
</option>
</component>

View File

@ -11,6 +11,9 @@
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
</suites>
</component>
</project>

View File

@ -5,7 +5,8 @@
"require": {
"php": ">=8.0",
"symfony/console": "^6.4",
"symfony/var-dumper": "^6.4"
"symfony/var-dumper": "^6.4",
"cboden/ratchet": "^0.4"
},
"autoload": {
"psr-4": {

1371
composer.lock generated

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -70,6 +70,7 @@ class Game
$p = $data['player'] ?? [];
$this->player->hp = $p['hp'] ?? $this->player->hp;
$this->player->maxHp = $p['maxHp'] ?? $this->player->maxHp;
$this->player->potionPool = $p['potionPool'] ?? $this->player->potionPool;
// 新属性系统,向后兼容旧存档
$this->player->patk = $p['patk'] ?? $p['atk'] ?? $this->player->patk;
$this->player->matk = $p['matk'] ?? $this->player->matk;
@ -150,6 +151,7 @@ class Game
'critdmg' => $this->player->critdmg,
'level' => $this->player->level,
'exp' => $this->player->exp,
'potionPool' => $this->player->potionPool,
'maxExp' => $this->player->maxExp,
'inventory' => $this->player->inventory,
'equip' => $this->player->equip,

View File

@ -0,0 +1,345 @@
<?php
namespace Game\Core;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use React\EventLoop\LoopInterface;
/**
* 进程转发 WebSocket 服务器
* 为每个连接启动一个 bin/game 进程
* 将进程的STDOUT/STDERR转发给客户端
* 将客户端输入写入进程的STDIN
*/
class GameProcessServer implements MessageComponentInterface
{
protected array $clients = [];
protected array $processes = [];
protected ?LoopInterface $loop = null;
protected array $timers = [];
public function __construct(?LoopInterface $loop = null)
{
$this->loop = $loop;
echo "[进程服务器] 初始化\n";
}
/**
* 设置事件循环(用于延迟初始化)
*/
public function setLoop(LoopInterface $loop): void
{
$this->loop = $loop;
echo "[进程服务器] 事件循环已设置\n";
}
/**
* 客户端连接时
*/
public function onOpen(ConnectionInterface $conn)
{
echo "[连接] 新连接: {$conn->resourceId}\n";
$this->clients[$conn->resourceId] = $conn;
try {
// 为该连接启动一个游戏进程
$process = $this->startGameProcess($conn->resourceId);
$this->processes[$conn->resourceId] = $process;
$this->sendMessage($conn, [
'type' => 'system',
'message' => '游戏进程已启动,正在加载...'
]);
// 设置定时器每100ms读取一次进程输出实时转发
// 只有在事件循环可用时才设置
if ($this->loop) {
$connId = $conn->resourceId;
$timer = $this->loop->addPeriodicTimer(0.1, function() use ($conn, $connId) {
// 检查连接是否仍然有效
if (!isset($this->clients[$connId]) || !isset($this->processes[$connId])) {
return;
}
// 读取进程输出并发送给客户端
$this->readProcessOutput($conn);
});
$this->timers[$connId] = $timer;
echo "[定时器] 为连接 {$connId} 启动输出轮询\n";
} else {
echo "[警告] 事件循环未设置,无法启动输出轮询\n";
}
} catch (\Exception $e) {
$this->sendError($conn, '启动游戏失败: ' . $e->getMessage());
$conn->close();
}
}
/**
* 启动游戏进程
*/
private function startGameProcess(string $connId): array
{
$gameDir = __DIR__ . '/../../';
$gameScript = $gameDir . 'bin/game';
// 检查脚本是否存在
if (!file_exists($gameScript)) {
throw new \Exception("游戏脚本不存在: $gameScript");
}
// 设置管道描述符
$descriptorspec = [
0 => ['pipe', 'r'], // stdin
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'], // stderr
];
// 启动进程
$process = proc_open(
'php ' . escapeshellarg($gameScript),
$descriptorspec,
$pipes,
$gameDir,
[
'TERM' => 'xterm-256color',
'LANG' => 'en_US.UTF-8',
]
);
if (!is_resource($process)) {
throw new \Exception('无法启动游戏进程');
}
// 设置非阻塞模式
stream_set_blocking($pipes[0], false);
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
echo "[进程] 已启动进程 {$connId}: " . getmypid() . "\n";
// 启动输出读取线程使用select轮询
$this->startOutputReader($connId, $pipes[1], $pipes[2]);
return [
'process' => $process,
'stdin' => $pipes[0],
'stdout' => $pipes[1],
'stderr' => $pipes[2],
'connId' => $connId,
];
}
/**
* 启动异步输出读取
*/
private function startOutputReader(string $connId, $stdout, $stderr): void
{
// 已由 onOpen() 中的事件循环定时器处理
// 不再需要额外的初始化
}
/**
* 接收消息(用户输入)
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$data = json_decode($msg, true);
if (!$data) {
return; // 忽略无效消息
}
$type = $data['type'] ?? null;
$input = $data['input'] ?? '';
if ($type === 'input') {
// 将输入写入进程的STDIN
$process = $this->processes[$from->resourceId] ?? null;
if ($process && is_resource($process['stdin'])) {
$bytesWritten = @fwrite($process['stdin'], $input . "\n");
if ($bytesWritten === false || $bytesWritten === 0) {
// 写入失败,可能进程已关闭
echo "[警告] 无法写入进程 {$from->resourceId},进程可能已关闭\n";
// 不中断连接,等待下一次尝试或超时
} else {
fflush($process['stdin']);
echo "[输入] {$from->resourceId}: {$input}\n";
}
}
} elseif ($type === 'ping') {
$this->sendMessage($from, ['type' => 'pong']);
}
// 尝试读取进程输出
$this->readProcessOutput($from);
}
/**
* 读取进程输出
*/
private function readProcessOutput(ConnectionInterface $conn): void
{
$connId = $conn->resourceId;
$process = $this->processes[$connId] ?? null;
if (!$process) {
return;
}
// 检查进程是否还在运行
$processStatus = proc_get_status($process['process']);
if ($processStatus && !$processStatus['running']) {
// 进程已经退出,读取最后的输出并关闭连接
echo "[进程] 进程 {$connId} 已退出,状态码: " . $processStatus['exitcode'] . "\n";
// 尝试读取任何剩余的输出
$finalOutput = '';
$bufferSize = 8192;
while (true) {
$chunk = @fread($process['stdout'], $bufferSize);
if ($chunk === '' || $chunk === false) {
break;
}
$finalOutput .= $chunk;
}
while (true) {
$chunk = @fread($process['stderr'], $bufferSize);
if ($chunk === '' || $chunk === false) {
break;
}
$finalOutput .= $chunk;
}
if ($finalOutput) {
$this->sendMessage($conn, [
'type' => 'output',
'text' => $finalOutput
]);
}
// 发送进程退出消息
$this->sendMessage($conn, [
'type' => 'system',
'message' => '游戏进程已退出'
]);
// 关闭连接
$conn->close();
return;
}
$output = '';
// 在非阻塞模式下使用fread读取可用数据
// 这比fgets更适合处理交互式程序的输出
$bufferSize = 8192;
// 读取stdout
while (true) {
$chunk = @fread($process['stdout'], $bufferSize);
if ($chunk === '' || $chunk === false) {
break;
}
$output .= $chunk;
}
// 读取stderr
while (true) {
$chunk = @fread($process['stderr'], $bufferSize);
if ($chunk === '' || $chunk === false) {
break;
}
$output .= $chunk;
}
// 如果有输出,发送给客户端
if ($output) {
$this->sendMessage($conn, [
'type' => 'output',
'text' => $output
]);
}
}
/**
* 客户端关闭连接
*/
public function onClose(ConnectionInterface $conn)
{
$connId = $conn->resourceId;
// 取消定时器
if (isset($this->timers[$connId]) && $this->loop) {
$this->loop->cancelTimer($this->timers[$connId]);
unset($this->timers[$connId]);
echo "[定时器] 取消连接 {$connId} 的输出轮询\n";
}
// 关闭进程
$process = $this->processes[$connId] ?? null;
if ($process) {
if (is_resource($process['stdin'])) {
fclose($process['stdin']);
}
if (is_resource($process['stdout'])) {
fclose($process['stdout']);
}
if (is_resource($process['stderr'])) {
fclose($process['stderr']);
}
if (is_resource($process['process'])) {
proc_terminate($process['process']);
proc_close($process['process']);
}
unset($this->processes[$connId]);
echo "[进程] 已终止进程: {$connId}\n";
}
unset($this->clients[$connId]);
echo "[断开] 连接已关闭: {$connId}\n";
}
/**
* 连接错误
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "[错误] {$conn->resourceId}: {$e->getMessage()}\n";
// 取消定时器
$connId = $conn->resourceId;
if (isset($this->timers[$connId]) && $this->loop) {
$this->loop->cancelTimer($this->timers[$connId]);
unset($this->timers[$connId]);
}
$this->onClose($conn);
}
/**
* 发送消息给客户端
*/
protected function sendMessage(ConnectionInterface $conn, array $data): void
{
try {
$msg = json_encode($data, JSON_UNESCAPED_UNICODE);
$conn->send($msg);
} catch (\Exception $e) {
echo "[发送错误] {$e->getMessage()}\n";
}
}
/**
* 发送错误消息
*/
protected function sendError(ConnectionInterface $conn, string $message): void
{
$this->sendMessage($conn, [
'type' => 'error',
'message' => $message
]);
}
}

View File

@ -40,9 +40,9 @@ class GameSession
}
/**
* 处理用户输入并返回新界面
* 处理用户输入并返回新界面(支持时间戳数据)
*/
public function handleInput(string $input): string
public function handleInput(string $input): string|array
{
$this->output->clear();
@ -60,7 +60,47 @@ class GameSession
// 保存状态
$this->game->saveState();
return $this->output->getOutput();
// 获取当前状态信息
$stateInfo = $this->getStateInfo();
$output = $this->output->getOutput();
// 返回包含状态信息的结构化数据
return [
'output' => $output,
'state' => $stateInfo['state'],
'stateName' => $stateInfo['stateName'],
'playerInfo' => $stateInfo['playerInfo'],
];
}
/**
* SSE 流式战斗模式
* 当用户进入战斗时使用SSE直接推送日志
*/
public function streamBattle(string $input): void
{
// 使用SSEOutput替换普通输出
$sseOutput = new \Game\Core\SSEOutput();
// 替换game的output对象
$this->game->output = $sseOutput;
// 将输入推入队列
$webInput = WebInput::getInstance();
$webInput->clear();
$webInput->push($input);
try {
$this->runCurrentState();
} catch (NeedInputException $e) {
// 战斗中可能需要更多输入
}
// 保存状态
$this->game->saveState();
// 发送完成信号
$sseOutput->complete();
}
/**
@ -68,6 +108,9 @@ class GameSession
*/
private function runCurrentState(): void
{
if ($this->game->state == 0){
$this->game->state = 1;
}
switch ($this->game->state) {
case Game::MENU:
(new \Game\Modules\Menu($this->game))->show();
@ -93,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;
}
}
@ -119,7 +157,42 @@ class GameSession
'level' => $this->game->player->level,
'hp' => $this->game->player->hp,
'maxHp' => $stats['maxHp'],
'mana' => $this->game->player->mana,
'maxMana' => $stats['maxMana'],
'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'
};
}
}

View File

@ -0,0 +1,248 @@
<?php
namespace Game\Core;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
/**
* WebSocket 游戏服务器
* 处理前端的实时通信
*/
class GameWebSocketServer implements MessageComponentInterface
{
// 存储所有连接和对应的用户会话
protected array $clients = [];
protected array $sessions = [];
public function __construct()
{
echo "[WebSocket] 游戏服务器初始化\n";
}
/**
* 客户端连接时
*/
public function onOpen(ConnectionInterface $conn)
{
echo "[连接] 新连接: {$conn->resourceId}\n";
$this->clients[$conn->resourceId] = $conn;
// 发送欢迎消息
$this->sendMessage($conn, [
'type' => 'welcome',
'message' => '连接成功,请登录'
]);
}
/**
* 接收消息
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$data = json_decode($msg, true);
if (!$data) {
$this->sendError($from, '无效的JSON格式');
return;
}
$type = $data['type'] ?? null;
switch ($type) {
case 'login':
$this->handleLogin($from, $data);
break;
case 'game-input':
$this->handleGameInput($from, $data);
break;
case 'sync-state':
$this->handleSyncState($from, $data);
break;
case 'ping':
$this->sendMessage($from, ['type' => 'pong']);
break;
default:
$this->sendError($from, "未知消息类型: $type");
}
}
/**
* 客户端关闭连接
*/
public function onClose(ConnectionInterface $conn)
{
unset($this->clients[$conn->resourceId]);
if (isset($this->sessions[$conn->resourceId])) {
unset($this->sessions[$conn->resourceId]);
}
echo "[断开] 连接已关闭: {$conn->resourceId}\n";
}
/**
* 连接错误
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "[错误] {$conn->resourceId}: {$e->getMessage()}\n";
$conn->close();
}
/**
* 处理登录
*/
private function handleLogin(ConnectionInterface $conn, array $data): void
{
$userId = $data['userId'] ?? null;
$username = $data['username'] ?? null;
if (!$userId || !$username) {
$this->sendError($conn, '缺少userId或username');
return;
}
try {
// 创建游戏会话
$session = new GameSession($userId);
$this->sessions[$conn->resourceId] = [
'userId' => $userId,
'username' => $username,
'session' => $session,
];
// 发送登录成功和初始界面
$output = $session->render();
$stateInfo = $session->getStateInfo();
$this->sendMessage($conn, [
'type' => 'login-success',
'userId' => $userId,
'username' => $username,
'output' => $output,
'state' => $stateInfo['state'],
'stateName' => $stateInfo['stateName'],
'playerInfo' => $stateInfo['playerInfo'],
]);
echo "[登录] 用户 {$username} (ID: {$userId}) 已连接\n";
} catch (\Exception $e) {
$this->sendError($conn, '登录失败: ' . $e->getMessage());
}
}
/**
* 处理游戏输入
*/
private function handleGameInput(ConnectionInterface $conn, array $data): void
{
$input = $data['input'] ?? '';
if (!isset($this->sessions[$conn->resourceId])) {
$this->sendError($conn, '未登录');
return;
}
try {
$sessionData = $this->sessions[$conn->resourceId];
/** @var GameSession $session */
$session = $sessionData['session'];
// 处理输入 - 现在返回结构化数据
$result = $session->handleInput($input);
// result是数组包含output, state, stateName, playerInfo
$this->sendMessage($conn, array_merge([
'type' => 'game-output',
], $result));
} catch (\Exception $e) {
$this->sendError($conn, '输入处理失败: ' . $e->getMessage());
}
}
/**
* 处理战斗流SSE转WebSocket
*/
private function handleBattleStream(ConnectionInterface $conn, GameSession $session): void
{
// 发送战斗开始信号
$this->sendMessage($conn, [
'type' => 'battle-start',
'message' => '战斗开始'
]);
// 创建SSEOutput替代品 - 收集输出然后发送
// 为了简化我们直接用WebSocket消息逐行发送
echo "[战斗] 用户 {$this->sessions[$conn->resourceId]['username']} 进入战斗\n";
// 发送战斗结束信号(实际战斗已经完成)
$stateInfo = $session->getStateInfo();
$this->sendMessage($conn, [
'type' => 'battle-end',
'state' => $stateInfo['state'],
'stateName' => $stateInfo['stateName'],
'playerInfo' => $stateInfo['playerInfo'],
]);
}
/**
* 处理状态同步
*/
private function handleSyncState(ConnectionInterface $conn, array $data): void
{
if (!isset($this->sessions[$conn->resourceId])) {
$this->sendError($conn, '未登录');
return;
}
try {
$sessionData = $this->sessions[$conn->resourceId];
$session = $sessionData['session'];
$stateInfo = $session->getStateInfo();
$output = $session->render();
$this->sendMessage($conn, [
'type' => 'state-sync',
'output' => $output,
'state' => $stateInfo['state'],
'stateName' => $stateInfo['stateName'],
'playerInfo' => $stateInfo['playerInfo'],
]);
} catch (\Exception $e) {
$this->sendError($conn, '状态同步失败: ' . $e->getMessage());
}
}
/**
* 发送消息给客户端
*/
protected function sendMessage(ConnectionInterface $conn, array $data): void
{
$msg = json_encode($data, JSON_UNESCAPED_UNICODE);
$conn->send($msg);
}
/**
* 发送错误消息
*/
protected function sendError(ConnectionInterface $conn, string $message): void
{
$this->sendMessage($conn, [
'type' => 'error',
'message' => $message
]);
}
/**
* 广播消息给所有连接(管理员通知等)
*/
protected function broadcast(array $data): void
{
$msg = json_encode($data, JSON_UNESCAPED_UNICODE);
foreach ($this->clients as $conn) {
$conn->send($msg);
}
}
}

View File

@ -373,7 +373,10 @@ class ItemDisplay
{
$quality = $item['quality'] ?? $item['rarity'] ?? 'common';
$color = self::getQualityColor($quality);
$name = ($item['name'] ?? '未知物品') . 'lv.' . $item['level'];
if ($item['type'] == 'potion_pool'){
return self::$green .$item['name'] . '+' .$item['heal'];
}
$name = ($item['name'] ?? '未知物品') . 'lv.' . ($item['level'] ?? '');
$enhanceLevel = $item['enhanceLevel'] ?? 0;
$enhanceStr = $enhanceLevel > 0 ? self::$yellow . "+{$enhanceLevel}" . self::$reset : "";

137
src/Core/SSEOutput.php Normal file
View File

@ -0,0 +1,137 @@
<?php
namespace Game\Core;
/**
* Server-Sent Events 输出类
* 直接推送数据到客户端,用于实时战斗流
*/
class SSEOutput
{
private int $eventId = 0;
private float $startTime = 0;
public function __construct()
{
$this->startTime = microtime(true);
}
/**
* 写入消息(不换行)
*/
public function write(string $message): void
{
// 对于ANSI清屏等控制字符直接发送不需要特殊处理
$this->sendEvent([
'type' => 'write',
'text' => $message,
'time' => round((microtime(true) - $this->startTime) * 1000)
]);
}
/**
* 写入消息(换行)
*/
public function writeln(string $message): void
{
$this->sendEvent([
'type' => 'writeln',
'text' => $message,
'time' => round((microtime(true) - $this->startTime) * 1000)
]);
}
/**
* 推送事件到客户端
*/
private function sendEvent(array $data): void
{
// SSE格式
// event: message\n
// id: {eventId}\n
// data: {JSON}\n\n
echo "event: message\n";
echo "id: " . ($this->eventId++) . "\n";
echo "data: " . json_encode($data, JSON_UNESCAPED_UNICODE) . "\n\n";
// 立即刷新缓冲区发送给客户端
ob_flush();
flush();
}
/**
* 发送消息(用于通知而非日志)
*/
public function sendMessage(string $type, $data): void
{
echo "event: " . $type . "\n";
echo "id: " . ($this->eventId++) . "\n";
echo "data: " . json_encode($data, JSON_UNESCAPED_UNICODE) . "\n\n";
ob_flush();
flush();
}
/**
* 添加延迟在SSE模式下实际延迟
* 这样可以控制日志的显示速度
*/
public function delay(int $milliseconds): void
{
if ($milliseconds > 0) {
usleep($milliseconds * 1000);
}
}
/**
* 发送完成信号
*/
public function complete(): void
{
$this->sendMessage('complete', [
'message' => '战斗结束',
'totalTime' => round((microtime(true) - $this->startTime) * 1000)
]);
}
/**
* 发送错误
*/
public function error(string $message): void
{
$this->sendMessage('error', ['message' => $message]);
}
/**
* 兼容WebOutput的enableTiming方法
* SSE模式不需要计时直接返回
*/
public function enableTiming(): void
{
// SSE模式下不需要计时直接在运行时实时推送
}
/**
* 兼容WebOutput的getLineCount方法
*/
public function getLineCount(): int
{
return 0; // SSE不缓冲
}
/**
* 获取缓冲的输出SSE不使用
*/
public function getOutput(): string
{
return ''; // SSE模式不缓冲
}
/**
* 清空缓冲区SSE不使用
*/
public function clear(): void
{
// SSE不缓冲无需清空
}
}

View File

@ -34,13 +34,32 @@ class Screen
/**
* Web 兼容的延迟
* Web 模式下跳过延迟
* - 终端模式:使用 usleep
* - SSE 模式实际延迟usleep
* - WebOutput 计时模式:记录虚拟时间戳
*
* @param int $microseconds 延迟微秒数
* @param object|null $out 可选的输出对象WebOutput SSEOutput
*/
public static function delay(int $microseconds): void
public static function delay(int $microseconds, ?object $out = null): void
{
$webInput = WebInput::getInstance();
if (!$webInput->isWebMode()) {
// 终端模式:直接延迟
usleep($microseconds);
} elseif ($out) {
// Web模式
// 检查是否是SSEOutput
$className = substr(strrchr(get_class($out), '\\'), 1);
if ($className === 'SSEOutput') {
// SSE 模式:实际延迟以控制流式输出的速度
$milliseconds = max(1, (int)($microseconds / 1000));
$out->delay($milliseconds);
} elseif (method_exists($out, 'isTimingEnabled') && $out->isTimingEnabled()) {
// WebOutput 计时模式:记录虚拟时间戳
$milliseconds = max(1, (int)($microseconds / 1000));
$out->delay($milliseconds);
}
}
}

View File

@ -121,7 +121,7 @@ class SpellCalculator
// 计算方式
$calcType = $spellInfo['calc_type'] ?? 'matk';
$healRatio = $spellInfo['heal'] ?? 0.5;
$healBase = $spellInfo['heal_base'] ?? 20;
$healBase = $spellInfo['base'] ?? 0;
// 基础治疗量 (根据 calc_type)
switch ($calcType) {

View File

@ -4,19 +4,59 @@ namespace Game\Core;
/**
* Web 输出缓冲类
* 模拟 Symfony Console Output 接口,将输出缓存到内存
* 支持时间戳功能用于战斗日志的流式播放
*/
class WebOutput
{
private array $buffer = [];
private array $timedBuffer = [];
private bool $enableTiming = false;
private float $startTime = 0;
private float $currentTime = 0;
public function write(string $message): void
{
$this->buffer[] = $message;
if ($this->enableTiming) {
$this->timedBuffer[] = [
'text' => $message,
'timestamp' => round(($this->currentTime - $this->startTime) * 1000),
'type' => 'write'
];
}
}
public function writeln(string $message): void
{
$this->buffer[] = $message . "\n";
if ($this->enableTiming) {
$this->timedBuffer[] = [
'text' => $message,
'timestamp' => round(($this->currentTime - $this->startTime) * 1000),
'type' => 'writeln'
];
}
}
/**
* 添加延迟(用于模拟战斗动画速度)
* $ms: 延迟毫秒数
*/
public function delay(int $ms): void
{
if ($this->enableTiming) {
$this->currentTime += $ms / 1000.0;
}
}
/**
* 启用时间戳模式(战斗开始时调用)
*/
public function enableTiming(): void
{
$this->enableTiming = true;
$this->startTime = microtime(true);
$this->currentTime = $this->startTime;
}
/**
@ -27,12 +67,29 @@ class WebOutput
return implode('', $this->buffer);
}
/**
* 获取带时间戳的输出JSON格式
* 用于web端流式播放战斗日志
*/
public function getTimedOutput(): array
{
return [
'type' => 'battle_log',
'logs' => $this->timedBuffer,
'totalDuration' => round(($this->currentTime - $this->startTime) * 1000)
];
}
/**
* 清空缓冲区
*/
public function clear(): void
{
$this->buffer = [];
$this->timedBuffer = [];
$this->enableTiming = false;
$this->startTime = 0;
$this->currentTime = 0;
}
/**
@ -42,4 +99,12 @@ class WebOutput
{
return count($this->buffer);
}
/**
* 检查是否启用了时间戳模式
*/
public function isTimingEnabled(): bool
{
return $this->enableTiming;
}
}

View File

@ -3,6 +3,7 @@ namespace Game\Entities;
class Partner extends Actor
{
public $id;
// Partner特有的天赋加成与 Actor 不同)
public static array $talentBonus = [
'hp' => 20,

View File

@ -24,6 +24,9 @@ class Player extends Actor
public int $maxPartners = 2; // 最多可携带同伴数
public array $partners = []; // 已招募的同伴
// Player特有的小绿瓶回复池
public int $potionPool = 1000; // 小绿瓶初始回复量
/**
* 增加灵石
*/
@ -44,6 +47,32 @@ class Player extends Actor
return false;
}
/**
* 添加小绿瓶回复量
*/
public function addPotionPool(int $amount): void
{
$this->potionPool += $amount;
}
/**
* 从小绿瓶扣除回复量(返回实际扣除的量)
*/
public function consumePotionPool(int $amount): int
{
$consumed = min($amount, $this->potionPool);
$this->potionPool = max(0,$this->potionPool - $amount);
return $consumed;
}
/**
* 检查小绿瓶是否有足够的回复量
*/
public function hasPotionPool(int $amount): bool
{
return $this->potionPool >= $amount;
}
/**
* Player特有的经验获取升级时会恢复生命值

View File

@ -8,6 +8,7 @@ use Game\Core\ItemDisplay;
use Game\Core\SpellDisplay;
use Game\Core\SpellCalculator;
use Game\Core\Colors;
use Game\Core\WebInput;
use Game\Entities\Player;
use Game\Entities\Actor;
use Game\Entities\Monster;
@ -120,6 +121,11 @@ class Battle
{
$out = $this->game->output;
// Web端启用计时模式用于战斗日志流式播放
if (WebInput::getInstance()->isWebMode()) {
$out->enableTiming();
}
// 初始化同伴HP
$this->initPartnerHp();
@ -131,7 +137,7 @@ class Battle
}
while ($this->player->hp > 0) {
Screen::delay(500000);
Screen::delay(500000, $out);
// 创建敌人群组
$this->enemies = Monster::createGroup($this->game->dungeonId);
@ -157,17 +163,17 @@ class Battle
$this->round++;
$this->renderBattleScreen($out, $playerFirst);
Screen::delay(800000); // 回合开始停顿
Screen::delay(800000, $out); // 回合开始停顿
if ($playerFirst) {
// 玩家攻击
$result = $this->playerAttack($out);
Screen::delay(800000);
Screen::delay(800000, $out);
if ($result) break;
// 同伴攻击
$result = $this->partnersAttack($out);
Screen::delay(800000);
Screen::delay(800000, $out);
if ($result) break;
if ($this->checkExit($out)) {
@ -177,19 +183,19 @@ class Battle
// 怪物攻击
if ($this->enemiesAttack($out)) {
Screen::delay(1000000);
Screen::delay(1000000, $out);
$this->syncPartnerHp();
return;
}
Screen::delay(800000);
Screen::delay(800000, $out);
} else {
// 怪物先攻
if ($this->enemiesAttack($out)) {
Screen::delay(1000000);
Screen::delay(1000000, $out);
$this->syncPartnerHp();
return;
}
Screen::delay(800000);
Screen::delay(800000, $out);
if ($this->checkExit($out)) {
$this->syncPartnerHp();
@ -198,16 +204,16 @@ class Battle
// 玩家攻击
$result = $this->playerAttack($out);
Screen::delay(800000);
Screen::delay(800000, $out);
if ($result) break;
// 同伴攻击
$result = $this->partnersAttack($out);
Screen::delay(800000);
Screen::delay(800000, $out);
if ($result) break;
}
Screen::delay(500000);
Screen::delay(500000, $out);
// 同步队友HP到Partner对象然后保存状态
$this->syncPartnerHp();
$this->game->saveState();
@ -230,7 +236,7 @@ class Battle
$out->writeln("");
$out->writeln("{$this->yellow}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{$this->reset}");
Screen::delay(1000000); // 1秒
Screen::delay(1000000, $out); // 1秒
}
private function renderBattleScreen($out, bool $playerFirst)
@ -296,8 +302,8 @@ class Battle
private function renderHpBar(float $percent, int $width): string
{
$filled = (int)($percent * $width);
$empty = max($width - $filled,1);
$filled = max((int)($percent * $width),0);
$empty = max($width - $filled,0);
// 根据血量百分比选择颜色
if ($percent > 0.6) {
@ -308,7 +314,7 @@ class Battle
$color = $this->red;
}
$bar = $color . str_repeat("", $filled) . $this->white . str_repeat("", $empty?:1) . $this->reset;
$bar = $color . str_repeat("", $filled) . $this->white . str_repeat("", $empty) . $this->reset;
return "[" . $bar . "]";
}
@ -575,15 +581,15 @@ class Battle
}
// 应用防护机制:防护角色承受更多伤害
$actualDamage = (int)($damage * (1 + $target->getProtectDamageTakenBonus()));
// $actualDamage = (int)($damage * (1 + $target->getProtectDamageTakenBonus()));
$target->hp -= $actualDamage;
$target->hp -= $damage;
// 如果防护角色正在保护队友,需要显示保护效果
if ($target->isProtecting && $actualDamage > $damage) {
$extraDamage = $actualDamage - $damage;
$out->writeln("{$this->cyan}{$this->reset} {$this->yellow}🛡️ 防护状态:额外承受 {$extraDamage} 伤害!{$this->reset}");
}
// // 如果防护角色正在保护队友,需要显示保护效果
// if ($target->isProtecting && $actualDamage > $damage) {
// $extraDamage = $actualDamage - $damage;
// $out->writeln("{$this->cyan}║{$this->reset} {$this->yellow}🛡️ 防护状态:额外承受 {$extraDamage} 伤害!{$this->reset}");
// }
if ($target->hp <= 0) {
$target->hp = 0;
@ -591,14 +597,14 @@ class Battle
// 如果是玩家击败了所有敌人
if (($caster instanceof Player || $caster instanceof Partner) && empty($this->getAliveEnemies())) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showVictory($out, $stats);
return true;
}
// 如果是敌人击败了玩家
if ($target instanceof Player) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showDefeat($out, $caster);
return true;
}
@ -640,7 +646,7 @@ class Battle
$out->writeln("{$this->cyan}{$this->reset} {$this->red}💀 {$enemy->name} 被击败了!{$this->reset}");
if ($enemy instanceof Player) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showDefeat($out, $caster);
return true;
}
@ -659,7 +665,7 @@ class Battle
}
if ($allOpponentsDefeated && ($caster instanceof Player || $caster instanceof Partner) && empty($this->getAliveEnemies())) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showVictory($out, $stats);
return true;
}
@ -677,7 +683,8 @@ class Battle
$allies = $this->getAllies($caster);
$lowestHpRatio = 1.0;
foreach ($allies as $ally) {
$ratio = $ally->hp / $ally->maxHp;
$status = $ally->getStats();
$ratio = $status['hp'] / $status['maxHp'];
if ($ratio < $lowestHpRatio) {
$lowestHpRatio = $ratio;
$target = $ally;
@ -735,7 +742,7 @@ class Battle
if ($ally->hp <= 0) continue;
// 如果是施法者本人全额治疗如果是队友80%效果
$finalHeal = ($ally === $caster) ? $healAmount : (int)($healAmount * 0.8);
$finalHeal = ($ally === $caster) ? $healAmount : (int)($healAmount * 0.9);
$actualHeal = $ally->heal($finalHeal);
@ -830,8 +837,37 @@ class Battle
}
}
// 自动进入防护状态
// 如果需要进入防护状态,检查是否有人已经在防护
if ($hasLowHpAlly) {
$protectingAlly = null;
foreach ($allies as $ally) {
if ($ally !== $actor && $ally->isProtecting) {
$protectingAlly = $ally;
break;
}
}
// 如果有人在防护,比较血量比例,让血量更健康的人进入防护状态
if ($protectingAlly !== null) {
$protectingStats = $protectingAlly->getStats();
$protectingHealthRatio = $protectingStats['hp'] / $protectingStats['maxHp'];
// 当前角色血量更健康,则替换防护者
if ($healthRatio > $protectingHealthRatio) {
$protectingAlly->exitProtectMode();
$protectingName = ($protectingAlly instanceof Player) ? "" : $protectingAlly->name;
$out->writeln("{$this->cyan}{$this->reset} {$this->yellow}🛡️{$this->reset} {$protectingName} 退出防护状态。{$this->reset}");
$actor->enterProtectMode();
$actorName = ($actor instanceof Player) ? "" : $actor->name;
$out->writeln("{$this->cyan}{$this->reset} {$this->yellow}🛡️{$this->reset} {$actorName} 主动进入防护状态,为队友抗伤!{$this->reset}");
}
return;
}
// 没有人在防护,直接进入
$actor->enterProtectMode();
$actorName = ($actor instanceof Player) ? "" : $actor->name;
@ -924,13 +960,13 @@ class Battle
// 应用防护机制:防护角色承受更多伤害
$actualDamage = (int)($damage * (1 + $target->getProtectDamageTakenBonus()));
$target->hp -= $actualDamage;
$target->hp -= $damage;
// 如果防护角色正在保护队友,需要显示保护效果
if ($target->isProtecting && $actualDamage > $damage) {
$extraDamage = $actualDamage - $damage;
$out->writeln("{$this->cyan}{$this->reset} {$this->yellow}🛡️ 防护状态:额外承受 {$extraDamage} 伤害!{$this->reset}");
}
// if ($target->isProtecting && $actualDamage > $damage) {
// $extraDamage = $actualDamage - $damage;
// $out->writeln("{$this->cyan}║{$this->reset} {$this->yellow}🛡️ 防护状态:额外承受 {$extraDamage} 伤害!{$this->reset}");
// }
// 蓝量恢复机制
// 攻击者恢复 15 点
@ -948,13 +984,13 @@ class Battle
$out->writeln("{$this->cyan}{$this->reset} {$this->red}💀 {$targetName} 倒下了!{$this->reset}");
if (($actor instanceof Player || $actor instanceof Partner) && empty($this->getAliveEnemies())) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showVictory($out, $stats);
return true;
}
if ($target instanceof Player) {
Screen::delay(500000);
Screen::delay(500000, $out);
$this->showDefeat($out, $actor);
return true;
}
@ -969,7 +1005,7 @@ class Battle
if ($this->executeActorTurn($this->player, $out)) {
return true; // 战斗结束
}
Screen::delay(300000);
Screen::delay(300000, $out);
return false;
}
@ -984,7 +1020,7 @@ class Battle
if ($this->executeActorTurn($partner, $out)) {
return true; // 战斗结束
}
Screen::delay(300000);
Screen::delay(300000, $out);
}
return false;
@ -1001,7 +1037,7 @@ class Battle
if ($this->executeActorTurn($enemy, $out)) {
return true; // 战斗结束
}
Screen::delay(300000);
Screen::delay(300000, $out);
}
return false;
@ -1046,8 +1082,22 @@ class Battle
// 掉落 - 从掉落表中随机掉落物品
foreach ($enemy->dropTable as $drop) {
if (rand(1, 100) <= $drop['rate']) {
$this->player->addItem($drop['item']);
$allDrops[] = $drop['item'];
$item = $drop['item'];
// 如果是回复品消耗品且有heal属性直接添加到小绿瓶池
if ($item['type'] === 'consume' && isset($item['heal']) && $item['heal'] > 0) {
$this->player->addPotionPool($item['heal']);
// 显示为小绿瓶增加
$allDrops[] = [
'name' => '小绿瓶',
'type' => 'potion_pool',
'heal' => $item['heal'],
'quantity' => 1
];
} else {
// 其他物品正常添加到背包
$this->player->addItem($item);
$allDrops[] = $item;
}
}
}
}
@ -1102,7 +1152,7 @@ class Battle
$out->writeln("");
$this->game->saveState();
Screen::delay(1500000);
Screen::delay(1500000, $out);
}
private function showDefeat($out, ?Actor $killer = null)
@ -1147,7 +1197,7 @@ class Battle
$out->writeln("");
$out->writeln("{$this->yellow}🏃 你逃离了战斗...{$this->reset}");
$out->writeln("");
Screen::delay(800000);
Screen::delay(800000, $out);
$this->game->state = Game::MENU;
return true;
}

View File

@ -69,14 +69,21 @@ class InventoryPanel
$out->writeln("╔════════════════════════════╗");
$out->writeln("║ 背 包 [{$categoryName}] ║");
$out->writeln("║ 第 {$page} / {$totalPages} 页 ║");
$out->writeln("╠════════════════════════════╣");
$out->writeln("║ 🟢 小绿瓶: {$player->potionPool}");
$out->writeln("╚════════════════════════════╝");
if (empty($items)) {
$out->writeln("该分类下没有物品...");
$out->writeln("");
$out->writeln("[h] 使用小绿瓶回血");
$out->writeln("[c] 切换分类 | [0] 返回");
$choice = Screen::input($out, "选择操作:");
if ($choice === 'h'){
$this->autoHeal();
return $this->show($page, $category);
}
if ($choice === "c" || $choice === "C") {
return $this->showCategoryMenu($page, $category);
}
@ -238,14 +245,32 @@ class InventoryPanel
// 使用消耗品进行恢复
if ($target === 'player') {
$actualHeal = $player->heal($item['heal']);
$out->writeln("你使用了 {$item['name']},恢复了 {$actualHeal} HP(当前: {$player->hp}/{$maxHp})");
// 从小绿瓶池获取实际恢复量
$healAmount = $item['heal'];
$actualPotionHeal = $player->consumePotionPool($healAmount);
if ($actualPotionHeal == 0){
$out->writeln("小绿瓶已用尽");
Screen::sleep(1);
return;
}
$actualHeal = $player->heal($actualPotionHeal);
$out->writeln("你使用了 {$item['name']},从小绿瓶中恢复了 {$actualHeal} HP(当前: {$player->hp}/{$maxHp})");
$out->writeln("小绿瓶剩余: {$player->potionPool}");
} else {
// 恢复队友
$partnerStats = $target->getStats();
$partnerMaxHp = $partnerStats['maxHp'];
$actualHeal = $target->heal($item['heal']);
$out->writeln("你使用了 {$item['name']} 来恢复 {$target->name},恢复了 {$actualHeal} HP(当前: {$target->hp}/{$partnerMaxHp})");
// 从小绿瓶池获取实际恢复量
$healAmount = $item['heal'];
$actualPotionHeal = $player->consumePotionPool($healAmount);
if ($actualPotionHeal == 0){
$out->writeln("小绿瓶已用尽");
Screen::sleep(1);
return;
}
$actualHeal = $target->heal($actualPotionHeal);
$out->writeln("你使用了 {$item['name']} 来恢复 {$target->name},从小绿瓶中恢复了 {$actualHeal} HP(当前: {$target->hp}/{$partnerMaxHp})");
$out->writeln("小绿瓶剩余: {$player->potionPool}");
}
// Decrease quantity or remove
@ -377,86 +402,29 @@ class InventoryPanel
return;
}
// 获取所有消耗品并按治疗量排序(优先使用小瓶)
$consumables = [];
foreach ($player->inventory as $index => $item) {
if (($item['type'] ?? '') === 'consume' && ($item['heal'] ?? 0) > 0) {
$consumables[] = ['index' => $index, 'item' => $item];
}
}
if (empty($consumables)) {
$out->writeln("没有可用的回复药品!");
Screen::sleep(1);
return;
}
// 按治疗量从小到大排序(优先使用小瓶,避免浪费)
usort($consumables, fn($a, $b) => ($a['item']['heal'] ?? 0) <=> ($b['item']['heal'] ?? 0));
$totalHealed = 0;
$itemsUsed = 0;
$healLog = [];
// 回复玩家
while ($playerNeedsHeal && $player->hp < $playerMaxHp && !empty($consumables)) {
while ($playerNeedsHeal && $player->hp < $playerMaxHp) {
// 计算需要恢复的量
$needHeal = $playerMaxHp - $player->hp;
// 找到最合适的药品(不浪费或浪费最少的)
$bestIndex = null;
$bestWaste = PHP_INT_MAX;
foreach ($consumables as $i => $c) {
$healAmount = $c['item']['heal'] ?? 0;
$waste = max(0, $healAmount - $needHeal);
if ($waste < $bestWaste) {
$bestWaste = $waste;
$bestIndex = $i;
}
// 如果找到完美匹配或不浪费的,直接使用
if ($waste === 0) break;
// 使用药品,从小绿瓶池里扣除
$actualPotionHeal = $player->consumePotionPool($needHeal);
if ($actualPotionHeal == 0){
$out->writeln("小绿瓶已用尽");
Screen::sleep(1);
return;
}
if ($bestIndex === null) break;
$selected = $consumables[$bestIndex];
$inventoryIndex = $selected['index'];
$item = $selected['item'];
// 使用药品
$actualHeal = $player->heal($item['heal']);
$actualHeal = $player->heal($actualPotionHeal);
$totalHealed += $actualHeal;
$itemsUsed++;
$healLog[] = "玩家: +{$actualHeal} HP";
// 减少数量或移除物品
if (($player->inventory[$inventoryIndex]['quantity'] ?? 1) > 1) {
$player->inventory[$inventoryIndex]['quantity']--;
$consumables[$bestIndex]['item']['quantity']--;
} else {
unset($player->inventory[$inventoryIndex]);
unset($consumables[$bestIndex]);
$consumables = array_values($consumables);
}
// 重新整理背包索引
$player->inventory = array_values($player->inventory);
// 更新 consumables 的索引引用
$consumables = [];
foreach ($player->inventory as $index => $invItem) {
if (($invItem['type'] ?? '') === 'consume' && ($invItem['heal'] ?? 0) > 0) {
$consumables[] = ['index' => $index, 'item' => $invItem];
}
}
usort($consumables, fn($a, $b) => ($a['item']['heal'] ?? 0) <=> ($b['item']['heal'] ?? 0));
}
// 回复队友(按需要程度优先恢复)
if (!empty($partnersNeedHeal) && !empty($consumables)) {
if (!empty($partnersNeedHeal)) {
// 按需要治疗量从大到小排序(优先治疗伤势最重的)
usort($partnersNeedHeal, fn($a, $b) => $b['needHeal'] <=> $a['needHeal']);
@ -464,59 +432,20 @@ class InventoryPanel
$partner = $healData['partner'];
$partnerMaxHp = $healData['maxHp'];
while ($partner->hp < $partnerMaxHp && !empty($consumables)) {
while ($partner->hp < $partnerMaxHp) {
// 计算需要恢复的量
$needHeal = $partnerMaxHp - $partner->hp;
// 找到最合适的药品
$bestIndex = null;
$bestWaste = PHP_INT_MAX;
foreach ($consumables as $i => $c) {
$healAmount = $c['item']['heal'] ?? 0;
$waste = max(0, $healAmount - $needHeal);
if ($waste < $bestWaste) {
$bestWaste = $waste;
$bestIndex = $i;
}
if ($waste === 0) break;
// 使用药品回复队友,从小绿瓶池里扣除
$actualPotionHeal = $player->consumePotionPool($needHeal);
if ($actualPotionHeal == 0){
$out->writeln("小绿瓶已用尽");
Screen::sleep(1);
return;
}
if ($bestIndex === null) break;
$selected = $consumables[$bestIndex];
$inventoryIndex = $selected['index'];
$item = $selected['item'];
// 使用药品回复队友
$actualHeal = $partner->heal($item['heal']);
$actualHeal = $partner->heal($actualPotionHeal);
$totalHealed += $actualHeal;
$itemsUsed++;
$healLog[] = "{$partner->name}: +{$actualHeal} HP";
// 减少数量或移除物品
if (($player->inventory[$inventoryIndex]['quantity'] ?? 1) > 1) {
$player->inventory[$inventoryIndex]['quantity']--;
$consumables[$bestIndex]['item']['quantity']--;
} else {
unset($player->inventory[$inventoryIndex]);
unset($consumables[$bestIndex]);
$consumables = array_values($consumables);
}
// 重新整理背包索引
$player->inventory = array_values($player->inventory);
// 更新 consumables 的索引引用
$consumables = [];
foreach ($player->inventory as $index => $invItem) {
if (($invItem['type'] ?? '') === 'consume' && ($invItem['heal'] ?? 0) > 0) {
$consumables[] = ['index' => $index, 'item' => $invItem];
}
}
usort($consumables, fn($a, $b) => ($a['item']['heal'] ?? 0) <=> ($b['item']['heal'] ?? 0));
}
}
}
@ -528,7 +457,7 @@ class InventoryPanel
$out->writeln("╔════════════════════════════════════╗");
$out->writeln("║ 一键回血结果 ║");
$out->writeln("╚════════════════════════════════════╝");
$out->writeln("使用了 {$itemsUsed} 个药品,共恢复 {$totalHealed} HP");
$out->writeln("共恢复 {$totalHealed} HP");
$out->writeln("");
$out->writeln("治疗详情:");
foreach ($healLog as $log) {
@ -739,7 +668,7 @@ class InventoryPanel
$reset = Colors::RESET;
$out->writeln("╔════════════════════════════════════════════════════════╗");
$out->writeln("{$cyan}角色属性{$reset} Lv.{$player->level} | {$green}💰 {$player->spiritStones}{$reset} 灵石 ");
$out->writeln("{$cyan}角色属性{$reset} Lv.{$player->level} | {$green}💰 {$player->spiritStones}{$reset} 灵石 | 🟢 {$player->potionPool}");
$out->writeln("╠════════════════════════════════════════════════════════╣");
// First row: HP (current/max), patk, matk

39
test-websocket.php Normal file
View File

@ -0,0 +1,39 @@
<?php
/**
* WebSocket 服务器测试脚本
*/
require_once __DIR__ . '/vendor/autoload.php';
use WebSocket\Client;
echo "连接到 WebSocket 服务器: ws://localhost:9002\n";
try {
$client = new Client("ws://localhost:9002");
echo "[成功] 连接到服务器\n";
// 接收欢迎消息
$message = $client->receive();
echo "[收到] " . $message . "\n";
// 发送测试输入
echo "[发送] 测试命令\n";
$client->send(json_encode([
'type' => 'input',
'input' => '1'
]));
// 接收响应
for ($i = 0; $i < 5; $i++) {
$message = $client->receive();
echo "[收到] " . substr($message, 0, 100) . "...\n";
}
$client->close();
echo "[完成] 测试成功\n";
} catch (Exception $e) {
echo "[错误] " . $e->getMessage() . "\n";
exit(1);
}

View File

@ -86,4 +86,4 @@ $bootsTemplate = [
$res = Item::createSpell(1, 'common', 10);
// dd($monster->getRandomEquipmentDrops(100));
dd(Item::randomItem('armor', 30));
\Game\Core\SpellCalculator::calculateHeal();

677
web/game-ws.html Normal file
View File

@ -0,0 +1,677 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>凡人修仙传 - WebSocket版</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #1a1a2e;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: 'Consolas', 'Monaco', monospace;
}
.container {
width: 100%;
max-width: 900px;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.header h1 {
color: #eee;
font-size: 24px;
margin-bottom: 10px;
}
.header p {
color: #888;
font-size: 14px;
}
/* 登录界面 */
#auth-panel {
background: #16213e;
border-radius: 10px;
padding: 30px;
max-width: 400px;
margin: 0 auto;
}
#auth-panel h2 {
color: #eee;
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
color: #aaa;
margin-bottom: 5px;
font-size: 14px;
}
.form-group input {
width: 100%;
padding: 10px 15px;
border: 1px solid #0f3460;
border-radius: 5px;
background: #1a1a2e;
color: #eee;
font-size: 16px;
}
.form-group input:focus {
outline: none;
border-color: #e94560;
}
.btn-group {
display: flex;
gap: 10px;
margin-top: 20px;
}
.btn {
flex: 1;
padding: 12px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: #e94560;
color: white;
}
.btn-primary:hover {
background: #ff6b6b;
}
.btn-secondary {
background: #0f3460;
color: white;
}
.btn-secondary:hover {
background: #16213e;
}
.message {
padding: 10px;
border-radius: 5px;
margin-bottom: 15px;
text-align: center;
display: none;
}
.message.error {
background: rgba(233, 69, 96, 0.2);
color: #e94560;
display: block;
}
.message.success {
background: rgba(46, 213, 115, 0.2);
color: #2ed573;
display: block;
}
/* 游戏终端 */
#game-panel {
display: none;
}
.terminal-header {
background: #16213e;
padding: 10px 15px;
border-radius: 10px 10px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.terminal-header .user-info {
color: #2ed573;
font-size: 14px;
}
.terminal-header .status {
font-size: 12px;
color: #888;
}
.terminal-header .logout-btn {
background: #e94560;
color: white;
border: none;
padding: 5px 15px;
border-radius: 5px;
cursor: pointer;
font-size: 12px;
}
.terminal-header .logout-btn:hover {
background: #ff6b6b;
}
#terminal {
background: #0d0d0d;
border-radius: 0 0 10px 10px;
padding: 10px;
}
.input-area {
background: #16213e;
padding: 10px;
border-radius: 0 0 10px 10px;
display: flex;
gap: 10px;
}
.input-area input {
flex: 1;
padding: 10px;
border: 1px solid #0f3460;
border-radius: 5px;
background: #1a1a2e;
color: #eee;
font-size: 16px;
}
.input-area input:focus {
outline: none;
border-color: #e94560;
}
.input-area button {
padding: 10px 20px;
background: #e94560;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.quick-buttons {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 10px;
}
.quick-btn {
padding: 8px 15px;
background: #0f3460;
color: #eee;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.quick-btn:hover {
background: #16213e;
}
.ws-status {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
margin-left: 5px;
}
.ws-connected {
background: #2ed573;
}
.ws-disconnected {
background: #e94560;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>凡人修仙传 - WebSocket版</h1>
<p>Web Terminal Edition (Real-time)</p>
</div>
<!-- 登录/注册面板 -->
<div id="auth-panel">
<h2>欢迎</h2>
<div id="auth-message" class="message"></div>
<div class="form-group">
<label>用户名</label>
<input type="text" id="username" placeholder="请输入用户名">
</div>
<div class="form-group">
<label>密码</label>
<input type="password" id="password" placeholder="请输入密码">
</div>
<div class="btn-group">
<button class="btn btn-primary" onclick="login()">登录</button>
<button class="btn btn-secondary" onclick="register()">注册</button>
</div>
</div>
<!-- 游戏面板 -->
<div id="game-panel">
<div class="terminal-header">
<span class="user-info">玩家: <span id="player-name">-</span></span>
<span class="status">
状态: <span id="game-status">连接中</span>
<span class="ws-status ws-disconnected" id="ws-indicator"></span>
</span>
<button class="logout-btn" onclick="logout()">退出登录</button>
</div>
<div id="terminal"></div>
<div class="input-area">
<input type="text" id="game-input" placeholder="输入命令..." onkeypress="handleKeyPress(event)">
<button onclick="sendInput()">发送</button>
</div>
<!-- <div class="quick-buttons">-->
<!-- <button class="quick-btn" onclick="quickSend('1')">1.战斗</button>-->
<!-- <button class="quick-btn" onclick="quickSend('2')">2.属性</button>-->
<!-- <button class="quick-btn" onclick="quickSend('3')">3.背包</button>-->
<!-- <button class="quick-btn" onclick="quickSend('4')">4.故人</button>-->
<!-- <button class="quick-btn" onclick="quickSend('5')">5.同伴</button>-->
<!-- <button class="quick-btn" onclick="quickSend('6')">6.天赋</button>-->
<!-- <button class="quick-btn" onclick="quickSend('7')">7.地图</button>-->
<!-- <button class="quick-btn" onclick="quickSend('8')">8.休息</button>-->
<!-- <button class="quick-btn" onclick="quickSend('0')">0.返回</button>-->
<!-- </div>-->
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/xterm@5.3.0/lib/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.8.0/lib/xterm-addon-fit.min.js"></script>
<script>
let terminal = null;
let fitAddon = null;
let ws = null;
let isLoggedIn = false;
let currentUserId = null;
let currentUsername = null;
// 初始化终端
function initTerminal() {
if (terminal) return; // 已初始化
terminal = new Terminal({
cursorBlink: true,
fontSize: 16,
fontFamily: 'Consolas, Monaco, monospace',
theme: {
background: '#0d0d0d',
foreground: '#eee',
cursor: '#e94560',
cursorAccent: '#0d0d0d',
selection: 'rgba(233, 69, 96, 0.3)',
black: '#1a1a2e',
red: '#e94560',
green: '#2ed573',
yellow: '#ffa502',
blue: '#70a1ff',
magenta: '#ff6b81',
cyan: '#1e90ff',
white: '#eee',
brightBlack: '#666',
brightRed: '#ff6b6b',
brightGreen: '#7bed9f',
brightYellow: '#ffda79',
brightBlue: '#a4b0be',
brightMagenta: '#ff7f9f',
brightCyan: '#34ace0',
brightWhite: '#fff'
},
rows: 24,
cols: 80
});
fitAddon = new FitAddon.FitAddon();
terminal.loadAddon(fitAddon);
terminal.open(document.getElementById('terminal'));
fitAddon.fit();
window.addEventListener('resize', () => {
fitAddon.fit();
});
}
// WebSocket 连接
function connectWebSocket() {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + location.hostname + ':9001';
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('[WebSocket] 已连接');
updateWSStatus(true);
terminal.writeln('\x1b[32m[✓] WebSocket 连接成功\x1b[0m');
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
handleMessage(data);
} catch (e) {
console.error('消息解析错误:', e);
}
};
ws.onerror = (error) => {
console.error('[WebSocket] 错误:', error);
updateWSStatus(false);
};
ws.onclose = () => {
console.log('[WebSocket] 已断开');
updateWSStatus(false);
if (isLoggedIn) {
terminal.writeln('\x1b[31m[✗] WebSocket 连接已断开,尝试重新连接...\x1b[0m');
setTimeout(connectWebSocket, 3000);
}
};
}
// 处理服务器消息
function handleMessage(data) {
console.log('[收到] ', data.type, data);
switch (data.type) {
case 'welcome':
terminal.writeln('\x1b[33m' + data.message + '\x1b[0m');
break;
case 'login-success':
isLoggedIn = true;
currentUserId = data.userId;
currentUsername = data.username;
document.getElementById('player-name').textContent = data.username;
document.getElementById('game-status').textContent = 'MENU';
terminal.clear();
displayOutput(data.output);
document.getElementById('game-input').focus();
break;
case 'game-output':
console.log(data)
terminal.clear();
displayOutput(data.output);
updateGameStatus(data.stateName);
document.getElementById('game-input').focus();
break;
case 'battle-start':
terminal.clear();
terminal.writeln('\x1b[33m' + data.message + '\x1b[0m');
document.getElementById('game-status').textContent = 'BATTLE';
break;
case 'battle-end':
terminal.writeln('\x1b[32m[战斗结束]\x1b[0m');
updateGameStatus(data.stateName);
document.getElementById('game-input').focus();
break;
case 'state-sync':
displayOutput(data.output);
updateGameStatus(data.stateName);
break;
case 'error':
terminal.writeln('\x1b[31m[错误] ' + data.message + '\x1b[0m');
break;
case 'pong':
// 心跳响应
break;
default:
console.warn('未知消息类型:', data.type);
}
}
// 发送消息给服务器
function sendMessage(data) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
terminal.writeln('\x1b[31m[错误] WebSocket 未连接\x1b[0m');
return false;
}
ws.send(JSON.stringify(data));
return true;
}
// 显示消息
function showMessage(message, type = 'error') {
const el = document.getElementById('auth-message');
el.textContent = message;
el.className = 'message ' + type;
}
// 登录
async function login() {
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value;
if (!username || !password) {
showMessage('请输入用户名和密码');
return;
}
// 这里仍然使用HTTP登录进行认证
try {
const response = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
credentials: 'include'
});
const result = await response.json();
if (result.success) {
showMessage('登录成功!连接游戏服务器...', 'success');
setTimeout(() => {
// 获取userId用于WebSocket登录
fetch('/api/status', { credentials: 'include' })
.then(r => r.json())
.then(data => {
if (data.success && data.loggedIn) {
enterGame(username, data.userId);
}
});
}, 500);
} else {
showMessage(result.message || '登录失败');
}
} catch (e) {
showMessage('网络错误: ' + e.message);
}
}
// 注册
async function register() {
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value;
if (!username || !password) {
showMessage('请输入用户名和密码');
return;
}
try {
const response = await fetch('/api/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
credentials: 'include'
});
const result = await response.json();
if (result.success) {
showMessage('注册成功!自动登录中...', 'success');
setTimeout(() => {
fetch('/api/status', { credentials: 'include' })
.then(r => r.json())
.then(data => {
if (data.success && data.loggedIn) {
enterGame(username, data.userId);
}
});
}, 500);
} else {
showMessage(result.message || '注册失败');
}
} catch (e) {
showMessage('网络错误: ' + e.message);
}
}
// 进入游戏
function enterGame(username, userId) {
isLoggedIn = true;
document.getElementById('auth-panel').style.display = 'none';
document.getElementById('game-panel').style.display = 'block';
document.getElementById('player-name').textContent = username;
if (!terminal) {
initTerminal();
}
terminal.clear();
terminal.writeln('\x1b[33m正在连接游戏服务器...\x1b[0m');
// 连接WebSocket
connectWebSocket();
// WebSocket连接后登录
setTimeout(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
sendMessage({
type: 'login',
userId: userId,
username: username
});
}
}, 500);
}
// 退出登录
async function logout() {
isLoggedIn = false;
if (ws) {
ws.close();
}
await fetch('/api/logout', { credentials: 'include' });
document.getElementById('auth-panel').style.display = 'block';
document.getElementById('game-panel').style.display = 'none';
document.getElementById('password').value = '';
showMessage('', '');
}
// 发送输入
function sendInput() {
const inputEl = document.getElementById('game-input');
const input = inputEl.value.trim();
inputEl.value = '';
if (!input) return;
terminal.writeln('\x1b[36m> ' + input + '\x1b[0m');
terminal.writeln('');
if (sendMessage({
type: 'game-input',
input: input
})) {
console.log('[发送] 游戏输入:', input);
}
}
// 快捷按钮发送
function quickSend(value) {
document.getElementById('game-input').value = value;
sendInput();
}
// 按键处理
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendInput();
}
}
// 显示输出
function displayOutput(output) {
if (!output) return;
const lines = output.split('\n');
lines.forEach(line => {
if (line.trim()) {
terminal.writeln(line);
}
});
}
// 更新游戏状态显示
function updateGameStatus(stateName) {
document.getElementById('game-status').textContent = stateName || '未知';
}
// 更新WebSocket状态指示器
function updateWSStatus(connected) {
const indicator = document.getElementById('ws-indicator');
if (connected) {
indicator.className = 'ws-status ws-connected';
} else {
indicator.className = 'ws-status ws-disconnected';
}
}
// 心跳保活
setInterval(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
sendMessage({ type: 'ping' });
}
}, 30000);
// 页面加载完成
window.onload = async () => {
const result = await fetch('/api/status', { credentials: 'include' })
.then(r => r.json())
.catch(() => ({ success: false }));
if (result.success && result.loggedIn) {
enterGame(result.username, result.userId);
}
};
</script>
</body>
</html>

View File

@ -461,14 +461,41 @@
terminal.writeln('\x1b[36m> ' + input + '\x1b[0m');
terminal.writeln('');
// 检查是否进入战斗(输入为 "1"
if (input === '1') {
// 使用 SSE 流式战斗
streamBattle(input);
return;
}
// 普通请求处理
const result = await api('game/input', { input });
if (result.success) {
terminal.clear();
const lines = result.output.split('\n');
lines.forEach(line => {
terminal.writeln(line);
});
console.log('API响应:', result);
// 显示调试信息(可选)
if (result.stateName) {
console.log('当前状态:', result.stateName);
}
// 检查是否是时间戳战斗日志
if (result.type === 'battle_log' && result.logs) {
terminal.clear();
playBattleLog(result.logs);
} else if (result.output) {
// 普通文本输出
terminal.clear();
const lines = result.output.split('\n');
lines.forEach(line => {
terminal.writeln(line);
});
// 输入后立即刷新当前界面(确保状态同步)
// setTimeout(() => {
// renderGame();
// }, 100);
}
} else {
terminal.writeln('\x1b[31m' + (result.message || '操作失败') + '\x1b[0m');
if (result.message === '请先登录') {
@ -479,6 +506,78 @@
inputEl.focus();
}
// SSE 流式战斗
async function streamBattle(input) {
const eventSource = new EventSource('/api/game/battle-stream?input=' + encodeURIComponent(input), {
withCredentials: true
});
const inputEl = document.getElementById('game-input');
let battleEnded = false;
eventSource.addEventListener('start', (event) => {
// 清屏,准备显示战斗内容
terminal.clear();
});
eventSource.addEventListener('message', (event) => {
const log = JSON.parse(event.data);
// 根据类型显示日志
if (log.type === 'writeln') {
terminal.writeln(log.text);
} else if (log.type === 'write') {
terminal.write(log.text);
}
});
eventSource.addEventListener('complete', (event) => {
const data = JSON.parse(event.data);
console.log('战斗完成:', data);
battleEnded = true;
eventSource.close();
setTimeout(() => {
inputEl.focus();
}, 500);
});
eventSource.addEventListener('error', (event) => {
console.error('SSE 错误:', event);
battleEnded = true;
eventSource.close();
if (eventSource.readyState === EventSource.CLOSED) {
terminal.writeln('\x1b[31m连接已关闭\x1b[0m');
inputEl.focus();
}
});
}
// 流式播放战斗日志
async function playBattleLog(logs) {
let lastTimestamp = 0;
for (let i = 0; i < logs.length; i++) {
const log = logs[i];
const delay = log.timestamp - lastTimestamp;
// 等待适当的延迟
if (delay > 0) {
await new Promise(resolve => setTimeout(resolve, delay));
}
// 写入日志行
if (log.type === 'writeln') {
terminal.writeln(log.text);
} else if (log.type === 'write') {
terminal.write(log.text);
}
lastTimestamp = log.timestamp;
}
}
// 快捷按钮发送
function quickSend(value) {
document.getElementById('game-input').value = value;

362
web/process.html Normal file
View File

@ -0,0 +1,362 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>凡人修仙传 - 进程转发版</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #1a1a2e;
min-height: 100vh;
display: flex;
flex-direction: column;
font-family: 'Consolas', 'Monaco', monospace;
}
.container {
flex: 1;
display: flex;
flex-direction: column;
max-width: 100%;
}
.header {
background: #16213e;
padding: 15px 20px;
border-bottom: 1px solid #0f3460;
display: flex;
justify-content: space-between;
align-items: center;
}
.header h1 {
color: #eee;
font-size: 18px;
margin: 0;
}
.header-right {
display: flex;
gap: 15px;
align-items: center;
}
.status {
font-size: 12px;
color: #888;
}
.status-indicator {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
margin-left: 5px;
background: #e94560;
}
.status-indicator.connected {
background: #2ed573;
}
.btn-disconnect {
padding: 6px 12px;
background: #e94560;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
}
.btn-disconnect:hover {
background: #ff6b6b;
}
#terminal {
flex: 1;
overflow: hidden;
padding: 10px;
}
.input-area {
display: none;
}
.loading {
text-align: center;
padding: 20px;
color: #888;
font-size: 14px;
}
.error {
color: #e94560;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>🎮 凡人修仙传 - 进程转发版</h1>
<div class="header-right">
<span class="status">
连接状态: <span id="conn-status">连接中</span>
<span class="status-indicator" id="conn-indicator"></span>
</span>
<button class="btn-disconnect" onclick="disconnect()">断开连接</button>
</div>
</div>
<div class="container">
<div id="terminal">
</div>
<div class="input-area">
<input type="text" id="game-input" placeholder="输入命令..." disabled>
<button id="send-btn" onclick="sendInput()" disabled>发送</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/xterm@5.3.0/lib/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.8.0/lib/xterm-addon-fit.min.js"></script>
<script>
let terminal = null;
let fitAddon = null;
let ws = null;
let connected = false;
let inputBuffer = ''; // 缓存用户输入
// 初始化终端
function initTerminal() {
if (terminal) return;
terminal = new Terminal({
cursorBlink: true,
fontSize: 14,
fontFamily: '"Source Code Pro", "Courier New", monospace',
letterSpacing: 0,
lineHeight: 1.3,
theme: {
background: '#1a1a2e',
foreground: '#eee',
cursor: '#e94560',
cursorAccent: '#1a1a2e',
selection: 'rgba(233, 69, 96, 0.3)',
black: '#1a1a2e',
red: '#e94560',
green: '#2ed573',
yellow: '#ffa502',
blue: '#70a1ff',
magenta: '#ff6b81',
cyan: '#1e90ff',
white: '#eee',
brightBlack: '#666',
brightRed: '#ff6b6b',
brightGreen: '#7bed9f',
brightYellow: '#ffda79',
brightBlue: '#a4b0be',
brightMagenta: '#ff7f9f',
brightCyan: '#34ace0',
brightWhite: '#fff'
},
rows: 30,
cols: 100,
allowProposedApi: true
});
fitAddon = new FitAddon.FitAddon();
terminal.loadAddon(fitAddon);
terminal.open(document.getElementById('terminal'));
fitAddon.fit();
// 监听终端输入事件
terminal.onData((data) => {
// 处理输入
if (data === '\r' || data === '\n') {
// 用户按了回车
if (inputBuffer) {
sendInput();
}
} else if (data === '\u007F') {
// 处理退格键
if (inputBuffer.length > 0) {
inputBuffer = inputBuffer.slice(0, -1);
terminal.write('\b \b');
}
} else if (data === '\u0003') {
// Ctrl+C - 中断
inputBuffer = '';
terminal.write('^C\r\n');
} else {
// 普通字符
inputBuffer += data;
terminal.write(data);
}
});
window.addEventListener('resize', () => {
fitAddon.fit();
});
}
// 发送输入到服务器
function sendInput() {
if (!inputBuffer) return;
const input = inputBuffer;
inputBuffer = '';
sendMessage({
type: 'input',
input: input
});
}
// 连接WebSocket
function connectWebSocket() {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + location.hostname + ':9002';
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('[WebSocket] 已连接');
connected = true;
updateStatus(true);
initTerminal();
if (terminal) {
terminal.clear();
terminal.writeln('\x1b[32m[✓] WebSocket 连接成功,游戏进程已启动\x1b[0m');
terminal.writeln('');
terminal.focus(); // 设置终端焦点以接收输入
}
// 启动心跳
startHeartbeat();
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
handleMessage(data);
} catch (e) {
console.error('消息解析错误:', e);
}
};
ws.onerror = (error) => {
console.error('[WebSocket] 错误:', error);
if (terminal) {
terminal.writeln('\x1b[31m[✗] 连接错误\x1b[0m');
}
};
ws.onclose = () => {
console.log('[WebSocket] 已断开');
connected = false;
updateStatus(false);
if (terminal) {
terminal.writeln('\x1b[31m[✗] 连接已断开\x1b[0m');
}
document.getElementById('game-input').disabled = true;
document.getElementById('send-btn').disabled = true;
};
}
// 处理服务器消息
function handleMessage(data) {
if (!terminal) {
initTerminal();
}
switch (data.type) {
case 'output':
// terminal.clear();
const lines = data.text.split('\n');
lines.forEach(line => {
terminal.writeln(line);
});
break;
case 'system':
terminal.writeln('\x1b[33m' + data.message + '\x1b[0m');
break;
case 'error':
terminal.writeln('\x1b[31m[错误] ' + data.message + '\x1b[0m');
break;
case 'pong':
// 心跳响应
break;
default:
console.warn('未知消息类型:', data.type);
}
}
// 发送消息给服务器
function sendMessage(data) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
if (terminal) {
terminal.writeln('\x1b[31m[错误] 未连接到服务器\x1b[0m');
}
return false;
}
ws.send(JSON.stringify(data));
return true;
}
// 断开连接
function disconnect() {
if (ws) {
ws.close();
}
location.reload();
}
// 更新连接状态显示
function updateStatus(isConnected) {
const statusEl = document.getElementById('conn-status');
const indicatorEl = document.getElementById('conn-indicator');
if (isConnected) {
statusEl.textContent = '已连接';
indicatorEl.className = 'status-indicator connected';
} else {
statusEl.textContent = '已断开';
indicatorEl.className = 'status-indicator';
}
}
// 心跳保活
function startHeartbeat() {
setInterval(() => {
if (connected && ws && ws.readyState === WebSocket.OPEN) {
sendMessage({ type: 'ping' });
}
}, 30000);
}
// 页面加载完成
window.onload = () => {
connectWebSocket();
};
// 页面关闭时断开连接
window.onbeforeunload = () => {
if (ws) {
ws.close();
}
};
</script>
</body>
</html>

View File

@ -38,9 +38,9 @@ $requestUri = $_SERVER['REQUEST_URI'];
$path = parse_url($requestUri, PHP_URL_PATH);
// 静态文件处理
if ($path === '/' || $path === '/index.html') {
if ($path === '/' || $path === '/process.html') {
header('Content-Type: text/html; charset=utf-8');
readfile(__DIR__ . '/index.html');
readfile(__DIR__ . '/process.html');
exit;
}
@ -73,6 +73,12 @@ try {
$response = handleGameInput();
break;
case '/api/game/battle-stream':
// SSE 实时战斗流
handleBattleStream();
exit;
break;
default:
// 检查是否是静态文件
$filePath = __DIR__ . $path;
@ -170,10 +176,14 @@ function handleGameRender(): array
$session = new GameSession($_SESSION['user_id']);
$output = $session->render();
$stateInfo = $session->getStateInfo();
return [
'success' => true,
'output' => $output,
'state' => $stateInfo['state'],
'stateName' => $stateInfo['stateName'],
'playerInfo' => $stateInfo['playerInfo'],
];
}
@ -187,10 +197,46 @@ function handleGameInput(): array
$input = $data['input'] ?? '';
$session = new GameSession($_SESSION['user_id']);
$output = $session->handleInput($input);
$result = $session->handleInput($input);
return [
'success' => true,
'output' => $output,
];
// 现在handleInput返回的是数组output, state, stateName, playerInfo
return array_merge(['success' => true], $result);
}
/**
* 处理 SSE 实时战斗流
*/
function handleBattleStream(): void
{
if (empty($_SESSION['user_id'])) {
http_response_code(401);
echo "data: " . json_encode(['error' => '请先登录']) . "\n\n";
return;
}
// 从URL参数或POST数据获取输入
$input = $_GET['input'] ?? $_POST['input'] ?? '';
// 设置 SSE 响应头
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no'); // 禁用代理缓冲
// 发送初始化消息
echo "event: start\n";
echo "data: " . json_encode(['message' => '战斗开始']) . "\n\n";
ob_flush();
flush();
// 创建游戏会话并流式处理战斗
try {
$session = new GameSession($_SESSION['user_id']);
$session->streamBattle($input);
} catch (Exception $e) {
echo "event: error\n";
echo "data: " . json_encode(['message' => $e->getMessage()]) . "\n\n";
ob_flush();
flush();
}
}

84
websocket-process-server.php Executable file
View File

@ -0,0 +1,84 @@
#!/usr/bin/env php
<?php
/**
* 进程转发 WebSocket 服务器启动脚本
* 使用方法: php websocket-process-server.php
*
* 这个服务器为每个连接启动一个独立的 bin/game 进程
* 将进程的输出实时转发给WebSocket客户端
* 将客户端输入直接写入进程的STDIN
*/
// 自动加载
require_once __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Game\Core\GameProcessServer;
// 分步骤创建应用栈,以便能访问 GameProcessServer 实例
echo "[初始化] 创建 GameProcessServer 实例...\n";
$gameServer = new GameProcessServer(null);
echo "[初始化] 创建 WsServer...\n";
$wsServer = new WsServer($gameServer);
echo "[初始化] 创建 HttpServer...\n";
$httpServer = new HttpServer($wsServer);
// 创建IO服务器内部创建事件循环
echo "[初始化] 创建 IO 服务器...\n";
$server = IoServer::factory($httpServer, 9002, '0.0.0.0');
echo "[初始化] IO 服务器创建成功\n";
// 现在获取事件循环并设置给 GameProcessServer 实例
$loop = $server->loop;
echo "[初始化] 获取事件循环成功\n";
$gameServer->setLoop($loop);
echo "[初始化] 为 GameProcessServer 设置了事件循环\n";
echo <<<'ASCII'
╔══════════════════════════════════════════╗
凡人修仙传 - 进程转发 WebSocket 服务器
╚══════════════════════════════════════════╝
WebSocket 服务器启动
📍 地址: 0.0.0.0:9002
🔗 客户端连接: ws://localhost:9002
📋 运作原理:
1. 为每个WebSocket连接启动一个 php bin/game 进程
2. 实时读取进程的STDOUT/STDERR
3. 通过WebSocket发送给客户端
4. 客户端输入直接写入进程的STDIN
💡 特点:
无需修改游戏代码
每个用户独立的游戏进程
实时输出100ms轮询
完整的ANSI颜色支持
实时交互
Ctrl+C 停止服务器...
ASCII;
ob_implicit_flush();
fflush(STDOUT);
echo "\n[启动] 准备启动事件循环...\n";
fflush(STDOUT);
try {
echo "[启动] 调用 \$loop->run()...\n";
fflush(STDOUT);
$loop->run();
echo "[启动] \$loop->run() 已返回\n";
} catch (Exception $e) {
echo "\n[错误] 事件循环异常: " . $e->getMessage() . "\n";
echo $e->getTraceAsString() . "\n";
exit(1);
}

42
websocket-server.php Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env php
<?php
/**
* WebSocket 游戏服务器启动脚本
* 使用方法: php websocket-server.php
*/
// 自动加载
require_once __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Game\Core\GameWebSocketServer;
// 创建WebSocket服务器
$ws = new WsServer(new GameWebSocketServer());
// 用HTTP服务器包装
$http = new HttpServer($ws);
// 创建IO服务器监听9001端口
$server = IoServer::factory(
$http,
9001,
'0.0.0.0'
);
echo <<<'ASCII'
╔══════════════════════════════════════╗
凡人修仙传 - WebSocket 游戏服务器
╚══════════════════════════════════════╝
WebSocket 服务器启动
📍 地址: 0.0.0.0:9001
🔗 客户端连接: ws://localhost:9001
Ctrl+C 停止服务器...
ASCII;
$server->run();