File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/StatisticsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\CorporateIdentity;
use App\TeamStatistic;
use GoogleSearch;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
class StatisticsController extends Controller
{
private $color;
public function __construct()
{
$corporateIdentity = CorporateIdentity::first();
$this->color = $corporateIdentity->icon_color;
}
public function index()
{
$teamStatistic = TeamStatistic::select('id', 'team_name', 'team_league', 'team_season', 'api_key', 'api_key_standings', 'synchronization_frequency', 'last_load_date', 'active', 'carousel_matches', 'carousel_results')->first();
return view('statistics.statistics')->with('teamStatistic', $teamStatistic);
}
public function update(Request $request)
{
if (TeamStatistic::where('id', $request->input('id'))->first()) {
TeamStatistic::where('id', $request->input('id'))->update($request->all());
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $request->input('id')));
} else {
$model = TeamStatistic::create($request->all());
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
}
}
public function syncData($forcedSync = false, $teamStatistic = null)
{
if (!$teamStatistic)
$teamStatistic = TeamStatistic::first();
if ($teamStatistic && $teamStatistic->active && (!isset($teamStatistic->last_load_date) || $this->validateNewDataSynchronization($teamStatistic->last_load_date, $teamStatistic->synchronization_frequency) || $forcedSync)) {
$googleSearch = new GoogleSearch($teamStatistic->api_key);
$team = $teamStatistic->team_name ?? config('app.client');
$queryMatches = [
"q" => $team . " partidos",
"location" => "Colombia",
"hl" => "es",
"gl" => "co",
];
$resultMatches = $googleSearch->get_json($queryMatches);
$teamStatistic->query_matches = json_encode($queryMatches);
if (isset($resultMatches->sports_results))
$teamStatistic->result_matches = json_encode($resultMatches);
else
return response()->json(array('r' => false, 'm' => 'No se encontraron datos sincronizados!'), 200);
if ($forcedSync)
$teamStatistic->result_standings = null;
$teamStatistic->last_load_date = Carbon::now();
$teamStatistic->update();
$this->clearCache();
return response()->json(array('r' => true, 'm' => 'Datos sincronizados con éxito!'), 200);
}
}
public function syncDataApiFootball()
{
$data = new \stdClass();
$teamStatistic = TeamStatistic::select('team_league', 'team_season', 'api_key_standings')->first();
$data->key = $teamStatistic->api_key_standings;
$data->league = $teamStatistic->team_league;
$data->season = $teamStatistic->team_season;
return view('statistics.api-football-default', compact('data'));
}
public function saveDataApiFootball(Request $request)
{
if ($request->input('result_standings')) {
$teamStatistic = TeamStatistic::first();
$teamStatistic->result_standings = $request->input('result_standings');
$teamStatistic->last_load_date = Carbon::now();
$teamStatistic->update();
$this->clearCache();
}
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $request->input('id')));
}
public function showData($type = 'all', $color = null)
{
$color = $color ? '#' . $color : $this->color;
if (!Cache::get('showData-' . $type)) {
$data = new \stdClass();
$teamStatistic = TeamStatistic::first();
if ($teamStatistic) {
$data->carousel_matches = $teamStatistic->carousel_matches;
$data->carousel_results = $teamStatistic->carousel_results;
if (!$data->carousel_matches && !$data->carousel_results) {
$resultMatches = json_decode($teamStatistic->result_matches);
if (isset($resultMatches)) {
// RESUMEN PARTIDOS
if (isset($resultMatches->sports_results->games)) {
$this->validateMatches($data, $resultMatches->sports_results->games, true);
}
// PARTIDO IMPORTANTE O ACTUAL
if (isset($resultMatches->sports_results->game_spotlight))
$this->validateMatches($data, [$resultMatches->sports_results->game_spotlight], false);
}
if (isset($resultMatches->sports_results->game_spotlight))
$data->matches = array_merge($data->previous_matches ?? [], $data->next_matches ?? []);
else
$data->matches = array_merge($data->next_matches ?? [], $data->previous_matches ?? []);
// TABLA DE RESULTADOS
if (isset($teamStatistic->result_standings))
$data->standings = $teamStatistic->result_standings;
}
}
$data->type = $type;
Cache::put('showData-' . $type, $data, config('cache.time'));
} else {
$data = Cache::get('showData-' . $type);
}
$view = 'statistics.results';
if (isset($type) && ($type == 'all' || $type == 'standings' || $type == 'standing')) {
$view = 'statistics.results-carousel';
}
return view($view, compact('data', 'color'));
}
private function validateNewDataSynchronization($datetime, $synchronizationFrequency)
{
$date = Carbon::parse($datetime);
$now = Carbon::now();
$diff = $date->diffInMinutes($now);
return $diff >= $synchronizationFrequency;
}
private function validateMatches($data, $matches, $reset = true)
{
$nextMatches = $reset ? array() : $data->next_matches ?? array();
$previousMatches = $reset ? array() : $data->previous_matches ?? array();
foreach ($matches as $match) {
$this->validateMatchFields($match);
$finalized = false;
if (isset($match->status) || isset($match->teams[0]->score))
$finalized = true;
if (!$finalized && $reset) {
if (!isset($data->next_match)) {
$data->next_match = $match;
}
$nextMatches[] = $match;
} else {
if (!isset($data->last_match)) {
$data->last_match = $match;
}
$previousMatches[] = $match;
}
}
if (count($nextMatches) == 0 && count($previousMatches) > 0) {
$lastMatch = $previousMatches[0];
$lastMatch->last = true;
$nextMatches[] = $lastMatch;
} else if (count($previousMatches) == 0 && count($nextMatches) > 0 && !$reset) {
foreach ($nextMatches as $match) {
$match->next = true;
}
$nextMatch = $nextMatches[0];
$previousMatches = $nextMatches;
$nextMatches = array();
$nextMatches[] = $nextMatch;
}
$data->next_matches = $nextMatches;
$data->previous_matches = $previousMatches;
}
private function validateMatchFields($match)
{
if (!isset($match->stadium) && isset($match->stadion))
$match->stadium = $match->stadion;
if (isset($match->stadium) && !str_contains($match->stadium, 'Estadio'))
$match->stadium = 'Estadio ' . $match->stadium;
if (!isset($match->tournament) && isset($match->league))
$match->tournament = $match->league;
// MAPEO ESTADO
if (isset($match->status) && in_array($match->status, ['FT', 'Fin']))
$match->status = 'Finalizado';
if (!isset($match->status) && isset($match->stage) && in_array($match->stage, ['Full-time', 'Finalizado'])) {
$match->status = 'Finalizado';
$match->stage = '';
}
// MAPEO PARTIDO
if (isset($match->stage)) {
if (str_contains($match->stage, 'Matchday'))
$match->stage = str_replace('Matchday', 'Partido', $match->stage);
if (str_contains($match->stage, 'of'))
$match->stage = str_replace('of', 'de', $match->stage);
if (str_contains($match->stage, 'Leg'))
$match->stage = str_replace('Leg', 'Ronda', $match->stage);
}
// MAPEO DIAS
if (isset($match->date)) {
switch ($match->date) {
case str_contains($match->date, 'Sun'):
$match->date = str_replace('Sun', 'Domingo', $match->date);
break;
case str_contains($match->date, 'Sat'):
$match->date = str_replace('Sat', 'Sábado', $match->date);
break;
case str_contains($match->date, 'Fri'):
$match->date = str_replace('Fri', 'Viernes', $match->date);
break;
case str_contains($match->date, 'Thu'):
$match->date = str_replace('Thu', 'Jueves', $match->date);
break;
case str_contains($match->date, 'Wed'):
$match->date = str_replace('Wed', 'Miércoles', $match->date);
break;
case str_contains($match->date, 'Tue'):
$match->date = str_replace('Tue', 'Martes', $match->date);
break;
case str_contains($match->date, 'Mon'):
$match->date = str_replace('Mon', 'Lunes', $match->date);
break;
case str_contains($match->date, 'Yesterday'):
$match->date = str_replace('Yesterday', 'Ayer', $match->date);
break;
case str_contains($match->date, 'Today'):
$match->date = str_replace('Today', 'Hoy', $match->date);
break;
}
}
}
private function clearCache()
{
Cache::forget('showData-all');
Cache::forget('showData-only');
Cache::forget('showData-matches');
Cache::forget('showData-standing');
Cache::forget('showData-standings');
}
}