Update: 2026-07-02 15:35:12
This commit is contained in:
@@ -8,10 +8,30 @@ if (!$driver_id) {
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Get Country and Currency Info
|
||||
$stmtKazan = $con->prepare("SELECT country, currency FROM kazan LIMIT 1");
|
||||
$stmtKazan->execute();
|
||||
$kazan = $stmtKazan->fetch(PDO::FETCH_ASSOC) ?: ["country" => "Jordan", "currency" => "JOD"];
|
||||
global $redis;
|
||||
$redisKey = "gamification_dashboard:{$driver_id}";
|
||||
|
||||
// 1. Try to read from Redis first
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
$cached = $redis->get($redisKey);
|
||||
if ($cached) {
|
||||
$data = json_decode($cached, true);
|
||||
if ($data) {
|
||||
jsonSuccess($data);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis read error in getGamificationDashboard: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to Database & External API
|
||||
// Get Country and Currency Info from Environment Variables (to avoid querying kazan table)
|
||||
$country = getenv('APP_COUNTRY') ?: getenv('COUNTRY') ?: "Jordan";
|
||||
$currency = getenv('APP_CURRENCY') ?: getenv('CURRENCY') ?: "JOD";
|
||||
$kazan = ["country" => $country, "currency" => $currency];
|
||||
|
||||
// 2. Get Total Completed Trips
|
||||
$stmtTrips = $con->prepare("SELECT COUNT(*) as count FROM `ride` WHERE driver_id = :driver_id AND status = 'Finished'");
|
||||
@@ -94,10 +114,33 @@ try {
|
||||
}
|
||||
|
||||
// 8. Calculate Normalized Points
|
||||
// 10 pts per finished trip, 100 pts per referral invite, 2 pts per behavior score point + claimed challenge points
|
||||
$normalizedPoints = ($totalTrips * 10) + ($totalReferrals * 100) + ((int)$behavior['avg_score'] * 2) + $challengePoints;
|
||||
|
||||
jsonSuccess([
|
||||
// 9. Fetch Available Challenges
|
||||
$stmtCh = $con->prepare("SELECT * FROM gamification_challenges WHERE is_active = 1 AND country = :country ORDER BY target_count ASC");
|
||||
$stmtCh->execute([':country' => $kazan['country']]);
|
||||
$challengesRaw = $stmtCh->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$challenges = [];
|
||||
$tier = 'Bronze'; // Placeholder logic
|
||||
foreach ($challengesRaw as $chRaw) {
|
||||
$isCompleted = false;
|
||||
$isClaimed = false;
|
||||
|
||||
$challenges[] = [
|
||||
'id' => (int)$chRaw['id'],
|
||||
'title' => $chRaw['title'],
|
||||
'description' => $chRaw['description'],
|
||||
'type' => $chRaw['challenge_type'],
|
||||
'target' => (int)$chRaw['target_count'],
|
||||
'reward_amount' => (float)$chRaw['reward_amount'],
|
||||
'reward_type' => $chRaw['reward_type'],
|
||||
'is_completed' => $isCompleted,
|
||||
'is_claimed' => $isClaimed
|
||||
];
|
||||
}
|
||||
|
||||
$responseData = [
|
||||
"country" => $kazan["country"],
|
||||
"currency" => $kazan["currency"],
|
||||
"totalTrips" => $totalTrips,
|
||||
@@ -110,10 +153,23 @@ try {
|
||||
"maxSpeed" => (float)$behavior["max_speed"],
|
||||
"todayTrips" => $todayTrips,
|
||||
"todayEarnings" => $todayEarnings,
|
||||
"totalPoints" => $normalizedPoints
|
||||
]);
|
||||
"totalPoints" => $normalizedPoints,
|
||||
"tier" => $tier,
|
||||
"challenges" => $challenges
|
||||
];
|
||||
|
||||
} catch (PDOException $e) {
|
||||
// 10. Save the compiled data into Redis
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
$redis->setex($redisKey, 900, json_encode($responseData));
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis write error in getGamificationDashboard: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
jsonSuccess($responseData);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("getGamificationDashboard Error: " . $e->getMessage());
|
||||
jsonError("An internal error occurred. Please try again later.");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,26 @@ require_once __DIR__ . '/../../connect.php';
|
||||
$type = isset($_POST['type']) ? filterRequest("type") : 'trips';
|
||||
|
||||
try {
|
||||
global $redis;
|
||||
$redisKey = "gamification_leaderboard:{$type}";
|
||||
|
||||
// 1. Try to read from Redis first
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
$cached = $redis->get($redisKey);
|
||||
if ($cached) {
|
||||
$data = json_decode($cached, true);
|
||||
if ($data !== null) {
|
||||
jsonSuccess($data);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis read error in getLeaderboard: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to Database
|
||||
if ($type === 'earnings') {
|
||||
$sql = "
|
||||
SELECT
|
||||
@@ -53,8 +73,20 @@ if ($stmt->rowCount() > 0) {
|
||||
$row['rank'] = $rank++;
|
||||
}
|
||||
|
||||
jsonSuccess($rows);
|
||||
$responseData = $rows;
|
||||
} else {
|
||||
jsonSuccess([]);
|
||||
$responseData = [];
|
||||
}
|
||||
|
||||
// 3. Save the DB result into Redis for future reads
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
// Cache leaderboard for 1 hour (3600 seconds)
|
||||
$redis->setex($redisKey, 3600, json_encode($responseData));
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis write error in getLeaderboard: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
jsonSuccess($responseData);
|
||||
?>
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
|
||||
$driver_id = filterRequest("driver_id");
|
||||
if (empty($driver_id)) {
|
||||
jsonError("Missing driver_id");
|
||||
exit;
|
||||
}
|
||||
|
||||
global $redis;
|
||||
$redisKey = "driver_streak:{$driver_id}";
|
||||
|
||||
// 1. Try to read from Redis first (Cache-Aside pattern)
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
$cached = $redis->get($redisKey);
|
||||
if ($cached) {
|
||||
$data = json_decode($cached, true);
|
||||
if ($data) {
|
||||
// Return immediately if found in cache!
|
||||
jsonSuccess($data);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis read error in get_streak: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to Database if not found in Redis
|
||||
$stmt = $con->prepare("SELECT current_streak, zero_commission_until FROM driver_streaks WHERE driver_id = ?");
|
||||
$stmt->execute([$driver_id]);
|
||||
$streak = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($streak) {
|
||||
$now = new DateTime();
|
||||
$until = new DateTime($streak['zero_commission_until']);
|
||||
|
||||
$isActive = false;
|
||||
if ($streak['zero_commission_until'] !== '2000-01-01 00:00:00' && $now < $until) {
|
||||
$isActive = true;
|
||||
}
|
||||
|
||||
$responseData = [
|
||||
'streak_count' => intval($streak['current_streak']),
|
||||
'is_zero_commission_active' => $isActive,
|
||||
'zero_commission_until' => $streak['zero_commission_until']
|
||||
];
|
||||
} else {
|
||||
// Default if no record yet
|
||||
$responseData = [
|
||||
'streak_count' => 0,
|
||||
'is_zero_commission_active' => false,
|
||||
'zero_commission_until' => '2000-01-01 00:00:00'
|
||||
];
|
||||
}
|
||||
|
||||
// 3. Save the DB result into Redis for future reads
|
||||
if (isset($redis)) {
|
||||
try {
|
||||
// Cache it for 2 hours (7200 seconds)
|
||||
$redis->setex($redisKey, 7200, json_encode($responseData));
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis write error in get_streak: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
jsonSuccess($responseData);
|
||||
?>
|
||||
Reference in New Issue
Block a user