Initial commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:onsolgo/core/constants.dart';
|
||||
|
||||
class AchievementManager {
|
||||
static Future<void> unlock(String uid, String achId) async {
|
||||
final ref = FirebaseFirestore.instance.collection('users').doc(uid).collection('achievements').doc(achId);
|
||||
final doc = await ref.get();
|
||||
if (!doc.exists) {
|
||||
await ref.set({'unlockedAt': FieldValue.serverTimestamp()});
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> incrementStat(String uid, String field, {int amount = 1}) async {
|
||||
final userRef = FirebaseFirestore.instance.collection('users').doc(uid);
|
||||
await userRef.update({field: FieldValue.increment(amount)});
|
||||
|
||||
final snap = await userRef.get();
|
||||
final d = snap.data() as Map<String, dynamic>;
|
||||
|
||||
// Threshold Checks
|
||||
if (field == 'pagesRead') {
|
||||
int p = safeInt(d['pagesRead']);
|
||||
if (p >= 10) unlock(uid, "page_turner_1");
|
||||
if (p >= 50) unlock(uid, "page_turner_2");
|
||||
if (p >= 200) unlock(uid, "page_turner_3");
|
||||
}
|
||||
if (field == 'chaptersRead') {
|
||||
int c = safeInt(d['chaptersRead']);
|
||||
if (c >= 1) unlock(uid, "chapter_complete_1");
|
||||
if (c >= 10) unlock(uid, "chapter_complete_2");
|
||||
if (c >= 50) unlock(uid, "chapter_complete_3");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user