diff --git a/app/Http/Controllers/Api/PlanetStatusController.php b/app/Http/Controllers/Api/PlanetStatusController.php index ca622ad..8360f63 100644 --- a/app/Http/Controllers/Api/PlanetStatusController.php +++ b/app/Http/Controllers/Api/PlanetStatusController.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; class PlanetStatusController extends Controller { @@ -40,16 +41,51 @@ public function planetsAtTime(Request $request) { $time = Carbon::parse($request->input('time')); $planets = Planet::with(['history' => function (Builder $q) use ($time) { - $q->latest()->where('updated_at', '<', $time)->limit(1); + $q->latest()->where('created_at', '<', $time)->limit(1); }])->get(); return response()->json($planets->pluck('history')->OneEntryArrayList(), 200); } + public function activeCampaigns() { + + $planets = Planet::whereHas('campaigns', function ($query) { + $query->where('ended_at', null); + })->get(); + + $planets->load(['campaigns' => function ($query) { + $query->where('ended_at', null)->limit(1); + }]); + + return response()->json($planets, 200); + + } + public function latestPlanets(Request $request) { $planets = Planet::all(); return response()->json($planets, 200); } + + public function planetHistory(Request $request, $planet_index) { + + $request->validate([ + 'size' => ['nullable', 'integer'] + ]); + + $limit = $request->input('size', 100); + + $planet = Planet::where('index', $planet_index)->first(); + + if (!$planet) { + return response()->json([ + 'error' => 'planet_index is not valid', + 'code' => 404 + ], 404); + } else { + return response()->json($planet->planetHistory()->limit(), 200); + } + + } } diff --git a/app/Http/Controllers/PlanetCampaignController.php b/app/Http/Controllers/PlanetCampaignController.php new file mode 100644 index 0000000..b3e0d07 --- /dev/null +++ b/app/Http/Controllers/PlanetCampaignController.php @@ -0,0 +1,36 @@ +get(); + + return response()->json($campaigns, 200); + + } + + public function index(Request $request) { + + $request->validate([ + 'id' => ['integer', 'nullable'] + ]); + + $campaignQuery = PlanetCampaign::query(); + + if ($request->has('id')) { + $id = $request->input('id'); + $campaignQuery->where('id', $id); + } + + $campaigns = $campaignQuery->limit(100)->get(); + + return response()->json($campaigns, 200); + + } +} diff --git a/app/Models/Planet.php b/app/Models/Planet.php index a3b1210..bdb7362 100644 --- a/app/Models/Planet.php +++ b/app/Models/Planet.php @@ -26,10 +26,13 @@ class Planet extends Model protected $hidden = [ 'created_at', - 'index' ]; public function history() { return $this->hasMany(PlanetHistory::class, 'index', 'index'); } + + public function campaigns() { + return $this->hasMany(PlanetCampaign::class, 'planetIndex', 'index'); + } } diff --git a/app/Models/PlanetCampaign.php b/app/Models/PlanetCampaign.php new file mode 100644 index 0000000..02f611e --- /dev/null +++ b/app/Models/PlanetCampaign.php @@ -0,0 +1,32 @@ +belongsTo(Planet::class, 'planetIndex', 'index'); + } +} diff --git a/app/Models/PlanetHistory.php b/app/Models/PlanetHistory.php index 7339ab3..fc404f1 100644 --- a/app/Models/PlanetHistory.php +++ b/app/Models/PlanetHistory.php @@ -21,4 +21,17 @@ class PlanetHistory extends Model 'regenPerSecond', 'players', ]; + + protected $hidden = [ + + ]; + + public function planet() { + return $this->belongsTo(Planet::class, 'index', 'index'); + } + + public function campaigns() { + error_log($this->created_at); + return $this->hasMany(PlanetCampaign::class, 'planetIndex', 'index'); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 95ad536..ca7f8e3 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use ErrorException; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\ServiceProvider; @@ -22,7 +23,11 @@ public function boot(): void { Collection::macro('OneEntryArrayList', function () { return $this->map(function ($item) { - return $item[0]; + try { + return $item[0]; + } catch (ErrorException) { + return null; + } }); }); } diff --git a/database/migrations/2024_04_04_213034_create_planet_campaigns_table.php b/database/migrations/2024_04_04_213034_create_planet_campaigns_table.php new file mode 100644 index 0000000..3197e6f --- /dev/null +++ b/database/migrations/2024_04_04_213034_create_planet_campaigns_table.php @@ -0,0 +1,32 @@ +unsignedBigInteger('id')->nullable()->primary(); + $table->unsignedBigInteger('planetIndex'); + $table->unsignedInteger('type'); + $table->unsignedInteger('count'); + $table->unsignedBigInteger('warId'); + $table->timestamp('ended_at')->nullable()->default(null); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('planet_campaigns'); + } +}; diff --git a/resources/views/errors/404.blade.php b/resources/views/errors/404.blade.php new file mode 100644 index 0000000..1900ea7 --- /dev/null +++ b/resources/views/errors/404.blade.php @@ -0,0 +1 @@ +Not a route diff --git a/routes/api.php b/routes/api.php index c612e3e..5cad517 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,7 @@ json()['id']; - $planetRequest = Http::get("https://api.live.prod.thehelldiversgame.com/api/WarSeason/$currentWarId/Status"); + $planetRequest = Http::withHeaders([ + 'Accept-Language' => 'en-EN' + ])->get("https://api.live.prod.thehelldiversgame.com/api/WarSeason/$currentWarId/Status"); if ($planetRequest->successful()) { $data = $planetRequest->json(); + /* ----- Set the current PlanetStatus ----- */ + + // Set default information foreach ($data['planetStatus'] as $planet) { $planet['warId'] = $currentWarId; @@ -52,10 +59,35 @@ )) { $history->touch(); } else { - PlanetHistory::create($planet); - Log::debug($history->toArray()); - Log::debug(print_r($planet)); + PlanetHistory::create($planet) + } + } + + // Mark old campaigns as marked if they're not in the array + $oldCampaigns = PlanetCampaign::where('ended_at', null); + $newCampaignIds = collect($data['campaigns'])->pluck('id')->all(); + + foreach ($oldCampaigns as $oldCampaign) { + if (!in_array($oldCampaign->id, $newCampaignIds)) { + $oldCampaign->ended_at = now(); + } + } + + // Check for planet campaigns and add them + foreach ($data['campaigns'] as $dataCampaign) { + + $planetCampaign = PlanetCampaign::where('id', $dataCampaign['id'])->first(); + + // Check if the campaign is new or an old one + if ($planetCampaign == null) { + + // Merge war id into the array and create a new PlanetCampaign with it + $dataCampaign['warId'] = $currentWarId; + + PlanetCampaign::create($dataCampaign); + } + } }