feat: add gamification endpoints for driver behavior
This commit is contained in:
34
ride/gamification/claimChallengeReward.php
Normal file
34
ride/gamification/claimChallengeReward.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
|
$driver_id = filterRequest("driver_id");
|
||||||
|
$points = filterRequest("points"); // Reward points amount
|
||||||
|
$challenge_id = filterRequest("challenge_id");
|
||||||
|
|
||||||
|
// Check if already claimed today to prevent spam
|
||||||
|
$checkSql = "SELECT id FROM driverWallet WHERE driverID = :driver_id AND paymentMethod = :challenge_id AND DATE(dateCreated) = CURDATE()";
|
||||||
|
$stmtCheck = $con->prepare($checkSql);
|
||||||
|
$stmtCheck->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmtCheck->bindParam(':challenge_id', $challenge_id, PDO::PARAM_STR);
|
||||||
|
$stmtCheck->execute();
|
||||||
|
|
||||||
|
if ($stmtCheck->rowCount() > 0) {
|
||||||
|
jsonError("Reward already claimed today");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert into driver wallet
|
||||||
|
$paymentID = "CHL_" . time();
|
||||||
|
$sql = "INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driver_id, :paymentID, :amount, :method)";
|
||||||
|
$stmt = $con->prepare($sql);
|
||||||
|
$stmt->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmt->bindParam(':paymentID', $paymentID, PDO::PARAM_STR);
|
||||||
|
$stmt->bindParam(':amount', $points, PDO::PARAM_STR);
|
||||||
|
$stmt->bindParam(':method', $challenge_id, PDO::PARAM_STR);
|
||||||
|
|
||||||
|
if ($stmt->execute()) {
|
||||||
|
jsonSuccess("Reward claimed successfully");
|
||||||
|
} else {
|
||||||
|
jsonError("Failed to claim reward");
|
||||||
|
}
|
||||||
|
?>
|
||||||
31
ride/gamification/getDriverBehavior.php
Normal file
31
ride/gamification/getDriverBehavior.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
|
$driver_id = filterRequest("driver_id");
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
SELECT
|
||||||
|
COALESCE(ROUND(AVG(behavior_score), 1), 100) as avg_score,
|
||||||
|
COALESCE(SUM(hard_brakes), 0) as total_hard_brakes,
|
||||||
|
COALESCE(MAX(max_speed), 0) as max_speed
|
||||||
|
FROM `driver_behavior`
|
||||||
|
WHERE driver_id = :driver_id
|
||||||
|
AND created_at >= DATE(NOW()) - INTERVAL 30 DAY
|
||||||
|
";
|
||||||
|
|
||||||
|
$stmt = $con->prepare($sql);
|
||||||
|
$stmt->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
if ($row && $row['max_speed'] !== null) {
|
||||||
|
jsonSuccess([$row]);
|
||||||
|
} else {
|
||||||
|
jsonSuccess([[
|
||||||
|
"avg_score" => 100,
|
||||||
|
"total_hard_brakes" => 0,
|
||||||
|
"max_speed" => 0
|
||||||
|
]]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
54
ride/gamification/getLeaderboard.php
Normal file
54
ride/gamification/getLeaderboard.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
|
$type = isset($_POST['type']) ? filterRequest("type") : 'trips';
|
||||||
|
|
||||||
|
if ($type === 'earnings') {
|
||||||
|
$sql = "
|
||||||
|
SELECT
|
||||||
|
d.id as driver_id,
|
||||||
|
COALESCE(d.name, d.nameArabic, d.firstName, 'Driver') as name,
|
||||||
|
d.personal_photo as photoUrl,
|
||||||
|
COALESCE(SUM(p.amount), 0) as value
|
||||||
|
FROM `driver` d
|
||||||
|
JOIN `payments` p ON d.id = p.driverID
|
||||||
|
WHERE p.created_at >= DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY)
|
||||||
|
GROUP BY d.id
|
||||||
|
ORDER BY value DESC
|
||||||
|
LIMIT 10
|
||||||
|
";
|
||||||
|
} else {
|
||||||
|
// Default to trips
|
||||||
|
$sql = "
|
||||||
|
SELECT
|
||||||
|
d.id as driver_id,
|
||||||
|
COALESCE(d.name, d.nameArabic, d.firstName, 'Driver') as name,
|
||||||
|
d.personal_photo as photoUrl,
|
||||||
|
COUNT(r.id) as value
|
||||||
|
FROM `driver` d
|
||||||
|
JOIN `ride` r ON d.id = r.driver_id
|
||||||
|
WHERE r.status = 'Finished'
|
||||||
|
AND r.created_at >= DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY)
|
||||||
|
GROUP BY d.id
|
||||||
|
ORDER BY value DESC
|
||||||
|
LIMIT 10
|
||||||
|
";
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt = $con->prepare($sql);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
if ($stmt->rowCount() > 0) {
|
||||||
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
// Add Rank manually to support older MySQL versions
|
||||||
|
$rank = 1;
|
||||||
|
foreach ($rows as &$row) {
|
||||||
|
$row['rank'] = $rank++;
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonSuccess($rows);
|
||||||
|
} else {
|
||||||
|
jsonSuccess([]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
30
ride/gamification/getReferralStats.php
Normal file
30
ride/gamification/getReferralStats.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
|
$driver_id = filterRequest("driver_id");
|
||||||
|
|
||||||
|
// Driver invites count
|
||||||
|
$sqlDriver = "SELECT COUNT(*) as total FROM invites WHERE driverId = :driver_id AND isInstall = 1";
|
||||||
|
$stmtD = $con->prepare($sqlDriver);
|
||||||
|
$stmtD->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmtD->execute();
|
||||||
|
$driverCount = $stmtD->fetchColumn();
|
||||||
|
|
||||||
|
// Passenger invites count
|
||||||
|
$sqlPass = "SELECT COUNT(*) as total FROM invitesToPassengers WHERE driverId = :driver_id AND isInstall = 1";
|
||||||
|
$stmtP = $con->prepare($sqlPass);
|
||||||
|
$stmtP->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmtP->execute();
|
||||||
|
$passengerCount = $stmtP->fetchColumn();
|
||||||
|
|
||||||
|
// Rewards calculation (100 pts per driver, 50 pts per passenger)
|
||||||
|
$totalRewards = ($driverCount * 100) + ($passengerCount * 50);
|
||||||
|
|
||||||
|
jsonSuccess([
|
||||||
|
[
|
||||||
|
"driverInvites" => (int)$driverCount,
|
||||||
|
"passengerInvites" => (int)$passengerCount,
|
||||||
|
"totalRewards" => $totalRewards
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
?>
|
||||||
32
ride/gamification/getWeeklyAggregate.php
Normal file
32
ride/gamification/getWeeklyAggregate.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
|
$driver_id = filterRequest("driver_id");
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
SELECT
|
||||||
|
DATE(r.created_at) as day,
|
||||||
|
COUNT(r.id) as trips,
|
||||||
|
COALESCE(SUM(p.amount), 0) as earnings,
|
||||||
|
COALESCE(SUM(r.duration)/60, 0) as hours
|
||||||
|
FROM `ride` r
|
||||||
|
LEFT JOIN `payments` p ON r.id = p.rideId
|
||||||
|
WHERE r.driver_id = :driver_id
|
||||||
|
AND r.status = 'Finished'
|
||||||
|
AND r.created_at >= DATE(NOW()) - INTERVAL 6 DAY
|
||||||
|
GROUP BY DATE(r.created_at)
|
||||||
|
ORDER BY DATE(r.created_at) ASC
|
||||||
|
";
|
||||||
|
|
||||||
|
$stmt = $con->prepare($sql);
|
||||||
|
$stmt->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
if ($stmt->rowCount() > 0) {
|
||||||
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
jsonSuccess($rows);
|
||||||
|
} else {
|
||||||
|
// Return empty array instead of error so the app doesn't crash
|
||||||
|
jsonSuccess([]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user