This commit is contained in:
salam committed 2026-10-04 15:25:03 +02:00
1 parent 9bcd4432c6
commit 6f92f9e1d5
1209 files changed
+220940

No files matched your search

@@ -0,0 +1,30 @@
<?php
namespace App\Filament\Widgets\Charts;
class HelperMoveingAverage
{
public static function get(&$data)
{
if (! $data) {
return;
}
$data['labels'] = array_slice($data['labels'], 2);
$datasets = $data['datasets'];
foreach ($datasets as $k => $dataset) {
$arr = $dataset['data'];
$new = [];
$last = count($arr);
foreach ($arr as $key => $value) {
if ($key == 1 && $last > 2) {
$new[] = round(((float) $arr[0] + (float) $value + (float) $arr[2]) / 3, 1);
} elseif ($key > 1 && $key < $last - 1) {
$new[] = round(((float) $arr[$key - 1] + (float) $value + (float) $arr[$key + 1]) / 3, 1);
}
}
$data['datasets'][$k]['data'] = $new;
}
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Filament\Widgets\Charts;
class HelperMoveingAveragePerformens
{
public static function get(&$data)
{
if (! $data) {
return;
}
$data['labels'] = array_slice($data['labels'], 2);
$datasets = $data['datasets'];
foreach ($datasets as $k => $dataset) {
$arr = $dataset['data'];
// dd($arr);
$new = [];
$last = count($arr);
foreach ($arr as $key => $value) {
if ($key == 1 && $last > 2) {
$new[] = round(($arr[0] + $value + $arr[2]) / 3, 2);
} elseif ($key > 1 && $key < $last - 1) {
$new[] = round(($arr[$key - 1] + $value + $arr[$key + 1]) / 3, 2);
}
}
$data['datasets'][$k]['data'] = $new;
}
}
public static function fillHoles(array &$arr)
{
if (! $arr) {
return;
}
if (! $arr[0]) {
// get first value in array and copy it to start if start is null
$arr[0] = current(array_filter($arr));
}
$priv = 0;
foreach ($arr as $key => $value) {
if ($value) {
$priv = $value;
} else {
if ($priv) {
$arr[$key] = $priv;
}
}
}
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Filament\Widgets\Charts;
class HelperStatisticType
{
protected static array $types = [
'gls' => 'Goals',
'crn' => 'Corner',
'fls' => 'Fouls Commited',
'ofs' => 'Offside',
'crd' => 'Cards',
'snt' => 'Shots on Target',
'tsh' => 'Total Shots',
];
public static function get($filter): string
{
$tmp = $filter['statistic_type'] ?? 'none';
if (isset(self::$types[$tmp])) {
return self::$types[$tmp];
}
return 'Goals';
}
}
+440
View File
@@ -0,0 +1,440 @@
<?php
namespace App\Filament\Widgets\Charts;
use App\Filament\Widgets\Charts\LoadEvents\LoadEventsAverages;
use App\Filament\Widgets\Charts\LoadEvents\LoadEventsQuery;
use App\Filament\Widgets\Charts\LoadEvents\LoadExpectations;
use App\Models\StatisticFixtures;
use Illuminate\Support\Carbon;
class LoadEvents
{
public static function load(array $filter, bool $hasdots)
{
$league_id = (int) $filter['league_id'] ?? 0;
$match_id = (int) $filter['match_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$statistic_type = $filter['statistic_type'] ?? 'gls';
$team_position = $filter['team_position'];
$seasonmode = $filter['seasonmode'];
$accuracy = (float) $filter['accuracy'] ?? 0.5;
$similar_past_matches = $filter['similar_past_matches'];
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
/**
* IF WITH SIMILAR_PAST_MATCHES
*
* 'none' => 'None',
* 'league' => 'League',
* 'all' => 'All Database',
*/
$handicap_range = [];
$total_range = [];
$match = StatisticFixtures::with('home')->with('away')->where('id', (int) $match_id)->first();
// dd($match);
if (! $match) {
return [];
}
if ($similar_past_matches != 'none') {
$team_id = 0;
$team_position = '';
$exp_handicap = $match->exp_away - $match->exp_home;
$exp_total = $match->exp_away + $match->exp_home;
$handicap_range = [$exp_handicap - $accuracy, $exp_handicap + $accuracy];
$total_range = [$exp_total - $accuracy, $exp_total + $accuracy];
// dd($exp_handicap,$exp_total, $handicap_range, $total_range);
$home_team = $match->home->ss_team_name;
$away_team = $match->away->ss_team_name;
} else {
$home_team = 'In Favour';
$away_team = 'Against';
}
/**
* STATTISTIC DATA
*/
$res = LoadEventsQuery::load($filter, $handicap_range, $total_range, $match);
$dataset_home = [];
$dataset_away = [];
$dataset_exp_home = [];
$dataset_exp_away = [];
$results_per_event = [];
$playing_date = [];
$labels = [];
$events = [];
$results = ['w' => 0, 'd' => 0, 'l' => 0, 'w2' => 0, 'l2' => 0];
if (! $team_id || ! \in_array($team_position, ['home', 'away'])) {
$team_position = 'all';
}
if (! isset($res['all']) || ! isset($res[$team_position])) {
return [];
}
$expectations = LoadExpectations::load($res['all'], $match, $statistic_type);
$label1 = '';
$label2 = '';
$label3 = '';
$label4 = '';
if ($team_id) {
foreach ($res[$team_position] as $value) {
$labels[] = round($value['starttime']->diffInDays(Carbon::now()));
$playing_date[] = $value['starttime']->format('Y-m-d H:i');
$results_per_event[] = $value['val_home'].' : '.$value['val_away'];
$val_home = (int) $value['val_home'];
$val_away = (int) $value['val_away'];
if ($similar_past_matches != 'none') {
switch ($value['result_code']) {
case '1':
$results['w']++;
$results['l2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['l']++;
$results['w2']++;
break;
}
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
}
} else {
if ($team_position == 'home') {
switch ($value['result_code']) {
case '1':
$results['w']++;
$results['l2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['l']++;
$results['w2']++;
break;
}
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
}
} elseif ($team_position == 'away') {
switch ($value['result_code']) {
case '1':
$results['l']++;
$results['w2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['w']++;
$results['l2']++;
break;
}
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
}
} else {
// dd($team_id, $value['home_id'], $value['away_id']);
switch ($value['result_code']) {
case '1':
if ($team_id == $value['home_id']) {
$results['l2']++;
$results['w']++;
} else {
$results['w2']++;
$results['l']++;
}
break;
case 'X':
$results['d']++;
break;
case '2':
if ($team_id == $value['home_id']) {
$results['l']++;
$results['w2']++;
} else {
$results['l2']++;
$results['w']++;
}
break;
}
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
}
}
}
if (isset($val_home) && isset($val_away)) {
$events[] = $value['home'].' vs '.$value['away'];
if ($team_id == $value['home_id']) {
$dataset_home[] = $val_home;
$dataset_away[] = $val_away;
} elseif ($team_id == $value['away_id']) {
$dataset_home[] = $val_away;
$dataset_away[] = $val_home;
}
// $dataset_home[] = $val_home;
// $dataset_away[] = $val_away;
}
}
if ($similar_past_matches != 'none') {
$label1 = 'Home';
$label2 = 'Away';
} else {
if ($team_id == $match->home->ss_team_id) {
$label1 = $match->home->ss_team_name;
} else {
$label1 = $match->away->ss_team_name;
}
$label2 = 'Opponent';
}
} else {
$label1 = $match->home->ss_team_name.' +';
$label2 = $match->home->ss_team_name.' -';
$label3 = $match->away->ss_team_name.' +';
$label4 = $match->away->ss_team_name.' -';
$dataset_home2 = [];
$dataset_away2 = [];
$home_id = $match->home_id;
$away_id = $match->away_id;
foreach ($res[$team_position] as $value) {
$labels[] = round($value['starttime']->diffInDays(Carbon::now()));
switch ($value['result_code']) {
case '1':
$results['w']++;
$results['l2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['l']++;
$results['w2']++;
break;
}
if ($team_id) {
if ($team_id == $value['home_id']) {
$dataset_home[] = (int) $value['val_home'];
$dataset_away[] = (int) $value['val_away'];
} elseif ($team_id == $value['away_id']) {
$dataset_home[] = (int) $value['val_away'];
$dataset_away[] = (int) $value['val_home'];
}
} else {
$dataset_home[] = (int) $value['val_home'];
$dataset_away[] = (int) $value['val_away'];
}
// Home playing AWAY done and commited
if ($match->away_id == $value['home_id']) {
$dataset_home2[] = (int) $value['val_home'];
$dataset_away2[] = (int) $value['val_away'];
} else {
$dataset_home2[] = (int) $value['val_away'];
$dataset_away2[] = (int) $value['val_home'];
}
}
$n = max(count($dataset_home), count($dataset_away), count($dataset_away2), count($dataset_home2));
if ($n) {
$f = array_first($labels);
$step = round($f / $n);
$labels = [];
for ($i = $f; $i >= 0; $i -= $step) {
$labels[] = $i;
}
}
}
// dd($events, array_sum($dataset_home), array_sum($dataset_away));
// dd($events, count($dataset_home), count($dataset_away), count($dataset_home2), count($dataset_away2));
self::shortenEnding($labels, $dataset_home, $dataset_away);
// dd($dataset_home);
$data = [
'datasets' => [
[
'label' => $label1,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_home,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
'pointStyle' => $hasdots,
'fill' => [
'target' => 1,
'above' => '#fb2c3680',
'below' => '#fdc70080',
],
],
[
'label' => $label2,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_away,
'tension' => 0.5,
'borderColor' => '#fdc700',
'backgroundColor' => '#fdc700',
'pointStyle' => $hasdots,
'fill' => false,
'hidden' => true,
],
],
'labels' => $labels,
'description' => $description ?? '',
'results' => $results,
'home_team' => $home_team,
'away_team' => $away_team,
'similar_past_matches' => $similar_past_matches,
'expectations_home' => $expectations['home'],
'expectations_away' => $expectations['away'],
];
if ($team_id && $statistic_type == 'gls') {
$data['datasets'][] = [
'label' => 'Goals '.$label1,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_exp_home,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'pointStyle' => $hasdots,
'hidden' => true,
];
$data['datasets'][] = [
'label' => 'Goals '.$label2,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_exp_away,
'tension' => 0.5,
'borderColor' => '#2b7fff',
'backgroundColor' => '#2b7fff',
'pointStyle' => $hasdots,
'hidden' => true,
];
}
if (isset($dataset_home2) && isset($dataset_away2)) {
$data['datasets'][] = [
'label' => $label3,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_home2,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'pointStyle' => $hasdots,
'hidden' => true,
];
$data['datasets'][] = [
'label' => $label4,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_away2,
'tension' => 0.5,
'borderColor' => '#2b7fff',
'backgroundColor' => '#2b7fff',
'pointStyle' => $hasdots,
'hidden' => true,
];
}
// dd($data);
/**
* LEAGUE AVERAGES
*/
if ($team_id && $similar_past_matches == 'none') {
LoadEventsAverages::load($data, $filter, $labels, $events);
}
return $data;
}
public static function shortenEnding(array &$labels, array &$homes, array &$aways)
{
if (! isset($homes['all'])) {
return;
}
$count_home = 0;
$count_away = 0;
for ($i = \count($homes) - 1; $i > 0; $i--) {
if ($homes[$i] == null) {
$count_home++;
} else {
break;
}
}
for ($i = \count($aways) - 1; $i > 0; $i--) {
if ($aways[$i] == null) {
$count_away++;
} else {
break;
}
}
$keeps = count($homes['all']) - min($count_home, $count_away) - 1;
$homes = array_slice($homes, 0, $keeps);
$aways = array_slice($aways, 0, $keeps);
$labels = array_slice($labels, 0, $keeps);
}
}
@@ -0,0 +1,278 @@
<?php
namespace App\Filament\Widgets\Charts\LoadEvents;
use App\Models\StatistikoEvent;
class LoadEventsAverages
{
public static function load(array &$data, array $filter, array $labels, array $events)
{
$tournament_id = (int) $filter['league_id'] ?? 0;
$team_position = $filter['team_position'];
$team_id = (int) $filter['team_id'] ?? 0;
$statistic_type = $filter['statistic_type'] ?? 'gls';
$season = $filter['season'] ?? 0;
$season_or_date = $filter['season_or_date'] ?? 'season';
$start = $filter['start_date'] ?? 0;
$end = $filter['end_date'] ?? 0;
if ($season_or_date != 'season') {
$season = 0;
} else {
$end = 0;
$start = 0;
}
$res = StatistikoEvent::where('league_id', $tournament_id)
->when($start, function ($query) use ($start) {
return $query->whereDate('starttime', '>=', $start);
})->when($end, function ($query) use ($end) {
return $query->whereDate('starttime', '<=', $end);
})->when($season, function ($query) use ($season) {
return $query->whereIn('season', $season);
})
->orderBy('starttime', 'ASC')
->get();
// dd($res);
if ($res) {
$team_home = [];
$team_away = [];
$league_home = [];
$league_away = [];
// 1) la media dei falli di tutte le partite giocate dal Flamengo
// (esempio, giocate 6 partite con falli 17-11-15-14-15-12, la media è 14)
// 2) la media dei falli di tutte le partite giocate nel campionato Brasiliano
// (esempio 6 giornate giocate = 60 partite quindi 120 squadre, quindi 120 dati tipo 17-12-12-15-8-15-13-21-...,
// quindi la media di questi 120 dati)
foreach ($res as $value) {
switch ($statistic_type) {
case 'gls':
$league_home[] = (int) $value['gls_h'];
$league_away[] = (int) $value['gls_a'];
break;
case 'crn':
$league_home[] = (int) $value['crn_h'];
$league_away[] = (int) $value['crn_a'];
break;
case 'fls':
$league_home[] = (int) $value['fls_h'];
$league_away[] = (int) $value['fls_a'];
break;
case 'ofs':
$league_home[] = (int) $value['ofs_h'];
$league_away[] = (int) $value['ofs_a'];
break;
case 'crd':
$league_home[] = (int) $value['crd_h'];
$league_away[] = (int) $value['crd_a'];
break;
case 'snt':
$league_home[] = (int) $value['snt_h'];
$league_away[] = (int) $value['snt_a'];
break;
case 'tsh':
$league_home[] = (int) $value['tsh_h'];
$league_away[] = (int) $value['tsh_a'];
break;
}
if ((! $team_position || $team_position == 'all') && ($team_id == $value['home_id'] or $team_id == $value['away_id'])) {
switch ($statistic_type) {
case 'gls':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['gls_h'];
$team_away[] = (int) $value['gls_a'];
} else {
$team_home[] = (int) $value['gls_a'];
$team_away[] = (int) $value['gls_h'];
}
break;
case 'crn':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['crn_h'];
$team_away[] = (int) $value['crn_a'];
} else {
$team_home[] = (int) $value['crn_a'];
$team_away[] = (int) $value['crn_h'];
}
break;
case 'fls':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['fls_h'];
$team_away[] = (int) $value['fls_a'];
} else {
$team_home[] = (int) $value['fls_a'];
$team_away[] = (int) $value['fls_h'];
}
break;
case 'ofs':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['ofs_h'];
$team_away[] = (int) $value['ofs_a'];
} else {
$team_home[] = (int) $value['ofs_a'];
$team_away[] = (int) $value['ofs_h'];
}
break;
case 'crd':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['crd_h'];
$team_away[] = (int) $value['crd_a'];
} else {
$team_home[] = (int) $value['crd_a'];
$team_away[] = (int) $value['crd_h'];
}
break;
case 'snt':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['snt_h'];
$team_away[] = (int) $value['snt_a'];
} else {
$team_home[] = (int) $value['snt_a'];
$team_away[] = (int) $value['snt_h'];
}
break;
case 'tsh':
if ($team_id == $value['home_id']) {
$team_home[] = (int) $value['tsh_h'];
$team_away[] = (int) $value['tsh_a'];
} else {
$team_home[] = (int) $value['tsh_a'];
$team_away[] = (int) $value['tsh_h'];
}
break;
}
} elseif ($team_position == 'home' && $team_id == $value['home_id']) {
switch ($statistic_type) {
case 'gls':
$team_home[] = (int) $value['gls_h'];
$team_away[] = (int) $value['gls_a'];
break;
case 'crn':
$team_home[] = (int) $value['crn_h'];
$team_away[] = (int) $value['crn_a'];
break;
case 'fls':
$team_home[] = (int) $value['fls_h'];
$team_away[] = (int) $value['fls_a'];
break;
case 'ofs':
$team_home[] = (int) $value['ofs_h'];
$team_away[] = (int) $value['ofs_a'];
break;
case 'crd':
$team_home[] = (int) $value['crd_h'];
$team_away[] = (int) $value['crd_a'];
break;
case 'snt':
$team_home[] = (int) $value['snt_h'];
$team_away[] = (int) $value['snt_a'];
break;
case 'tsh':
$team_home[] = (int) $value['tsh_h'];
$team_away[] = (int) $value['tsh_a'];
break;
}
} elseif ($team_position == 'away' && $team_id == $value['away_id']) {
switch ($statistic_type) {
case 'gls':
$team_home[] = (int) $value['gls_a'];
$team_away[] = (int) $value['gls_h'];
break;
case 'crn':
$team_home[] = (int) $value['crn_a'];
$team_away[] = (int) $value['crn_h'];
break;
case 'fls':
$team_home[] = (int) $value['fls_a'];
$team_away[] = (int) $value['fls_h'];
break;
case 'ofs':
$team_home[] = (int) $value['ofs_a'];
$team_away[] = (int) $value['ofs_h'];
break;
case 'crd':
$team_home[] = (int) $value['crd_a'];
$team_away[] = (int) $value['crd_h'];
break;
case 'snt':
$team_home[] = (int) $value['snt_a'];
$team_away[] = (int) $value['snt_h'];
break;
case 'tsh':
$team_home[] = (int) $value['tsh_a'];
$team_away[] = (int) $value['tsh_h'];
break;
}
}
}
$average_team = 0;
$average_team_home = 0;
$average_team_away = 0;
$average_league = 0;
$average_league_home = 0;
$average_league_away = 0;
// dd($team_home);
if ($team_id && $team_home) {
$sum = array_sum($team_home) + array_sum($team_away);
$count = count($team_home) + count($team_away);
$average_team = round($sum / $count, 1);
$average_team_home = round(array_sum($team_home) / count($team_home), 1);
$average_team_away = round(array_sum($team_away) / count($team_away), 1);
$average_league_home = $league_home ? round(array_sum($league_home) / \count($league_home), 1) : 0;
$average_league_away = $league_away ? round(array_sum($league_away) / \count($league_away), 1) : 0;
$media_league = array_merge($league_away, $league_home);
$average_league = $media_league ? round(array_sum($media_league) / \count($media_league), 1) : 0;
}
$data_team = array_fill(0, count($labels), $average_team);
$data_league = array_fill(0, count($labels), $average_league);
$data['datasets'][] = [
'label' => 'Average Team',
'events' => $events,
'data' => $data_team,
'tension' => 0.5,
'borderColor' => '#ad46ff',
'backgroundColor' => '#ad46ff',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_team_home' => $average_team_home,
'average_team_away' => $average_team_away,
];
$data['datasets'][] = [
'label' => 'Average League',
'events' => $events,
'data' => $data_league,
'tension' => 0.5,
'borderColor' => '#00bba7',
'backgroundColor' => '#00bba7',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_league_home' => $average_league_home,
'average_league_away' => $average_league_away,
];
}
}
}
@@ -0,0 +1,205 @@
<?php
namespace App\Filament\Widgets\Charts\LoadEvents;
use App\Filament\Pages\Selections\Helper\GetSeason;
use App\Models\SofaScoreRosa;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Facades\Log;
class LoadEventsQuery
{
public static function load(array $filter, array $handicap_range, array $total_range, object $match)
{
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$season = $filter['season'];
$seasonmode = $filter['seasonmode'];
$season_or_date = $filter['season_or_date'];
$start = $filter['start_date'] ?? 0;
$end = $filter['end_date'] ?? 0;
if ($season_or_date == 'season') {
$start = 0;
$end = 0;
}
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$player_on_field = $filter['player_on_field'] ?? 'all';
$similar_past_matches = $filter['similar_past_matches'];
$statistic_type = $filter['statistic_type'] ?? 'gls';
$team_position = $filter['team_position'];
$home_id = $match->home_id;
$away_id = $match->away_id;
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
if ($similar_past_matches != 'none') {
$team_position = '';
$team_id = 0;
$ss_player_id = 0;
}
// dd($season);
$season = GetSeason::load($season, $seasonmode);
/**
* IF WITH PLAYER
*/
$matches_played = [];
if ($ss_player_id) {
$matches_season = StatistikoEvent::where(function ($query) use ($team_id, $team_position) {
switch ($team_position) {
case 'home':
return $query->where('home_id', '=', $team_id);
case 'away':
return $query->where('away_id', '=', $team_id);
default:
return $query->where('home_id', '=', $team_id)
->orWhere('away_id', '=', $team_id);
}
})->pluck('match_id')->toArray();
$matches_played = StatistikoPlayer::query()
->whereIn('match_id', $matches_season)
->where('ss_player_id', $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
// 'all' => 'All',
// 'lineup' => 'Lineup',
// 'substituted' => 'Substituted',
// 'notplayed' => 'Not Played',
switch ($player_on_field) {
case 'all':
return $query->where('minutes_played', '>', 0);
case 'lineup':
return $query->where('lineup', 'T')->where('minutes_played', '>', 0);
case 'substituted':
return $query->where('lineup', 'P')->where('minutes_played', '>', 0);
default:
return $query->where('minutes_played', '>', 0);
}
})
->pluck('match_id')->toArray();
if (! $matches_played) {
$matches_played[] = -1;
}
}
$team_map = SofaScoreRosa::pluck('ss_team_name', 'ss_team_id')->toArray();
// dd($handicap_range, $total_range);
/**
* STATTISTIC DATA
*/
// dd($team_id, $home_id, $away_id);
$res = StatistikoEvent::where(function ($query) use ($league_id, $similar_past_matches) {
switch ($similar_past_matches) {
case 'none':
case 'league':
return $query->where('league_id', $league_id);
}
})
->where(function ($query) use ($team_id, $home_id, $away_id, $similar_past_matches) {
if ($similar_past_matches == 'none') {
if ($team_id) {
return $query->orWhere('home_id', $team_id)
->orWhere('away_id', '=', $team_id);
} else {
return $query->whereIn('home_id', [$home_id, $away_id])->orWhereIn('away_id', [$home_id, $away_id]);
}
}
})
->when($matches_played, function ($query) use ($matches_played, $player_on_field) {
if ($player_on_field == 'notplayed') {
return $query->whereNotIn('match_id', $matches_played);
} else {
return $query->whereIn('match_id', $matches_played);
}
})
->when($handicap_range, function ($query) use ($handicap_range) {
return $query->whereBetween('exp_handicap', $handicap_range);
})->when($total_range, function ($query) use ($total_range) {
return $query->whereBetween('exp_total', $total_range);
})
->when($start, function ($query) use ($start) {
return $query->whereDate('starttime', '>=', $start);
})->when($end, function ($query) use ($end) {
return $query->whereDate('starttime', '<=', $end);
})
->when($season, function ($query) use ($season, $season_or_date, $similar_past_matches) {
if ($similar_past_matches == 'none' && $season_or_date == 'season') {
return $query->whereIn('season', $season);
}
})
->orderBy('starttime', 'ASC')
->get();
// dd($res);
// dd($season, $league_id, $res);
Log::info('season', $season);
$arr = [];
foreach ($res as $value) {
$tmp = [];
$tmp['match_id'] = $value->match_id;
$tmp['sport_id'] = $value->sport_id;
$tmp['league_id'] = $value->league_id;
$tmp['home_id'] = $value->home_id;
$tmp['away_id'] = $value->away_id;
$tmp['starttime'] = $value->starttime;
$tmp['season'] = $value->season;
$tmp['exp_home'] = $value->exp_home;
$tmp['exp_away'] = $value->exp_away;
$tmp['exp_handicap'] = $value->exp_handicap;
$tmp['exp_total'] = $value->exp_total;
$tmp['result_code'] = $value->result_code;
$tmp['home'] = $team_map[$value->home_id] ?? 0;
$tmp['away'] = $team_map[$value->away_id] ?? 0;
switch ($statistic_type) {
case 'gls':
$tmp['val_home'] = $value->gls_h;
$tmp['val_away'] = $value->gls_a;
break;
case 'crn':
$tmp['val_home'] = $value->crn_h;
$tmp['val_away'] = $value->crn_a;
break;
case 'fls':
$tmp['val_home'] = $value->fls_h;
$tmp['val_away'] = $value->fls_a;
break;
case 'ofs':
$tmp['val_home'] = $value->ofs_h;
$tmp['val_away'] = $value->ofs_a;
break;
case 'crd':
$tmp['val_home'] = $value->crd_h;
$tmp['val_away'] = $value->crd_a;
break;
case 'snt':
$tmp['val_home'] = $value->snt_h;
$tmp['val_away'] = $value->snt_a;
break;
case 'tsh':
$tmp['val_home'] = $value->tsh_h;
$tmp['val_away'] = $value->tsh_a;
break;
}
$arr['all'][] = $tmp;
if ($value->home_id == $team_id) {
$arr['home'][] = $tmp;
}
if ($value->away_id == $team_id) {
$arr['away'][] = $tmp;
}
}
return $arr;
}
}
@@ -0,0 +1,144 @@
<?php
namespace App\Filament\Widgets\Charts\LoadEvents;
use App\Filament\Pages\Selections\Helper\GetSeason;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
class LoadEventsQueryAll
{
public static function load(array $filter, object $match)
{
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$season = $filter['season'];
$seasonmode = $filter['seasonmode'];
$season_or_date = $filter['season_or_date'];
$accuracy = (float) $filter['accuracy'] ?? 0.5;
$start = $filter['start_date'] ?? 0;
$end = $filter['end_date'] ?? 0;
if ($season_or_date == 'season') {
$start = 0;
$end = 0;
}
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$player_on_field = $filter['player_on_field'] ?? 'all';
$similar_past_matches = $filter['similar_past_matches'];
$team_position = $filter['team_position'];
$home_id = $match->home_id;
$away_id = $match->away_id;
$handicap_range = [];
$total_range = [];
if ($similar_past_matches != 'none') {
$team_id = 0;
$team_position = '';
$exp_handicap = $match->exp_away - $match->exp_home;
$exp_total = $match->exp_away + $match->exp_home;
$handicap_range = [$exp_handicap - $accuracy, $exp_handicap + $accuracy];
$total_range = [$exp_total - $accuracy, $exp_total + $accuracy];
}
if ($similar_past_matches != 'none') {
$team_position = '';
$team_id = 0;
$ss_player_id = 0;
}
// dd($season);
$season = GetSeason::load($season, $seasonmode);
/**
* IF WITH PLAYER
*/
$matches_played = [];
if ($ss_player_id) {
$matches_season = StatistikoEvent::whereIn('season', $season)
->where(function ($query) use ($team_id, $team_position) {
switch ($team_position) {
case 'home':
return $query->where('home_id', '=', $team_id);
case 'away':
return $query->where('away_id', '=', $team_id);
default:
return $query->where('home_id', '=', $team_id)
->orWhere('away_id', '=', $team_id);
}
})->pluck('match_id')->toArray();
$matches_played = StatistikoPlayer::query()
->whereIn('match_id', $matches_season)
->where('ss_player_id', $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
// 'all' => 'All',
// 'lineup' => 'Lineup',
// 'substituted' => 'Substituted',
// 'notplayed' => 'Not Played',
switch ($player_on_field) {
case 'all':
return $query->where('minutes_played', '>', 0);
case 'lineup':
return $query->where('lineup', 'T')->where('minutes_played', '>', 0);
case 'substituted':
return $query->where('lineup', 'P')->where('minutes_played', '>', 0);
default:
return $query->where('minutes_played', '>', 0);
}
})
->pluck('match_id')->toArray();
if (! $matches_played) {
$matches_played[] = -1;
}
}
// dd($handicap_range, $total_range);
/**
* STATTISTIC DATA
*/
return StatistikoEvent::with('home')->with('away')
->where(function ($query) use ($league_id, $similar_past_matches) {
switch ($similar_past_matches) {
case 'none':
case 'league':
return $query->where('league_id', $league_id);
}
})
->where(function ($query) use ($team_id, $home_id, $away_id, $similar_past_matches) {
if ($similar_past_matches == 'none') {
if ($team_id) {
return $query->orWhere('home_id', $team_id)
->orWhere('away_id', '=', $team_id);
} else {
return $query->whereIn('home_id', [$home_id, $away_id])->orWhereIn('away_id', [$home_id, $away_id]);
}
}
})
->when($matches_played, function ($query) use ($matches_played, $player_on_field) {
if ($player_on_field == 'notplayed') {
return $query->whereNotIn('match_id', $matches_played);
} else {
return $query->whereIn('match_id', $matches_played);
}
})
->when($handicap_range, function ($query) use ($handicap_range) {
return $query->whereBetween('exp_handicap', $handicap_range);
})->when($total_range, function ($query) use ($total_range) {
return $query->whereBetween('exp_total', $total_range);
})
->when($start, function ($query) use ($start) {
return $query->whereDate('starttime', '>=', $start);
})->when($end, function ($query) use ($end) {
return $query->whereDate('starttime', '<=', $end);
})
->when($season, function ($query) use ($season, $season_or_date, $similar_past_matches) {
if ($similar_past_matches == 'none' && $season_or_date == 'season') {
return $query->whereIn('season', $season);
}
})
->orderBy('starttime', 'ASC')
->get();
}
}
@@ -0,0 +1,153 @@
<?php
namespace App\Filament\Widgets\Charts\LoadEvents;
use App\Models\StatistikoConfig;
class LoadExpectations
{
public static function load(array $data, object $match, string $statistic_type)
{
// dd($data, $match);
$config = StatistikoConfig::where('group', $statistic_type)->get();
$uba_home_made_total = 1;
$uba_home_made_home = 1;
$uba_home_got_total = 1;
$uba_home_got_home = 1;
$uba_away_made_total = 1;
$uba_away_made_home = 1;
$uba_away_got_total = 1;
$uba_away_got_home = 1;
foreach ($config as $value) {
$varname = str_replace($statistic_type.'_', '', $value->varname);
switch ($varname) {
case 'home_made_total':
$uba_home_made_total = (float) $value->value;
break;
case 'home_made_home':
$uba_home_made_home = (float) $value->value;
break;
case 'home_got_total':
$uba_home_got_total = (float) $value->value;
break;
case 'home_got_home':
$uba_home_got_home = (float) $value->value;
break;
case 'away_made_total':
$uba_away_made_total = (float) $value->value;
break;
case 'away_made_home':
$uba_away_made_home = (float) $value->value;
break;
case 'away_got_total':
$uba_away_got_total = (float) $value->value;
break;
case 'away_got_home':
$uba_away_got_home = (float) $value->value;
break;
}
}
$home_made_total = [];
$home_made_home = [];
$home_got_total = [];
$home_got_home = [];
$away_made_total = [];
$away_made_away = [];
$away_got_total = [];
$away_got_away = [];
$home_id = $match->home_id;
$away_id = $match->away_id;
foreach ($data as $value) {
// HOME
// if ($value['home_id'] == $home_id || $value['away_id'] == $home_id) {
// $home_made_total[] = $value['val_home'];
// $home_got_total[] = $value['val_away'];
// }
// if ($value['home_id'] == $home_id) {
// $home_made_home[] = $value['val_home'];
// $home_got_home[] = $value['val_away'];
// }
// AWAY
// if ($value['home_id'] == $away_id || $value['away_id'] == $away_id) {
// $away_made_total[] = $value['val_away'];
// $away_made_away[] = $value['val_home'];
// }
// if ($value['away_id'] == $away_id) {
// $away_made_away[] = $value['val_away'];
// $away_got_away[] = $value['val_home'];
// }
if ($value['home_id'] == $home_id) {
$home_made_total[] = $value['val_home'];
$home_made_home[] = $value['val_home'];
$home_got_total[] = $value['val_away'];
$home_got_home[] = $value['val_away'];
} elseif ($value['away_id'] == $home_id) {
$home_made_total[] = $value['val_away'];
$home_got_total[] = $value['val_away'];
} elseif ($value['home_id'] == $away_id) {
$home_made_total[] = $value['val_away'];
$home_got_total[] = $value['val_away'];
} elseif ($value['away_id'] == $away_id) {
$home_made_total[] = $value['val_home'];
$home_made_home[] = $value['val_home'];
$home_got_total[] = $value['val_away'];
$home_got_home[] = $value['val_home'];
}
if ($value['away_id'] == $away_id) {
$away_made_total[] = $value['val_away'];
$away_made_away[] = $value['val_away'];
$away_got_total[] = $value['val_home'];
$away_got_away[] = $value['val_home'];
} elseif ($value['home_id'] == $away_id) {
$away_made_total[] = $value['val_home'];
$away_got_total[] = $value['val_home'];
} elseif ($value['away_id'] == $home_id) {
$away_made_total[] = $value['val_home'];
$away_got_total[] = $value['val_home'];
} elseif ($value['home_id'] == $home_id) {
$away_made_total[] = $value['val_away'];
$away_made_away[] = $value['val_away'];
$away_got_total[] = $value['val_home'];
$away_got_away[] = $value['val_away'];
}
}
$summe_home = array_sum($home_made_total) * $uba_home_made_total;
$summe_home += array_sum($home_made_home) * $uba_home_made_home;
$summe_home += array_sum($home_got_total) * $uba_home_got_total;
$summe_home += array_sum($home_got_home) * $uba_home_got_home;
$count_home = \count($home_made_total);
$count_home += \count($home_made_home);
$count_home += \count($home_got_total);
$count_home += \count($home_got_home);
$count_home = ! $count_home ? 1 : $count_home;
$aspettativa_home = round($summe_home / $count_home, 1);
// dd($count_home, $summe_home, $aspettativa_home);
$summe_away = array_sum($away_made_total) * $uba_away_made_total;
$summe_away += array_sum($away_made_away) * $uba_away_made_home;
$summe_away += array_sum($away_got_total) * $uba_away_got_total;
$summe_away += array_sum($away_got_away) * $uba_away_got_home;
$count_away = \count($away_made_total);
$count_away += \count($away_made_away);
$count_away += \count($away_got_total);
$count_away += \count($away_got_away);
$count_away = ! $count_away ? 1 : $count_away;
$aspettativa_away = round($summe_away / $count_away, 1);
return ['home' => $aspettativa_home, 'away' => $aspettativa_away];
}
}
@@ -0,0 +1,605 @@
<?php
namespace App\Filament\Widgets\Charts;
use App\Models\SofaScoreRosa;
use App\Models\StatisticFixtures;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPerformance
{
public static $last_query = [];
public static $events_data = [];
public static function load(array $filter, bool $hasdots)
{
$league_id = (int) $filter['league_id'] ?? 0;
$match_id = (int) $filter['match_id'] ?? 0;
$team_position = $filter['team_position'];
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
$team_map = SofaScoreRosa::pluck('ss_team_name', 'ss_team_id')->toArray();
$match = StatisticFixtures::where('id', $match_id)->first();
if (! $match) {
return [];
}
$home_id = $match->home_id ?? 0;
$away_id = $match->away_id ?? 0;
$res = StatistikoEvent::where('league_id', $league_id)->get();
$home_matches = [];
$away_matches = [];
$league_matches = [];
$date_home = [];
$date_away = [];
$events_home = [];
$events_away = [];
$results_home = [];
$results_away = [];
$labels = [];
foreach ($res as $val) {
$league_matches[] = $val->match_id;
switch ($team_position) {
case 'home':
if ($val->home_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$home_matches[] = $val->match_id;
}
if ($val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$away_matches[] = $val->match_id;
}
break;
case 'away':
if ($val->away_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$home_matches[] = $val->match_id;
}
if ($val->home_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$away_matches[] = $val->match_id;
}
break;
default:
if ($val->home_id == $home_id or $val->away_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$home_matches[] = $val->match_id;
}
if ($val->home_id == $away_id or $val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$away_matches[] = $val->match_id;
}
break;
}
}
$res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$homes = self::filterMatches($res, $home_matches, $home_id);
// dd($homes['all']);
$res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$aways = self::filterMatches($res, $away_matches, $away_id);
// dd($aways['all']);
$c_home = isset($homes['all']) ? count($homes['all']) : 0;
$c_away = isset($aways['all']) ? count($aways['all']) : 0;
$length = max($c_home, $c_away);
self::shortenEnding($labels, $homes, $aways);
$home_av = self::makeAv($homes, $length);
$away_av = self::makeAv($aways, $length);
$res = StatistikoPlayer::whereIn('match_id', $league_matches)->select('match_id', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$league = self::filterMatches($res, $league_matches);
$league_av = self::leagueAv($league, $length);
$data = [
'datasets' => [
[
'label' => $team_map[$match->home_id],
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
'pointStyle' => $hasdots,
],
[
'label' => 'Average Team',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => 'transparent',
'pointStyle' => false,
'borderDash' => [5, 5],
],
[
'label' => 'Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => '#fe9a00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => 'transparent',
'pointStyle' => false,
'hidden' => true,
'borderDash' => [5, 5],
],
[
'label' => 'Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => '#fe9a00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'hidden' => true,
'borderDash' => [5, 5],
],
[
'label' => 'Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => '#fe9a00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fe9a00',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => $team_map[$match->away_id],
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00bc7d',
'backgroundColor' => '#00bc7d',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => false,
],
[
'label' => 'Average Team',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00bc7d',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => false,
],
[
'label' => 'Defence',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => '#7ccf00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Attak',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => '#7ccf00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Midfield',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => '#7ccf00',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7ccf00',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average League',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00a6f4',
'backgroundColor' => '#00a6f4',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => 'transparent',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['att'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => '#155dfc',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['mid'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => 'transparent',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
],
'labels' => $labels,
];
return $data;
}
public static function filterMatches($res, array $matches, $team_id = 0)
{
if (! $res->count()) {
return ['all', 'def', 'att', 'mid'];
}
$result = [];
foreach ($res as $value) {
$match_id = $value->match_id;
if (! isset($result[$match_id])) {
$result[$match_id]['all']['ply'] = 0;
$result[$match_id]['all']['sum_min'] = 0;
$result[$match_id]['all']['rating'] = 0;
$result[$match_id]['att']['ply'] = 0;
$result[$match_id]['att']['sum_min'] = 0;
$result[$match_id]['att']['rating'] = 0;
$result[$match_id]['mid']['ply'] = 0;
$result[$match_id]['mid']['sum_min'] = 0;
$result[$match_id]['mid']['rating'] = 0;
$result[$match_id]['def']['ply'] = 0;
$result[$match_id]['def']['sum_min'] = 0;
$result[$match_id]['def']['rating'] = 0;
}
if ($team_id == 0 || $value->activeteam_id == $team_id) {
if ($value->minutes_played > 0) {
$result[$match_id]['all']['ply']++;
$result[$match_id]['all']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['all']['rating'] += (float) $value->playerrating_value;
if ($value->player_position == 'M') {
$result[$match_id]['mid']['ply']++;
$result[$match_id]['mid']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['mid']['rating'] += (float) $value->playerrating_value;
} elseif ($value->player_position == 'F') {
$result[$match_id]['att']['ply']++;
$result[$match_id]['att']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['att']['rating'] += (float) $value->playerrating_value;
} elseif ($value->player_position == 'D') {
$result[$match_id]['def']['ply']++;
$result[$match_id]['def']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['def']['rating'] += (float) $value->playerrating_value;
}
}
}
}
// dd($result);
$return = [];
foreach ($matches as $matchid) {
if (isset($result[$matchid])) {
$return['all'][] = round($result[$matchid]['all']['rating'] / $result[$matchid]['all']['ply'], 1);
if ($result[$matchid]['def']['sum_min']) {
$return['def'][] = round($result[$matchid]['def']['rating'] / $result[$matchid]['def']['ply'], 1);
} else {
$return['def'][] = null;
}
if ($result[$matchid]['att']['sum_min']) {
$return['att'][] = round($result[$matchid]['att']['rating'] / $result[$matchid]['att']['ply'], 1);
} else {
$return['att'][] = null;
}
if ($result[$matchid]['mid']['sum_min']) {
$return['mid'][] = round($result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['ply'], 1);
} else {
$return['mid'][] = null;
}
} else {
$return['all'][] = null;
$return['def'][] = null;
$return['att'][] = null;
$return['mid'][] = null;
}
}
// dd($return);
// while (array_last($return['all']) == null) {
// }
return $return;
}
public static function shortenEnding(array &$labels, array &$homes, array &$aways)
{
// dd($labels, $homes, $aways);
if (! isset($homes['all'])) {
return;
}
$count_home = 0;
$count_away = 0;
if (isset($homes['all'])) {
for ($i = \count($homes['all']) - 1; $i > 0; $i--) {
if ($homes['all'][$i] == null) {
$count_home++;
} else {
break;
}
}
}
if (isset($aways['all'])) {
for ($i = \count($aways['all']) - 1; $i > 0; $i--) {
if ($aways['all'][$i] == null) {
$count_away++;
} else {
break;
}
}
}
$keeps = count($homes['all']) - min($count_home, $count_away) - 1;
if (isset($homes['all'])) {
$homes['all'] = array_slice($homes['all'], 0, $keeps);
$homes['def'] = array_slice($homes['def'], 0, $keeps);
$homes['att'] = array_slice($homes['att'], 0, $keeps);
$homes['mid'] = array_slice($homes['mid'], 0, $keeps);
}
if (isset($aways['all'])) {
$aways['all'] = array_slice($aways['all'], 0, $keeps);
$aways['def'] = array_slice($aways['def'], 0, $keeps);
$aways['att'] = array_slice($aways['att'], 0, $keeps);
$aways['mid'] = array_slice($aways['mid'], 0, $keeps);
}
$labels = array_slice($labels, 0, $keeps);
// $labels = range(1, $keeps);
}
public static function makeAv(array &$arr, int $length)
{
if (isset($arr['all']) && count($arr['all'])) {
$all = isset($arr['all']) ? array_sum($arr['all']) / count($arr['all']) : 1;
$def = isset($arr['def']) ? array_sum($arr['def']) / count($arr['def']) : 1;
$mid = isset($arr['mid']) ? array_sum($arr['mid']) / count($arr['mid']) : 1;
$att = isset($arr['att']) ? array_sum($arr['att']) / count($arr['att']) : 1;
$ret = [];
$ret['all'] = array_fill(0, $length, round($all, 2));
$ret['def'] = array_fill(0, $length, round($def, 2));
$ret['mid'] = array_fill(0, $length, round($mid, 2));
$ret['att'] = array_fill(0, $length, round($att, 2));
} else {
$ret = [];
$ret['all'] = [];
$ret['def'] = [];
$ret['mid'] = [];
$ret['att'] = [];
}
return $ret;
}
public static function leagueAv(array &$arr, int $len)
{
if (isset($arr['all'])) {
$all = array_sum($arr['all']) / count($arr['all']);
$def = array_sum($arr['def']) / count($arr['def']);
$mid = array_sum($arr['mid']) / count($arr['mid']);
$att = array_sum($arr['att']) / count($arr['att']);
} else {
$all = 0;
$def = 0;
$mid = 0;
$att = 0;
}
$ret = [];
$ret['all'] = array_fill(0, $len, round($all, 2));
$ret['def'] = array_fill(0, $len, round($def, 2));
$ret['mid'] = array_fill(0, $len, round($mid, 2));
$ret['att'] = array_fill(0, $len, round($att, 2));
return $ret;
}
}
@@ -0,0 +1,91 @@
<?php
namespace App\Filament\Widgets\Charts;
use App\Models\StatistikoDetail;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPlayerPerformance
{
public static $last_query = [];
public static function load($filter, bool $hasdots)
{
// if (self::$last_query) {
// return self::$last_query;
// }
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$statistic_type = $filter['statistic_type'] ?? 'gls';
$player_on_field = $filter['player_on_field'];
if (! $league_id) {
return [];
}
if (! $team_id) {
return [];
}
if (! $ss_player_id) {
return [];
}
$player_name = StatistikoDetail::where('ss_player_id', (int) $ss_player_id)->value('player_name');
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
$matches_played = StatistikoPlayer::with('event')
->where('ss_player_id', (int) $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
switch ($player_on_field) {
case 'lineup':
return $query->where('lineup', 'T');
case 'substituted':
return $query->where('lineup', 'P');
}
})->get();
$tmp = [];
foreach ($matches_played as $value) {
$val = $value->playerrating_value;
$key = $value->event ? round($value->event->starttime->diffInDays(Carbon::now())) : 0;
$tmp["$key"] = $val;
}
krsort($tmp);
$dataset = [];
$labels = [];
foreach ($tmp as $key => $value) {
$labels[] = $key;
$dataset[] = $value;
}
// $labels = array_keys($dataset);
$data = [
'datasets' => [
[
'label' => 'Player Rating',
'data' => $dataset,
'tension' => 0.5,
'borderColor' => '#36A2EB',
'pointStyle' => $hasdots,
],
],
'labels' => $labels,
'player_name' => $player_name,
];
self::$last_query = $data;
return $data;
}
}
@@ -0,0 +1,183 @@
<?php
namespace App\Filament\Widgets\Charts;
use App\Filament\Pages\Selections\Helper\GetSeason;
use App\Models\StatistikoDetail;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPlayerWidgets
{
public static $last_query = [];
public static function load(array $filter, bool $hasdots)
{
// dd($filter);
// if (self::$last_query) {
// return self::$last_query;
// }
$team_position = $filter['team_position'];
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$player_on_field = $filter['player_on_field'] ?? 'all';
$season = $filter['season'];
$seasonmode = $filter['seasonmode'];
$season = GetSeason::load($season, $seasonmode);
// dd($player_on_field);
$statistic_type = $filter['statistic_type'] ?? 'gls';
$statistic_name = HelperStatisticType::get($filter);
if (! $league_id) {
return [];
}
if (! $team_id) {
return [];
}
if (! $ss_player_id) {
return [];
}
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
$player_name = StatistikoDetail::where('ss_player_id', (int) $ss_player_id)->value('player_name');
$matches_season = StatistikoEvent::whereIn('season', $season)
->where(function ($query) use ($team_id, $team_position) {
switch ($team_position) {
case 'home':
return $query->where('home_id', '=', $team_id);
case 'away':
return $query->where('away_id', '=', $team_id);
default:
return $query->where('home_id', '=', $team_id)
->orWhere('away_id', '=', $team_id);
}
})->pluck('match_id')->toArray();
// dd($matches_season);
$matches_played = StatistikoPlayer::query()
->whereIn('match_id', $matches_season)
->where('ss_player_id', $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
// 'all' => 'All',
// 'lineup' => 'Lineup',
// 'substituted' => 'Substituted',
// 'notplayed' => 'Not Played',
switch ($player_on_field) {
case 'all':
return $query->where('minutes_played', '>', 0);
case 'lineup':
return $query->where('lineup', 'T')->where('minutes_played', '>', 0);
case 'substituted':
return $query->where('lineup', 'P')->where('minutes_played', '>', 0);
default:
return $query->where('minutes_played', '>', 0);
}
})
->pluck('match_id')->toArray();
$res = StatistikoPlayer::with('event')
->where('ss_player_id', (int) $ss_player_id)
->when($matches_played, function ($query) use ($matches_played, $player_on_field) {
if ($player_on_field == 'notplayed') {
return $query->whereNotIn('match_id', $matches_played);
} else {
return $query->whereIn('match_id', $matches_played);
}
})->get();
$tmp = [];
foreach ($res as $value) {
$val = 0;
$key = 0;
if (isset($value->event->starttime)) {
$key = (int) round($value->event->starttime->diffInDays(Carbon::now()));
switch ($statistic_type) {
case 'gls':
$val = (int) $value->goals;
break;
case 'crn':
$val = (int) 0;
break;
case 'fls':
$val = (int) $value->fouls;
break;
case 'ofs':
$val = (int) $value->offsides;
break;
case 'crd':
$val = (int) $value->card_yellow + $value->card_red + $value->card_yellowred;
break;
case 'snt':
$val = (int) $value->shots_on_target;
break;
case 'tsh':
$val = (int) $value->shots_off_target;
break;
}
$tmp[$key] = $val;
}
}
krsort($tmp);
$dataset = [];
$labels = [];
foreach ($tmp as $key => $value) {
$labels[] = $key;
$dataset[] = $value;
}
$avverage = array_sum($dataset) / count($dataset);
$dataset2 = array_fill(0, count($dataset), $avverage);
$data = [
'datasets' => [
[
'label' => ucfirst($statistic_name),
'data' => $dataset,
'tension' => 0.5,
'borderColor' => '#36A2EB',
'pointStyle' => $hasdots,
],
],
'labels' => $labels,
'player_name' => $player_name,
];
$data['datasets'][] = [
'label' => 'Average',
'data' => $dataset2,
'tension' => 0.5,
'borderColor' => '#ad46ff',
'backgroundColor' => '#ad46ff',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_team_home' => [],
'average_team_away' => [],
];
self::$last_query = $data;
return $data;
}
}