import type { TournamentKeys } from './keys.js'; import type { TournamentBetEntry, TournamentMatchEntry, TournamentParticipantEntry, TournamentState } from './types.js'; interface RedisClientLike { get(key: string): Promise; set(key: string, value: string): Promise; } const safeJsonParse = (raw: string | null): T | null => { if (!raw) { return null; } try { return JSON.parse(raw) as T; } catch { return null; } }; export class TournamentStore { constructor(private readonly redis: RedisClientLike, private readonly keys: TournamentKeys) {} async getState(): Promise { return safeJsonParse(await this.redis.get(this.keys.stateKey)); } async setState(state: TournamentState): Promise { await this.redis.set(this.keys.stateKey, JSON.stringify(state)); } async getParticipants(): Promise { return safeJsonParse(await this.redis.get(this.keys.participantsKey)) ?? []; } async setParticipants(participants: TournamentParticipantEntry[]): Promise { await this.redis.set(this.keys.participantsKey, JSON.stringify(participants)); } async getMatches(): Promise { return safeJsonParse(await this.redis.get(this.keys.matchesKey)) ?? []; } async setMatches(matches: TournamentMatchEntry[]): Promise { await this.redis.set(this.keys.matchesKey, JSON.stringify(matches)); } async getBettingEntries(): Promise { return safeJsonParse(await this.redis.get(this.keys.bettingKey)) ?? []; } async setBettingEntries(entries: TournamentBetEntry[]): Promise { await this.redis.set(this.keys.bettingKey, JSON.stringify(entries)); } async appendBettingEntry(entry: TournamentBetEntry): Promise { const entries = await this.getBettingEntries(); entries.push(entry); await this.setBettingEntries(entries); return entries; } }