Skip to content

Commit

Permalink
New endpoint to submit guesses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thombrix authored and loicsantschi01 committed Dec 12, 2023
1 parent acd32a5 commit 2406a9d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend-laravel/app/Http/Controllers/VilleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

class VilleController extends Controller
{
Expand Down Expand Up @@ -43,6 +44,13 @@ public function today()
return $ville;
}

public function guess($id)
{
// TODO: clean id
$a = new CityComparator();
return $a->compareToToday($id);
}

public function all()
{
$villes = Ville::all();
Expand Down
54 changes: 54 additions & 0 deletions backend-laravel/app/Utility/CityComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Utility;

use App\Models\Cycle;
use App\Models\Ville;

class CityComparator
{
public function compareToToday($guessedId)
{
$current = Cycle::getCurrentVille();

$guessedCity = Ville::with('canton')->
where('id', $guessedId)->firstOrFail();

if($current->id == $guessedId)
{
return ['city' => $guessedCity];
}

return [
'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()
];
}

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

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

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

private static function comparePosition()
{
return [];
}
}
1 change: 1 addition & 0 deletions backend-laravel/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@

Route::get('/ville/today', [VilleController::class, 'today']);
Route::get('/villes', [VilleController::class, 'all']);
Route::get('/ville/guess/{id}', [VilleController::class, 'guess']);

0 comments on commit 2406a9d

Please sign in to comment.