Commit Graph

17 Commits

Author SHA1 Message Date
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
0658960b70 优化 2025-12-06 22:59:52 +08:00
6aedb96342 优化 2025-12-06 20:52:40 +08:00
hantao
ecee1606f4 技能优化 2025-12-05 18:15:06 +08:00
hantao
c77620127d 技能优化 2025-12-05 17:38:10 +08:00
c2a2e93ff3 优化 2025-12-04 23:52:50 +08:00
hantao
9d9af5f96c 逻辑优化 2025-12-04 18:11:28 +08:00
595295ebf2 优化 2025-12-03 23:48:23 +08:00
9a5eb6433f 优化 2025-12-03 23:41:55 +08:00
hantao
05bff41e35 逻辑优化 2025-12-03 18:15:54 +08:00
826f0e38a4 法术 2025-12-02 23:13:38 +08:00
hantao
bacc28acce 操作优化 2025-12-02 17:56:53 +08:00
hantao
b4ec385827 添加怪物群 2025-12-02 13:45:20 +08:00
hantao
0c6cbd669a first 2025-12-01 18:13:15 +08:00