Commit Graph

9 Commits

Author SHA1 Message Date
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
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
hantao
0c6cbd669a first 2025-12-01 18:13:15 +08:00