Skip to content

Commit

Permalink
Variable name refactoring + comparePositionFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Thombrix committed Nov 25, 2023
1 parent ef94ddb commit dfeedfe
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions backend-laravel/app/Utility/CityComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Cycle;
use App\Models\Ville;
use App\Utility\Math;

class CityComparator
{
Expand All @@ -23,32 +24,42 @@ public function compareToToday($guessedId)
'city' => $guessedCity,
'canton_diff' => $current->canton_id == $guessedCity->canton_id ,
'pop_diff' => $current->population <=> $guessedCity->population,
'lang_diff' => self::compareLang($current, $guessedCity),
'position_diff' => self::comparePosition()
'lang_diff' => self::compareLang($guessedCity, $current),
'position_diff' => self::comparePosition($guessedCity, $current)
];
}

private static function compareLang($current, $guessedCity)
private static function compareLang($city1, $city2)
{
$currentLanguesIds = $current->langues->map(
$city1LanguesIds = $city1->langues->map(
function($l)
{
return $l->id;
}
)->toArray();

$guessedLanguesIds = $guessedCity->langues->map(
$city2LanguesIds = $city2->langues->map(
function($l)
{
return $l->id;
}
)->toArray();

return count(array_diff($currentLanguesIds, $guessedLanguesIds));
return count(array_diff($city1LanguesIds, $city2LanguesIds));
}

private static function comparePosition()
private static function comparePosition($city1, $city2)
{
return [];
$coord1 = $city1->coord;
$coord2 = $city2->coord;

// https://www.php.net/manual/en/function.list.php
list($lat1, $long1) = explode(', ', $coord1);
list($lat2, $long2) = explode(', ', $coord2);

return [
'distance' => Math::distance($lat1, $long1, $lat2, $long2),
'direction' => Math::direction($lat1, $long1, $lat2, $long2)
];
}
}

0 comments on commit dfeedfe

Please sign in to comment.