The code to obtain the best laps has been moved to the getBests method in App\Models\BestLapsModel.

This commit is contained in:
Alfonso Saavedra "Son Link" 2024-12-14 10:55:16 +01:00
parent fd255585fe
commit dbef4fa4d4
No known key found for this signature in database
GPG key ID: D3594BCF897F74D8
2 changed files with 29 additions and 29 deletions

View file

@ -3,6 +3,7 @@
namespace App\Controllers;
use App\Models\CarCatsModel;
use App\Models\TracksModel;
use App\Models\BestLapsModel;
use CodeIgniter\API\ResponseTrait;
class Home extends BaseController
@ -12,7 +13,8 @@ class Home extends BaseController
public function index()
{
$tplData = [];
$carCatModel = new CarCatsModel();
$carCatModel = new CarCatsModel;
$bestLapsModel = new BestLapsModel;
// select interested period
if(array_key_exists('period', $_COOKIE))
@ -124,33 +126,8 @@ class Home extends BaseController
## WITH A CAR OFT HIS CATEGORY
################################
*/
/*
$builder = $this->db->table('laps l');
$builder->select('l.race_id, r.track_id, r.car_id, r.user_id, r.timestamp, l.wettness, min(l.laptime) as bestlap, c.name AS car_name, t.name AS track_name, u.username');
$builder->join('races r', 'l.race_id = r.id');
$builder->join('cars c', 'c.id = r.car_id');
$builder->join('tracks t', 't.id = r.track_id');
$builder->join('users u', 'u.id = r.user_id');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->whereIn('r.car_id', $carsCatIds);
$builder->groupBy(['r.track_id', 'l.wettness']);
*/
$builder = $this->db->table('bests_laps bl');
$builder->join('laps l', 'l.id = bl.lap_id');
$builder->select('l.race_id, r.track_id, r.car_id, r.user_id, r.timestamp, l.wettness, bl.laptime AS bestlap, c.name AS car_name, t.name AS track_name, u.username');
$builder->join('races r', 'r.id = bl.race_id');
$builder->join('cars c', 'c.id = bl.car_id');
$builder->join('tracks t', 't.id = bl.track_id');
$builder->join('users u', 'u.id = bl.user_id');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->where('bl.car_cat', $carCatId);
$builder->groupBy(['r.track_id', 'l.wettness']);
$tplData['mylaps'] = [];
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $tplData['mylaps'] = $query->getResult();
$tplData['mylaps'] = $bestLapsModel->getBests($backto, $carCatId, 0, 0);
$tplData['tracks'] = [];
$builder = $this->db->table('races');

View file

@ -4,6 +4,29 @@ use App\Models\BaseModel;
class BestLapsModel extends BaseModel
{
protected $table = 'bests_laps';
protected $table = 'bests_laps bl';
protected $allowedFields = ['race_id', 'lap_id', 'track_id', 'car_cat', 'car_id', 'laptime', 'user_id', 'setup'];
public function getBests(int $backto, string $carCat, int $page=0, int $limit=20)
{
$from = $page * $limit;
$list = [];
$builder = $this->builder();
$builder->join('laps l', 'l.id = bl.lap_id');
$builder->select('l.race_id, r.track_id, r.car_id, r.user_id, r.timestamp, l.wettness, bl.laptime AS bestlap, c.name AS car_name, t.name AS track_name, u.username');
$builder->join('races r', 'r.id = bl.race_id');
$builder->join('cars c', 'c.id = bl.car_id');
$builder->join('tracks t', 't.id = bl.track_id');
$builder->join('users u', 'u.id = bl.user_id');
$builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto);
$builder->where('bl.car_cat', $carCat);
$builder->groupBy(['r.track_id', 'l.wettness']);
if ($limit > 0) $builder->limit($from, $limit);
$query = $builder->get();
if ($query && $query->getNumRows() > 0) $list = $query->getResult();
return $list;
}
}