Skip to content

Commit

Permalink
Push to production (#28)
Browse files Browse the repository at this point in the history
* Update console.php

* Fix the previous data saving implementation

* Fix planet histories table

* Add campaigns and endpoints

Added campaign tracking with endpoints and a new endpoint for retrieving planets with active campaigns.
  • Loading branch information
Kejax authored Apr 5, 2024
1 parent 2920e09 commit 5a537d4
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 7 deletions.
38 changes: 37 additions & 1 deletion app/Http/Controllers/Api/PlanetStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

}
}
36 changes: 36 additions & 0 deletions app/Http/Controllers/PlanetCampaignController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers;

use App\Models\PlanetCampaign;
use Illuminate\Http\Request;

class PlanetCampaignController extends Controller
{
public function active(Request $request) {

$campaigns = PlanetCampaign::where('ended_at', null)->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);

}
}
5 changes: 4 additions & 1 deletion app/Models/Planet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
32 changes: 32 additions & 0 deletions app/Models/PlanetCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PlanetCampaign extends Model
{
use HasFactory;

protected $table = 'planet_campaigns';

protected $increments = false;

protected $fillable = [
'id',
'planetIndex',
'type',
'count',
'warId',
'ended_at'
];

protected $hidden = [
'updated_at'
];

public function planet() {
return $this->belongsTo(Planet::class, 'planetIndex', 'index');
}
}
13 changes: 13 additions & 0 deletions app/Models/PlanetHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
7 changes: 6 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use ErrorException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\ServiceProvider;

Expand All @@ -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;
}
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('planet_campaigns', function (Blueprint $table) {
$table->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');
}
};
1 change: 1 addition & 0 deletions resources/views/errors/404.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not a route
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\Api\PlanetStatusController;
use App\Http\Controllers\PlanetCampaignController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -24,3 +25,9 @@
Route::get('/planets', [PlanetStatusController::class, 'latestPlanets']);

Route::get('/planets/at', [PlanetStatusController::class, 'planetsAtTime']);

Route::get('/planets/active', [PlanetStatusController::class, 'activeCampaigns']);

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

Route::get('/campaigns/active', [PlanetCampaignController::class, 'active']);
40 changes: 36 additions & 4 deletions routes/console.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use App\Models\Planet;
use App\Models\PlanetCampaign;
use App\Models\PlanetHistory;
use Carbon\Carbon;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Http;
Expand All @@ -28,11 +30,16 @@

$currentWarId = $warIdRequest->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;
Expand All @@ -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);

}

}

}
Expand Down

0 comments on commit 5a537d4

Please sign in to comment.