50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Models\GameEvent;
|
||
|
|
use App\Models\Movement;
|
||
|
|
use App\Models\Player;
|
||
|
|
use App\Models\Village;
|
||
|
|
use App\Models\World;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Start a world over: every village, player, movement, report and pending event of it
|
||
|
|
* goes; user accounts and the world's config stay. Players join again from scratch.
|
||
|
|
*/
|
||
|
|
class ResetWorld extends Command
|
||
|
|
{
|
||
|
|
protected $signature = 'game:reset-world {slug : The world to reset} {--force : Skip the confirmation}';
|
||
|
|
|
||
|
|
protected $description = 'Wipe a world back to empty, keeping user accounts and its config';
|
||
|
|
|
||
|
|
public function handle(): int
|
||
|
|
{
|
||
|
|
$world = World::where('slug', $this->argument('slug'))->first();
|
||
|
|
if (! $world) {
|
||
|
|
$this->error("No world [{$this->argument('slug')}].");
|
||
|
|
|
||
|
|
return self::FAILURE;
|
||
|
|
}
|
||
|
|
$villages = $world->villages()->count();
|
||
|
|
$players = $world->players()->count();
|
||
|
|
if (! $this->option('force') && ! $this->confirm("Reset {$world->name}: delete $villages villages and $players players?")) {
|
||
|
|
return self::FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::transaction(function () use ($world) {
|
||
|
|
// Children first; the rest (buildings, troops, orders, reports) cascade.
|
||
|
|
GameEvent::where('world_id', $world->id)->delete();
|
||
|
|
Movement::where('world_id', $world->id)->delete();
|
||
|
|
Village::where('world_id', $world->id)->delete();
|
||
|
|
Player::where('world_id', $world->id)->delete();
|
||
|
|
$world->update(['started_at' => now()]);
|
||
|
|
});
|
||
|
|
$this->info("{$world->name} is empty again: $villages villages and $players players removed.");
|
||
|
|
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|