106 lines
3.7 KiB
PHP
106 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Engine;
|
|
|
|
use App\Game\Map\BarbarianGrowth;
|
|
use App\Models\GameEvent;
|
|
use App\Models\Player;
|
|
use App\Models\UnitStack;
|
|
use App\Models\Village;
|
|
use App\Models\World;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Villages left behind turn barbarian (architecture.md, "Leaving"): when an account is
|
|
* deleted, or its player has been away too long. They keep their buildings and stock,
|
|
* lose their queues and the owner's troops, send others' support home, and from then on
|
|
* grow and hold a garrison like any barbarian village.
|
|
*/
|
|
final class Abandon
|
|
{
|
|
/** Turn one village barbarian; the caller holds its lock and saves it. */
|
|
public static function village(Village $village, CarbonImmutable $at): void
|
|
{
|
|
$sim = Simulation::of($village);
|
|
$sim->settle($at);
|
|
$touched = Nobility::of($village)->release($village);
|
|
|
|
$rally = Rally::of($village, $sim);
|
|
foreach ($village->troops->pluck('home_village_id')->unique() as $home) {
|
|
if ($home !== $village->id) {
|
|
$rally->sendHome($home, $at);
|
|
}
|
|
}
|
|
UnitStack::where('village_id', $village->id)->where('home_village_id', $village->id)->delete();
|
|
$village->setRelation('troops', collect());
|
|
GameEvent::where('village_id', $village->id)->where('type', GameEvent::RAID_PLANNED)->whereNull('processed_at')->delete();
|
|
|
|
$village->player_id = null;
|
|
$village->setRelation('player', null);
|
|
if ($village->crown) {
|
|
Seasons::recount($village, $at);
|
|
} else {
|
|
GameEvent::create([
|
|
'world_id' => $village->world_id,
|
|
'village_id' => $village->id,
|
|
'type' => GameEvent::BARBARIAN_GROWS,
|
|
'fire_at' => $at->addSeconds((new BarbarianGrowth($sim->config()))->interval()),
|
|
]);
|
|
}
|
|
Barbarians::of($village, $sim)->fill();
|
|
Push::queue(...$touched);
|
|
}
|
|
|
|
/**
|
|
* Players not seen for world.inactive_days (real days) lose their villages; players
|
|
* never seen since activity is tracked are left alone.
|
|
*
|
|
* @return int villages turned barbarian
|
|
*/
|
|
public static function inactive(CarbonImmutable $now): int
|
|
{
|
|
$count = 0;
|
|
foreach (World::whereNull('ended_at')->get() as $world) {
|
|
$days = (float) $world->gameConfig()->get('world.inactive_days', 0);
|
|
if ($days <= 0) {
|
|
continue;
|
|
}
|
|
$gone = Player::where('world_id', $world->id)->whereNotNull('last_active_at')
|
|
->where('last_active_at', '<', Engine::stamp($now->subSeconds((int) ($days * 86400))))
|
|
->whereHas('villages')->get();
|
|
foreach ($gone as $player) {
|
|
$count += self::player($player, $now);
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
/** Every village of a player turns barbarian, each under its own lock. */
|
|
public static function player(Player $player, CarbonImmutable $at): int
|
|
{
|
|
$engine = app(Engine::class);
|
|
$count = 0;
|
|
foreach (Village::where('player_id', $player->id)->pluck('id') as $id) {
|
|
$engine->catchUpAround($id, $at);
|
|
try {
|
|
$count += DB::transaction(function () use ($id, $player, $at) {
|
|
$village = Village::whereKey($id)->lockForUpdate()->first();
|
|
if (! $village || $village->player_id !== $player->id) {
|
|
return 0;
|
|
}
|
|
self::village($village, $at);
|
|
$village->save();
|
|
|
|
return 1;
|
|
});
|
|
} finally {
|
|
Push::flush();
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
}
|