···1+# Database to ATProtocol Migration Index
2+3+This document tracks all changes made during the migration from database-backed storage to ATProtocol-only data fetching.
4+5+## Overview
6+7+**Goal**: Remove all database dependencies and fetch data entirely from ATProtocol/Constellation.
8+9+**Approach**:
10+- Use Constellation API for game discovery
11+- Calculate action counts dynamically by fetching moves/passes
12+- Always fetch fresh data from ATProtocol
13+14+---
15+16+## Changes Made
17+18+### 1. ATProtocol Client Enhancements
19+**File**: `src/lib/atproto-client.ts`
20+**Commit**: 91f7916
21+22+**Added Functions**:
23+- `fetchGameResigns(gameAtUri)` - Fetch resign records from Constellation backlinks
24+- `calculateGameMetadata(gameUri, gameRecord, creatorDid, rkey)` - Calculate action count, last action type, and updated timestamp dynamically
25+- `listPlayerGames(did)` - List all game records from a player's PDS
26+- `fetchAllGames(knownPlayerDids)` - Aggregate games from multiple players and compute metadata
27+28+**New Interface**:
29+- `GameWithMetadata` - Extends GameRecord with computed fields: `uri`, `rkey`, `creatorDid`, `actionCount`, `lastActionType`, `updatedAt`
30+31+**Purpose**: Provide functions to fetch and compute game metadata without database queries.
32+33+---
34+35+## Remaining Changes
36+37+### 2. Homepage (In Progress)
38+**File**: `src/routes/+page.server.ts`
39+- Remove `getDb()` import
40+- Replace database queries with ATProtocol fetching
41+- Use `fetchAllGames()` or player-specific queries
42+43+### 3. Game Page
44+**File**: `src/routes/game/[id]/+page.server.ts`
45+- Remove database dependency
46+- Fetch game record directly from ATProtocol
47+48+### 4. Profile Page
49+**File**: `src/routes/profile/[did]/+page.server.ts`
50+- Remove database queries
51+- Use `listPlayerGames()` for player-specific game listing
52+53+### 5. API Routes
54+**Files**: Multiple API endpoint files
55+- `/api/games/+server.ts` (create game)
56+- `/api/games/[id]/join/+server.ts` (join game)
57+- `/api/games/[id]/move/+server.ts` (make move)
58+- `/api/games/[id]/pass/+server.ts` (pass turn)
59+- `/api/games/[id]/cancel/+server.ts` (resign)
60+- `/api/games/[id]/score/+server.ts` (submit scores)
61+- `/api/games/[id]/reaction/+server.ts` (add reaction)
62+- Other routes: board, nudge, nudge-image, scoring-board, og-image, profile
63+64+### 6. Cleanup
65+- Remove `src/lib/server/db.ts`
66+- Remove database dependencies from package.json
67+- Remove DATABASE_PATH from environment variables
68+69+---
70+71+## Testing Checklist
72+73+After migration, test:
74+- [ ] Homepage loads and displays games correctly
75+- [ ] Game page works with all game states
76+- [ ] Profile page shows player's games
77+- [ ] Creating new games works
78+- [ ] Joining games works
79+- [ ] Making moves works
80+- [ ] Passing works
81+- [ ] Resigning works
82+- [ ] Scoring works
83+- [ ] Reactions work
84+85+---
86+87+## Rollback Instructions
88+89+If something breaks:
90+91+1. Revert to commit before migration: `git log --oneline` to find the commit
92+2. Check this index to see what was changed
93+3. Each commit is atomic and can be reverted individually
94+95+---
96+97+## Notes
98+99+- The database was only used as a discovery index, not as source of truth
100+- All game data was already in ATProtocol records
101+- This migration makes the app more decentralized and resilient