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; } }