-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.php
More file actions
290 lines (270 loc) · 13.9 KB
/
user.php
File metadata and controls
290 lines (270 loc) · 13.9 KB
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
<!--validate-->
<?php
session_start();
include "logic\gameconstmastermind.php";
include "logic\gameconstwordle.php";
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/user.css">
<link rel="icon" type="image/x-icon" href="src/icon.ico">
<title>Pagina utente</title>
</head>
<body>
<?php
if (!isset($_SESSION["UtenteMindWords"])) {
header("Location: index.php");
}
include "logic/dbconnection.php";
?>
<nav>
<a class="linkLogo" href="index.php">
<img class="icon" src="src/game.png" alt="Logo MindWords">
<?php
echo "<h2>Ciao, ".$_SESSION["UtenteMindWords"]."!</h2>";
?>
</a>
<form action="user.php" method="get" class="selector">
<div class="container">
<p>Tentativi:</p>
<div class="option">
<p class="start">Facile</p>
<p class="middle">Medio</p>
<p class="end">Difficile</p>
</div>
<input type="text" name="trys" id="trys" readonly>
</div>
<div class="container">
<p>Lunghezza:</p>
<div class="option">
<p class="start">Facile</p>
<p class="middle">Medio</p>
<p class="end">Difficile</p>
</div>
<input type="text" name="len" id="len" readonly>
</div>
<div class="container">
<p>Modalità:</p>
<div class="option">
<p class="start selected">Wordle</p>
<p class="middle">Mistermind</p>
<input class="optionSubmit" type="submit" value="Invia">
</div>
<input type="text" name="mod" id="mod" value="0" readonly>
</div>
</form>
<div class="NavLeftElem">
<a href="logic/logout.php">Logout</a>
</div>
<script>let userData = null; </script>
</nav>
<div id="section">
<div class="data">
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["trys"]) && isset($_GET["len"]) && isset($_GET["mod"])) {
$trys = "";
$len = "";
// struttura contenente i dati su i giochi
$configs = [
"Wordle" => [
"trys" => [
"Facile" => EasyWordleTrys,
"Normale" => StdWordleTrys,
"Difficile" => HardWordleTrys,
],
"len" => [
"Facile" => EasyWordleLen,
"Normale" => StdWordleLen,
"Difficile" => HardWordleLen,
]
],
"Mastermind" => [
"trys" => [
"Facile" => EasyMasterTrys,
"Normale" => StdMasterTrys,
"Difficile" => HardMasterTrys,
],
"len" => [
"Facile" => EasyMasterSeq,
"Normale" => StdMasterSeq,
"Difficile" => HardMasterSeq,
]
],
];
$mode = $_GET["mod"] == 1 ? "Mastermind" : "Wordle";
$difficultyTrys = "";
$TrysSelected = 0;
switch ($_GET["trys"]) {
case 0:
$difficultyTrys = "Facile";
break;
case 1:
$difficultyTrys = "Normale";
break;
case 2:
$difficultyTrys = "Difficile";
break;
default:
$TrysSelected = 1;
break;
}
$difficultyLen = "";
$LenSelected = 0;
switch ($_GET["len"]) {
case 0:
$difficultyLen = "Facile";
break;
case 1:
$difficultyLen = "Normale";
break;
case 2:
$difficultyLen = "Difficile";
break;
default:
$LenSelected = 1;
break;
}
if (!$TrysSelected) {
$trys = $configs[$mode]["trys"][$difficultyTrys];
}
if (!$LenSelected) {
$len = $configs[$mode]["len"][$difficultyLen];
}
echo "<h2> Modalità: ".$mode. ($trys > 0 ? (", Numero tentativi: " . $trys) : "") . ($len > 0 ? (", Lunghezza sequenza: " . $len) : "") . "</h2>";
$sql = "SELECT s.inizio, floor(avg(time_to_sec(p.tempo))) as tempoMedio, avg(p.tentativi) as avgTentativi, sum(p.punteggio) as totSessione
from sessione as s left outer join partita as p on p.userSessione = s.iduser and p.inizioSessione = s.inizio
inner join modalita as m on m.idmodalita = s.modalita
where s.iduser = ? and m.nomemodalita = ? and (s.tentativiMax = ? or ?) and (s.lughezzaSequenza = ? or ?)
group by s.iduser, s.inizio, s.modalita
order by s.inizio limit 15";
if ($stmt = $db_connection->prepare($sql)) {
$stmt->bind_param('ssiiii', $_SESSION["UtenteMindWords"], $mode, $trys, $TrysSelected, $len, $LenSelected);
if ($stmt->execute()) {
$data = $stmt->get_result();
}
$stmt->close();
$jsdata = [];
while ($row = $data->fetch_assoc()) {
$jsdata[] = $row;
}
echo "<script>userData = " . json_encode($jsdata) . ";</script>";
}
}else {
echo "<h2>Nessuna modalità selezionata, selezionare almeno la modalià per vedere i propri risultati</h2>";
}
?>
<hr>
<h3>Punteggio totale:</h3>
<canvas width="800" height="200" id="punteggio"></canvas>
<h3>Tentativi medi:</h3>
<canvas width="800" height="200" id="tentativi"></canvas>
<h3>Tempo medio:</h3>
<canvas width="800" height="200" id="tempo"></canvas>
</div>
<div class="leaderBoard">
<div class="title">
<h1>Leaderboard</h1>
<a href="friend.php">Aggiungi nuovi amici</a>
</div>
<div class="spacing">
<div class="scroll">
<?php
if (($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["trys"]) && isset($_GET["len"]) && isset($_GET["mod"]))) {
$sqlboard = "SELECT g.iduser, cast(max(g.inizio) as date) as inizioTop, lughezzaSequenza, tentativiMax, punteggioSessione
from (
Select iduser, inizio, lughezzaSequenza, tentativiMax, ifnull(sum(p.punteggio), 0) as punteggioSessione
from sessione as s inner join modalita as a on a.idmodalita = s.modalita left outer join partita as p on s.iduser = p.userSessione and s.inizio = p.inizioSessione
where a.nomemodalita = ? and s.iduser in (
select *
from (
select user1 as amico FROM friend as f1 where f1.user2 = ? or f1.user1 = ?
union
select user2 as amico FROM friend as f2 where f2.user2 = ? or f2.user1 = ?
union
select ? as amico
) u
)
group by s.iduser, s.inizio
) g
where (g.punteggioSessione) = (
select max(g2.punteggioSessione)
from (
Select iduser, inizio, ifnull(sum(p.punteggio), 0) as punteggioSessione
from sessione as s inner join modalita as a on a.idmodalita = s.modalita left outer join partita as p on s.iduser = p.userSessione and s.inizio = p.inizioSessione
where a.nomemodalita = ? and s.iduser in (
select *
from (
select user1 as amico FROM friend as f1 where f1.user2 = ? or f1.user1 = ?
union
select user2 as amico FROM friend as f2 where f2.user2 = ? or f2.user1 = ?
union
select ? as amico
) u
)
group by s.iduser, s.inizio
) g2
where g2.iduser = g.iduser
)
group by g.iduser
order by g.punteggioSessione desc, g.Inizio";
echo "<div class='head'>
<p>Utente</p>
<p class='dataspacer middle'>Data</p>
<p class='middle'>Try</p>
<p class='middle'>Len</p>
<p class='last'>Point</p>
</div>";
if ($stmt = $db_connection->prepare($sqlboard)) {
$stmt->bind_param('ssssssssssss', $mode, $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"],$_SESSION["UtenteMindWords"], $mode, $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"]);
if ($stmt->execute()) {
$data = $stmt->get_result();
}else{
echo "<h1>Qualcosa è andato storto con la esecuzione query</h1>";
}
$stmt->close();
while ($row = $data->fetch_assoc()) {
if ($row["iduser"] == $_SESSION["UtenteMindWords"]) {
echo "<div class='me'>";
}else{
echo "<div>";
}
echo "<p>".$row["iduser"]."</p>
<p class='dataspacer middle'>". str_replace('-', '/', $row["inizioTop"] )."</p>
<p class='middle'>".$row["tentativiMax"]."</p>
<p class='middle'>".$row["lughezzaSequenza"]."</p>
<p class='last'>".$row["punteggioSessione"]."</p>
</div>";
}
}else{
echo "<h1>Qualcosa è andato storto con la query</h1>";
}
}else{
$sqlfriend = "SELECT if(user1 = ?, user2, user1) as amico FROM friend as f1 where f1.user2 = ? or f1.user1 = ?";
echo "<div class='head'><p>Amici</p></div>";
if ($stmt = $db_connection->prepare($sqlfriend)) {
$stmt->bind_param('sss', $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"], $_SESSION["UtenteMindWords"]);
if ($stmt->execute()) {
$data = $stmt->get_result();
}
$stmt->close();
while ($row = $data->fetch_assoc()) {
echo "<div><p>" .$row["amico"]."<p></div>";
}
}
}
$db_connection->close();
?>
</div>
</div>
</div>
</div>
<?php
include "view/footer.php"
?>
<script src="js/user.js"></script>
</body>
</html>