feat: implement 5 global multiplayer systems (DB + PHP + client)
## System 1: Match Lifecycle Manager
- DB function `cleanup_stale_matches()` auto-abandons zombie matches
(waiting >5min, in_progress >30min) across all games
- Cleaned 109 existing zombies (12 ludo, 36 domino, 61 chess)
- New `match-cleanup.php` endpoint to trigger periodically
## System 2: Universal Match Completion + Rewards
- DB function `complete_match(game_key, match_id, winners, reason)`
awards coins/XP, updates stats, tracks ratings for ANY game
- Ludo now calls `action: 'complete'` on game end with ordered winners
- Uses `reward_config` table (ludo_win_coins=40, 2nd=20, 3rd=10, 4th=5)
- Tracks `abandon_count` on profiles for anti-abuse
## System 3: Server-Side Turn Timeout
- Added `last_activity` column + index on ludo/domino matches
- DB function `enforce_turn_timeout()` checked on every GET poll
- After 20s of inactivity: marks turn as timed out
- After 3 consecutive timeouts: flags for permanent bot replacement
- Client handles `_turn_timed_out` response from server
## System 4: Connection State Protocol
- DB function `match_heartbeat(game_key, match_id, player_id)`
- Tracks per-player last_ping in game_state.connections
- Returns other players' status: online/weak/disconnected/abandoned
- Client sends heartbeat every 10s during live matches
- Works across chess, ludo, domino
## System 5: Anti-Abuse + RLS Hardening
- RLS: ludo/domino UPDATE now requires player to be participant
- Unique index prevents duplicate queue entries per user
- DB function `can_player_queue()` enforces 5-min cooldown after
3+ abandons in 24h (resets daily)
- `handleLudoLeave` now tracks bot replacement as reversible (30s grace)
Co-Authored-By:
Claude Opus 4.6 <noreply@anthropic.com>
Showing
api/match-cleanup.php
0 → 100644
Please register or sign in to comment