diff --git a/scripts/firehose-logger.ts b/scripts/firehose-logger.ts index 4a772aa..c9d0bbc 100644 --- a/scripts/firehose-logger.ts +++ b/scripts/firehose-logger.ts @@ -4,11 +4,11 @@ import { fileURLToPath } from 'url'; import { mkdir } from 'fs/promises'; const JETSTREAM_URL = 'wss://jetstream2.us-east.bsky.network/subscribe'; -const COLLECTIONS = ['blue.checkmate.game', 'blue.checkmate.challenge']; +const COLLECTION = 'blue.checkmate.game'; const __dirname = dirname(fileURLToPath(import.meta.url)); const logDir = join(__dirname, '..', 'data'); -const logFile = join(logDir, 'firehose.jsonl'); +const logFile = join(logDir, 'players.log'); await mkdir(logDir, { recursive: true }); const stream = createWriteStream(logFile, { flags: 'a' }); @@ -18,9 +18,7 @@ let reconnectAttempts = 0; function connect() { const url = new URL(JETSTREAM_URL); - for (const c of COLLECTIONS) { - url.searchParams.append('wantedCollections', c); - } + url.searchParams.set('wantedCollections', COLLECTION); if (cursor) url.searchParams.set('cursor', String(cursor)); console.log(`[firehose] connecting...${cursor ? ` (cursor: ${cursor})` : ''}`); @@ -35,21 +33,9 @@ function connect() { const data = JSON.parse(String(event.data)); if (data.time_us) cursor = data.time_us; if (data.kind !== 'commit') return; + if (data.commit.collection !== COLLECTION) return; - const line = JSON.stringify({ - time: new Date().toISOString(), - time_us: data.time_us, - did: data.did, - operation: data.commit.operation, - collection: data.commit.collection, - rkey: data.commit.rkey, - record: data.commit.record ?? null, - }); - - stream.write(line + '\n'); - console.log( - `[firehose] ${data.commit.operation} ${data.commit.collection} ${data.did.slice(0, 24)}...` - ); + stream.write(data.did + '\n'); }; ws.onclose = (event: CloseEvent) => { @@ -65,6 +51,5 @@ function connect() { }; } -console.log(`[firehose] logging to ${logFile}`); -console.log(`[firehose] watching: ${COLLECTIONS.join(', ')}`); +console.log(`[firehose] logging DIDs to ${logFile}`); connect(); diff --git a/src/lib/atproto.ts b/src/lib/atproto.ts index 6045ed7..2b552ee 100644 --- a/src/lib/atproto.ts +++ b/src/lib/atproto.ts @@ -68,8 +68,6 @@ export async function updateGame( $type: 'blue.checkmate.game', }; - console.log('[updateGame]', { did, rkey, collection: COLLECTIONS.game }); - await agent.com.atproto.repo.putRecord({ repo: did, collection: COLLECTIONS.game, @@ -239,23 +237,27 @@ export async function findGameRecordByParent( did: string, parentGameUri: string ): Promise<{ rkey: string; record: GameRecord } | null> { - const readAgent = did === agent.assertDid - ? agent - : await getPublicAgent(did); + try { + const readAgent = did === agent.assertDid + ? agent + : await getPublicAgent(did); - const response = await readAgent.com.atproto.repo.listRecords({ - repo: did, - collection: COLLECTIONS.game, - limit: 100, - }); + const response = await readAgent.com.atproto.repo.listRecords({ + repo: did, + collection: COLLECTIONS.game, + limit: 100, + }); - for (const rec of response.data.records) { - const value = rec.value as unknown as GameRecord; - if (value.parentGameUri === parentGameUri) { - return { rkey: rec.uri.split('/').pop()!, record: value }; + for (const rec of response.data.records) { + const value = rec.value as unknown as GameRecord; + if (value.parentGameUri === parentGameUri) { + return { rkey: rec.uri.split('/').pop()!, record: value }; + } } + return null; + } catch { + return null; } - return null; } export { COLLECTIONS }; diff --git a/src/lib/jetstream.ts b/src/lib/jetstream.ts index 3b08e7a..472238d 100644 --- a/src/lib/jetstream.ts +++ b/src/lib/jetstream.ts @@ -57,11 +57,9 @@ export class JetstreamConnection { url.searchParams.set('cursor', String(this.cursor)); } - console.log('[jetstream] connecting to', url.toString()); this.ws = new WebSocket(url.toString()); this.ws.onopen = () => { - console.log('[jetstream] connected'); this.reconnectAttempts = 0; this.stopPolling(); this.options.onConnectionChange?.(true); @@ -74,7 +72,6 @@ export class JetstreamConnection { } if (data.kind !== 'commit') return; const evt = data as JetstreamEvent; - console.log('[jetstream]', evt.commit.operation, evt.commit.collection, evt.did); if ( (evt.commit.operation === 'update' || evt.commit.operation === 'create') && evt.commit.collection === COLLECTIONS.game && @@ -87,8 +84,7 @@ export class JetstreamConnection { } }; - this.ws.onclose = (event) => { - console.log('[jetstream] closed:', event.code, event.reason); + this.ws.onclose = () => { this.options.onConnectionChange?.(false); if (!this.destroyed) { this.startPolling(); @@ -96,8 +92,7 @@ export class JetstreamConnection { } }; - this.ws.onerror = (event) => { - console.error('[jetstream] error:', event); + this.ws.onerror = () => { this.ws?.close(); }; } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index eaa5326..d8609ca 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -194,7 +194,7 @@ {:else}
- {liveConnected ? 'No active games right now. Be the first!' : 'Connecting to live feed...'} + {liveConnected ? 'No active games right now. ' : 'Connecting to live feed...'}{#if liveConnected}Start one!{/if}
{/if} diff --git a/src/routes/game/[did]/[rkey]/+page.svelte b/src/routes/game/[did]/[rkey]/+page.svelte index 3d97cb8..3930e79 100644 --- a/src/routes/game/[did]/[rkey]/+page.svelte +++ b/src/routes/game/[did]/[rkey]/+page.svelte @@ -679,7 +679,17 @@ Go home {:else} -Sign in with your Bluesky account to join this game
+Waiting for opponent
@@ -698,7 +708,7 @@ {copied ? 'Copied!' : 'Copy'}Sign in to join this game
-