From c7863dc98f608a279bb0e6a9d7466062dc996e48 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 6 Jul 2026 03:11:39 +0300 Subject: [PATCH] Update: 2026-07-06 03:11:39 --- backend/api/ride/get_competitor_context.php | 77 ++++++++++++++++----- 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/backend/api/ride/get_competitor_context.php b/backend/api/ride/get_competitor_context.php index a03dd09e..9ebab7d4 100644 --- a/backend/api/ride/get_competitor_context.php +++ b/backend/api/ride/get_competitor_context.php @@ -13,33 +13,67 @@ if (!$lat || !$lng || !$country) { exit; } +$lat = (float)$lat; +$lng = (float)$lng; $countryCode = strtoupper($country); if ($countryCode == 'JORDAN') $countryCode = 'JO'; if ($countryCode == 'SYRIA') $countryCode = 'SY'; if ($countryCode == 'EGYPT') $countryCode = 'EG'; -// Default Competitor info based on Country -$topComp = 'TaxiF'; -$multiplier = 1.15; // 15% more expensive by default +$avgPricePerKm = 0; +$topComp = 'TaxiF'; // Default -if ($countryCode === 'JO') { - $topComp = 'TaxiF'; - $multiplier = 1.15; // TaxiF is typically 15% more expensive -} else if ($countryCode === 'SY') { - $topComp = 'Yango'; - $multiplier = 1.12; -} else if ($countryCode === 'EG') { - $topComp = 'inDrive'; - $multiplier = 1.10; +try { + $redis = getRedisConnection(); + $cacheJson = $redis->get('siro:cache:pricing:grids'); + if ($cacheJson) { + $cacheData = json_decode($cacheJson, true); + if ($cacheData && isset($cacheData['grids'])) { + $gridSize = 0.025; + $gLat = round($lat / $gridSize) * $gridSize; + $gLng = round($lng / $gridSize) * $gridSize; + $gridKey = "{$countryCode}_" . number_format($gLat, 3) . "_" . number_format($gLng, 3); + + $grids = $cacheData['grids']; + if (isset($grids[$gridKey])) { + $avgPricePerKm = (float)$grids[$gridKey]['avg_price']; + $topComp = $grids[$gridKey]['top_competitor'] ?? 'TaxiF'; + } else { + $fallbackKey = "{$countryCode}_FALLBACK"; + if (isset($grids[$fallbackKey])) { + $avgPricePerKm = (float)$grids[$fallbackKey]['avg_price']; + $topComp = $grids[$fallbackKey]['top_competitor'] ?? 'TaxiF'; + } + } + } + } +} catch (Exception $e) { + // Continue with defaults if Redis fails } -// Calculate the competitor's total price based on our price -if ($siroPrice > 0) { +// If we couldn't get a price from Redis, use a smart default based on country +if ($avgPricePerKm <= 0) { + if ($countryCode === 'JO') { + $avgPricePerKm = 0.35; + $topComp = 'TaxiF'; + } else if ($countryCode === 'SY') { + $avgPricePerKm = 4000; + $topComp = 'Yango'; + } else if ($countryCode === 'EG') { + $avgPricePerKm = 15; + $topComp = 'inDrive'; + } +} + +// Calculate the competitor's total price based on distance and average market per-km rate +$competitorTotalPrice = round($distance * $avgPricePerKm, 2); + +// Make sure competitor is slightly higher than us for psychological effect +// if their raw math somehow ended up lower due to straight per-km multiplication +if ($siroPrice > 0 && $competitorTotalPrice <= $siroPrice) { + // Force a dynamic difference based on country + $multiplier = ($countryCode === 'JO') ? 1.15 : 1.10; $competitorTotalPrice = round($siroPrice * $multiplier, 2); -} else { - // Fallback if siro_price is 0 - $avgPricePerKm = ($countryCode === 'JO') ? 0.35 : (($countryCode === 'SY') ? 4000 : 15); - $competitorTotalPrice = round($distance * $avgPricePerKm, 2); } // Calculate savings @@ -55,8 +89,15 @@ $compNameAr = match(strtolower($topComp)) { 'yallago' => 'يلا غو', 'jenny' => 'جيني', 'taxif' => 'تكسي إف', + 'com.taxif.passenger' => 'تكسي إف', 'indrive' => 'إن درايف', 'yango' => 'يانغو', + 'petra' => 'بترا رايد', + 'petra ride' => 'بترا رايد', + 'gojo' => 'جو جو', + 'didi' => 'ديدي', + 'zakin' => 'زاكن', + 'tofaddal'=> 'تفضل', default => $topComp };