60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / store / game_state.go
4.0 KB
1package store 2 3import ( 4 "database/sql" 5 "fmt" 6 "time" 7 8 "github.com/google/uuid" 9) 10 11// GameStateSnapshot represents a cached game state from a tool call. 12type GameStateSnapshot struct { 13 ID string 14 Username string 15 ToolName string 16 Content string 17 GameTick int64 18 CapturedAt time.Time 19} 20 21// StoreGameStateSnapshot stores or updates a game state snapshot for a username+tool combination. 22// Uses UPSERT to replace old snapshots with new ones. 23func (s *Store) StoreGameStateSnapshot(username, toolName, content string, gameTick int64) error { 24 id := uuid.New().String() 25 capturedAt := time.Now().UTC().Unix() 26 27 _, err := s.db.Exec(` 28 INSERT INTO game_state_snapshots (id, username, tool_name, content, game_tick, captured_at) 29 VALUES (?, ?, ?, ?, ?, ?) 30 ON CONFLICT(username, tool_name) DO UPDATE SET 31 id = excluded.id, 32 content = excluded.content, 33 game_tick = excluded.game_tick, 34 captured_at = excluded.captured_at 35 `, id, username, toolName, content, gameTick, capturedAt) 36 37 return err 38} 39 40// GetGameStateSnapshot retrieves a specific snapshot for a username+tool combination. 41func (s *Store) GetGameStateSnapshot(username, toolName string) (*GameStateSnapshot, error) { 42 var snapshot GameStateSnapshot 43 var gameTick sql.NullInt64 44 var capturedAtUnix int64 45 46 err := s.db.QueryRow(` 47 SELECT id, username, tool_name, content, game_tick, captured_at 48 FROM game_state_snapshots 49 WHERE username = ? AND tool_name = ? 50 `, username, toolName).Scan( 51 &snapshot.ID, 52 &snapshot.Username, 53 &snapshot.ToolName, 54 &snapshot.Content, 55 &gameTick, 56 &capturedAtUnix, 57 ) 58 59 if err != nil { 60 return nil, err 61 } 62 63 if gameTick.Valid { 64 snapshot.GameTick = gameTick.Int64 65 } 66 snapshot.CapturedAt = time.Unix(capturedAtUnix, 0) 67 68 return &snapshot, nil 69} 70 71// GetAllGameStateSnapshots retrieves all snapshots for a username, ordered by tool name. 72func (s *Store) GetAllGameStateSnapshots(username string) ([]*GameStateSnapshot, error) { 73 rows, err := s.db.Query(` 74 SELECT id, username, tool_name, content, game_tick, captured_at 75 FROM game_state_snapshots 76 WHERE username = ? 77 ORDER BY tool_name ASC 78 `, username) 79 if err != nil { 80 return nil, fmt.Errorf("query game state snapshots: %w", err) 81 } 82 defer rows.Close() 83 84 var snapshots []*GameStateSnapshot 85 for rows.Next() { 86 var snapshot GameStateSnapshot 87 var gameTick sql.NullInt64 88 var capturedAtUnix int64 89 90 if err := rows.Scan( 91 &snapshot.ID, 92 &snapshot.Username, 93 &snapshot.ToolName, 94 &snapshot.Content, 95 &gameTick, 96 &capturedAtUnix, 97 ); err != nil { 98 return nil, fmt.Errorf("scan game state snapshot: %w", err) 99 } 100 101 if gameTick.Valid { 102 snapshot.GameTick = gameTick.Int64 103 } 104 snapshot.CapturedAt = time.Unix(capturedAtUnix, 0) 105 106 snapshots = append(snapshots, &snapshot) 107 } 108 109 return snapshots, rows.Err() 110} 111 112// DeleteGameStateSnapshotsForUsername deletes all snapshots for a username (e.g., on logout). 113func (s *Store) DeleteGameStateSnapshotsForUsername(username string) error { 114 _, err := s.db.Exec(` 115 DELETE FROM game_state_snapshots 116 WHERE username = ? 117 `, username) 118 return err 119} 120 121// DeleteGameStateSnapshot deletes a specific snapshot for a username+tool combination. 122func (s *Store) DeleteGameStateSnapshot(username, toolName string) error { 123 _, err := s.db.Exec(` 124 DELETE FROM game_state_snapshots 125 WHERE username = ? AND tool_name = ? 126 `, username, toolName) 127 return err 128} 129 130// RecencyMessage returns a human-readable recency string for a snapshot. 131func (s *GameStateSnapshot) RecencyMessage(currentTick int64) string { 132 if currentTick > 0 && s.GameTick > 0 { 133 ticksAgo := currentTick - s.GameTick 134 if ticksAgo == 0 { 135 return "just now" 136 } 137 if ticksAgo < 0 { 138 // Future tick (shouldn't happen, but handle gracefully) 139 return "just now" 140 } 141 return fmt.Sprintf("T%d ago (%ds)", ticksAgo, ticksAgo*10) 142 } 143 144 // Fallback to wall clock time 145 elapsed := time.Since(s.CapturedAt) 146 if elapsed < time.Minute { 147 return fmt.Sprintf("%.0fs ago", elapsed.Seconds()) 148 } 149 if elapsed < time.Hour { 150 return fmt.Sprintf("%.0f m ago", elapsed.Minutes()) 151 } 152 return fmt.Sprintf("%.1f h ago", elapsed.Hours()) 153}