Skip to content

Commit

Permalink
Add /players endpoint to production (#32)
Browse files Browse the repository at this point in the history
* Fix campaigns not saving

* Fix #2

* Added /players endpoint
  • Loading branch information
Kejax authored Apr 23, 2024
1 parent 4091ef4 commit 567b4ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/Http/Controllers/Api/PlanetStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,26 @@ public function planetHistory(Request $request, $planet_index) {
}

}

public function playersGlobal(Request $request) {

$data = $request->validate([
"time" => ["nullable", "date"]
]);

if (!$request->has('time')) {
$time = Carbon::now();
} else {
$time = Carbon::parse($data['time']);
}

$players = Planet::with(['history' => function (Builder $q) use ($time) {
$q->latest()->where('created_at', '<', $time)->limit(1);
}])->sum('players');

return response()->json([
"count" => $players
], 200);

}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
Route::get('/campaigns', [PlanetCampaignController::class, 'index']);

Route::get('/campaigns/active', [PlanetCampaignController::class, 'active']);

Route::get('/players', [PlanetStatusController::class, 'playersGlobal']);

0 comments on commit 567b4ba

Please sign in to comment.