toImmutable(); while ($villageId = $engine->nextDueVillage($now)) { try { $engine->catchUp($villageId, $now); } catch (Throwable $e) { report($e); $engine->recordFailure($villageId, $now); } $this->push($villageId); } if ($this->option('once')) { return self::SUCCESS; } // Sleep until the next event, at most a second, so new events are picked up. $next = $engine->nextFireAt(); $wait = $next ? ($next->getTimestampMs() - now()->getTimestampMs()) / 1000 : 1.0; usleep((int) (max(0.05, min(1.0, $wait)) * 1_000_000)); } while (true); } private function push(int $villageId): void { if (! ws_available()) { return; } $userId = Village::with('player')->find($villageId)?->player?->user_id; $sockets = $userId ? WebsocketService::getUserSocketIds($userId) : []; if ($sockets) { ws_whisper('village:updated', ['village_id' => $villageId], $sockets); } } }