This commit is contained in:
salam committed 2026-09-10 08:34:30 +02:00
1 parent 919ad14a9d
commit d947f40888
9 files changed
+520 -165

No files matched your search

+271 -33
View File
@@ -2,7 +2,11 @@
namespace App\Livewire;
use App\Models\SofaScorePlayerPerSeason;
use App\Models\SofaScorePlayerPosition;
use App\Models\SofaScoreRosa;
use App\Models\StatisticFixtures;
use App\Models\StatistikoDetail;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
@@ -34,6 +38,14 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
public array $sourceRecords = [];
public array $filters = [];
// slider
public float $x_value = 3;
// min
public float $y_value = 180;
public array $lists = [];
public array $results = [];
@@ -44,6 +56,8 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
public bool $calculationDisabled = true;
public bool $rangeXDisabled = true;
public string $ss_team_name = '';
public array $lineupsMap = [
@@ -160,6 +174,8 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
public function mount(array $filters)
{
$this->filters = $filters;
$this->ss_team_name = SofaScoreRosa::where('ss_team_id', $filters['team_id'])->value('ss_team_name');
$this->sourceRecords = PlayerCalculator::load($filters);
@@ -178,31 +194,33 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
// Log::debug('beschriftung', $this->beschriftung);
}
public function updateXvalue()
{
$this->calculationChecker();
}
public function getHeadingTable(): HtmlString
{
return new HtmlString('<div class="text-[11px] p-0 flex flex-row gap-4 font-bold font-mono" style="position: relative; left: -2px; bottom: -13px;">
<div>Gls</div>
<div>Fls</div>
<div>Ofs</div>
<div>Snt</div>
<div>Tsh</div>
<div>Crd</div>
<div>Foot</div>
<div>Evt</div>
<div>Ømin</div>
</div>');
<div>Gls</div>
<div>Fls</div>
<div>Ofs</div>
<div>Snt</div>
<div>Tsh</div>
<div>Crd</div>
<div>Foot</div>
<div>Evt</div>
<div>Ømin</div>
</div>');
}
public function form(Schema $schema): Schema
{
return $schema
->columns(4)
->components([
Select::make('lineup_selected')
->label('Lineup')
->live()
->hiddenLabel()
->columnSpan(2)
->prefix('Lineup')
->prefixIcon('icon-soccerplayer')
->searchable()
->options($this->lineup_options)
@@ -243,13 +261,13 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
->tooltip('Remove all players from field')
->icon('phosphor-arrows-clockwise')
->action(fn () => $this->clearAll()),
Action::make('calculate')
->hiddenLabel()
->tooltip('Calculate expected values')
->size(Size::Small)
->icon('phosphor-calculator')
->disabled($this->calculationDisabled)
->action(fn () => $this->calculateAspg()),
// Action::make('calculate')
// ->hiddenLabel()
// ->tooltip('Calculate expected values')
// ->size(Size::Small)
// ->icon('phosphor-calculator')
// ->disabled($this->calculationDisabled)
// ->action(fn () => $this->calculateAspg()),
Action::make('copy')
->tooltip('Copy results for pasting into a spread sheet')
->hiddenLabel()
@@ -317,9 +335,6 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
foreach ($this->sourceRecords as $k => $record) {
if ($record['id'] == (int) $player['id']) {
$this->calculationDisabled = false;
if ($this->boxes[$boxNumber]) {
$this->clearBox($boxNumber);
}
@@ -328,11 +343,34 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
$this->sourceRecords[$k]['position'] = $this->activeBoxes[$boxNumber];
}
}
$this->calculationChecker();
}
public function calculationChecker()
{
$count = 0;
foreach ($this->sourceRecords as $record) {
if (count($record)) {
$count++;
}
}
if ($count >= 9) {
$this->copyDisabled = false;
$this->rangeXDisabled = false;
$this->calculateAspg();
} else {
$this->copyDisabled = true;
$this->rangeXDisabled = true;
$this->clearAll();
}
}
public function updateLineup(?string $state)
{
$this->clearAll();
$this->calculationChecker();
if (isset($this->lineupsMap[$state])) {
$this->lineup = $this->lineupsMap[$state];
@@ -343,6 +381,7 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
public function clearBox(int $boxNumber): void
{
if (isset($this->boxes[$boxNumber])) {
$tmp = $this->boxes[$boxNumber];
$player_id = 0;
@@ -358,12 +397,9 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
$this->sourceRecords[$k]['active'] = 0;
}
}
}
}
public function calculateAspg(): void
{
$this->copyDisabled = false;
$this->calculationChecker();
}
}
public function clearAll(): void
@@ -374,18 +410,22 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
foreach ($this->sourceRecords as $k => $record) {
$this->sourceRecords[$k]['position'] = '';
$this->sourceRecords[$k]['active'] = 0;
$this->sourceRecords[$k]['gls'] = 0;
$this->sourceRecords[$k]['fls'] = 0;
$this->sourceRecords[$k]['ofs'] = 0;
$this->sourceRecords[$k]['snt'] = 0;
$this->sourceRecords[$k]['tsh'] = 0;
$this->sourceRecords[$k]['crd'] = 0;
}
$this->copyDisabled = true;
$this->calculationDisabled = true;
$this->calculationChecker();
}
public function copyResult()
{
return;
$copystring = '';
foreach ($this->results as $row) {
dd($row);
unset($row['origin']);
unset($row['pcolor']);
unset($row['ecolor']);
@@ -395,6 +435,204 @@ class BoardDispatcher extends Component implements HasActions, HasSchemas, HasTa
$this->js("navigator.clipboard.writeText(`$copystring`)");
}
public function calculateAspg(): void
{
// Calcolare Aspettativa Team per GOALS
// (il valore che appare quando viene selezionato un Evento): nell' esempio,
// se scegliamo come Team il Remo (Home) il valore di riferimento sarà 1.2
$team_id = $this->filters['team_id'];
$res = StatisticFixtures::where('id', $this->filters['match_id'])->first();
$asp_team = 0;
if ((int) $res->home_id == (int) $team_id) {
$asp_team = $res->exp_home;
} elseif ((int) $res->away_id == (int) $team_id) {
$asp_team = $res->exp_away;
}
// "atr" => 2.8868026211843
// "id" => 44691
// "name" => "Maicon"
// "on_field" => 0
// "position" => "DC"
// "sgf" => 1044
// "smg" => 1044
// "adg" => 90
// "as_goals" => 0.044
// "at_goals" => 67.511
// "as_cards" => 0.195
// "at_cards" => 67.54875
// "as_fouls" => 0.959
// "at_fouls" => 67.73975
// "as_offsides" => 0.036
// "at_offsides" => 67.509
// "as_total_shots" => 0.525
// "at_total_shots" => 67.63125
// "as_shots_on_target" => 0.263
// "at_shots_on_target" => 67.56575
$player = StatistikoDetail::where('ss_team_id', (int) $team_id)->where('position', '!=', 'GK')->where('is_active', 1)->get();
// Aspettativa Team di Riferimento
// (ATR) = Aspettativa Team * Numero di Giocatori di Team in Rosters (esclusi i portieri) / 10.
// Ad esempio per Remo (supponiamo 24 giocatori nel Roster esclusi i portieri): ATR = 1.2 * 24 / 10 = 2.88
$asp_team_riferimento = $asp_team * $player->count() / 10;
$player_totals = SofaScorePlayerPerSeason::with('details')->where('season', 'total')->where('ss_team_id', $team_id)->get();
$results = [];
foreach ($player_totals as $val) {
if ($val->details && $val->details->is_active) {
$ss_player_id = $val->ss_player_id;
$position = $val->details->position;
$tmp = [];
$tmp['id'] = $ss_player_id;
$tmp['name'] = $val->details->player_name;
$tmp['asp_team'] = $asp_team;
$tmp['asp_team_riferimento'] = $asp_team_riferimento;
$position_field = $this->get_position($ss_player_id);
if ($position_field) {
$position = $position_field;
$tmp['active'] = 1;
} else {
$tmp['active'] = 0;
}
$tmp['position'] = $position;
// Aspettativa di Gioco (AdG) = Somma Goals fatti da Player nel DB / Somma Minuti Giocati da Player (SMG) nel DB * 90.
// Se la SMG è < Y per quel Player prenderemo in considerazione solo l'Aspettativa Statistica (AS).
// Il valore Y dovrebbe essere configurabile in una interfaccia. Il valore iniziale lo fissiamo a Y = 180
// Somma Goals fatti da Player nel DB
$somma_goals = $val->goals;
$tmp['somma_goals'] = $val->goals;
// Somma Minuti Giocati da Player (SMG) nel DB
$somma_minuti = $val->minutes_played_home + $val->minutes_played_away;
$tmp['somma_minuti'] = $somma_minuti;
// Aspettativa di Gioco
// (AdG)
$asp_gioco = $somma_goals / $somma_minuti * 90;
$tmp['asp_gioco'] = $asp_gioco;
// il valore corrispondente al ruolo del Player (esempio un giocatore in Details ha ruolo = RW
// quindi Aspettativa Statistica (AS) per RW = 0.219)
$bettypes = ['goals', 'cards', 'fouls', 'offsides', 'total_shots', 'shots_on_target'];
foreach ($bettypes as $bettype) {
$asp_statistica = (float) SofaScorePlayerPosition::where('ss_code', $position)->value('exp_'.$bettype);
$tmp['asp_statistica_'.$bettype] = $asp_statistica;
if ($somma_minuti < $this->y_value) {
// Se la SMG è < Y per quel Player prenderemo in considerazione solo l'Aspettativa Statistica (AS).
$tmp['asp_teorica_'.$bettype] = $asp_statistica;
} else {
// Aspettativa Teorica (AT) Goals = (X * AdG + AS) / (1 + X)
// dove X è un valore che dovrebbe essere parametrizzabile da una interfaccia.
// Il valore iniziale lo fissiamo a X = 3
// (AT)
$tmp['asp_teorica_'.$bettype] = ($this->x_value * $asp_gioco + $asp_statistica) / (1 + $this->x_value);
}
}
// Calcolare la Somma di tutte le AT (colonna G della Tabella)
// Considerare la ATR calcolata allo Step 1 (colonna I della Tabella)
// Aspettativa per Player: AP = AT * ATR / TOT AT
$results[] = $tmp;
}
}
$bettypes = ['goals', 'cards', 'fouls', 'offsides', 'total_shots', 'shots_on_target'];
foreach ($bettypes as $bettype) {
$total_bettype = array_sum(array_column($results, 'asp_teorica_'.$bettype));
foreach ($results as $key => $value) {
// Aspettativa per Player: AP = AT * ATR / TOT AT
$results[$key]['asp_total_'.$bettype] = $total_bettype;
$results[$key]['asp_player_'.$bettype] = ((float) $value['asp_team'] * (float) $value['asp_teorica_'.$bettype]) / $total_bettype;
}
}
// dd($results);
// update frontend
foreach ($this->sourceRecords as $k => $record) {
// "id" => 787607
// "name" => "Keno"
// "preferred_foot" => "Right"
// "player_position" => "LW"
// "position_area" => "attack"
// "player_color" => "warning"
// "timed_color" => "danger"
// "events_played" => 7
// "everage_minutes" => 11
// "position" => "DC"
// "active" => 1
// "gls" => 0
// "fls" => 0
// "ofs" => 0
// "snt" => 0
// "tsh" => 0
// "crd" => 0
foreach ($results as $key => $result) {
// "id" => 44691
// "name" => "Maicon"
// "asp_team" => "1.11030870045550"
// "asp_team_riferimento" => 2.8868026211843
// "active" => 0
// "position" => "DC"
// "somma_goals" => 1044
// "somma_minuti" => 1044
// "asp_gioco" => 90
// "asp_statistica_goals" => 0.044
// "asp_teorica_goals" => 67.511
// "asp_statistica_cards" => 0.195
// "asp_teorica_cards" => 67.54875
// "asp_statistica_fouls" => 0.959
// "asp_teorica_fouls" => 67.73975
// "asp_statistica_offsides" => 0.036
// "asp_teorica_offsides" => 67.509
// "asp_statistica_total_shots" => 0.525
// "asp_teorica_total_shots" => 67.63125
// "asp_statistica_shots_on_target" => 0.263
// "asp_teorica_shots_on_target" => 67.56575
// "asp_player_goals" => 0.041110675617421
// "asp_player_cards" => 0.041126169295623
// "asp_player_fouls" => 0.041104958293849
// "asp_player_offsides" => 0.041105354678332
// "asp_player_total_shots" => 0.041028401589513
// "asp_player_shots_on_target" => 0.041094150075013
if ($record['id'] == $result['id']) {
// dd($result);
$this->sourceRecords[$k]['active'] = $result['active'];
$this->sourceRecords[$k]['gls'] = number_format($result['asp_player_goals'], 3);
$this->sourceRecords[$k]['fls'] = number_format($result['asp_player_fouls'], 3);
$this->sourceRecords[$k]['ofs'] = number_format($result['asp_player_offsides'], 3);
$this->sourceRecords[$k]['snt'] = number_format($result['asp_player_total_shots'], 3);
$this->sourceRecords[$k]['tsh'] = number_format($result['asp_player_shots_on_target'], 3);
$this->sourceRecords[$k]['crd'] = number_format($result['asp_player_cards'], 3);
}
}
}
}
public function get_position(int $ss_player_id)
{
// see if player is in field and if yes get new position
foreach ($this->sourceRecords as $record) {
if ($record['id'] == $ss_player_id) {
return $record['position'];
}
}
return false;
}
public function render()
{
return view('livewire.board-dispatcher');