Skip to content

Commit

Permalink
Merge pull request #18 from Kejax/main
Browse files Browse the repository at this point in the history
Changed how the history works
  • Loading branch information
Kejax authored Mar 27, 2024
2 parents f021328 + 8b0a1f4 commit f900fa2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
push:
branches:
- production
pull_request:
branches:
production

jobs:
deploy:
Expand Down
9 changes: 8 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ protected function schedule(Schedule $schedule): void
$planetModel->fill($planet);
$planetModel->save();

PlanetHistory::create($planet);
$history = PlanetHistory::orderBy('updated_at', 'DESC')->where('index', $planet['index'])->first();

if ($history && $history->toArray() == $planet) {
$history->touch();
} else {
PlanetHistory::create($planet);
}

}

}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/PlanetStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function planetsAtTime(Request $request) {
$time = Carbon::parse($request->input('time'));

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

return response()->json($planets->pluck('history')->OneEntryArrayList(), 200);
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Planet.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Planet extends Model
'created_at'
];

protected $hidden = [
'created_at',
'index'
];

public function history() {
return $this->hasMany(PlanetHistory::class, 'index', 'index');
}
Expand Down
9 changes: 2 additions & 7 deletions app/Models/PlanetHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class PlanetHistory extends Model
{
use HasFactory;

public $timestamps = false;

protected $table = 'planet_histories';

protected $fillable = [
Expand All @@ -20,10 +18,7 @@ class PlanetHistory extends Model
'health',
'regenPerSecond',
'players',
'created_at'
'created_at',
'updated_at'
];

// protected $hidden = [
// 'id'
// ];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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::table('planet_histories', function (Blueprint $table) {
$table->timestamp('updated_at');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('planet_histories', function (Blueprint $table) {
$table->removeColumn('updated_at');
});
}
};
36 changes: 36 additions & 0 deletions database/migrations/2024_03_27_123937_fix_regen_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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::table('planet_histories', function (Blueprint $table) {
$table->decimal('regenPerSecond', 8, 4)->change();
});

Schema::table('planets', function (Blueprint $table) {
$table->decimal('regenPerSecond', 8, 4)->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('planet_histories', function (Blueprint $table) {
$table->float('regenPerSecond', 8, 4)->change();
});

Schema::table('planets', function (Blueprint $table) {
$table->float('regenPerSecond', 8, 4)->change();
});
}
};
9 changes: 8 additions & 1 deletion routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@
foreach ($data['planetStatus'] as $planet) {

$planet['warId'] = $currentWarId;
$planet['regenPerSecond'] = round($planet['regenPerSecond'], 4);

$planetModel = Planet::firstOrNew(['index' => $planet['index']]);
$planetModel->fill($planet);
$planetModel->save();

PlanetHistory::create($planet);
$history = PlanetHistory::orderBy('updated_at', 'DESC')->where('index', $planet['index'])->first();

if ($history && $history->makeHidden(['created_at', 'updated_at', 'id'])->toArray() == $planet) {
$history->touch();
} else {
PlanetHistory::create($planet);
}
}

}
Expand Down

0 comments on commit f900fa2

Please sign in to comment.