-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatchResult.php
130 lines (121 loc) · 3.32 KB
/
MatchResult.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Lib\DsManager\Models;
use App\Lib\DsManager\Helpers\Randomizer;
use App\Lib\DsManager\Models\Common\DsManagerModel;
use App\Lib\Helpers\Config;
/**
* Class MatchResult
* @package App\Lib\DsManager\Models
*/
class MatchResult extends DsManagerModel
{
/**
* @var
*/
private $goalHome;
/**
* @var
*/
private $goalAway;
/**
* @var Team
*/
private $homeTeam;
/**
* @var Team
*/
private $awayTeam;
/**
* MatchResult constructor.
* @param $goalHome
* @param $goalAway
* @param Team $home
* @param Team $away
*/
public function __construct($goalHome, $goalAway, Team $home, Team $away)
{
$this->goalHome = $goalHome;
$this->goalAway = $goalAway;
$this->homeTeam = $home;
$this->awayTeam = $away;
}
/**
* @return array
*/
public function getWinnerLoser()
{
$isDraw = false;
$winner = $this->homeTeam;
$loser = $this->awayTeam;
if ($this->goalAway == $this->goalHome) {
$isDraw = true;
} else if ($this->goalHome < $this->goalAway) {
$winner = $this->awayTeam;
$loser = $this->homeTeam;
}
return [
'is_draw' => $isDraw,
'winner_id' => $winner->id,
'loser_id' => $loser->id
];
}
/**
* @return array
*/
public function toArray()
{
$result = [];
$result['home_team_id'] = $this->homeTeam->id;
$result['away_team_id'] = $this->awayTeam->id;
$result['goal_home'] = $this->goalHome;
$result['goal_away'] = $this->goalAway;
$result['info'] = $this->getWinnerLoser();
$result['info']['scorers'] = $this->getScorers();
$result['simulated'] = true;
return $result;
}
/**
* @return array
*/
private function getScorers()
{
$scorers = [
'home' => [],
'away' => []
];
for ($i = 0; $i < $this->goalHome; $i++) {
$scorers['home'][] = $this->pickAScorer($this->homeTeam);
}
for ($i = 0; $i < $this->goalAway; $i++) {
$scorers['away'][] = $this->pickAScorer($this->awayTeam);
}
return $scorers;
}
/**
* @param Team $team
* @return Player
*/
private function pickAScorer(Team $team)
{
$player = null;
if (Randomizer::boolOnPercentage(70)) {
$roles = Config::get('modules.roles');
$forwards = array_splice($roles, count($roles) / 2);
$pos = array_rand($forwards);
unset($forwards[$pos]);
$player = $team->getBestPlayerForRole($pos);
while (empty($player)) {
if (!empty($forwards)) {
$pos = array_rand($forwards);
unset($forwards[$pos]);
$player = $team->getBestPlayerForRole($pos);
} else {
$player = $team->roster[array_rand($team->roster)];
}
}
} else {
$player = $team->roster[array_rand($team->roster)];
}
return $player;
}
}