Skip to content

Commit

Permalink
feat: Add multiple API support.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed May 27, 2022
1 parent ab6c2d2 commit 4e68381
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 451 deletions.
16 changes: 16 additions & 0 deletions app/Apis/BasketballApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Apis;

use Illuminate\Support\Collection;

class BasketballApi extends LiveScoresApi
{
public function makeRequest(): Collection
{
return $this->client()
->get('/basketball/1.00?MD=1')
->throw()
->collect();
}
}
16 changes: 16 additions & 0 deletions app/Apis/FootballApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Apis;

use Illuminate\Support\Collection;

class FootballApi extends LiveScoresApi
{
public function makeRequest(): Collection
{
return $this->client()
->get('/soccer/1.00?MD=1')
->throw()
->collect();
}
}
53 changes: 53 additions & 0 deletions app/Apis/LiveScoresApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Apis;

use App\Contracts\ScoresApi;
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Collection;

abstract class LiveScoresApi implements ScoresApi
{
public function __construct(
private Factory $client = new Factory(),
private string $baseUrl = 'https://prod-public-api.livescore.com/v1/api/app/live',
) {
// ..
}

public function fetch(): Collection
{
return collect($this->makeRequest()->get('Stages'))->map(fn ($league) => [
'category' => $league['Cnm'],
'stage' => $league['Snm'],
'games' => collect($league['Events'])->map(fn ($event) => [
'time' => $event['Eps'],
'team_1' => [
'name' => $event['T1']['0']['Nm'],
'score' => $event['Tr1'] ?? '0',
],
'team_2' => [
'name' => $event['T2']['0']['Nm'],
'score' => $event['Tr2'] ?? '0',
],
]),
]);
}

public function makeRequest(): Collection
{
return $this->client()
->get('/soccer/1.00?MD=1')
->throw()
->collect();
}

protected function client(): PendingRequest
{
return $this->client
->baseUrl($this->baseUrl)
->asJson()
->acceptJson();
}
}
225 changes: 5 additions & 220 deletions app/Commands/GamesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,233 +2,18 @@

namespace App\Commands;

use App\Lib\LiveFootball\GamesRepository;
use App\Scores;
use LaravelZero\Framework\Commands\Command;
use function Termwind\live;
use function Termwind\select;
use function Termwind\terminal;

class GamesCommand extends Command
{
protected $signature = 'games';

public function handle(GamesRepository $gamesRepository)
public function handle(Scores $scores)
{
live(function () use ($gamesRepository) {
$games = $gamesRepository->fetch();

return view('games', [
'leagues' => $games['Stages'],
'active' => null,
]);
})->refreshEvery(seconds: 20);












return;
$select = select(fn ($options, $active) => view('games', [
'leagues' => $this->getAvailableGames(),
'active' => $active,
]), $this->getAvailableGames(true));

$select->refreshEvery(seconds: 1);
$select->render();

$active = $select->getActive();

terminal()->clear();

live(fn () => view('game', [
'timeLabel' => '1st half',
'time' => date('i:s'),
'score' => $this->getScores(),
'events' => $this->getEvents(),
]))->refreshEvery(seconds: 1);
}

private function getAvailableGames($onlyGames = false): array
{
$leagues = [[
'country' => 'Italy',
'name' => 'Serie A',
'games' => [[
'value' => 1,
'time' => now()->subMinutes(87)->subSeconds(12)->format('i:s'),
'home' => [
'team' => 'Juventus',
'total' => 0,
],
'away' => [
'team' => 'Cagliari',
'total' => 0,
],
]],
], [
'country' => 'Netherlands',
'name' => 'Eredivisie',
'games' => [[
'value' => 3,
'time' => now()->subMinutes(42)->subSeconds(21)->format('i:s'),
'home' => [
'team' => 'AZ Alkmaar',
'total' => 0,
],
'away' => [
'team' => 'Groningen',
'total' => 0,
],
]],
], [
'country' => 'Portugal',
'name' => 'Liga Bwin',
'games' => [[
'value' => 2,
'time' => now()->format('i:s'),
'home' => [
'team' => 'SL Benfica',
'total' => 3,
],
'away' => [
'team' => 'FC Porto',
'total' => 1,
],
]],
]];

if (! $onlyGames) {
return $leagues;
}

return collect($leagues)->pluck('games')->flatten(1)->toArray();
}

private function getScores(): array
{
return [
'home' => [
'team' => 'SL Benfica',
'total' => 3,
],
'away' => [
'team' => 'FC Porto',
'total' => 1,
],
];
}

private function getEvents(): array
{
return [[
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 16,
'player' => 'Dias G.',
], [
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 24,
'player' => 'Ramos G.',
], [
'team' => 'home',
'type' => 'goal',
'minute' => 28,
'player' => 'Ramos G.',
], [
'team' => 'away',
'type' => 'card',
'card' => 'yellow',
'minute' => 30,
'player' => 'Grujic M.',
], [
'team' => 'away',
'type' => 'card',
'card' => 'yellow',
'minute' => 40,
'player' => 'Pepe',
], [
'team' => 'home',
'type' => 'goal',
'minute' => 46,
'player' => 'Nunes D.',
'extra' => '(Penalti)',
], [
'type' => 'halftime',
], [
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 53,
'player' => 'Nunez D.',
], [
'team' => 'away',
'type' => 'sub',
'minute' => 61,
'out' => 'Seferovic H.',
'in' => 'Dias G.',
], [
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 62,
'player' => 'Lazaro V.',
], [
'team' => 'home',
'type' => 'goal',
'minute' => 63,
'player' => 'Ramos G.',
], [
'team' => 'away',
'type' => 'sub',
'minute' => 64,
'out' => 'Galeno',
'in' => 'Evanilson',
], [
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 71,
'player' => 'Almeida A.',
], [
'team' => 'home',
'type' => 'card',
'card' => 'yellow',
'minute' => 71,
'player' => 'Otamendi N.',
], [
'team' => 'home',
'type' => 'sub',
'minute' => 73,
'out' => 'Lazaro V.',
'in' => 'João Mário',
], [
'team' => 'home',
'type' => 'sub',
'minute' => 73,
'out' => 'Ramos G.',
'in' => 'Yaremchuk R.',
], [
'team' => 'away',
'type' => 'card',
'card' => 'yellow',
'minute' => 79,
'player' => 'Taremi M.',
], [
'team' => 'away',
'type' => 'goal',
'minute' => 95,
'player' => 'Zaidu',
'extra' => '(Pepê)',
], ];
live(fn () => view('games', [
'sports' => $scores->getScores(),
]))->refreshEvery(seconds: 20);
}
}
10 changes: 10 additions & 0 deletions app/Contracts/ScoresApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Contracts;

use Illuminate\Support\Collection;

interface ScoresApi
{
public function fetch(): Collection;
}
29 changes: 0 additions & 29 deletions app/Lib/LiveFootball/GamesRepository.php

This file was deleted.

1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use LaravelZero\Framework\Application;

class AppServiceProvider extends ServiceProvider
{
Expand Down
Loading

0 comments on commit 4e68381

Please sign in to comment.