game-village-politics-backend/app/Console/Commands/ResetWorld.php

51 lines
2.0 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();
// The next barbarian wave is planned again from now, on the worker's next loop.
$world->update(['started_at' => now(), 'crowns_placed_at' => null, 'barbarians_at' => null, 'holder_player_id' => null, 'winner_player_id' => null, 'ends_at' => null, 'ended_at' => null]);
});
$this->info("{$world->name} is empty again: $villages villages and $players players removed.");
return self::SUCCESS;
}
}