Browse Source

fixed problem with saving positions

Carello Qwerty 5 years ago
parent
commit
b7d4855586
1 changed files with 33 additions and 23 deletions
  1. 33 23
      app/Http/Controllers/Telegram.php

+ 33 - 23
app/Http/Controllers/Telegram.php

@@ -12,44 +12,54 @@ class Telegram extends Controller
     public function locations(Request $request){
 
         $message = $request['message'];
+
         if (isset($message['text'])){
             $text = substr($message['text'], 0, 1);
-            Log::info($text);
+
             if($text == '/'){
-                Log::info($text);
                 \Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
             }
         }
 
         if (isset($message['location'])) {
-            Log::info($message['from']['id']);
-            $user = User::where('telegram_id', $message['from']['id'])->get();
-            Log::info(['User--->' => $user]);
+            $user = User::where('telegram_id', $message['from']['id'])->first();
+
             if ($user){
                 $lastLocation = $user->lastCoordinates;
-                $minLat = $message['location']['latitude'] - 0.0005;
-                $maxLat = $message['location']['latitude'] + 0.0005;
-                $minLng = $message['location']['longitude'] - 0.0008;
-                $maxLng = $message['location']['longitude'] + 0.0008;
-                $newLat = false;
-                $newLng = false;
-                if (!$lastLocation->lat > $minLat && !$lastLocation->lat < $maxLat){
-                    $newLat = $message['location']['latitude'];
-                }
-                if (!$lastLocation->lng > $minLng && !$lastLocation->lng < $maxLng){
-                    $newLng = $message['location']['longitude'];
-                }
-                if($newLat || $newLng){
+
+                if ($lastLocation){
+                    $minLat = $message['location']['latitude'] - 0.0005;
+                    $maxLat = $message['location']['latitude'] + 0.0005;
+                    $minLng = $message['location']['longitude'] - 0.0008;
+                    $maxLng = $message['location']['longitude'] + 0.0008;
+                    $newLat = false;
+                    $newLng = false;
+
+                    if ($lastLocation->lat < $minLat || $lastLocation->lat > $maxLat){
+                        $newLat = $message['location']['latitude'];
+                    }
+
+                    if ($lastLocation->lng < $minLng || $lastLocation->lng > $maxLng){
+                        $newLng = $message['location']['longitude'];
+                    }
+
+                    if($newLat || $newLng){
+                        $newLocation = new Location();
+                        $newLocation->lat = $newLat ?: $lastLocation->lat;
+                        $newLocation->lng = $newLng ?: $lastLocation->lng;
+                        $newLocation->user_id = $user->id;
+                        $newLocation->save();
+                        Log::info('я сохранил новую координату');
+                    }
+
+                } else {
                     $newLocation = new Location();
-                    $newLocation->lat = $newLat ?: $lastLocation->lat;
-                    $newLocation->lng = $newLng ?: $lastLocation->lng;
+                    $newLocation->lat = $message['location']['latitude'];
+                    $newLocation->lng = $message['location']['longitude'];
                     $newLocation->user_id = $user->id;
                     $newLocation->save();
-                    Log::info('я сохранил новую координату');
                 }
             }
-
-            Log::info('я ничего не сохранил');
         }
     }
 }