1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace OSS\CoreBundle\Services;
|
4
|
|
|
|
5
|
|
|
use OSS\CoreBundle\Entity\Event;
|
6
|
|
|
use OSS\CoreBundle\Entity\Fixture;
|
7
|
|
|
use OSS\CoreBundle\Entity\Team;
|
8
|
|
|
use OSS\CoreBundle\Exception\MatchException;
|
9
|
|
|
|
10
|
|
|
class MatchEvaluationService
|
11
|
|
|
{
|
12
|
|
|
/**
|
13
|
|
|
* @param Fixture $fixture
|
14
|
|
|
*
|
15
|
|
|
* @throws MatchException
|
16
|
|
|
*/
|
17
|
6 |
|
public function evaluateCompleteMatch(Fixture $fixture)
|
18
|
|
|
{
|
19
|
6 |
|
for ($minute = 1; $minute <= $this->getMinutesToPlay(); $minute++) {
|
20
|
6 |
|
$this->evaluateMinuteOfMatch($fixture);
|
21
|
5 |
|
}
|
22
|
5 |
|
$fixture->setFinished(true);
|
23
|
5 |
|
if ($fixture->isDraw()) {
|
24
|
5 |
|
$fixture->getTeamHome()->addPoints(1);
|
25
|
5 |
|
$fixture->getTeamAway()->addPoints(1);
|
26
|
5 |
|
} elseif ($fixture->isHomeTeamWinner()) {
|
27
|
|
|
$fixture->getTeamHome()->addPoints(3);
|
28
|
|
|
} else {
|
29
|
|
|
$fixture->getTeamAway()->addPoints(3);
|
30
|
|
|
}
|
31
|
5 |
|
$fixture->getTeamHome()->addGoalsFor($fixture->getScoreHome());
|
32
|
5 |
|
$fixture->getTeamHome()->addGoalsAgainst($fixture->getScoreAway());
|
33
|
5 |
|
$fixture->getTeamAway()->addGoalsFor($fixture->getScoreAway());
|
34
|
5 |
|
$fixture->getTeamAway()->addGoalsAgainst($fixture->getScoreHome());
|
35
|
5 |
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @param Fixture $fixture
|
39
|
|
|
*
|
40
|
|
|
* @throws MatchException
|
41
|
|
|
*/
|
42
|
7 |
|
public function evaluateMinuteOfMatch(Fixture $fixture)
|
43
|
|
|
{
|
44
|
7 |
|
if (!$fixture->hasTeamHome()) {
|
45
|
1 |
|
throw new MatchException('no home team');
|
46
|
|
|
}
|
47
|
6 |
|
if (!$fixture->hasTeamAway()) {
|
48
|
|
|
throw new MatchException('no away team');
|
49
|
|
|
}
|
50
|
|
|
|
51
|
6 |
|
$fixture->incrementMinutesPlayed();
|
52
|
6 |
|
if (null === $fixture->getScoreHome()) {
|
53
|
6 |
|
$fixture->resetScoreHome();
|
54
|
6 |
|
}
|
55
|
6 |
|
if (null === $fixture->getScoreAway()) {
|
56
|
6 |
|
$fixture->resetScoreAway();
|
57
|
6 |
|
}
|
58
|
6 |
|
if ($this->happensEvent()) {
|
59
|
5 |
|
$fixture->addEvent($this->createRandomEvent($fixture));
|
60
|
5 |
|
}
|
61
|
6 |
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* @return int
|
65
|
|
|
*/
|
66
|
6 |
|
public function getMinutesToPlay()
|
67
|
|
|
{
|
68
|
6 |
|
return mt_rand(90, 95);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* @return bool
|
73
|
|
|
*/
|
74
|
7 |
|
public function happensEvent()
|
75
|
|
|
{
|
76
|
7 |
|
return mt_rand(1, 100) >= 95;
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* @param Fixture $fixture
|
81
|
|
|
*
|
82
|
|
|
* @return Event
|
83
|
|
|
*/
|
84
|
5 |
|
public function createRandomEvent(Fixture $fixture)
|
85
|
|
|
{
|
86
|
5 |
|
$possibleTeams = array($fixture->getTeamHome(), $fixture->getTeamAway());
|
87
|
|
|
|
88
|
|
|
/** @var Team $attackingTeam */
|
89
|
5 |
|
$attackingTeam = $possibleTeams[mt_rand(0, count($possibleTeams) - 1)];
|
90
|
5 |
|
$defendingTeam = $attackingTeam->equals($fixture->getTeamHome()) ? $fixture->getTeamAway() : $fixture->getTeamHome();
|
91
|
|
|
|
92
|
5 |
|
$attacker = $attackingTeam->getRandomPlayerFromLineup();
|
93
|
5 |
|
$defender = $defendingTeam->getRandomPlayerFromLineup();
|
94
|
|
|
|
95
|
5 |
|
$eventType = $attacker->getSkills()->getShooting() * 2 > $defender->getSkills()->getTackling() * 3 ? Event::TYPE_GOAL : Event::TYPE_CHANCE;
|
96
|
|
|
|
97
|
5 |
|
$event = Event::create($fixture, $eventType, $attackingTeam, $attacker, $fixture->getMinutesPlayed());
|
98
|
|
|
|
99
|
5 |
|
return $event;
|
100
|
|
|
}
|
101
|
|
|
}
|
102
|
|
|
|