Update: 2026-06-15 01:37:40
This commit is contained in:
@@ -191,6 +191,27 @@ class GamificationController extends GetxController {
|
||||
dailyGoal = (box.read('dailyGoal') ?? 0).toDouble();
|
||||
totalTrips = box.read('gamification_totalTrips') ?? 0;
|
||||
consecutiveDays = box.read('gamification_consecutiveDays') ?? 0;
|
||||
averageRating = (box.read('gamification_averageRating') ?? 5.0).toDouble();
|
||||
totalPoints = box.read('gamification_totalPoints') ?? 0;
|
||||
totalReferrals = box.read('gamification_totalReferrals') ?? 0;
|
||||
dailyEarnings = (box.read('gamification_dailyEarnings') ?? 0.0).toDouble();
|
||||
behaviorScore = (box.read('gamification_behaviorScore') ?? 100.0).toDouble();
|
||||
hardBrakes = box.read('gamification_hardBrakes') ?? 0;
|
||||
maxSpeed = (box.read('gamification_maxSpeed') ?? 0.0).toDouble();
|
||||
}
|
||||
|
||||
// ═══════ حفظ البيانات المحلية (التخزين المؤقت) ═══════
|
||||
void _saveLocalData() {
|
||||
box.write('gamification_totalTrips', totalTrips);
|
||||
box.write('gamification_consecutiveDays', consecutiveDays);
|
||||
box.write('gamification_averageRating', averageRating);
|
||||
box.write('gamification_totalPoints', totalPoints);
|
||||
box.write('gamification_totalReferrals', totalReferrals);
|
||||
box.write('gamification_dailyEarnings', dailyEarnings);
|
||||
box.write('gamification_behaviorScore', behaviorScore);
|
||||
box.write('gamification_hardBrakes', hardBrakes);
|
||||
box.write('gamification_maxSpeed', maxSpeed);
|
||||
box.write('gamification_last_fetch', DateTime.now().toIso8601String());
|
||||
}
|
||||
|
||||
// ═══════ حفظ الهدف اليومي ═══════
|
||||
@@ -330,85 +351,45 @@ class GamificationController extends GetxController {
|
||||
}
|
||||
|
||||
// ═══════ جلب البيانات من السيرفر ═══════
|
||||
Future<void> fetchGamificationData() async {
|
||||
Future<void> fetchGamificationData({bool force = false}) async {
|
||||
// التحقق من التخزين المؤقت (6 ساعات)
|
||||
String? lastFetchStr = box.read('gamification_last_fetch');
|
||||
if (!force && lastFetchStr != null) {
|
||||
try {
|
||||
DateTime lastFetch = DateTime.parse(lastFetchStr);
|
||||
if (DateTime.now().difference(lastFetch).inHours < 6) {
|
||||
debugPrint('ℹ️ [Gamification] Loading cached data (last fetch: $lastFetchStr)');
|
||||
_loadLocalData();
|
||||
_calculateLevel();
|
||||
_updateAchievementProgress();
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [Gamification] Failed to parse last fetch time: $e');
|
||||
}
|
||||
}
|
||||
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
try {
|
||||
// 1. جلب عدد الرحلات الكلي
|
||||
var tripRes = await CRUD().get(
|
||||
link: AppLink.getTripCountByCaptain,
|
||||
var res = await CRUD().get(
|
||||
link: AppLink.getGamificationDashboard,
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (tripRes != null && tripRes != 'failure') {
|
||||
var data = jsonDecode(tripRes);
|
||||
totalTrips =
|
||||
int.tryParse(data['message']?[0]?['count']?.toString() ?? '0') ?? 0;
|
||||
box.write('gamification_totalTrips', totalTrips);
|
||||
}
|
||||
|
||||
// 2. جلب التقييم
|
||||
var rateRes = await CRUD().get(
|
||||
link: AppLink.getDriverRate,
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (rateRes != null && rateRes != 'failure') {
|
||||
var data = jsonDecode(rateRes);
|
||||
averageRating =
|
||||
double.tryParse(data['message']?[0]?['rating']?.toString() ?? '5') ??
|
||||
5.0;
|
||||
}
|
||||
|
||||
// 3. جلب النقاط (الرصيد)
|
||||
var pointsRes = await CRUD().getWallet(
|
||||
link: AppLink.getDriverPaymentPoints,
|
||||
payload: {'driverID': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (pointsRes != null && pointsRes != 'failure') {
|
||||
var data = jsonDecode(pointsRes);
|
||||
totalPoints = double.tryParse(
|
||||
data['message']?[0]?['total_amount']?.toString() ?? '0')
|
||||
?.abs()
|
||||
.toInt() ??
|
||||
0;
|
||||
}
|
||||
|
||||
// 4. جلب عدد الدعوات
|
||||
var invRes = await CRUD().get(
|
||||
link: AppLink.getInviteDriver,
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (invRes != null && invRes != 'failure') {
|
||||
var data = jsonDecode(invRes);
|
||||
if (data['message'] is List) {
|
||||
totalReferrals = (data['message'] as List).length;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. جلب أرباح اليوم
|
||||
var todayRes = await CRUD().getWallet(
|
||||
link: AppLink.getDriverPaymentToday,
|
||||
payload: {'driverID': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (todayRes != null && todayRes != 'failure') {
|
||||
var data = jsonDecode(todayRes);
|
||||
dailyEarnings = double.tryParse(
|
||||
data['message']?[0]?['todayAmount']?.toString() ?? '0') ??
|
||||
0;
|
||||
}
|
||||
|
||||
// 6. جلب تقييم سلوك القيادة
|
||||
var behaviorRes = await CRUD().get(
|
||||
link: AppLink.getDriverBehavior,
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (behaviorRes != null && behaviorRes != 'failure') {
|
||||
var data = jsonDecode(behaviorRes);
|
||||
if (data['message'] is List && data['message'].isNotEmpty) {
|
||||
var behavior = data['message'][0];
|
||||
behaviorScore = double.tryParse(behavior['avg_score']?.toString() ?? '100') ?? 100.0;
|
||||
hardBrakes = int.tryParse(behavior['total_hard_brakes']?.toString() ?? '0') ?? 0;
|
||||
maxSpeed = double.tryParse(behavior['max_speed']?.toString() ?? '0') ?? 0.0;
|
||||
if (res != null && res != 'failure') {
|
||||
var data = jsonDecode(res);
|
||||
if (data['status'] == 'success' && data['message'] != null) {
|
||||
var details = data['message'];
|
||||
totalTrips = int.tryParse(details['totalTrips']?.toString() ?? '0') ?? 0;
|
||||
averageRating = double.tryParse(details['averageRating']?.toString() ?? '5.0') ?? 5.0;
|
||||
totalReferrals = int.tryParse(details['totalReferrals']?.toString() ?? '0') ?? 0;
|
||||
dailyEarnings = double.tryParse(details['todayEarnings']?.toString() ?? '0.0') ?? 0.0;
|
||||
behaviorScore = double.tryParse(details['behaviorScore']?.toString() ?? '100.0') ?? 100.0;
|
||||
hardBrakes = int.tryParse(details['hardBrakes']?.toString() ?? '0') ?? 0;
|
||||
maxSpeed = double.tryParse(details['maxSpeed']?.toString() ?? '0.0') ?? 0.0;
|
||||
totalPoints = int.tryParse(details['totalPoints']?.toString() ?? '0') ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,6 +401,7 @@ class GamificationController extends GetxController {
|
||||
|
||||
_calculateLevel();
|
||||
_updateAchievementProgress();
|
||||
_saveLocalData();
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user