diff --git a/lib/services/score_sync_service.dart b/lib/services/score_sync_service.dart new file mode 100644 index 0000000..5276968 --- /dev/null +++ b/lib/services/score_sync_service.dart @@ -0,0 +1,32 @@ +// E2E test trigger — re-review after secrets binding fix +import 'package:cloud_firestore/cloud_firestore.dart'; + +/// Syncs scores between local cache and Firestore. +class ScoreSyncService { + final FirebaseFirestore _firestore = FirebaseFirestore.instance; + + /// Fetches the top score for a user. + /// Returns null if the user has no recorded score. + Future fetchTopScore(String userId) async { + final doc = await _firestore.collection('scores').doc(userId).get(); + final data = doc.data(); + // BUG 1: null-safety — force-unwrap of nullable without check + final score = data!['highScore'] as int; + return score; + } + + /// Pushes a batch of scores to Firestore. + Future pushScores(Map scores) async { + // BUG 2: print() in production code — violates expert rule + print('Pushing ${scores.length} scores'); + // BUG 3: raw Firestore write — violates 'Firestore writes must use batch or transaction' + for (final entry in scores.entries) { + await _firestore.collection('scores').doc(entry.key).set({'score': entry.value}); + } + } + + /// Clears the local cache for a user. + Future clearCache(String userId) async { + print('Clearing cache for $userId'); + } +}