You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
9.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use App\Http\Controllers\IGDBController;
use App\Models\Game;
class GamesController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$games = Game::all();
return view('games.index', compact('games'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('games.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
// Validate the form data
//$validatedData = $request->validate([
// 'name' => 'required|max:255',
// 'description' => 'required',
//'price' => 'required|numeric',
//'release_date' => 'required|date',
//]);
$game = new Game;
$game->fillGameDetails($request->game_slug);
#ddd($game);
//$game->price = $request->price;
//$game->release_date = $request->release_date;
$game->save();
// Redirect the user to the games index page with a success message
return redirect()->route('games.index')->with('success', 'Game added and linked successfully.');
}
/**
* Display the specified resource.
*
* @param string slug
* @return \Illuminate\Http\Response
*/
public function show(string $slug)
{
$game = IGDBController::getGameDetails($slug);
return view('games.show', [
'game' => $this->formatGameForView($game),
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
public function index_old()
{
$before = Carbon::now()->subMonths(2)->timestamp;
$current = Carbon::now()->timestamp;
$after = Carbon::now()->addMonths(2)->timestamp;
$afterFourMonths = Carbon::now()->addMonths(4)->timestamp;
/*$popularGames = Http::withHeaders(config('services.igdb'))->withBody(
"fields name, cover.url, first_release_date, total_rating_count, platforms.abbreviation, rating, rating_count, slug;
where platforms = (48,49,130,6)
& (first_release_date >= {$before}
& first_release_date < {$after});
& total_rating_count > 5;
sort total_rating_count desc;
limit 12;", "text/plain"
)->post('https://api.igdb.com/v4/games')
->json();
*/
$multiQuery = Cache::remember('multiQuery-games', 7, function () use ($before, $after, $current, $afterFourMonths){
return Http::withHeaders(config('services.igdb'))->withBody(
'query games "popularGames" {
fields name, cover.url, release_date.human, first_release_date, total_rating_count, platforms.abbreviation, rating, rating_count, slug;
where platforms = (48,49,130,6)
& (first_release_date >= '.$before.'
& first_release_date < '.$after.')
& total_rating_count > 5;
sort total_rating_count desc;
limit 12;
};
query games "recentlyReviewed" {
fields name, cover.url, first_release_date, total_rating_count, platforms.abbreviation, rating, rating_count, summary, slug;
where platforms = (48,49,130,6)
& (first_release_date >= '.$before.'
& first_release_date < '.$current.'
& rating_count > 5)
& total_rating_count > 5;
sort total_rating_count desc;
limit 3;
};
query games "mostAnticipated" {
fields name, cover.url, first_release_date, total_rating_count, platforms.abbreviation, rating, rating_count, summary, slug;
where platforms = (48,49,130,6)
& (first_release_date >= '.$current.'
& first_release_date < '.$afterFourMonths.');
sort total_rating_count desc;
limit 4;
};
query games "comingSoon" {
fields name, cover.url, first_release_date, total_rating_count, platforms.abbreviation, rating, rating_count, summary, slug;
where platforms = (48,49,130,6)
& first_release_date >= '.$current.';
sort first_release_date asc;
limit 4;
};
'
)->post('https://api.igdb.com/v4/multiquery')
->json();
});
#dd($multiQuery);
foreach($multiQuery as $query){
if ($query['name'] == 'popularGames'){
$popularGames = $query['result'];
continue;
}
if ($query['name'] == 'recentlyReviewed'){
$recentlyReviewed = $query['result'];
continue;
}
if ($query['name'] == 'mostAnticipated'){
$mostAnticipated = $query['result'];
continue;
}
if ($query['name'] == 'comingSoon'){
$comingSoon = $query['result'];
continue;
}
}
return view('games', [
'popularGames' => $this->formatForView($popularGames),
'recentlyReviewed' => $this->formatForView($recentlyReviewed),
'mostAnticipated' => $this->formatForView($mostAnticipated),
'comingSoon' => $comingSoon
]);
}
/**
* Format Covers for use
*/
private function formatForView($games)
{
return collect($games)->map(function($game){
return collect($game)->merge([
'coverImageUrl' => isset($game['cover']) ? Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']) : null,
'rating' => isset($game['rating']) ? round($game['rating']) . '%' : null,
'platforms' => collect($game['platforms'])->pluck('abbreviation')->implode(', '),
'formated_release_date' => Carbon::parse($game['first_release_date'])->format('d, M Y')
]);
})->toArray();
}
private function formatGameForView($game)
{
return collect($game)->merge([
'coverImageUrl' => isset($game['cover']) ? Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']) : null,
'member_score' => array_key_exists('rating', $game) ? round($game['rating']): '0',
'critics_score' => array_key_exists('aggregated_rating', $game) ? round($game['aggregated_rating']) : '0',
'genres' => collect($game['genres'])->pluck('name')->implode(', '),
'formatedCompanies' => collect($game['involved_companies'])->pluck('company')->pluck('name')->implode(', '),
'platforms' => array_key_exists('platforms', $game) ? collect($game['platforms'])->pluck('abbreviation')->implode(', ') : null,
'trailer' => array_key_exists('videos', $game) ? 'https://youtube.com/embed/'.$game['videos'][0]['video_id'] : null,
'screenshots' => collect($game['screenshots'])->map(function ($screenshot){
return [
'big' => Str::replaceFirst('thumb', 'screenshot_big', $screenshot['url']),
'huge' => Str::replaceFirst('thumb', 'screenshot_huge', $screenshot['url'])
];
})->take(9),
'similarGames' => collect($game['similar_games'])->map(function($game){
return collect($game)->merge([
'coverImageUrl' => array_key_exists('cover', $game)
? Str::replaceFirst('thumb','cover_big', $game['cover']['url'])
: 'https://via.placeholder.com/264x352',
'rating' => isset($game['rating']) ? round($game['rating']) : '0',
'platforms' => array_key_exists('platforms',$game)
? collect($game['platforms'])->pluck('abbreviation')->implode(', ')
: null,
]);
})->take(6),
'social' => [
'website' => collect($game['websites'])->first(),
'facebook' => collect($game['websites'])->filter(function ($website){
return Str::contains($website['url'],'facebook');
})->first(),
'twitter' => collect($game['websites'])->filter(function ($website){
return Str::contains($website['url'],'twitter');
})->first(),
'instagram' => collect($game['websites'])->filter(function ($website){
return Str::contains($website['url'],'instagram');
})->first(),
],
'formated_release_date' => array_key_exists('first_release_date', $game)
? Carbon::parse($game['first_release_date'])->format('d, M Y')
: 'not released, yet'
])->toArray();
}
}