| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use App\CalendarGame;
- class Calendar extends Model
- {
- protected $fillable = [
- 'name',
- 'section_id',
- 'region_id',
- 'season_id',
- 'category_id',
- 'group_id',
- 'type',
- 'position',
- 'archived'
- ];
- public function season()
- {
- return $this->belongsTo('App\Season');
- }
- public function section()
- {
- return $this->belongsTo('App\Section');
- }
- public function region()
- {
- return $this->belongsTo('App\Section');
- }
- public function category()
- {
- return $this->belongsTo('App\Category');
- }
- public function group()
- {
- return $this->belongsTo('App\Group');
- }
- public function getRates()
- {
- /*
- Penalizzazioni
- Se mi ritiro prima del girone di ritorno
- Mi ritiro alla 10
- Dall’11 in poi scrivo riposo
- Gestione punti penalizzazione rinunce
- Se dal ritorno, quelle prima valide, quelle dopo il ritiro 0-6 tavolino
- Se è stata esclusa in classifica tutto a 0
- */
- $aRate = array();
- // Carico le squadre
- $games = CalendarGame::where('calendar_id', '=', $this->id)->get();
- foreach($games as $g)
- {
- $home_team_id = $g->home_team_id;
- //if ($g->type == 'RITORNO')
- // $home_team_id = $g->away_team_id;
- $away_team_id = $g->away_team_id;
- //if ($g->type == 'RITORNO')
- // $homeaway_team_id = $g->home_team_id;
- if ($home_team_id != null && $away_team_id != null)
- {
- if (!isset($aRate[$home_team_id]))
- {
- $t = Team::findOrFail($home_team_id);
- $points = 0;
- if ($t->penality > 0)
- $points -= $t->penality;
- $penality = '';
- $order = 0;
- if ($t->excluded)
- {
- $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $order = -999;
- }
- if ($t->type != '' && $t->type != '-' && !$t->excluded)
- {
- $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $order = -999;
- }
- if ($t->penality > 0 && $penality == '')
- {
- $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
- // $order = -555;
- }
- $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $penality, 'order' => $order);
- }
- if (!isset($aRate[$away_team_id]))
- {
- $t = Team::findOrFail($away_team_id);
- $points = 0;
- if ($t->penality > 0)
- $points -= $t->penality;
- $penality = '';
- $order = 0;
- if ($t->excluded)
- {
- $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $order = -999;
- }
- if ($t->type != '' && $t->type != '-' && !$t->excluded)
- {
- $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $order = -999;
- }
- if ($t->penality > 0 && $penality == '')
- {
- $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
- // $order = -555;
- }
- $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $penality, 'order' => $order);
- }
- if ($home_team_id != NULL && $away_team_id != NULL && $g->played)
- {
- if (!isset($aRate[$home_team_id]))
- {
- $t = Team::findOrFail($home_team_id);
- $points = 0;
- if ($t->penality > 0)
- $points -= $t->penality;
- $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $order);
- }
- if (!isset($aRate[$away_team_id]))
- {
- $t = Team::findOrFail($away_team_id);
- $points = 0;
- if ($t->penality > 0)
- $points -= $t->penality;
- $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $order);
- }
- // Controllo la squadra di casa
- $t = Team::findOrFail($home_team_id);
- $p_home = '';
- $home_order = 0;
- $home_pen = false;
- $home_no_game = false;
- if ($t->excluded)
- {
- $home_pen = true;
- $home_order = -999;
- $p_home .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $home_order, 'penality' => $p_home);
- }
- if ($t->type != '' && $t->type != '-' && !$t->excluded)
- {
- $home_pen = true;
- $home_order = -999;
- $p_home .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $home_order, 'penality' => $p_home);
- }
- if ($t->penality > 0 && $p_home == '')
- {
- $p_home .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
- //$home_order = -555;
- }
- $t = Team::findOrFail($away_team_id);
- $p_away = '';
- $away_order = 0;
- $away_pen = false;
- $away_no_game = false;
- if ($t->excluded)
- {
- $away_pen = true;
- $away_order = -999;
- $p_away .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_away);
- }
- if ($t->type != '' && $t->type != '-' && !$t->excluded)
- {
- $away_pen = true;
- $away_order = -999;
- $p_away .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
- $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_away);
- }
- if ($t->penality > 0 && $p_away == '')
- {
- $p_away .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
- // $away_order = -555;
- }
- // HOME
- $t = Team::findOrFail($home_team_id);
- if (($t->type != '' && $t->type != '-') || $t->excluded)
- {
- if ($t->type == 'andata')
- {
- $home_no_game = true;
- $g->home_goals = 0;
- $g->home_points = 0;
- $g->away_goals = 0;
- $g->away_points = 0;
- /*
- if ($g->type == 'ANDATA')
- {
- if ($g->day > $t->day)
- {
- $g->home_goals = 0;
- $g->home_points = 0;
- }
- }
- if ($g->type == 'RITORNO')
- {
- $g->home_goals = 0;
- $g->home_points = 0;
- }
- */
- }
- if ($t->type == 'ritorno')
- {
- if ($g->type == 'RITORNO')
- {
- if ($g->day > $t->day)
- {
- $g->home_goals = 0;
- $g->home_points = 0;
- $g->away_goals = 6;
- $g->away_points = 3;
- }
- }
- }
- }
- // AWAY
- $t = Team::findOrFail($away_team_id);
- if (($t->type != '' && $t->type != '-') || $t->excluded)
- {
- if ($t->type == 'andata')
- {
- $away_no_game = true;
- $g->away_goals = 0;
- $g->away_points = 0;
- $g->home_goals = 0;
- $g->home_points = 0;
- /*if ($g->type == 'ANDATA')
- {
- if ($g->day > $t->day)
- {
- $g->away_goals = 0;
- $g->away_points = 0;
- }
- }
- if ($g->type == 'RITORNO')
- {
- $g->away_goals = 0;
- $g->away_points = 0;
- }*/
- }
- if ($t->type == 'ritorno')
- {
- if ($g->type == 'RITORNO')
- {
- if ($g->day > $t->day)
- {
- $g->away_goals = 0;
- $g->away_points = 0;
- $g->home_goals = 6;
- $g->home_points = 3;
- }
- }
- }
- }
- $c_home = $aRate[$home_team_id];
- $POINTS = $c_home["POINTS"] + $g->home_points;
- if ($home_order == -999)
- $aRate[$home_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_home);
- if ($g->played && !$away_no_game)
- {
- $PG = $c_home["PG"] + ($g->played ? 1 : 0);
- $V = $c_home["V"] + ($g->home_goals > $g->away_goals ? 1 : 0);
- $N = $c_home["N"] + ($g->home_goals == $g->away_goals ? 1 : 0);
- $P = $c_home["P"] + ($g->home_goals < $g->away_goals ? 1 : 0);
- $GF = $c_home["GF"] + $g->home_goals;
- $GS = $c_home["GS"] + $g->away_goals;
- $DR = $GF - $GS;
- }
- else
- {
- $PG = $c_home["PG"];
- $V = $c_home["V"];
- $N = $c_home["N"];
- $P = $c_home["P"];
- $GF = $c_home["GF"];
- $GS = $c_home["GS"];
- $DR = $GF - $GS;
- }
- if (!$home_pen)
- $aRate[$home_team_id] = array('team' => $c_home["team"], 'POINTS' => $POINTS, 'PG' => $PG, 'V' => $V, 'N' => $N, 'P' => $P, 'GF' => $GF, 'GS' => $GS, 'DR' => $DR, 'penality' => $p_home, 'order' => $home_order);
- else
- $aRate[$home_team_id] = array('team' => $c_home["team"], 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $p_home, 'order' => $home_order);
- $c_away = $aRate[$away_team_id];
- $POINTS = $c_away["POINTS"] + $g->away_points;
- if ($away_order == -999)
- $POINTS = 0;
- if ($g->played && !$home_no_game)
- {
- $PG = $c_away["PG"] + ($g->played ? 1 : 0);
- $V = $c_away["V"] + ($g->away_goals > $g->home_goals ? 1 : 0);
- $N = $c_away["N"] + ($g->away_goals == $g->home_goals ? 1 : 0);
- $P = $c_away["P"] + ($g->away_goals < $g->home_goals ? 1 : 0);
- $GF = $c_away["GF"] + $g->away_goals;
- $GS = $c_away["GS"] + $g->home_goals;
- $DR = $GF - $GS;
- }
- else
- {
- $PG = $c_away["PG"];
- $V = $c_away["V"];
- $N = $c_away["N"];
- $P = $c_away["P"];
- $GF = $c_away["GF"];
- $GS = $c_away["GS"];
- $DR = $GF - $GS;
- }
- if (!$away_pen)
- $aRate[$away_team_id] = array('team' => $c_away["team"], 'POINTS' => $POINTS,'PG' => $PG, 'V' => $V, 'N' => $N, 'P' => $P, 'GF' => $GF, 'GS' => $GS, 'DR' => $DR, 'penality' => $p_away, 'order' => $away_order);
- else
- $aRate[$away_team_id] = array('team' => $c_away["team"], 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $p_away, 'order' => $away_order);
- }
- }
- }
- $sorts = array('order' => 'desc', 'POINTS' => 'desc', 'DR' => 'desc', 'GF' => 'desc');
- usort($aRate, function($a, $b) use ($sorts) {
- foreach($sorts as $field => $direction) {
- if ($a[$field] != $b[$field]) {
- if ($direction == 'asc') {
- return $a[$field] < $b[$field] ? -1 : 1;
- }
- return $a[$field] < $b[$field] ? 1 : -1;
- }
- }
- return 0;
- });
- return $aRate;
- }
- public function by_points($a, $b)
- {
- return $a['POINTS'] > $b['POINTS'];
- }
- }
|