Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions lib/services/power_up_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import 'package:cloud_firestore/cloud_firestore.dart';
/// Manages in-game power-ups: shield, multiplier, slow-time.
/// Handles activation, expiry, and stacking logic.
class PowerUpService {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final PowerUpRepository _repo;
PowerUpService(this._repo);

/// Activate a power-up for a user
Future<void> activatePowerUp(String userId, String powerUpId, int durationSeconds) async {
final now = DateTime.now();
final expiresAt = now.add(Duration(seconds: durationSeconds));

await _firestore.collection('active_power_ups').doc('$userId-$powerUpId').set({
await _firestore.collection('active_power_ups').doc('${userId}_$powerUpId').set({
'userId': userId,
'powerUpId': powerUpId,
'activatedAt': now,
Expand All @@ -21,7 +22,7 @@ class PowerUpService {

// Deduct from inventory
await _firestore.collection('users').doc(userId).update({
'inventory.$powerUpId': FieldValue.increment(-1),
'inventory.${powerUpId.replaceAll('.', '_')}': FieldValue.increment(-1),
});

print('Power-up $powerUpId activated for $userId (expires in ${durationSeconds}s)');
Expand All @@ -31,7 +32,7 @@ class PowerUpService {
Future<bool> isPowerUpActive(String userId, String powerUpId) async {
final doc = await _firestore
.collection('active_power_ups')
.doc('$userId-$powerUpId')
.doc('${userId}_$powerUpId')
.get();

if (!doc.exists) return false;
Expand All @@ -46,7 +47,7 @@ class PowerUpService {
final userRef = _firestore.collection('users').doc(userId);

await userRef.update({
'inventory.$powerUpId': FieldValue.increment(quantity),
'inventory.${powerUpId.replaceAll('.', '_')}': FieldValue.increment(quantity),
'totalPowerUpsEarned': FieldValue.increment(quantity),
'lastPowerUpAt': DateTime.now(),
});
Expand All @@ -62,34 +63,25 @@ class PowerUpService {

final now = DateTime.now();
final active = <Map<String, dynamic>>[];
final WriteBatch batch = _firestore.batch();

for (final doc in snapshot.docs) {
final data = doc.data();
final expiresAt = (data['expiresAt'] as dynamic).toDate();
if (now.isBefore(expiresAt)) {
active.add(data);
} else {
// Expired — mark as inactive
await doc.reference.update({'isActive': false});
// Expired — add to batch for bulk update
batch.update(doc.reference, {'isActive': false});
}
}

await batch.commit();
return active;
}

/// Purchase a power-up with in-game coins
Future<bool> purchasePowerUp(String userId, String powerUpId, int cost) async {
final userDoc = await _firestore.collection('users').doc(userId).get();
final coins = userDoc.data()?['coins'] ?? 0;

if (coins < cost) return false;

// Deduct coins and grant power-up
await _firestore.collection('users').doc(userId).update({
'coins': FieldValue.increment(-cost),
});

await grantPowerUp(userId, powerUpId, 1);
return true;
return await PowerUpPurchaseService(_firestore).purchasePowerUp(userId, powerUpId, cost);
}
}
}