diff --git a/app/Config/Routes.php b/app/Config/Routes.php index db571a0..8b619eb 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -29,4 +29,13 @@ $routes->group('register', static function ($routes) { $routes->post('newuser', 'Register::newuser'); $routes->get('new_captcha', 'Register::newCaptcha'); $routes->get('ok', 'Register::ok'); +}); + +$routes->group('api', static function ($routes) { + $routes->get('bests_laps', 'Api::getBestsLaps'); + $routes->get('most_active_users', 'Api::getMostActiveUsers'); +}); + +$routes->group('test', static function ($routes) { + $routes->get('tables', 'Test::tables'); }); \ No newline at end of file diff --git a/app/Controllers/Api.php b/app/Controllers/Api.php new file mode 100644 index 0000000..c47033a --- /dev/null +++ b/app/Controllers/Api.php @@ -0,0 +1,63 @@ +request->getGet('period'); + $carCatId = $this->request->getGet('car_cat'); + + if (!$period || !$carCatId) return $this->fail('The period and/or category were not indicated'); + + $page = $this->request->getGet('page'); + $limit = $this->request->getGet('limit'); + if (!$page | !is_numeric($page)) $page = 0; + if (!$limit | !is_numeric($limit)) $limit = 0; + + [$list, $total] = $bestLapsModel->getBests($period, $carCatId, $page, $limit); + + return $this->respond(['data' => $list, 'total' => $total]); + } + + public function getMostActiveUsers() + { + $period = $this->request->getGet('period'); + $carCatId = $this->request->getGet('car_cat'); + + if (!$period || !$carCatId) return $this->fail('The period and/or category were not indicated'); + + $backto = getDateDiff($period); + $carCatsModel = new CarCatsModel; + + $carsCatIds = $carCatsModel->getCarsInCat($carCatId); + + $builder = $this->db->table('races r'); + $builder->select('COUNT(*) AS count, u.username'); + $builder->join('users u', 'u.id = r.user_id'); + $builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto); + $builder->whereIn('r.car_id', $carsCatIds); + $builder->groupBy('u.username'); + $builder->orderBy('count DESC'); + + $query = $builder->get(20); + $list = []; + $total = 0; + + if ($query && $query->getNumRows() > 0) { + $list = $query->getResult(); + $total = $query->getNumRows(); + } + + return $this->respond(['data' => $list, 'total' => $total]); + } +} \ No newline at end of file diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index e305ca1..be4b729 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -84,13 +84,14 @@ class Home extends BaseController $categoriesList = $carCatModel->select('id, name, count(carId) as totalCars')->groupBy('id')->findAll(); $currCat = $carCatModel->find($carCatId); - $carsCatList = $carCatModel->select('carId')->where('id', $carCatId)->findAll(); + //$carsCatList = $carCatModel->select('carId')->where('id', $carCatId)->findAll(); + $carsCatIds = $carCatModel->getCarsInCat($carCatId); $tplData['currCat'] = $currCat; $tplData['carCategoriesList'] = $categoriesList; - $carsCatIds = []; - foreach ($carsCatList as $car) $carsCatIds[] = $car->carId; + //$carsCatIds = []; + //foreach ($carsCatList as $car) $carsCatIds[] = $car->carId; //UGLY: there is some category that have no car assigned so create a fake $carsql for them //to prevent errors in the generated queries @@ -108,6 +109,7 @@ class Home extends BaseController ################################ */ + /* $builder = $this->db->table('races r'); $builder->select('r.user_id, COUNT(*) AS count, u.username'); $builder->join('users u', 'u.id = r.user_id'); @@ -119,6 +121,7 @@ class Home extends BaseController $tplData['users'] = []; $query = $builder->get(); if ($query && $query->getNumRows() > 0) $tplData['users'] = $query->getResult(); + */ /* ################################ @@ -126,8 +129,7 @@ class Home extends BaseController ## WITH A CAR OFT HIS CATEGORY ################################ */ - - $tplData['mylaps'] = $bestLapsModel->getBests($period, $carCatId, 0, 0); + //list($tplData['mylaps'], $_) = $bestLapsModel->getBests($period, $carCatId, 0, 0); $tplData['tracks'] = []; $builder = $this->db->table('races'); @@ -173,9 +175,9 @@ class Home extends BaseController $query = $builder->get(); if ($query && $query->getNumRows() > 0) $tplData['cars'] = $query->getResult(); - echo get_header('Home'); + echo get_header('Home', ['minidt.css']); echo view('main', $tplData); - echo get_footer(); + echo get_footer(['minidt.js', 'home_tables.js']); } public function error404() diff --git a/app/Models/BestLapsModel.php b/app/Models/BestLapsModel.php index 21e4168..f324a81 100644 --- a/app/Models/BestLapsModel.php +++ b/app/Models/BestLapsModel.php @@ -9,7 +9,7 @@ class BestLapsModel extends BaseModel public function getBests(string $period, string $carCat, int $page=0, int $limit=20) { - $from = $page * $limit; + $offset = $page * $limit; $list = []; switch ($period) @@ -36,7 +36,19 @@ class BestLapsModel extends BaseModel break; } + $total = 0; $builder = $this->builder(); + $builder->select('COUNT(*) AS total'); + $builder->join('laps l', 'l.id = bl.lap_id'); + $builder->join('races r', 'r.id = bl.race_id'); + $builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto); + $builder->where('bl.car_cat', $carCat); + $builder->groupBy(['r.track_id', 'l.wettness']); + $query = $builder->get(); + + if ($query) $total = $query->getNumRows(); + + $builder->resetQuery(); $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'); @@ -46,11 +58,11 @@ class BestLapsModel extends BaseModel $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); + if ($limit > 0) $builder->limit($limit, $offset); $query = $builder->get(); if ($query && $query->getNumRows() > 0) $list = $query->getResult(); - return $list; + return [$list, $total]; } } diff --git a/app/Models/CarCatsModel.php b/app/Models/CarCatsModel.php index c48ee98..e05148f 100644 --- a/app/Models/CarCatsModel.php +++ b/app/Models/CarCatsModel.php @@ -6,4 +6,19 @@ class CarCatsModel extends BaseModel { protected $table = 'cars_cats'; protected $allowedFields = ['id', 'name', 'carId']; + + /** + * Return all cars in the indicated category + * @param mixed $carCatId The ID od the category + * @return array A array with all cars ID in teh category + */ + public function getCarsInCat($carCatId) + { + $carsCatList = $this->select('carId')->where('id', $carCatId)->findAll(); + + $carsCatIds = []; + foreach ($carsCatList as $car) $carsCatIds[] = $car->carId; + + return $carsCatIds; + } } diff --git a/app/Views/main.php b/app/Views/main.php index b078b11..ded3ed6 100644 --- a/app/Views/main.php +++ b/app/Views/main.php @@ -57,77 +57,17 @@ Most active users
- - - - - - - - - - - - - - - -
PilotRaces
- username, 'user', $user->username) ?> - - count ?> -
+
+
+

Bests lap for each track
-

- - - - - - - - - - - - - - track_id; - $car = $mylap->car_id; - ?> - - - - - - - - - - - -
TrackPilotCarLaptimeWeatherDateSession
- track_id, 'track', $mylap->track_name) ?> - - username, 'user', $mylap->username) ?> - - car_id, 'car', $mylap->car_name) ?> - - bestlap); ?> - - wettness); ?> - - timestamp; ?> - - #race_id?> -
+ +
+
+

Most used Tracks
diff --git a/app/Views/templates/header.php b/app/Views/templates/header.php index f31ef6d..b41c3cf 100644 --- a/app/Views/templates/header.php +++ b/app/Views/templates/header.php @@ -8,6 +8,11 @@ <?=$title?> + + + "> + +