1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Service; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
6
|
|
|
use Zend\ServiceManager\ServiceManager; |
7
|
|
|
use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator; |
8
|
|
|
use PlaygroundGame\Service\Game; |
9
|
|
|
use Zend\Db\Sql\Sql; |
10
|
|
|
use Zend\Db\Adapter\Adapter; |
11
|
|
|
|
12
|
|
|
class Mission extends Game implements ServiceManagerAwareInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var MissionMapperInterface |
17
|
|
|
*/ |
18
|
|
|
protected $missionMapper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var MissionGameMapperInterface |
22
|
|
|
*/ |
23
|
|
|
protected $missionGameMapper; |
24
|
|
|
|
25
|
|
|
protected $options; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Find games associated to a mission and add the last entry of the user if it exists |
29
|
|
|
* @param unknown $mission |
30
|
|
|
* @return multitype:NULL |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function getMissionGames($mission, $user) |
33
|
|
|
{ |
34
|
|
|
$games = array(); |
35
|
|
|
$missionGames = $this->findGamesByMission($mission); |
36
|
|
|
foreach ($missionGames as $missionGame) { |
37
|
|
|
$entry = $this->checkExistingEntry($missionGame->getGame(), $user); |
38
|
|
|
$games[$missionGame->getGame()->getIdentifier()]['game'] = $missionGame; |
39
|
|
|
$games[$missionGame->getGame()->getIdentifier()]['entry'] = $entry; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $games; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* findMissionGameByMission : find associated games to a mission |
47
|
|
|
* @param Mission $mission |
48
|
|
|
* |
49
|
|
|
* @return Collection de MissionGame $missionGames |
50
|
|
|
*/ |
51
|
|
|
public function findGamesByMission($mission) |
52
|
|
|
{ |
53
|
|
|
return $this->getMissionGameMapper()->findBy(array('mission'=>$mission)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function checkMissionCondition($game, $winner, $prediction, $entry) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$missionGame = $this->findMissionGameByGame($game); |
|
|
|
|
59
|
|
|
if (empty($missionGame)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($missionGame->getMission()->getActive() === false) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$nextMissionGame = $this->getMissionGameMapper()->getNextGame( |
68
|
|
|
$missionGame->getMission(), |
69
|
|
|
$missionGame->getPosition() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
if (empty($nextMissionGame)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (empty($missionGameConditions)) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($missionGameConditions as $missionGameCondition) { |
83
|
|
|
|
84
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// On passe au suivant si on a gagné |
89
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) { |
90
|
|
|
if (!($winner || $prediction)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// On passe au suivant si on a perdu |
96
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) { |
97
|
|
|
if ($winner || $prediction) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// On passe au suivant si on a perdu |
103
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) { |
104
|
|
|
if (!$entry) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
if (!($entry->getPoints() > $missionGameCondition->getValue())) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// On passe au suivant si on a perdu |
113
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) { |
114
|
|
|
if (!$entry) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
if (!($entry->getPoints() < $missionGameCondition->getValue())) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $nextMissionGame->getGame(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getGameEntity() |
127
|
|
|
{ |
128
|
|
|
return new \PlaygroundGame\Entity\Mission; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* getMissionMapper |
133
|
|
|
* |
134
|
|
|
* @return MissionMapperInterface |
135
|
|
|
*/ |
136
|
|
|
public function getMissionMapper() |
137
|
|
|
{ |
138
|
|
|
if (null === $this->missionMapper) { |
139
|
|
|
$this->missionMapper = $this->getServiceManager()->get('playgroundgame_mission_mapper'); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->missionMapper; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* setMissionMapper |
147
|
|
|
* |
148
|
|
|
* @param MissionMapperInterface $missionMapper |
149
|
|
|
* @return Mission |
150
|
|
|
*/ |
151
|
|
|
public function setMissionMapper($missionMapper) |
152
|
|
|
{ |
153
|
|
|
$this->missionMapper = $missionMapper; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* getMissionGameMapper |
160
|
|
|
* |
161
|
|
|
* @return MissionGameMapperInterface |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function getMissionGameMapper() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if (null === $this->missionGameMapper) { |
166
|
|
|
$this->missionGameMapper = $this->getServiceManager()->get('playgroundgame_mission_game_mapper'); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->missionGameMapper; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* setMissionMapper |
174
|
|
|
* |
175
|
|
|
* @param MissionMapperInterface $missionGameMapper |
176
|
|
|
* @return Mission |
177
|
|
|
*/ |
178
|
|
|
public function setMissionGameMapper($missionGameMapper) |
179
|
|
|
{ |
180
|
|
|
$this->missionGameMapper = $missionGameMapper; |
|
|
|
|
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.