Archived
1
0
Fork 0
This repository has been archived on 2021-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
ifnews/app/Commands/Freshlist.php

132 lines
3.2 KiB
PHP

<?php
namespace App\Commands;
use LaravelZero\Framework\Commands\Command;
use Log;
use App\Models\Game;
use App\Models\Language;
class Freshlist extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'freshlist {keyword?}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Print new games';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$now = date('Y-m-d');
$week_ago = date('Y-m-d 00:00:00', strtotime('1 week ago'));
$games = Game::with(['authors', 'tags'])
->where('release_date', '<=', $now)
->where('release_date', '>', $week_ago)
->orderBy('source')
->get();
$gamebooks = [];
$quests_ru = [];
$quests_en = [];
$visualnovels = [];
$ru = Language::findByCode('ru');
foreach ($games as $game) {
$tags = $game->tags->pluck('title',' id')->toArray();
$tags = array_values($tags);
$tags = array_map('strtolower', $tags);
if (array_intersect([
'adult',
'xxx',
'18+',
'porn',
'porngame',
'pointnclick',
'lewd',
'erotic',
'nsfw',
'bitsy',
'vr',
], $tags) !== []) {
continue;
}
if (strpos($game->title, 'PDF') !== false) {
$gamebooks[] = $game;
}
else if ($game->source === 'Anivisual') {
$visualnovels[] = $game;
} else if ($game->languages->where('id', $ru->id)->first()) {
$quests_ru[] = $game;
} else {
if (empty($quests_en[$game->source])) {
$quests_en[$game->source] = [];
}
$quests_en[$game->source][] = $game;
}
}
if (!empty($gamebooks)) {
$this->printList('Книги-игры', $gamebooks);
}
if (!empty($visualnovels)) {
$this->printList('Визуальные новеллы', $visualnovels);
}
if (!empty($quests_ru)) {
$this->printList('Текстовые квесты', $quests_ru);
}
if (!empty($quests_en)) {
foreach ($quests_en as $source => $list) {
$this->printSpoiler($source, $list);
}
}
}
protected function printSpoiler($title, $list) {
echo '<spoiler title="'.$title.'">';
$this->printList($title, $list);
echo '</spoiler>';
}
protected function printList($title, $list) {
echo '<h4>'.$title.'</h4><ul>';
foreach ($list as $game) {
$this->printGame($game);
}
echo '</ul>'.PHP_EOL;
}
protected function printGame($game) {
echo '<li>';
$authors = $game->authors->toArray();
if (!empty($authors)) {
echo '<em>';
echo implode(', ', array_map(function($model) {
return trim($model['name']);
}, $authors));
echo '</em> — ';
}
echo "<a rel='nofollow' target='_blank' href='".trim($game->url)."'>«".htmlentities(trim($game->title))."»</a>";
$description = $game->short_description;
if (empty($description)) {
$description = $game->description;
}
if (!empty($description)) {
echo PHP_EOL.'<blockquote>'.$game->normalizeDescription($description).'</blockquote>';
}
echo '</li>'.PHP_EOL;
}
}