1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Mapper; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Mapper\AbstractMapper; |
6
|
|
|
|
7
|
|
|
class Entry extends AbstractMapper |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function countByGame(\PlaygroundGame\Entity\Game $game) |
11
|
|
|
{ |
12
|
|
|
$query = $this->em->createQuery('SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e WHERE e.game = :game'); |
13
|
|
|
$query->setParameter('game', $game); |
14
|
|
|
return $query->getSingleScalarResult(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function draw($game, $userClass, $total) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$sql =' |
20
|
|
|
SELECT |
21
|
|
|
u.user_id uid, |
22
|
|
|
u.username, |
23
|
|
|
u.firstname, |
24
|
|
|
u.lastname, |
25
|
|
|
u.email, |
26
|
|
|
u.optin_partner, |
27
|
|
|
e.created_at ecreated_at, |
28
|
|
|
e.updated_at eupdated_at, |
29
|
|
|
e.* |
30
|
|
|
FROM game_entry as e |
31
|
|
|
INNER JOIN user AS u ON e.user_id = u.user_id |
32
|
|
|
WHERE e.game_id = :game_id AND e.drawable = 1 |
33
|
|
|
GROUP BY u.user_id |
34
|
|
|
ORDER BY RAND() |
35
|
|
|
LIMIT :total |
36
|
|
|
'; |
37
|
|
|
|
38
|
|
|
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->em); |
39
|
|
|
$rsm->addRootEntityFromClassMetadata( |
40
|
|
|
'\PlaygroundGame\Entity\Entry', |
41
|
|
|
'e', |
42
|
|
|
array('id' => 'id', 'created_at' => 'ecreated_at', 'updated_at' => 'eupdated_at') |
43
|
|
|
); |
44
|
|
|
$query = $this->em->createNativeQuery($sql, $rsm); |
45
|
|
|
$query->setParameter('game_id', $game->getId()); |
46
|
|
|
$query->setParameter('total', $total); |
47
|
|
|
|
48
|
|
|
return $query->getResult(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function queryByGame(\PlaygroundGame\Entity\Game $game) |
52
|
|
|
{ |
53
|
|
|
$query = $this->em->createQuery('SELECT e FROM PlaygroundGame\Entity\Entry e WHERE e.game = :game'); |
54
|
|
|
$query->setParameter('game', $game); |
55
|
|
|
return $query; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findByGameId($game) |
59
|
|
|
{ |
60
|
|
|
return $this->getEntityRepository()->findBy(array('game' => $game)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all the entries of the player except those offered as bonus |
65
|
|
|
* |
66
|
|
|
* @param unknown_type $game |
67
|
|
|
* @param unknown_type $user |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function findLastEntriesByUser($game, $user, $dateLimit) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$query = $this->em->createQuery( |
72
|
|
|
'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e |
73
|
|
|
WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date' |
74
|
|
|
); |
75
|
|
|
$query->setParameter('user', $user); |
76
|
|
|
$query->setParameter('game', $game); |
77
|
|
|
$query->setParameter('date', $dateLimit); |
78
|
|
|
|
79
|
|
|
$total = $query->getSingleScalarResult(); |
80
|
|
|
|
81
|
|
|
return $total; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function findLastEntriesByAnonymousIdentifier($game, $anonymousIdentifier, $dateLimit) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
$query = $this->em->createQuery( |
88
|
|
|
'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e |
89
|
|
|
WHERE e.anonymousIdentifier = :anonymousIdentifier AND e.game = :game |
90
|
|
|
AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date' |
91
|
|
|
); |
92
|
|
|
$query->setParameter('anonymousIdentifier', $anonymousIdentifier); |
93
|
|
|
$query->setParameter('game', $game); |
94
|
|
|
$query->setParameter('date', $dateLimit); |
95
|
|
|
|
96
|
|
|
$total = $query->getSingleScalarResult(); |
97
|
|
|
|
98
|
|
|
return $total; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function getDateLimit($limitScale) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$now = new \DateTime("now"); |
104
|
|
|
switch ($limitScale) { |
105
|
|
|
case 'always': |
106
|
|
|
$interval = 'P100Y'; |
107
|
|
|
$now->sub(new \DateInterval($interval)); |
108
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
109
|
|
|
break; |
110
|
|
|
case 'day': |
111
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
112
|
|
|
break; |
113
|
|
|
case 'week': |
114
|
|
|
$interval = 'P7D'; |
115
|
|
|
$now->sub(new \DateInterval($interval)); |
116
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
117
|
|
|
break; |
118
|
|
|
case 'month': |
119
|
|
|
$interval = 'P1M'; |
120
|
|
|
$now->sub(new \DateInterval($interval)); |
121
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
122
|
|
|
break; |
123
|
|
|
case 'year': |
124
|
|
|
$interval = 'P1Y'; |
125
|
|
|
$now->sub(new \DateInterval($interval)); |
126
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
127
|
|
|
break; |
128
|
|
|
default: |
129
|
|
|
$interval = 'P100Y'; |
130
|
|
|
$now->sub(new \DateInterval($interval)); |
131
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $dateLimit; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function findLastEntriesByIp($game, $ip, $limitScale) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$dateLimit = $this->getDateLimit($limitScale); |
140
|
|
|
|
141
|
|
|
$query = $this->em->createQuery( |
142
|
|
|
'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e |
143
|
|
|
WHERE e.ip = :ip AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date' |
144
|
|
|
); |
145
|
|
|
$query->setParameter('ip', $ip); |
146
|
|
|
$query->setParameter('game', $game); |
147
|
|
|
$query->setParameter('date', $dateLimit); |
148
|
|
|
|
149
|
|
|
$total = $query->getSingleScalarResult(); |
150
|
|
|
|
151
|
|
|
return $total; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* get users with only one participation able to |
156
|
|
|
* replay the game in the timeframe (I except offered entries marked as bonus) |
157
|
|
|
* |
158
|
|
|
* @param unknown_type $game |
159
|
|
|
*/ |
160
|
|
|
public function findPlayersWithOneEntryBy($game) |
161
|
|
|
{ |
162
|
|
|
$dateLimit = $this->getDateLimit($game->getPlayLimitScale()); |
163
|
|
|
|
164
|
|
|
$query = $this->em->createQuery( |
165
|
|
|
'SELECT e, u FROM PlaygroundGame\Entity\Entry e |
166
|
|
|
JOIN e.user u |
167
|
|
|
WHERE e.game = :game |
168
|
|
|
AND (e.bonus = 0 OR e.bonus IS NULL) |
169
|
|
|
GROUP BY e.user |
170
|
|
|
HAVING COUNT(e.id) = 1 |
171
|
|
|
AND e.created_at <= :date ' |
172
|
|
|
); |
173
|
|
|
$query->setParameter('game', $game); |
174
|
|
|
$query->setParameter('date', $dateLimit); |
175
|
|
|
|
176
|
|
|
$result = $query->getResult(); |
177
|
|
|
|
178
|
|
|
return $result; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Compte les nombre de participations bonus |
183
|
|
|
* @param unknown_type $game |
184
|
|
|
* @param unknown_type $user |
185
|
|
|
*/ |
186
|
|
|
public function checkBonusEntry($game, $user) |
187
|
|
|
{ |
188
|
|
|
$query = $this->em->createQuery( |
189
|
|
|
'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e |
190
|
|
|
WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL)' |
191
|
|
|
); |
192
|
|
|
$query->setParameter('user', $user); |
193
|
|
|
$query->setParameter('game', $game); |
194
|
|
|
$nbEntries = $query->getSingleScalarResult(); |
195
|
|
|
|
196
|
|
|
$query = $this->em->createQuery( |
197
|
|
|
'SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e |
198
|
|
|
WHERE e.user = :user AND e.game = :game AND e.bonus = 1' |
199
|
|
|
); |
200
|
|
|
$query->setParameter('user', $user); |
201
|
|
|
$query->setParameter('game', $game); |
202
|
|
|
$nbBonusEntries = $query->getSingleScalarResult(); |
203
|
|
|
|
204
|
|
|
if (($nbEntries - $nbBonusEntries) <= 0) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return true; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getEntityRepository() |
212
|
|
|
{ |
213
|
|
|
if (null === $this->er) { |
214
|
|
|
$this->er = $this->em->getRepository('PlaygroundGame\Entity\Entry'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->er; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.