score.class.php 11.1 KB
Newer Older
maryam's avatar
done  
maryam committed
1
2
3
4
<?php

class score
{
arnaudbey's avatar
arnaudbey committed
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
    private $user = '';
    private $userid = '';
    private $mode = '';
    private $result2 = '';
    private $row = '';
    private $usertab = array();
    private $userScoreTab = array();
    private $scoreTab = array();

    private $userName = '';
    private $userHighScore = false;
    private $coef = array();
    private $spokenLang = array();
    private $coefLangue = array();
    private $allScores = array();
    private $allScoresSorted = array();

    public function set_mode($mode)
    {
        $this->mode = $mode;
    }

    public function process()
    {
        if ($this->init()) {
maryam's avatar
done  
maryam committed
30
            $this->getscore();
arnaudbey's avatar
arnaudbey committed
31

maryam's avatar
done  
maryam committed
32
33
            return $this->display();
        }
David's avatar
David committed
34

arnaudbey's avatar
arnaudbey committed
35
36
        return false;
    }
David's avatar
David committed
37

arnaudbey's avatar
arnaudbey committed
38
39
40
41
42
43
44
    private function init()
    {
        //récupération de userid
        $this->user = user::getInstance();
        $this->userid = $this->user->id;
        $this->userName = $this->user->username;
        $this->coef = $this->getCoeffScore();
maryam's avatar
done  
maryam committed
45

arnaudbey's avatar
arnaudbey committed
46
47
        return true;
    }
maryam's avatar
done  
maryam committed
48

arnaudbey's avatar
arnaudbey committed
49
50
    private function getscore()
    {
David's avatar
David committed
51

arnaudbey's avatar
arnaudbey committed
52
53
        //connexion à la BD
        $db = db::getInstance();
David's avatar
David committed
54

arnaudbey's avatar
arnaudbey committed
55
56
        //variable qui permet d'afficher les $this->scorE meilleurs scores
        $this->scorE = highscore;
David's avatar
David committed
57

arnaudbey's avatar
arnaudbey committed
58
59
        //Récupération du score
        $sql = "SELECT 
David's avatar
David committed
60
61
			* FROM score WHERE userid='".$this->userid."' AND  first_game_time!=''";

arnaudbey's avatar
arnaudbey committed
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
        $this->result = $db->query($sql);

        if ($this->result->num_rows > 0) {
            while ($this->res = mysqli_fetch_assoc($this->result)) {
                $classementGlobal = 0;

                $i = 0;

                $sql = "SELECT 
					* FROM score WHERE langue='".$this->res['langue']."' AND first_game_time!='' ORDER BY `scoreGlobal` DESC";
                $this->result2 = $db->query($sql);
                $this->row = $this->result2->num_rows;

                while ($this->res2 = mysqli_fetch_assoc($this->result2)) {
                    ++$i;

                    $name = $this->getName($this->res2['userid']);

                  //  if($this->userid == $this->res2["userid"]){
                    //	$this->usertab[$this->res2["langue"]][$name]["place"] = $i;
                    //}

                    if ($i <= $this->scorE) {
                        if ($this->userid == $this->res2['userid']) {
                            $this->userHighScore = true;
                            //$this->usertab[$this->res2["langue"]][$name]["place"] = $i;						
                        }
                    } else {
                        if (!$this->userHighScore) {
                            if ($this->userid == $this->res2['userid']) {
                                $this->userClassement = $i;
                            }
                        }
                    }
                    $this->usertab[$this->res2['langue']][$name]['scoreGlobal'] = $this->res2['scoreGlobal'];
                    $this->usertab[$this->res2['langue']][$name]['scoreOracle'] = $this->res2['scoreOracle'];
                    $this->usertab[$this->res2['langue']][$name]['scoreDruide'] = $this->res2['scoreDruide'];
                    $this->usertab[$this->res2['langue']][$name]['scoreDevin'] = $this->res2['scoreDevin'];

                    $this->langue = $this->res2['langue'];

                    if (!isset($this->userScoreTab[$this->langue])) {
                        $this->userScoreTab[$this->langue] = array();
                    }

                    array_push($this->userScoreTab[$this->langue], array($name => array('scoreGlobal' => $this->res2['scoreGlobal'],
                                                                               'scoreOracle' => $this->res2['scoreOracle'],
                                                                               'scoreDruide' => $this->res2['scoreDruide'],
                                                                               'scoreDevin' => $this->res2['scoreDevin'],
                                                                               'classement' => $i,
                                                                                )));
                }

                if (!$this->userHighScore) { //si le joueur n'est pas dans le topFive
                    $this->scoreTab[$this->langue] = $this->scoreDefine($this->userClassement, $this->userScoreTab, $this->langue);
                }
            }
        }

        //Score GLOBAL
        $sql = "SELECT 
David's avatar
David committed
123
			* FROM score WHERE first_game_time!=''";
arnaudbey's avatar
arnaudbey committed
124
        $this->resu = $db->query($sql);
David's avatar
David committed
125

arnaudbey's avatar
arnaudbey committed
126
        while ($this->resultatt = mysqli_fetch_assoc($this->resu)) {
David's avatar
David committed
127

arnaudbey's avatar
arnaudbey committed
128
129
            //on récupère le score global de l'utilisateur et le nombre de langues joué
            $totalScoreUser = $this->calculScoreGlobal($this->resultatt['userid']);
David's avatar
David committed
130

arnaudbey's avatar
arnaudbey committed
131
            $name = $this->getName($this->resultatt['userid']);
David's avatar
David committed
132

arnaudbey's avatar
arnaudbey committed
133
134
135
136
137
            if (!isset($tabName[$name])) {
                $tabName[$name] = $name;
                array_push($this->allScores, array('name' => $name, 'ScoreGlobal' => $totalScoreUser[0], 'NbLangue' => $totalScoreUser[1]));
            }
        }
David's avatar
David committed
138

arnaudbey's avatar
arnaudbey committed
139
        usort($this->allScores, $this->make_comparer(['ScoreGlobal', SORT_DESC]));
David's avatar
David committed
140

arnaudbey's avatar
arnaudbey committed
141
        $i = 0;
David's avatar
David committed
142

arnaudbey's avatar
arnaudbey committed
143
144
        foreach ($this->allScores as $key => $tab) {
            ++$i;
David's avatar
David committed
145

arnaudbey's avatar
arnaudbey committed
146
            $this->allScores[$key]['classement'] = $i;
David's avatar
David committed
147

arnaudbey's avatar
arnaudbey committed
148
149
150
151
            if ($tab['name'] == $this->userName) {
                $classementUser = $i;
            }
        }
David's avatar
David committed
152

arnaudbey's avatar
arnaudbey committed
153
154
155
        if ($classementUser > $this->scorE) {
            $this->scoreGlobalTab = $this->scoreDefine($classementUser, $this->allScores, '');
        }
David's avatar
David committed
156

arnaudbey's avatar
arnaudbey committed
157
158
        return false;
    }
David's avatar
David committed
159

arnaudbey's avatar
arnaudbey committed
160
161
162
    private function getName($id)
    {
        $db = db::getInstance();
David's avatar
David committed
163

arnaudbey's avatar
arnaudbey committed
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
        $sql = 'SELECT `username`
		            	FROM user
		            	WHERE userid = '.intval($id);

        $this->resultaT = $db->query($sql);
        $this->reS = mysqli_fetch_assoc($this->resultaT);

        return $this->reS['username'];
    }

    private function calculScoreGlobal($id)
    {
        //connexion à la BD
        $db = db::getInstance();
        $i = 0;
        $max = 0;
        $nblangues = 0;
        $scoreTotal = 0;

        $sql = "SELECT * FROM `score` WHERE  first_game_time !='' AND userid = '".intval($id)."' ORDER BY `scoreGlobal` DESC";
        $this->resultt = $db->query($sql);
        $this->spokenLang = $this->getLvlLang($id);
        //print_r($this->spokenLang);
        //On récupère les langues parlées par l'utilisateur avec le niveau

        //On récupère le coefficient en fonction du niveau dans la langue apprise


        while ($this->ress = mysqli_fetch_assoc($this->resultt)) {
            ++$nblangues;

            if (isset($this->spokenLang[$this->ress['langue']])) {
                //On récupère le coefficient en fonction du niveau dans la langue apprise
            $coeffNiveau = $this->getCoeffLang($this->spokenLang[$this->ress['langue']]);
            } else {
                $coeffNiveau = 1;     //valeur par défaut si l'utilisateur n'a joué qu'en mode devin
            }

            if (isset($this->coef[$i])) {
                $max = $i;
                $scoreTotal += $this->ress['scoreGlobal'] * $coeffNiveau * $this->coef[$i];
                ++$i;
            } else {
                $scoreTotal += $this->ress['scoreGlobal'] * $coeffNiveau * $this->coef[$max];
            }
        }
maryam's avatar
done  
maryam committed
210

arnaudbey's avatar
arnaudbey committed
211
212
        return array($scoreTotal,$nblangues);
    }
David's avatar
David committed
213

arnaudbey's avatar
arnaudbey committed
214
215
216
217
218
    private function getCoeffScore()
    {
        //connexion à la BD
        $db = db::getInstance();
        $coefficient = array();
David's avatar
David committed
219

arnaudbey's avatar
arnaudbey committed
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
        $sql = 'SELECT * FROM `coeff_statut`';
        $this->resultat = $db->query($sql);

        while ($this->res = mysqli_fetch_assoc($this->resultat)) {
            array_push($coefficient, $this->res['coeff']);
        }

        return $coefficient;
    }

    private function getLvlLang($id)
    {
        //connexion à la BD
        $db = db::getInstance();

        $sql = "SELECT * FROM user_niveau WHERE userid ='".intval($id)."'";
        $this->resultat = $db->query($sql);
        $langue = array();
        $niveau = array();

        while ($this->res = mysqli_fetch_assoc($this->resultat)) {
            $langue = explode(';', $this->res['spoken_lang']);
            $niveau = explode(';', $this->res['niveau']);
        }
        $taille = sizeof($langue);

        for ($i = 0; $i < $taille;++$i) {
            if ($langue[$i] != '') {
                $spokLang[$langue[$i]] = $niveau[$i];
            }
        }

        return $spokLang;
    }

    private function getCoeffLang($niveau)
    {
        //connexion à la BD
        $db = db::getInstance();

        $sql = "SELECT * FROM `coeff_niveau_langue` WHERE niveau_langue='".$niveau."'";
        $this->resultat = $db->query($sql);
        $this->res = mysqli_fetch_assoc($this->resultat);

        return $this->res['coeff'];
    }

    private function make_comparer()
    {
        // Normalize criteria up front so that the comparer finds everything tidy
        $criteria = func_get_args();
        foreach ($criteria as $index => $criterion) {
            $criteria[$index] = is_array($criterion)
                ? array_pad($criterion, 3, null)
                : array($criterion, SORT_ASC, null);
        }

        return function ($first, $second) use ($criteria) {
            foreach ($criteria as $criterion) {
                // How will we compare this round?
                list($column, $sortOrder, $projection) = $criterion;
                $sortOrder = $sortOrder === SORT_DESC ? -1 : 1;

                // If a projection was defined project the values now
                if ($projection) {
                    $lhs = call_user_func($projection, $first[$column]);
                    $rhs = call_user_func($projection, $second[$column]);
                } else {
                    $lhs = $first[$column];
                    $rhs = $second[$column];
                }

                // Do the actual comparison; do not return if equal
                if ($lhs < $rhs) {
                    return -1 * $sortOrder;
                } elseif ($lhs > $rhs) {
                    return 1 * $sortOrder;
                }
            }

            return 0; // tiebreakers exhausted, so $first == $second
        };
    }

    private function scoreDefine($userClassement, $tab, $langue)
    {
        $scoreclassement = array();

        $this->scoreOver = highscoreOver;
        ++$this->scoreOver;

        $j = $userClassement - $this->scoreOver; //joueurs au dessus
        $jj = $userClassement + $this->scoreOver; //joueurs qui précédent

        while ($j <= $jj) {
            if ($langue != '') {
                if (isset($tab[$langue][$j])) {
                    if (!isset($scoreclassement[$langue])) {
                        $scoreclassement[$langue] = array();
                    }
                    array_push($scoreclassement[$langue], $tab[$langue][$j]);
                }
            } else {
                if (isset($tab[$j])) {
                    array_push($scoreclassement, $tab[$j]);
                }
            }
            ++$j;
        }
        if ($langue != '') {
            return $scoreclassement[$langue];
        } else {
            return $scoreclassement;
        }
    }

    private function display()
    {
        include './views/score.html';
David's avatar
David committed
339

maryam's avatar
done  
maryam committed
340
        return true;
arnaudbey's avatar
arnaudbey committed
341
    }
maryam's avatar
done  
maryam committed
342
}