|
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
|
|
|
return $this->getMissionGameMapper()->findBy(array('mission'=>$mission)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
public function checkMissionCondition($game, $winner, $prediction, $entry) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$missionGame = $this->findMissionGameByGame($game); |
|
|
|
|
|
|
58
|
|
|
if(empty($missionGame)){ |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if($missionGame->getMission()->getActive() === false){ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$nextMissionGame = $this->getMissionGameMapper()->getNextGame($missionGame->getMission(), $missionGame->getPosition()); |
|
67
|
|
|
|
|
68
|
|
|
if(empty($nextMissionGame)){ |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if(empty($missionGameConditions)){ |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach ($missionGameConditions as $missionGameCondition) { |
|
79
|
|
|
|
|
80
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) { |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// On passe au suivant si on a gagné |
|
85
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) { |
|
86
|
|
|
if(!($winner || $prediction)){ |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// On passe au suivant si on a perdu |
|
92
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) { |
|
93
|
|
|
if($winner || $prediction){ |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// On passe au suivant si on a perdu |
|
99
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) { |
|
100
|
|
|
if (!$entry) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
if(!($entry->getPoints() > $missionGameCondition->getValue())){ |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// On passe au suivant si on a perdu |
|
109
|
|
|
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) { |
|
110
|
|
|
if (!$entry) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
if(!($entry->getPoints() < $missionGameCondition->getValue())){ |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $nextMissionGame->getGame(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getGameEntity() |
|
123
|
|
|
{ |
|
124
|
|
|
return new \PlaygroundGame\Entity\Mission; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* getMissionMapper |
|
129
|
|
|
* |
|
130
|
|
|
* @return MissionMapperInterface |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getMissionMapper() |
|
133
|
|
|
{ |
|
134
|
|
|
if (null === $this->missionMapper) { |
|
135
|
|
|
$this->missionMapper = $this->getServiceManager()->get('playgroundgame_mission_mapper'); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->missionMapper; |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* setMissionMapper |
|
143
|
|
|
* |
|
144
|
|
|
* @param MissionMapperInterface $missionMapper |
|
145
|
|
|
* @return User |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setMissionMapper($missionMapper) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->missionMapper = $missionMapper; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* getMissionGameMapper |
|
156
|
|
|
* |
|
157
|
|
|
* @return MissionGameMapperInterface |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function getMissionGameMapper() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
if (null === $this->missionGameMapper) { |
|
162
|
|
|
$this->missionGameMapper = $this->getServiceManager()->get('playgroundgame_mission_game_mapper'); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->missionGameMapper; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* setMissionMapper |
|
170
|
|
|
* |
|
171
|
|
|
* @param MissionMapperInterface $missionMapper |
|
|
|
|
|
|
172
|
|
|
* @return User |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setMissionGameMapper($missionGameMapper) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->missionGameMapper = $missionGameMapper; |
|
177
|
|
|
|
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
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.