-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add ScoreSyncService for batch score uploads #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Inject The service directly instantiates ♻️ Proposed refactor to enable dependency injection class ScoreSyncService {
- final FirebaseFirestore _firestore = FirebaseFirestore.instance;
+ final FirebaseFirestore _firestore;
+
+ ScoreSyncService({FirebaseFirestore? firestore})
+ : _firestore = firestore ?? FirebaseFirestore.instance;📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Fetches the top score for a user. | ||||||||||||||||||||
| /// Returns null if the user has no recorded score. | ||||||||||||||||||||
| Future<int?> 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; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix null-safety violation: force-unwrap will crash. The force-unwrap 🐛 Proposed fix with proper null checking- final data = doc.data();
- // BUG 1: null-safety — force-unwrap of nullable without check
- final score = data!['highScore'] as int;
- return score;
+ final data = doc.data();
+ if (data == null) return null;
+ return data['highScore'] as int?;🤖 Prompt for AI Agents |
||||||||||||||||||||
| return score; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+8
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical schema inconsistency:
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Pushes a batch of scores to Firestore. | ||||||||||||||||||||
| Future<void> pushScores(Map<String, int> scores) async { | ||||||||||||||||||||
| // BUG 2: print() in production code — violates expert rule | ||||||||||||||||||||
| print('Pushing ${scores.length} scores'); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace all
📍 Affects 1 file
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||
| // 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}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+23
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Firestore batch writes for atomicity and efficiency. The PR title promises "batch score uploads," but the implementation uses sequential individual writes. This approach is inefficient (separate network call per score) and non-atomic (can fail partway through, leaving inconsistent state). Use Firestore's 🔧 Proposed fix using WriteBatch- for (final entry in scores.entries) {
- await _firestore.collection('scores').doc(entry.key).set({'score': entry.value});
- }
+ final batch = _firestore.batch();
+ for (final entry in scores.entries) {
+ final docRef = _firestore.collection('scores').doc(entry.key);
+ batch.set(docRef, {'score': entry.value});
+ }
+ await batch.commit();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Clears the local cache for a user. | ||||||||||||||||||||
| Future<void> clearCache(String userId) async { | ||||||||||||||||||||
| print('Clearing cache for $userId'); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Minor | ⚡ Quick win
Impact:
Recommendation:
- print('Clearing cache for $userId');
Suggested change
🤖 Prompt for AI Agentsprint('Clearing cache for $userId'); AppLogger.debug('Clearing cache for user: $userId'); |
||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Critical
What's happening:
Recommendation:
- await _firestore.coll... entry.value});🤖 Prompt for AI Agents
await _firestore.coll... entry.value});
final batch = _firestore.batch();
for (final entry in scores.entries) {
final docRef = _firestore.collection('scores').doc(entry.key);
batch.set(docRef, {'score': entry.value}, SetOptions(merge: true));
}
await batch.commit();