echoOutput = config('nntmux.echocli'); $this->imageService = $imageService ?? new ReleaseImageService; $this->pubkey = Settings::settingValue('amazonpubkey'); $this->privkey = Settings::settingValue('amazonprivkey'); $this->asstag = Settings::settingValue('amazonassociatetag'); $this->gameQty = (Settings::settingValue('maxgamesprocessed') !== '') ? (int) Settings::settingValue('maxgamesprocessed') : 150; $this->sleepTime = (Settings::settingValue('amazonsleep') !== '') ? (int) Settings::settingValue('amazonsleep') : 1000; $this->imgSavePath = config('nntmux_settings.covers_path').'/console/'; $this->renamed = (int) Settings::settingValue('lookupgames') === 2; $this->failCache = []; } // ======================================== // Console Info Retrieval Methods // ======================================== /** * Get console info by ID. */ public function getConsoleInfo(int $id): ?Model { return ConsoleInfo::query() ->where('consoleinfo.id', $id) ->select('consoleinfo.*', 'genres.title as genres') ->leftJoin('genres', 'genres.id', '=', 'consoleinfo.genres_id') ->first(); } /** * Get console info by name using full-text search. */ public function getConsoleInfoByName(string $title, string $platform): Model|false { $searchWords = ''; $title = preg_replace('/( - | -|\(.+\)|\(|\))/', ' ', $title); $title = preg_replace('/[^\w ]+/', '', $title); $title = trim(trim(preg_replace('/\s\s+/i', ' ', $title))); foreach (explode(' ', $title) as $word) { $word = trim(rtrim(trim($word), '-')); if ($word !== '' && $word !== '-') { $word = '+'.$word; $searchWords .= sprintf('%s ', $word); } } $searchWords = trim($searchWords.'+'.$platform); return ConsoleInfo::search($searchWords)->first() ?? false; } // ======================================== // Browse/Range Methods // ======================================== /** * Get console games range with pagination. * * @throws \Exception */ public function getConsoleRange(int $page, array $cat, int $start, int $num, string $orderBy, array $excludedCats = []): array { $page = max(1, $page); $start = max(0, $start); $browseBy = $this->getBrowseBy(); $catsrch = ''; if (\count($cat) > 0 && (int) $cat[0] !== -1) { $catsrch = Category::getCategorySearch($cat); } $exccatlist = ''; if (\count($excludedCats) > 0) { $exccatlist = ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')'; } $order = $this->getConsoleOrder($orderBy); $calcSql = sprintf( " SELECT SQL_CALC_FOUND_ROWS con.id, GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_id FROM consoleinfo con LEFT JOIN releases r ON con.id = r.consoleinfo_id WHERE con.title != '' AND con.cover = 1 AND r.passwordstatus %s %s %s %s GROUP BY con.id ORDER BY %s %s %s", app(Releases\ReleaseBrowseService::class)->showPasswords(), $browseBy, $catsrch, $exccatlist, $order[0], $order[1], ($start === false ? '' : ' LIMIT '.$num.' OFFSET '.$start) ); $cached = Cache::get(md5($calcSql.$page)); if ($cached !== null) { $consoles = $cached; } else { $data = DB::select($calcSql); $consoles = ['total' => DB::select('SELECT FOUND_ROWS() AS total'), 'result' => $data]; $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); Cache::put(md5($calcSql.$page), $consoles, $expiresAt); } $consoleIDs = $releaseIDs = []; if (\is_array($consoles['result'])) { foreach ($consoles['result'] as $console => $id) { $consoleIDs[] = $id->id; $releaseIDs[] = $id->grp_release_id; } } $sql = sprintf( " SELECT GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_id, GROUP_CONCAT(r.rarinnerfilecount ORDER BY r.postdate DESC SEPARATOR ',') AS grp_rarinnerfilecount, GROUP_CONCAT(r.haspreview ORDER BY r.postdate DESC SEPARATOR ',') AS grp_haspreview, GROUP_CONCAT(r.passwordstatus ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_password, GROUP_CONCAT(r.guid ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_guid, GROUP_CONCAT(rn.releases_id ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_nfoid, GROUP_CONCAT(g.name ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_grpname, GROUP_CONCAT(r.searchname ORDER BY r.postdate DESC SEPARATOR '#') AS grp_release_name, GROUP_CONCAT(r.postdate ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_postdate, GROUP_CONCAT(r.adddate ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_adddate, GROUP_CONCAT(r.size ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_size, GROUP_CONCAT(r.totalpart ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_totalparts, GROUP_CONCAT(r.comments ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_comments, GROUP_CONCAT(r.grabs ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_grabs, GROUP_CONCAT(df.failed ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_failed, GROUP_CONCAT(cp.title, ' > ', c.title ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_catname, GROUP_CONCAT(r.fromname ORDER BY r.postdate DESC SEPARATOR ',') AS grp_release_fromname, con.*, r.consoleinfo_id, g.name AS group_name, genres.title AS genre, rn.releases_id AS nfoid FROM releases r LEFT OUTER JOIN usenet_groups g ON g.id = r.groups_id LEFT OUTER JOIN release_nfos rn ON rn.releases_id = r.id LEFT OUTER JOIN dnzb_failures df ON df.release_id = r.id LEFT OUTER JOIN categories c ON c.id = r.categories_id LEFT OUTER JOIN root_categories cp ON cp.id = c.root_categories_id INNER JOIN consoleinfo con ON con.id = r.consoleinfo_id INNER JOIN genres ON con.genres_id = genres.id WHERE con.id IN (%s) AND r.id IN (%s) %s GROUP BY con.id ORDER BY %s %s", (! empty($consoleIDs) ? implode(',', $consoleIDs) : -1), (! empty($releaseIDs) ? implode(',', $releaseIDs) : -1), $catsrch, $order[0], $order[1] ); $return = Cache::get(md5($sql.$page)); if ($return !== null) { return $return; } $return = DB::select($sql); if (\count($return) > 0) { $return[0]->_totalcount = $consoles['total'][0]->total ?? 0; } $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); Cache::put(md5($sql.$page), $return, $expiresAt); return $return; } /** * Get console order array. */ public function getConsoleOrder(string $orderBy): array { $order = ($orderBy === '') ? 'r.postdate' : $orderBy; $orderArr = explode('_', $order); $orderfield = match ($orderArr[0]) { 'title' => 'con.title', 'platform' => 'con.platform', 'releasedate' => 'con.releasedate', 'genre' => 'con.genres_id', 'size' => 'r.size', 'files' => 'r.totalpart', 'stats' => 'r.grabs', default => 'r.postdate', }; $ordersort = (isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1])) ? $orderArr[1] : 'desc'; return [$orderfield, $ordersort]; } /** * Get console ordering options. */ public function getConsoleOrdering(): array { return [ 'title_asc', 'title_desc', 'posted_asc', 'posted_desc', 'size_asc', 'size_desc', 'files_asc', 'files_desc', 'stats_asc', 'stats_desc', 'platform_asc', 'platform_desc', 'releasedate_asc', 'releasedate_desc', 'genre_asc', 'genre_desc', ]; } /** * Get browse by options. */ public function getBrowseByOptions(): array { return ['platform' => 'platform', 'title' => 'title', 'genre' => 'genres_id']; } /** * Get browse by SQL clause. */ public function getBrowseBy(): string { $browseBy = ' '; foreach ($this->getBrowseByOptions() as $bbk => $bbv) { if (! empty($_REQUEST[$bbk])) { $bbs = stripslashes($_REQUEST[$bbk]); $browseBy .= ' AND con.'.$bbv.' LIKE '.escapeString('%'.$bbs.'%'); } } return $browseBy; } // ======================================== // Update Methods // ======================================== /** * Update console info record. */ public function update( int $id, string $title, ?string $asin, ?string $url, ?int $salesrank, ?string $platform, ?string $publisher, ?string $releasedate, ?string $esrb, int $cover, ?int $genresId, string $review = 'review' ): void { $releasedate = $releasedate !== '' ? $releasedate : null; $review = $review === 'review' ? $review : substr($review, 0, 3000); ConsoleInfo::query() ->where('id', $id) ->update([ 'title' => $title, 'asin' => $asin, 'url' => $url, 'salesrank' => $salesrank, 'platform' => $platform, 'publisher' => $publisher, 'releasedate' => $releasedate, 'esrb' => $esrb, 'cover' => $cover, 'genres_id' => $genresId, 'review' => $review, ]); } // ======================================== // IGDB Integration Methods // ======================================== /** * Update console info from IGDB. * * * @throws \Exception */ public function updateConsoleInfo(array $gameInfo): int { $consoleId = self::CONS_NTFND; $igdb = $this->fetchIGDBProperties($gameInfo['title'], $gameInfo['node']); if ($igdb !== false) { if ($igdb['coverurl'] !== '') { $igdb['cover'] = 1; } else { $igdb['cover'] = 0; } $consoleId = $this->updateConsoleTable($igdb); if ($this->echoOutput && $consoleId !== -2) { cli()->header('Added/updated game: '). cli()->alternateOver(' Title: '). cli()->primary($igdb['title']). cli()->alternateOver(' Platform: '). cli()->primary($igdb['platform']). cli()->alternateOver(' Genre: '). cli()->primary($igdb['consolegenre']); } } return $consoleId; } /** * Fetch IGDB properties for a game. * * @throws \Exception */ public function fetchIGDBProperties(string $gameInfo, string $gamePlatform): bool|array|\StdClass { $bestMatch = false; $gamePlatform = $this->replacePlatform($gamePlatform); if (config('config.credentials.client_id') !== '' && config('config.credentials.client_secret') !== '') { try { $result = Game::where('name', $gameInfo)->get(); if (! empty($result)) { $bestMatchPct = 0; foreach ($result as $res) { similar_text(strtolower($gameInfo), strtolower($res->name), $percent); if ($percent >= 90 && $percent > $bestMatchPct) { $bestMatch = $res->id; $bestMatchPct = $percent; } } if ($bestMatch !== false) { $game = Game::with([ 'cover' => ['url'], 'screenshots' => ['url'], 'involved_companies' => ['company', 'publisher'], 'themes', ])->where('id', $bestMatch)->first(); $publishers = []; if (! empty($game->involved_companies)) { foreach ($game->involved_companies as $publisher) { if ($publisher['publisher'] === true) { $company = Company::find($publisher['company']); $publishers[] = $company['name']; } } } $genres = []; if (! empty($game->themes)) { foreach ($game->themes as $theme) { $genres[] = $theme['name']; } } $genreKey = $this->getGenreKey(implode(',', $genres)); $platform = ''; if (! empty($game->platforms)) { foreach ($game->platforms as $platforms) { $percentCurrent = 0; $gamePlatforms = Platform::where('id', $platforms)->get(); foreach ($gamePlatforms as $gamePlat) { similar_text($gamePlat['name'], $gamePlatform, $percent); if ($percent >= 85 && $percent > $percentCurrent) { $percentCurrent = $percent; $platform = $gamePlat['name']; break; } } } } return [ 'title' => $game->name, 'asin' => (string) $game->id, 'review' => $game->summary ?? '', 'coverurl' => ! empty($game->cover->url) ? 'https:'.$game->cover->url : '', 'releasedate' => ! empty($game->first_release_date) ? $game->first_release_date->format('Y-m-d') : now()->format('Y-m-d'), 'esrb' => ! empty($game->aggregated_rating) ? round($game->aggregated_rating).'%' : 'Not Rated', 'url' => $game->url ?? '', 'publisher' => ! empty($publishers) ? implode(',', $publishers) : 'Unknown', 'platform' => $platform ?? '', 'consolegenre' => ! empty($genres) ? implode(',', $genres) : 'Unknown', 'consolegenreid' => $genreKey, 'salesrank' => '', ]; } cli()->notice('IGDB returned no valid results'); return false; } cli()->notice('IGDB found no valid results'); return false; } catch (ClientException $e) { if ($e->getCode() === 429) { $this->igdbSleep = now()->endOfMonth(); } } catch (\Exception $e) { cli()->error('Error fetching IGDB properties: '.$e->getMessage()); return false; } } return false; } // ======================================== // Release Processing Methods // ======================================== /** * Process console releases. * * @throws \Exception */ public function processConsoleReleases(): void { $query = Release::query() ->select(['searchname', 'id']) ->whereBetween('categories_id', [Category::GAME_ROOT, Category::GAME_OTHER]) ->whereNull('consoleinfo_id'); if ($this->renamed === true) { $query->where('isrenamed', '=', 1); } $res = $query->limit($this->gameQty)->orderBy('postdate')->get(); $releaseCount = $res->count(); if ($res instanceof \Traversable && $releaseCount > 0) { if ($this->echoOutput) { cli()->header('Processing '.$releaseCount.' console release(s).'); } foreach ($res as $arr) { $startTime = now()->timestamp; $usedAmazon = false; $gameId = self::CONS_NTFND; $gameInfo = $this->parseTitle($arr['searchname']); if ($gameInfo !== false) { if ($this->echoOutput) { cli()->info('Looking up: '.$gameInfo['title'].' ('.$gameInfo['platform'].')'); } // Check for existing console entry. $gameCheck = $this->getConsoleInfoByName($gameInfo['title'], $gameInfo['platform']); if ($gameCheck === false && \in_array($gameInfo['title'].$gameInfo['platform'], $this->failCache, false)) { // Lookup recently failed, no point trying again if ($this->echoOutput) { cli()->info('Cached previous failure. Skipping.'); } $gameId = -2; } elseif ($gameCheck === false) { $gameId = $this->updateConsoleInfo($gameInfo); $usedAmazon = true; if ($gameId === self::CONS_NTFND) { $this->failCache[] = $gameInfo['title'].$gameInfo['platform']; } } else { if ($this->echoOutput) { cli()->headerOver('Found Local: '). cli()->primary("{$gameInfo['title']} - {$gameInfo['platform']}"); } $gameId = $gameCheck['id'] ?? -2; } } elseif ($this->echoOutput) { echo '.'; } // Update release. Release::query()->where('id', $arr['id'])->update(['consoleinfo_id' => $gameId]); // Sleep to not flood amazon. $diff = floor((now()->timestamp - $startTime) * 1000000); if ($this->sleepTime * 1000 - $diff > 0 && $usedAmazon === true) { usleep((int) ($this->sleepTime * 1000 - $diff)); } } } elseif ($this->echoOutput) { cli()->header('No console releases to process.'); } } // ======================================== // Title Parsing Methods // ======================================== /** * Parse release title for game info. */ public function parseTitle(string $releaseName): array|false { $releaseName = preg_replace('/\sMulti\d?\s/i', '', $releaseName); $result = []; // Get name of the game from name of release. if (preg_match('/^(.+((abgx360EFNet|EFNet\sFULL|FULL\sabgxEFNet|abgx\sFULL|abgxbox360EFNet)\s|illuminatenboard\sorg|Place2(hom|us)e.net|united-forums? co uk|\(\d+\)))?(?P