@@ -13,118 +13,118 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class MatchResult extends DsManagerModel |
| 15 | 15 | {
|
| 16 | - /** |
|
| 17 | - * @var |
|
| 18 | - */ |
|
| 19 | - private $goalHome; |
|
| 20 | - /** |
|
| 21 | - * @var |
|
| 22 | - */ |
|
| 23 | - private $goalAway; |
|
| 24 | - /** |
|
| 25 | - * @var Team |
|
| 26 | - */ |
|
| 27 | - private $homeTeam; |
|
| 28 | - /** |
|
| 29 | - * @var Team |
|
| 30 | - */ |
|
| 31 | - private $awayTeam; |
|
| 16 | + /** |
|
| 17 | + * @var |
|
| 18 | + */ |
|
| 19 | + private $goalHome; |
|
| 20 | + /** |
|
| 21 | + * @var |
|
| 22 | + */ |
|
| 23 | + private $goalAway; |
|
| 24 | + /** |
|
| 25 | + * @var Team |
|
| 26 | + */ |
|
| 27 | + private $homeTeam; |
|
| 28 | + /** |
|
| 29 | + * @var Team |
|
| 30 | + */ |
|
| 31 | + private $awayTeam; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * MatchResult constructor. |
|
| 35 | - * @param $goalHome |
|
| 36 | - * @param $goalAway |
|
| 37 | - * @param Team $home |
|
| 38 | - * @param Team $away |
|
| 39 | - */ |
|
| 40 | - public function __construct($goalHome, $goalAway, Team $home, Team $away) |
|
| 41 | - {
|
|
| 42 | - $this->goalHome = $goalHome; |
|
| 43 | - $this->goalAway = $goalAway; |
|
| 44 | - $this->homeTeam = $home; |
|
| 45 | - $this->awayTeam = $away; |
|
| 46 | - } |
|
| 33 | + /** |
|
| 34 | + * MatchResult constructor. |
|
| 35 | + * @param $goalHome |
|
| 36 | + * @param $goalAway |
|
| 37 | + * @param Team $home |
|
| 38 | + * @param Team $away |
|
| 39 | + */ |
|
| 40 | + public function __construct($goalHome, $goalAway, Team $home, Team $away) |
|
| 41 | + {
|
|
| 42 | + $this->goalHome = $goalHome; |
|
| 43 | + $this->goalAway = $goalAway; |
|
| 44 | + $this->homeTeam = $home; |
|
| 45 | + $this->awayTeam = $away; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @return array |
|
| 50 | - */ |
|
| 51 | - public function getWinnerLoser() |
|
| 52 | - {
|
|
| 53 | - $isDraw = false; |
|
| 54 | - $winner = $this->awayTeam; |
|
| 55 | - $loser = $this->homeTeam; |
|
| 56 | - if ($this->goalAway == $this->goalHome) {
|
|
| 57 | - $isDraw = true; |
|
| 58 | - } else if ($this->goalHome < $this->goalAway) {
|
|
| 59 | - $winner = $this->awayTeam; |
|
| 60 | - $loser = $this->homeTeam; |
|
| 61 | - } |
|
| 62 | - return [ |
|
| 63 | - 'is_draw' => $isDraw, |
|
| 64 | - 'winner_id' => $winner->id, |
|
| 65 | - 'loser_id' => $loser->id |
|
| 66 | - ]; |
|
| 67 | - } |
|
| 48 | + /** |
|
| 49 | + * @return array |
|
| 50 | + */ |
|
| 51 | + public function getWinnerLoser() |
|
| 52 | + {
|
|
| 53 | + $isDraw = false; |
|
| 54 | + $winner = $this->awayTeam; |
|
| 55 | + $loser = $this->homeTeam; |
|
| 56 | + if ($this->goalAway == $this->goalHome) {
|
|
| 57 | + $isDraw = true; |
|
| 58 | + } else if ($this->goalHome < $this->goalAway) {
|
|
| 59 | + $winner = $this->awayTeam; |
|
| 60 | + $loser = $this->homeTeam; |
|
| 61 | + } |
|
| 62 | + return [ |
|
| 63 | + 'is_draw' => $isDraw, |
|
| 64 | + 'winner_id' => $winner->id, |
|
| 65 | + 'loser_id' => $loser->id |
|
| 66 | + ]; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - public function toArray() |
|
| 73 | - {
|
|
| 74 | - $result = []; |
|
| 75 | - $result['home_team_id'] = $this->homeTeam->id; |
|
| 76 | - $result['away_team_id'] = $this->awayTeam->id; |
|
| 77 | - $result['goal_home'] = $this->goalHome; |
|
| 78 | - $result['goal_away'] = $this->goalAway; |
|
| 79 | - $result['info'] = $this->getWinnerLoser(); |
|
| 80 | - $result['info']['scorers'] = $this->getScorers(); |
|
| 81 | - $result['simulated'] = true; |
|
| 82 | - return $result; |
|
| 83 | - } |
|
| 84 | - /** |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - private function getScorers() |
|
| 88 | - {
|
|
| 89 | - $scorers = [ |
|
| 90 | - 'home' => [], |
|
| 91 | - 'away' => [] |
|
| 92 | - ]; |
|
| 93 | - for ($i = 0; $i < $this->goalHome; $i++) {
|
|
| 94 | - $scorers['home'][] = $this->pickAScorer($this->homeTeam); |
|
| 95 | - } |
|
| 96 | - for ($i = 0; $i < $this->goalAway; $i++) {
|
|
| 97 | - $scorers['away'][] = $this->pickAScorer($this->awayTeam); |
|
| 98 | - } |
|
| 99 | - return $scorers; |
|
| 100 | - } |
|
| 69 | + /** |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + public function toArray() |
|
| 73 | + {
|
|
| 74 | + $result = []; |
|
| 75 | + $result['home_team_id'] = $this->homeTeam->id; |
|
| 76 | + $result['away_team_id'] = $this->awayTeam->id; |
|
| 77 | + $result['goal_home'] = $this->goalHome; |
|
| 78 | + $result['goal_away'] = $this->goalAway; |
|
| 79 | + $result['info'] = $this->getWinnerLoser(); |
|
| 80 | + $result['info']['scorers'] = $this->getScorers(); |
|
| 81 | + $result['simulated'] = true; |
|
| 82 | + return $result; |
|
| 83 | + } |
|
| 84 | + /** |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + private function getScorers() |
|
| 88 | + {
|
|
| 89 | + $scorers = [ |
|
| 90 | + 'home' => [], |
|
| 91 | + 'away' => [] |
|
| 92 | + ]; |
|
| 93 | + for ($i = 0; $i < $this->goalHome; $i++) {
|
|
| 94 | + $scorers['home'][] = $this->pickAScorer($this->homeTeam); |
|
| 95 | + } |
|
| 96 | + for ($i = 0; $i < $this->goalAway; $i++) {
|
|
| 97 | + $scorers['away'][] = $this->pickAScorer($this->awayTeam); |
|
| 98 | + } |
|
| 99 | + return $scorers; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * @param Team $team |
|
| 104 | - * @return Player |
|
| 105 | - */ |
|
| 106 | - private function pickAScorer(Team $team) |
|
| 107 | - {
|
|
| 108 | - $player = null; |
|
| 109 | - if (Randomizer::boolOnPercentage(70)) {
|
|
| 110 | - $roles = Config::get('modules.roles');
|
|
| 111 | - $forwards = array_splice($roles, count($roles) / 2); |
|
| 112 | - $pos = array_rand($forwards); |
|
| 113 | - unset($forwards[$pos]); |
|
| 114 | - $player = $team->getBestPlayerForRole($pos); |
|
| 115 | - while (empty($player)) {
|
|
| 116 | - if (!empty($forwards)) {
|
|
| 117 | - $pos = array_rand($forwards); |
|
| 118 | - unset($forwards[$pos]); |
|
| 119 | - $player = $team->getBestPlayerForRole($pos); |
|
| 120 | - } else {
|
|
| 121 | - $player = $team->roster[array_rand($team->roster)]; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } else {
|
|
| 125 | - $player = $team->roster[array_rand($team->roster)]; |
|
| 126 | - } |
|
| 127 | - return $player; |
|
| 128 | - } |
|
| 102 | + /** |
|
| 103 | + * @param Team $team |
|
| 104 | + * @return Player |
|
| 105 | + */ |
|
| 106 | + private function pickAScorer(Team $team) |
|
| 107 | + {
|
|
| 108 | + $player = null; |
|
| 109 | + if (Randomizer::boolOnPercentage(70)) {
|
|
| 110 | + $roles = Config::get('modules.roles');
|
|
| 111 | + $forwards = array_splice($roles, count($roles) / 2); |
|
| 112 | + $pos = array_rand($forwards); |
|
| 113 | + unset($forwards[$pos]); |
|
| 114 | + $player = $team->getBestPlayerForRole($pos); |
|
| 115 | + while (empty($player)) {
|
|
| 116 | + if (!empty($forwards)) {
|
|
| 117 | + $pos = array_rand($forwards); |
|
| 118 | + unset($forwards[$pos]); |
|
| 119 | + $player = $team->getBestPlayerForRole($pos); |
|
| 120 | + } else {
|
|
| 121 | + $player = $team->roster[array_rand($team->roster)]; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } else {
|
|
| 125 | + $player = $team->roster[array_rand($team->roster)]; |
|
| 126 | + } |
|
| 127 | + return $player; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
| 131 | 131 | \ No newline at end of file |
@@ -8,91 +8,91 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class MatchResult extends Match |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected $fillable = [ |
|
| 15 | - 'goal_home', |
|
| 16 | - 'goal_away', |
|
| 17 | - 'info', |
|
| 18 | - 'simulated' |
|
| 19 | - ]; |
|
| 11 | + /** |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected $fillable = [ |
|
| 15 | + 'goal_home', |
|
| 16 | + 'goal_away', |
|
| 17 | + 'info', |
|
| 18 | + 'simulated' |
|
| 19 | + ]; |
|
| 20 | 20 | |
| 21 | - protected $hidden = [ |
|
| 22 | - 'home_team_id', |
|
| 23 | - 'away_team_id', |
|
| 24 | - 'created_at', |
|
| 25 | - 'updated_at' |
|
| 26 | - ]; |
|
| 21 | + protected $hidden = [ |
|
| 22 | + 'home_team_id', |
|
| 23 | + 'away_team_id', |
|
| 24 | + 'created_at', |
|
| 25 | + 'updated_at' |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $casts = [ |
|
| 32 | - 'info' => 'json', |
|
| 33 | - 'simulated' => 'boolean' |
|
| 34 | - ]; |
|
| 28 | + /** |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $casts = [ |
|
| 32 | + 'info' => 'json', |
|
| 33 | + 'simulated' => 'boolean' |
|
| 34 | + ]; |
|
| 35 | 35 | |
| 36 | - public static function resolveAttributes(array $attributes, $matchId) |
|
| 37 | - { |
|
| 38 | - if (array_key_exists('info', $attributes)) { |
|
| 39 | - if (array_key_exists('scorers', $attributes['info'])) { |
|
| 40 | - foreach ($attributes['info']['scorers']['home'] as $scorerHome) { |
|
| 41 | - self::addScorer($matchId, $attributes['home_team_id'], $scorerHome->id); |
|
| 42 | - } |
|
| 43 | - foreach ($attributes['info']['scorers']['away'] as $scorerAway) { |
|
| 44 | - self::addScorer($matchId, $attributes['away_team_id'], $scorerAway->id); |
|
| 45 | - } |
|
| 46 | - unset($attributes['info']['scorers']); |
|
| 47 | - } |
|
| 48 | - $attributes['info'] = json_encode($attributes['info']); |
|
| 49 | - } |
|
| 50 | - return $attributes; |
|
| 51 | - } |
|
| 36 | + public static function resolveAttributes(array $attributes, $matchId) |
|
| 37 | + { |
|
| 38 | + if (array_key_exists('info', $attributes)) { |
|
| 39 | + if (array_key_exists('scorers', $attributes['info'])) { |
|
| 40 | + foreach ($attributes['info']['scorers']['home'] as $scorerHome) { |
|
| 41 | + self::addScorer($matchId, $attributes['home_team_id'], $scorerHome->id); |
|
| 42 | + } |
|
| 43 | + foreach ($attributes['info']['scorers']['away'] as $scorerAway) { |
|
| 44 | + self::addScorer($matchId, $attributes['away_team_id'], $scorerAway->id); |
|
| 45 | + } |
|
| 46 | + unset($attributes['info']['scorers']); |
|
| 47 | + } |
|
| 48 | + $attributes['info'] = json_encode($attributes['info']); |
|
| 49 | + } |
|
| 50 | + return $attributes; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - private static function addScorer($matchId, $teamId, $playerId) |
|
| 54 | - { |
|
| 55 | - $scorer = MatchPlayer::where( |
|
| 56 | - [ |
|
| 57 | - 'match_id' => $matchId, |
|
| 58 | - 'team_id' => $teamId, |
|
| 59 | - 'player_id' => $playerId |
|
| 60 | - ] |
|
| 61 | - )->where('goals', '>', 0)->first(); |
|
| 62 | - if (!empty($scorer)) { |
|
| 63 | - $scorer->goals = $scorer->goals + 1; |
|
| 64 | - $scorer->vote = $scorer->vote + rand(0, 1); |
|
| 65 | - $scorer->save(); |
|
| 66 | - } else { |
|
| 67 | - MatchPlayer::create( |
|
| 68 | - [ |
|
| 69 | - 'match_id' => $matchId, |
|
| 70 | - 'team_id' => $teamId, |
|
| 71 | - 'player_id' => $playerId, |
|
| 72 | - 'goals' => 1 |
|
| 73 | - ] |
|
| 74 | - ); |
|
| 75 | - } |
|
| 76 | - } |
|
| 53 | + private static function addScorer($matchId, $teamId, $playerId) |
|
| 54 | + { |
|
| 55 | + $scorer = MatchPlayer::where( |
|
| 56 | + [ |
|
| 57 | + 'match_id' => $matchId, |
|
| 58 | + 'team_id' => $teamId, |
|
| 59 | + 'player_id' => $playerId |
|
| 60 | + ] |
|
| 61 | + )->where('goals', '>', 0)->first(); |
|
| 62 | + if (!empty($scorer)) { |
|
| 63 | + $scorer->goals = $scorer->goals + 1; |
|
| 64 | + $scorer->vote = $scorer->vote + rand(0, 1); |
|
| 65 | + $scorer->save(); |
|
| 66 | + } else { |
|
| 67 | + MatchPlayer::create( |
|
| 68 | + [ |
|
| 69 | + 'match_id' => $matchId, |
|
| 70 | + 'team_id' => $teamId, |
|
| 71 | + 'player_id' => $playerId, |
|
| 72 | + 'goals' => 1 |
|
| 73 | + ] |
|
| 74 | + ); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - public function scorers() |
|
| 79 | - { |
|
| 80 | - return $this->belongsToMany( |
|
| 81 | - Player::class, |
|
| 82 | - 'match_players', |
|
| 83 | - 'match_id' |
|
| 84 | - )->withPivot( |
|
| 85 | - 'team_id', |
|
| 86 | - 'goals' |
|
| 87 | - )->where( |
|
| 88 | - 'goals', '>', 0 |
|
| 89 | - ); |
|
| 90 | - } |
|
| 78 | + public function scorers() |
|
| 79 | + { |
|
| 80 | + return $this->belongsToMany( |
|
| 81 | + Player::class, |
|
| 82 | + 'match_players', |
|
| 83 | + 'match_id' |
|
| 84 | + )->withPivot( |
|
| 85 | + 'team_id', |
|
| 86 | + 'goals' |
|
| 87 | + )->where( |
|
| 88 | + 'goals', '>', 0 |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - public function scopeComplete($query) |
|
| 93 | - { |
|
| 94 | - return parent::scopeComplete($query)->with('scorers'); |
|
| 95 | - } |
|
| 92 | + public function scopeComplete($query) |
|
| 93 | + { |
|
| 94 | + return parent::scopeComplete($query)->with('scorers'); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | \ No newline at end of file |