Calendar.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\CalendarGame;
  5. class Calendar extends Model
  6. {
  7. protected $fillable = [
  8. 'name',
  9. 'section_id',
  10. 'region_id',
  11. 'season_id',
  12. 'category_id',
  13. 'group_id',
  14. 'type',
  15. 'position',
  16. 'archived'
  17. ];
  18. public function season()
  19. {
  20. return $this->belongsTo('App\Season');
  21. }
  22. public function section()
  23. {
  24. return $this->belongsTo('App\Section');
  25. }
  26. public function region()
  27. {
  28. return $this->belongsTo('App\Section');
  29. }
  30. public function category()
  31. {
  32. return $this->belongsTo('App\Category');
  33. }
  34. public function group()
  35. {
  36. return $this->belongsTo('App\Group');
  37. }
  38. public function getRates()
  39. {
  40. /*
  41. Penalizzazioni
  42. Se mi ritiro prima del girone di ritorno
  43. Mi ritiro alla 10
  44. Dall’11 in poi scrivo riposo
  45. Gestione punti penalizzazione rinunce
  46. Se dal ritorno, quelle prima valide, quelle dopo il ritiro 0-6 tavolino
  47. Se è stata esclusa in classifica tutto a 0
  48. */
  49. $aRate = array();
  50. // Carico le squadre
  51. $games = CalendarGame::where('calendar_id', '=', $this->id)->get();
  52. foreach($games as $g)
  53. {
  54. $home_team_id = $g->home_team_id;
  55. //if ($g->type == 'RITORNO')
  56. // $home_team_id = $g->away_team_id;
  57. $away_team_id = $g->away_team_id;
  58. //if ($g->type == 'RITORNO')
  59. // $homeaway_team_id = $g->home_team_id;
  60. if ($home_team_id != null && $away_team_id != null)
  61. {
  62. if (!isset($aRate[$home_team_id]))
  63. {
  64. $t = Team::findOrFail($home_team_id);
  65. $points = 0;
  66. if ($t->penality > 0)
  67. $points -= $t->penality;
  68. $penality = '';
  69. $order = 0;
  70. if ($t->excluded)
  71. {
  72. $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  73. $order = -999;
  74. }
  75. if ($t->type != '' && $t->type != '-' && !$t->excluded)
  76. {
  77. $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  78. $order = -999;
  79. }
  80. if ($t->penality > 0 && $penality == '')
  81. {
  82. $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
  83. // $order = -555;
  84. }
  85. $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);
  86. }
  87. if (!isset($aRate[$away_team_id]))
  88. {
  89. $t = Team::findOrFail($away_team_id);
  90. $points = 0;
  91. if ($t->penality > 0)
  92. $points -= $t->penality;
  93. $penality = '';
  94. $order = 0;
  95. if ($t->excluded)
  96. {
  97. $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  98. $order = -999;
  99. }
  100. if ($t->type != '' && $t->type != '-' && !$t->excluded)
  101. {
  102. $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  103. $order = -999;
  104. }
  105. if ($t->penality > 0 && $penality == '')
  106. {
  107. $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
  108. // $order = -555;
  109. }
  110. $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);
  111. }
  112. if ($home_team_id != NULL && $away_team_id != NULL && $g->played)
  113. {
  114. if (!isset($aRate[$home_team_id]))
  115. {
  116. $t = Team::findOrFail($home_team_id);
  117. $points = 0;
  118. if ($t->penality > 0)
  119. $points -= $t->penality;
  120. $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);
  121. }
  122. if (!isset($aRate[$away_team_id]))
  123. {
  124. $t = Team::findOrFail($away_team_id);
  125. $points = 0;
  126. if ($t->penality > 0)
  127. $points -= $t->penality;
  128. $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);
  129. }
  130. // Controllo la squadra di casa
  131. $t = Team::findOrFail($home_team_id);
  132. $p_home = '';
  133. $home_order = 0;
  134. $home_pen = false;
  135. $home_no_game = false;
  136. if ($t->excluded)
  137. {
  138. $home_pen = true;
  139. $home_order = -999;
  140. $p_home .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  141. $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);
  142. }
  143. if ($t->type != '' && $t->type != '-' && !$t->excluded)
  144. {
  145. $home_pen = true;
  146. $home_order = -999;
  147. $p_home .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  148. $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);
  149. }
  150. if ($t->penality > 0 && $p_home == '')
  151. {
  152. $p_home .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
  153. //$home_order = -555;
  154. }
  155. $t = Team::findOrFail($away_team_id);
  156. $p_away = '';
  157. $away_order = 0;
  158. $away_pen = false;
  159. $away_no_game = false;
  160. if ($t->excluded)
  161. {
  162. $away_pen = true;
  163. $away_order = -999;
  164. $p_away .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  165. $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);
  166. }
  167. if ($t->type != '' && $t->type != '-' && !$t->excluded)
  168. {
  169. $away_pen = true;
  170. $away_order = -999;
  171. $p_away .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
  172. $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);
  173. }
  174. if ($t->penality > 0 && $p_away == '')
  175. {
  176. $p_away .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
  177. // $away_order = -555;
  178. }
  179. // HOME
  180. $t = Team::findOrFail($home_team_id);
  181. if (($t->type != '' && $t->type != '-') || $t->excluded)
  182. {
  183. if ($t->type == 'andata')
  184. {
  185. $home_no_game = true;
  186. $g->home_goals = 0;
  187. $g->home_points = 0;
  188. $g->away_goals = 0;
  189. $g->away_points = 0;
  190. /*
  191. if ($g->type == 'ANDATA')
  192. {
  193. if ($g->day > $t->day)
  194. {
  195. $g->home_goals = 0;
  196. $g->home_points = 0;
  197. }
  198. }
  199. if ($g->type == 'RITORNO')
  200. {
  201. $g->home_goals = 0;
  202. $g->home_points = 0;
  203. }
  204. */
  205. }
  206. if ($t->type == 'ritorno')
  207. {
  208. if ($g->type == 'RITORNO')
  209. {
  210. if ($g->day > $t->day)
  211. {
  212. $g->home_goals = 0;
  213. $g->home_points = 0;
  214. $g->away_goals = 6;
  215. $g->away_points = 3;
  216. }
  217. }
  218. }
  219. }
  220. // AWAY
  221. $t = Team::findOrFail($away_team_id);
  222. if (($t->type != '' && $t->type != '-') || $t->excluded)
  223. {
  224. if ($t->type == 'andata')
  225. {
  226. $away_no_game = true;
  227. $g->away_goals = 0;
  228. $g->away_points = 0;
  229. $g->home_goals = 0;
  230. $g->home_points = 0;
  231. /*if ($g->type == 'ANDATA')
  232. {
  233. if ($g->day > $t->day)
  234. {
  235. $g->away_goals = 0;
  236. $g->away_points = 0;
  237. }
  238. }
  239. if ($g->type == 'RITORNO')
  240. {
  241. $g->away_goals = 0;
  242. $g->away_points = 0;
  243. }*/
  244. }
  245. if ($t->type == 'ritorno')
  246. {
  247. if ($g->type == 'RITORNO')
  248. {
  249. if ($g->day > $t->day)
  250. {
  251. $g->away_goals = 0;
  252. $g->away_points = 0;
  253. $g->home_goals = 6;
  254. $g->home_points = 3;
  255. }
  256. }
  257. }
  258. }
  259. $c_home = $aRate[$home_team_id];
  260. $POINTS = $c_home["POINTS"] + $g->home_points;
  261. if ($home_order == -999)
  262. $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);
  263. if ($g->played && !$away_no_game)
  264. {
  265. $PG = $c_home["PG"] + ($g->played ? 1 : 0);
  266. $V = $c_home["V"] + ($g->home_goals > $g->away_goals ? 1 : 0);
  267. $N = $c_home["N"] + ($g->home_goals == $g->away_goals ? 1 : 0);
  268. $P = $c_home["P"] + ($g->home_goals < $g->away_goals ? 1 : 0);
  269. $GF = $c_home["GF"] + $g->home_goals;
  270. $GS = $c_home["GS"] + $g->away_goals;
  271. $DR = $GF - $GS;
  272. }
  273. else
  274. {
  275. $PG = $c_home["PG"];
  276. $V = $c_home["V"];
  277. $N = $c_home["N"];
  278. $P = $c_home["P"];
  279. $GF = $c_home["GF"];
  280. $GS = $c_home["GS"];
  281. $DR = $GF - $GS;
  282. }
  283. if (!$home_pen)
  284. $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);
  285. else
  286. $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);
  287. $c_away = $aRate[$away_team_id];
  288. $POINTS = $c_away["POINTS"] + $g->away_points;
  289. if ($away_order == -999)
  290. $POINTS = 0;
  291. if ($g->played && !$home_no_game)
  292. {
  293. $PG = $c_away["PG"] + ($g->played ? 1 : 0);
  294. $V = $c_away["V"] + ($g->away_goals > $g->home_goals ? 1 : 0);
  295. $N = $c_away["N"] + ($g->away_goals == $g->home_goals ? 1 : 0);
  296. $P = $c_away["P"] + ($g->away_goals < $g->home_goals ? 1 : 0);
  297. $GF = $c_away["GF"] + $g->away_goals;
  298. $GS = $c_away["GS"] + $g->home_goals;
  299. $DR = $GF - $GS;
  300. }
  301. else
  302. {
  303. $PG = $c_away["PG"];
  304. $V = $c_away["V"];
  305. $N = $c_away["N"];
  306. $P = $c_away["P"];
  307. $GF = $c_away["GF"];
  308. $GS = $c_away["GS"];
  309. $DR = $GF - $GS;
  310. }
  311. if (!$away_pen)
  312. $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);
  313. else
  314. $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);
  315. }
  316. }
  317. }
  318. $sorts = array('order' => 'desc', 'POINTS' => 'desc', 'DR' => 'desc', 'GF' => 'desc');
  319. usort($aRate, function($a, $b) use ($sorts) {
  320. foreach($sorts as $field => $direction) {
  321. if ($a[$field] != $b[$field]) {
  322. if ($direction == 'asc') {
  323. return $a[$field] < $b[$field] ? -1 : 1;
  324. }
  325. return $a[$field] < $b[$field] ? 1 : -1;
  326. }
  327. }
  328. return 0;
  329. });
  330. return $aRate;
  331. }
  332. public function by_points($a, $b)
  333. {
  334. return $a['POINTS'] > $b['POINTS'];
  335. }
  336. }