-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadgeit.php
489 lines (369 loc) · 13 KB
/
badgeit.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/*
BADG IT !!
~~~
Thibaut Villemont / modern~tree
L'objectif n'est pas de mesurer la valeur d'un utilisateur mais plutôt son engagement au sein de la communauté.
Le nombre de point permet d'obtenir un indice sur l'engagement de la personne définissant un éventuel rôle (==> Badge).
*/
Class Badgeit {
private $db;
private $prfxr = "fanbase_";
private $tbl_fan = "fan";
private $flash;
private $flash_ending = "<br>";
function __construct() {
$this->db = new DB();
}
public function showFlash() {
print $this->flash;
}
/**
* Exploit Library : create an exploit in the library
* @param array $params (name_exploits,points_earning,category)
* @return
*/
public function createExploit($params){
if($params && is_array($params)){
$name_exploits=$params[0];
$points_earning=$params[1];
$category=$params[2];
$req="INSERT INTO ".$this->prfxr."exploits_library VALUES ('','$name_exploits','$points_earning','$category')";
if($this->db->query($req) === true ) {
$this->flash.="exploit ajouté à la librairie".$this->flash_ending;
} else {
$this->flash.="exploit déjà ajouté à la librairie".$this->flash_ending;
}
}
}
/**
* Exploit Library : Show exploits/exploit
* @param int $id exploit ID
* @return sql results
*/
public function showExploit($id = null){
if($id == null){
$req="SELECT * FROM ".$this->prfxr."exploits_library";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->prfxr."exploits_library WHERE id = '$id'";
return $this->db->see($req);
}
}
/**
* Exploit Library : update an exploit in the library
* @param int $id exploit ID
* @param array $params (name_exploits,points_earning,category)
* @return
*/
public function updateExploit($id, $params){
$name_exploits=$params[0];
$points_earning=$params[1];
$category=$params[2];
$req = "UPDATE ".$this->prfxr."exploits_library SET name='$name_exploits', points='$points_earning', category='$category' WHERE id='$id'";
if($this->db->query($req) === true ) {
$this->flash.= "Exploit mis à jour".$this->flash_ending;
}
else {
$this->flash.="Error : ".__FUNCTION__."()".$this->flash_ending;
}
}
/**
* Exploit : Update all exploits with new point
* @param int $exploit_id_id exploit ID
* @return
*/
public function updateExploitAllPoint($exploit_id_id){
$point=$this->showExploit($exploit_id_id);
$pointNew = $point['points'];
$req = "UPDATE ".$this->prfxr."exploits SET point='$pointNew' WHERE exploit_id='$exploit_id_id'";
if($this->db->query($req) === true ) {
$this->flash.= "Tous les scores des exploits mis à jour".$this->flash_ending;
}
else {
$this->flash.="Error : ".__FUNCTION__."()".$this->flash_ending;
}
}
/**
* Exploit : list exploits per user and/or exploit
* @param int $user_id user ID
* @param int $id exploit ID
* @return sql results
*/
public function showPlayerExploit($user_id, $id = null){
if($id == null){
$req="SELECT * FROM ".$this->prfxr."exploits WHERE user_id = '$user_id' order by date DESC";
return $this->db->query($req);
}
elseif($id == "allByExploit") {
$req="SELECT * FROM ".$this->prfxr."exploits WHERE exploit_id = '$id'";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->prfxr."exploits WHERE user_id = '$user_id' and exploit_id = '$id'";
return $this->db->query($req);
}
}
/**
* Exploit : save exploit for one user
* @param int $exploit_id exploit_lib ID
* @param int $user_id
* @param array $params exploit params
* @param date $date date() or null = now
* @return
*/
public function savePlayerExploit($exploit_id,$user_id,$params=array(),$date) {
$point=$this->showExploit($exploit_id);
if($date == null) { $date = date('Y-m-d H:i.s', time()); }
$req="INSERT INTO ".$this->prfxr."exploits VALUES ('','$user_id','$exploit_id','".$point['points']."','".serialize($params)."','$date')";
if($this->db->query($req) === true ) {
$this->flash.="exploit effectué par $user_id".$this->flash_ending;
} else {
$this->flash.="Error : ".__FUNCTION__."()".$this->flash_ending;
}
}
/**
* Exploit : update one exploit
* @param int $exploit_id_id
* @param int $exploit_id exploit_lib ID
* @param int $user_id
* @param array $params exploit params
* @return
*/
public function updatePlayerExploit($exploit_id_id, $exploit_id,$user_id,$params=array()) {
$point=$this->showExploit($exploit_id);
$pointNew = $point['points'];
$paramsNew=serialize($params);
$req = "UPDATE ".$this->prfxr."exploits SET point='$pointNew', params='$paramsNew', date=NOW() WHERE id='$exploit_id_id'";
if($this->db->query($req) === true ) {
$this->flash.="exploit $exploit_id_id modifié".$this->flash_ending;
} else {
$this->flash.="Error : ".__FUNCTION__."()".$this->flash_ending;
}
}
/**
* User : update points & exploits
* @param int $user_id
* @return
*/
public function updatePlayerPoints($user_id){
$req = "SELECT count(point) as nbexploit, sum(point) as nbpoint FROM ".$this->prfxr."exploits WHERE user_id='$user_id'";
$res = $this->db->see($req);
$nbexploit = $res['nbexploit'];
if($res['nbpoint'] == null) : $nbpoint = 0; else : $nbpoint = $res['nbpoint']; endif;
$date = date('Y-m-d H:i.s', time());
$req = "UPDATE ".$this->prfxr."exploits_user SET total_point=$nbpoint, total_exploit=$nbexploit, updated_at='$date' WHERE id='$user_id'";
$test = $this->db->query($req);
if($test == false ) {
$this->flash.= "hello newbies, at this time, $user_id have earn $nbpoint points with $nbexploit exploits, this level is : xxx".$this->flash_ending;
}
if($test === true ) {
if(!$this->db->see("SELECT 1 FROM ".$this->prfxr."exploits_user WHERE id='$user_id'")){
$req2="INSERT INTO ".$this->prfxr."exploits_user VALUES ('$user_id','$user_id','','$nbpoint','$nbexploit','','$date')";
$this->db->query($req2);
$this->flash.= "hello newbies, at this time, $user_id have earn $nbpoint points with $nbexploit exploits, this level is : xxx".$this->flash_ending;
} else {
$this->flash.= "at this time, $user_id have earn $nbpoint points with $nbexploit exploits, this level is : xxx".$this->flash_ending;
}
}
}
/**
* User : show info about exploits per unique user or all user
* @param int $user_id
* @return sql (id,other_id,email,total_point,total_exploit,total_badge,updated_at)
*/
public function showPlayer($user_id){
if($user_id != null){
$req="SELECT * FROM ".$this->prfxr."exploits_user WHERE id = '$user_id'";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->prfxr."exploits_user "; // fan
return $this->db->query($req);
}
}
/**
* Fan : show info about fan
* @param int $user_id
* @return
*/
public function showFan($user_id){
if($user_id != null){
$req="SELECT * FROM ".$this->tbl_fan." WHERE id = '$user_id'";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->tbl_fan;
return $this->db->query($req);
}
}
/**
* User : list user per total_point (FUTUR:chosse)
* @param int $limit limit de classement
* @return sql (id,total_point,total_exploit,total_badge,email)
*/
public function showPlayerClassement($limit=null){
$req="SELECT a.id, a.total_point, a.total_exploit, a.total_badge, b.email FROM ".$this->prfxr."exploits_user a, ".$this->tbl_fan." b WHERE a.id = b.id ORDER BY a.total_point DESC LIMIT $limit";
return $this->db->query($req);
}
/**
* Badge Library : create badge in the library
* @param array $params (name_badge,advantage,rules,min_point,manuorauto,callback)
* @return
*/
public function createBadge($params)
{
if($params && is_array($params)){
$name_badge = $params[0];
$advantageforuser = $params[1];
$rules = $params[2];
$min_points = $params[3];
$manuorauto = $params[4];
$callback = $params[5];
$req="INSERT INTO ".$this->prfxr."badge_library VALUES ('','$name_badge','".mysql_real_escape_string($advantageforuser)."','$rules','$min_points','$manuorauto','$callback')";
if($this->db->query($req) === true ) {
$this->flash.="badge ajouté à la librairie".$this->flash_ending;
} else {
$this->flash.="badge DEJA ajouté à la librairie".$this->flash_ending;
}
}
}
/**
* Badge Library : show
* @param int $id
* @return sql [description]
*/
public function showBadge($id = null){
if($id == null){
$req="SELECT * FROM ".$this->prfxr."badge_library";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->prfxr."badge_library WHERE id = '$id'";
return $this->db->see($req);
}
}
/**
* Badge Library : update badge
* @param int $id badge lib ID
* @param array $params (create array)
* @return
*/
public function updateBadge($id, $params){
$name_badge=$params[0];
$avantage=$params[1];
$rules=$rules[2];
$min_points=$rules[3];
$manuorauto=$rules[4];
$callback=$rules[5];
$req = "UPDATE ".$this->prfxr."badge_library SET name_badge='$name_badge', avantage='$avantage', rules='$rules', min_points='$min_points', manuorauto='$manuorauto', callback='$callback' WHERE id='$id'";
if($this->db->query($req) === true ) {
$this->flash.= "Badge mis à jour".$this->flash_ending;
}
else {
$this->flash.="Error : ".__FUNCTION__."()".$this->flash_ending;
}
}
/**
* Badge : Check if the user earn badges / Update badge for one user
* @param int $user_id
* @return
*/
public function updatePlayerBadge($user_id){
$user = $this->db->see("SELECT * FROM ".$this->prfxr."exploits_user WHERE id='$user_id'");
$req="SELECT * FROM ".$this->prfxr."badge_library";
$Badges =$this->db->query($req);
$pass = false;
$badgeAquired = $user['total_badge'];
while($row = mysql_fetch_array($Badges)){
// Minimum de point requis
if($user['total_point']>=$row['min_points']) { $pass = true; }
if($pass === true and !empty($row['rules'])) {
$pass = false;
// rules requises
if (strpos($row['rules'],'||') !== true) {
$rules = explode(';', $row['rules']);
foreach ($rules as $key => $rule) { // AND
$a = explode(':', $rule);
$b = preg_split('(-|\+|\*|\/|>|<)',$a[1]);
$c = preg_match('/(-|\+|\*|\/|>|<)/',$a[1],$matches);
$id = $b[0];
$count = $b[1];
$operateur = $matches[0];
$test = $this->db->see("SELECT count(id) as nb FROM ".$this->prfxr."exploits WHERE exploit_id='$id' AND user_id='$user_id'");
if( $test == true ) {
switch ($operateur) {
case '<':
if($test['nb']<$count) { $pass = true; } else { break; }
break;
case '>':
if($test['nb']>$count) { $pass = true; } else { break; }
break;
}
}
}
}
// OR
if (strpos($row['rules'],'||') !== false) {
$rules = explode('||', $row['rules']);
foreach ($rules as $key => $rule) { // OR
$a = explode(':', $rule);
$b = preg_split('(-|\+|\*|\/|>|<)',$a[1]);
$c = preg_match('/(-|\+|\*|\/|>|<)/',$a[1],$matches);
$id = $b[0];
$count = $b[1];
$operateur = $matches[0];
$test = $this->db->see("SELECT count(id) as nb FROM ".$this->prfxr."exploits WHERE exploit_id='$id' AND user_id='$user_id'");
if( $test == true ) {
switch ($operateur) {
case '<':
if($test['nb']<$count) { $pass = true; break 2; } else { break; }
break;
case '>':
if($test['nb']>$count) { $pass = true; break 2; } else { break; }
break;
}
}
}
}
}
if($pass === true) {
$date = date('Y-m-d H:i.s', time());
$req="INSERT INTO ".$this->prfxr."badge VALUES ('','".$row['id']."','$user_id','$date')";
if($this->db->query($req) === true ) {
$this->flash.="badge ".$row['name_badge']." ajouté à ".$user['id']."".$this->flash_ending;
$badgeAquired++;
} else {
$this->flash.="badge ".$row['name_badge']." DEJA ajouté à ".$user['id']."".$this->flash_ending;
}
}
}
// update scores
$date = date('Y-m-d H:i.s', time());
$req = "UPDATE ".$this->prfxr."exploits_user SET total_badge='$badgeAquired', updated_at='$date' WHERE id='$user_id'";
if($this->db->query($req) === true ) {
$this->flash.="maj count badges for ".$user['id']."".$this->flash_ending;
}
}
/**
* Badge : show badge per user, badge or both
* @param int $user_id
* @param int $id badge ID
* @return [type] [description]
*/
public function showPlayerBadge($user_id, $id = null){
if($id == null){
$req="SELECT * FROM ".$this->prfxr."badge WHERE user_id = '$user_id'";
return $this->db->query($req);
}
elseif($id == "allByExploit") {
$req="SELECT * FROM ".$this->prfxr."badge WHERE badge_id = '$id'";
return $this->db->query($req);
}
else {
$req="SELECT * FROM ".$this->prfxr."badge WHERE user_id = '$user_id' and badge_id = '$id'";
return $this->db->query($req);
}
}
};