subSeconds(static::PLAYER_HEARTBEAT_TIMEOUT * 60)) ->delete(); $log->info('Queued players: ' . json_encode($cleanedQueuedPlayers, JSON_PRETTY_PRINT)); $closedGamesToKill = Game::where('status', '=', MatchStatus::Closed->value) ->where('updated_at', '<', Carbon::now()->subSeconds(static::GAME_MAX_TIME * 60)) ->get(); foreach ($closedGamesToKill as $game) { //archive the games set to closed, so we don't lose the chat history $game->archiveGame(Faction::None); $game->status = MatchStatus::Killed; $game->save(); } $log->info('Closed games set to Killed: ' . json_encode($closedGamesToKill->getQueueableIds(), JSON_PRETTY_PRINT)); // delete games where the hunter crashed on loading into the arena, leaving the game stuck at created $deletedCreatedGames = Game::where('status', '=', MatchStatus::Created) ->where('created_at', '<', Carbon::now()->subSeconds(static::CREATED_GAME_TIMEOUT)) ->delete(); $log->info('Deleted Stuck Created games: ' . json_encode($deletedCreatedGames, JSON_PRETTY_PRINT)); // Select User Ids that joined a game and haven't sent the match request for a period of time // This porpably means they crashed or never joined the game in the first place. $usersToRemove = DB::table('game_user')->join('games', 'game_user.game_id', '=', 'games.id') ->whereIn('games.status', [ MatchStatus::Created->value, MatchStatus::Opened->value, ]) ->where('game_user.updated_at', '<', Carbon::now()->subSeconds(static::PLAYER_HEARTBEAT_TIMEOUT * 60)) ->get(['user_id']); $userIdArray = []; foreach ($usersToRemove as $user) { $userIdArray[] = $user->user_id; } $log->info('Deleting players that are in a mach, but didnt send a heartbeat: ' . json_encode($usersToRemove, JSON_PRETTY_PRINT)); // Delete a game if one of the to be removed players is the host. Game::whereIn('creator_user_id', $userIdArray)->delete(); // Delete them afterwards. DB::table('game_user')->whereIn('user_id', $userIdArray)->delete(); } }