-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick.php
291 lines (265 loc) · 8.66 KB
/
pick.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
<?php
require_once('common/head.php');
$connexion = $database->getConnection();
$matchId = $_GET['idMatch'];
$playerId = $_GET['id'];
$playerNickname;
$opponentId = '';
$opponentNickname = '';
$maps;
$heroes;
$tableExist;
$pickStateMaps;
$idPlayerWhoMakeChoise = '';
// protection ici pour voir si le match "matche" (vive la tautologie) bien avec le joueur
// TODO
// aller chequer dans la db si idPlayer&&idMatch "exists" or smth like that
$sql = "SELECT * FROM matchs_joueurs WHERE id_match=:matchId AND id_joueur=:playerId";
$query = new Query($database, $sql);
$query->bind(':matchId', $matchId, PDO::PARAM_INT);
$query->bind(':playerId', $playerId, PDO::PARAM_INT);
if ($query->execute()) {
$nbr = $query->getResult();
if(!count($nbr) > 0)
{
echo "nice try... mais non ^^ <br>get out from here... <br>I see you.<br>With LoVe, your admin <3";
die();
}
} else {
global $glob_debug;
if ($glob_debug) {
echo 'ERREUR SQL MAPS';
}
exit;
}
// si pick terminé rediriger vers les resultats
$sql = "SELECT heroes FROM matchs WHERE id_match=:idMatch";
$query = new Query($database, $sql);
$query->bind(':idMatch', $matchId, PDO::PARAM_INT);
if ($query->execute()) {
$truc = $query->getResult()[0]['heroes'];
if($truc != NULL)
header('Location: pickResults.php?matchId='.$matchId);
} else {
global $glob_debug;
if ($glob_debug) {
echo 'ERREUR SQL MAPS';
}
exit;
}
// ********************** recuperer les maps **********************
$sql = "select * from hotsmaps";
$query = new Query($database, $sql);
if ($query->execute()) {
$maps = $query->getResult();
} else {
global $glob_debug;
if ($glob_debug) {
echo 'ERREUR SQL MAPS';
}
exit;
}
// ********************** recuperer les heros **********************
$sql = "select * from hotsheroes";
$query = new Query($database, $sql);
if ($query->execute()) {
$heroes = $query->getResult();
} else {
global $glob_debug;
if ($glob_debug) {
echo 'ERREUR SQL MAPS';
}
exit;
}
// ********************** get player nickname **********************
$sql = 'SELECT pseudo FROM joueurs WHERE id_joueur=:userId';
$query = new Query($database, $sql);
$query->bind(':userId', $playerId, PDO::PARAM_INT);
if ($query->execute()) {
// Test if the user has a pseudo or not
if ($query->getResult()) {
$playerNickname = $query->getResult()[0]['pseudo'];
} else {
$playerNickname = 'unknown';
}
} else {
global $glob_debug;
if ($glob_debug) {
echo 'ERREUR SQL MAPS';
}
exit;
}
// ********************** chercher l'id de l'adversaire **********************
$sql = "SELECT joueurs.id_joueur, pseudo FROM matchs_equipes
LEFT JOIN equipes_joueur ON matchs_equipes.id_equipe=equipes_joueur.id_equipes
LEFT JOIN joueurs ON equipes_joueur.id_joueur=joueurs.id_joueur
WHERE id_match=:matchId AND level=1";
$query = new Query($database, $sql);
$query->bind(':matchId', $matchId, PDO::PARAM_INT);
// recuperer les deux jouers(chefs d'équipe), concernant ce match
if ($query->execute()) {
$players = $query->getResult();
} else {
global $glob_debug;
if ($glob_debug)
echo 'ERREUR SQL MAPS';
exit;
}
foreach ($players as $player) {
if ($player['id_joueur'] != $playerId) {
$opponentNickname = $player['pseudo'];
$opponentId = $player['id_joueur'];
break;
}
}
// ********************** verifier l'existence de la table temporaire **********************
$sql = "SHOW TABLES LIKE 'pick_$matchId'";
$query = new Query($database, $sql);
if ($query->execute()) {
$result = $query->getResult();
$tableExist = (count($result) > 0) ? true : false;
} else {
global $glob_debug;
if ($glob_debug)
echo 'ERREUR SQL MAPS';
exit;
}
// si la table existe
if ($tableExist) {
// recuperer les id's et les etats 'checked' des maps
$sql = "SELECT mapId, checked FROM pick_$matchId";
$req = $connexion->prepare($sql);
$req->execute();
$pickStateMaps = $req->fetchAll(PDO::FETCH_ASSOC);
// recuperer les id's et les etats 'checked' des heroes
$sql = "SELECT heroId, checked FROM pickhero_$matchId";
$req = $connexion->prepare($sql);
$req->execute();
$pickStateHeroes = $req->fetchAll(PDO::FETCH_ASSOC);
}
// si la table n'exitste pas
else {
// creation de la table pickmaps_n
$sql = "CREATE TABLE hehlanbd.pick_$matchId ("
. "mapId INT NOT NULL AUTO_INCREMENT, "
. "checked BOOLEAN NOT NULL, "
. "PRIMARY KEY (mapId))";
$req = $connexion->prepare($sql);
$req->execute();
// creation de la table pickheroes_n
$sql = "CREATE TABLE hehlanbd.pickhero_$matchId ("
. "heroId INT NOT NULL AUTO_INCREMENT, "
. "checked BOOLEAN NOT NULL, "
. "PRIMARY KEY (heroId))";
$req = $connexion->prepare($sql);
$req->execute();
// remplissage maps
$counter = 0;
$sql = "INSERT INTO hehlanbd.pick_$matchId (mapId, checked) VALUES ";
foreach ($maps as $map) {
if ($counter < count($maps) - 1) {
$sql .= " ('" . $map['id'] . "', FALSE),";
$counter++;
} else {
$sql .= " ('" . $map['id'] . "', FALSE)";
}
}
$req = $connexion->prepare($sql);
$req->execute();
// remplissage heroes
$counter = 0;
$sql = "INSERT INTO hehlanbd.pickhero_$matchId (heroId, checked) VALUES ";
foreach ($heroes as $hero) {
if ($counter < count($heroes) - 1) {
$sql .= " ('" . $hero['id'] . "', FALSE),";
$counter++;
} else {
$sql .= " ('" . $hero['id'] . "', FALSE)";
}
}
$req = $connexion->prepare($sql);
$req->execute();
// recuperer les id's et les etats 'checked' des maps
$sql = "SELECT mapId, checked FROM pick_$matchId";
$req = $connexion->prepare($sql);
$req->execute();
$pickStateMaps = $req->fetchAll(PDO::FETCH_ASSOC);
// recuperer les id's et les etats 'checked' des heroes
$sql = "SELECT heroId, checked FROM pickhero_$matchId";
$req = $connexion->prepare($sql);
$req->execute();
$pickStateHeroes = $req->fetchAll(PDO::FETCH_ASSOC);
}
// ********************** determiner à qui est le tour **********************
$sql = "SELECT idChief, checked FROM matchs WHERE id_match=:idMatch";
$query = new Query($database, $sql);
$query->bind(':idMatch', $matchId, PDO::PARAM_INT);
if ($query->execute()) {
$idChief = $query->getResult()[0]['idChief'];
$checked = $query->getResult()[0]['checked'];
if ($idChief == NULL) {
$sql = "UPDATE matchs SET idChief=:idChief WHERE id_match=:idMatch";
$query = new Query($database, $sql);
$query->bind(':idMatch', $matchId, PDO::PARAM_INT);
$query->bind(':idChief', $playerId, PDO::PARAM_INT);
if ($query->execute()) {
$idPlayerWhoMakeChoise = $playerId;
} else {
global $glob_debug;
if ($glob_debug)
echo 'ERREUR SQL MAPS';
exit;
}
}
else if ($idChief != $playerId) {
if ($checked) {
$idPlayerWhoMakeChoise = $playerId;
}
} else if ($idChief == $playerId && !$checked){
$idPlayerWhoMakeChoise = $playerId;
}
else {
$idPlayerWhoMakeChoise = NULL;
}
} else {
global $glob_debug;
if ($glob_debug)
echo 'ERREUR SQL MAPS';
exit;
}
$phase;
// determiner la phase du pick
$sql = "SELECT idMap FROM matchs WHERE id_match=:idMatch";
$query = new Query($database, $sql);
$query->bind(':idMatch', $matchId, PDO::PARAM_INT);
if ($query->execute()) {
$idMap = $query->getResult()[0]['idMap'];
if ($idMap == 0) {
// on est tjs au stade de pick les maps
$phase = "maps";
}
// on est au stade de pick les heros
else {
$phase = "heroes";
}
} else {
global $glob_debug;
if ($glob_debug)
echo 'ERREUR SQL MAPS';
exit;
}
// ********************** Applying Template **********************
$smarty->assign('con', $connected);
$smarty->assign('matchId', $matchId);
$smarty->assign('maps', $maps); // les chemins vers les img des maps et les id's
$smarty->assign('heroes', $heroes);
$smarty->assign('playerId', $playerId);
$smarty->assign('playerNickname', $playerNickname);
$smarty->assign('opponentId', $opponentId);
$smarty->assign('opponentNickname', $opponentNickname);
$smarty->assign('pickStateMaps', $pickStateMaps); // les id's des maps et etat checked(bool)
$smarty->assign('pickStateHeroes', $pickStateHeroes); // les id's des heroes et etat checked(bool)
$smarty->assign('idPlayerWhoMakeChoise', $idPlayerWhoMakeChoise);
$smarty->assign('phase', $phase);
$smarty->display('default/pick.tpl');
?>