-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.php
200 lines (170 loc) · 4.48 KB
/
game.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
<?php
$tiempo = 0;
$preguntastring = "";
$tiempo60 = 0;
$continue = 0;
if(isset($_GET['pid']))
{
//Conectar
$cn = mysqli_connect("localhost","easyquiz","some_pass","easyquiz",3306);
/* verificar la conexión */
if (mysqli_connect_errno()) {
printf("Falló la conexión: %s\n", mysqli_connect_error());
exit();
}
//SANITIZAR GET PID PARA EVITAR INJECTION
$_GET['pid'] = mysqli_escape_string($cn, $_GET['pid']);
if (ctype_digit($_GET['pid']))
{
$pid = $_GET['pid'];
if ($pid < 0)
{
echo "ERROR EN EL PID";
exit();
}
$res1 = mysqli_query($cn,"Select score,timestamp,time60,qid from easyquiz.participants where id = '$pid';");
if(mysqli_error($cn)!="")
{
echo mysqli_error($cn);
$pid = -1;
exit();
}
if (mysqli_num_rows($res1) == 1)
{
$sdata = mysqli_fetch_assoc($res1);
if ($sdata['timestamp'] == "")
{
$tiempo=time();
//Get time +60
$tiempo60=($tiempo+60);
//Guardar tiempo +60 en el perfil de la DB - Not sure is needed
$res2 = mysqli_query($cn,"UPDATE easyquiz.participants SET timestamp = '$tiempo', time60 = '$tiempo60' WHERE id = '$pid';");
}
else
{
$tiempo = time();
$tiempo60 = $sdata['time60'];
}
}
else
{
echo "ID is wrong. I dont know why, but is wrong";
$pid = -1;
exit();
}
}
}
if($pid > 0)
{
if ($tiempo > 0)
{
if ($tiempo < $tiempo60)
{
//Check last answer
if(isset($_POST['datos']))
{
//SANITIZAR DATOS
$aid = $_POST['answ'];
$qid = $sdata['qid'];
$res5 = mysqli_query($cn,"Select answer from easyquiz.quizes where idquizes = '$aid';");
$adata = mysqli_fetch_assoc($res5);
$res5 = mysqli_query($cn,"Select answer from easyquiz.quizes where idquizes = '$qid';");
$qdata = mysqli_fetch_assoc($res5);
if ( $aid == $qid or $adata['answer'] == $qdata['answer'] )
{
$score = $sdata['score'];
$score = $score + 5;
$res3 = mysqli_query($cn,"UPDATE easyquiz.participants SET score = '$score' WHERE id = '$pid';");
}
}
$preguntaid=rand(1, 12);
//levantar pregunta de la DB
$res5 = mysqli_query($cn,"Select question,answer from easyquiz.quizes where idquizes = '$preguntaid';");
$qdata = mysqli_fetch_assoc($res5);
if(mysqli_error($cn)!="")
{
echo mysqli_error($cn);
}
$preguntastring = $qdata['question'];
//Insert preguntaid en la DB
$res4 = mysqli_query($cn,"UPDATE easyquiz.participants SET qid = $preguntaid WHERE id = '$pid';");
$respuestas=array();
array_push($respuestas, $preguntaid);
for ($i = 1; $i <= 3; $i++)
{
$respid=rand(1, 12);
if ($respid != $preguntaid)
{
if (!in_array ($respid,$respuestas))
{
array_push($respuestas, $respid);
}
else
{
$i--;
}
}
else
{
$i--;
}
}
sort($respuestas);
$continue = 1;
}
else
{
$continue = 0;
}
}
}
?>
<html>
<head>
<title>Easy Quiz</title>
</head>
<body>
<br />
<?php
//Todavia puede seguir respondiendo
if ($continue == 1)
{
?>
Dale, te quedan <?php echo ($tiempo60-$tiempo) ?> segundos!!!!
<br />
<h1><?php echo $preguntastring; ?></h1>
<form action ="game.php?pid=<?=$pid ?>" method ="post">
<div>
<?php
//DEBUG:
//print_r($respuestas);
//Traer de la DB String para el ID
foreach($respuestas as $rid)
{
?>
<input type="radio" id="<?=$rid ?>" name="answ" value="<?=$rid ?>">
<label for="<?=$rid ?>"><?php echo 'texto de '.$rid; ?></label>
<br />
<?php
}
?>
</div>
<input type="hidden" id="datos" name="datos" value="<?=$pid ?>">
<br / > <br />
<input type="submit" value="Responder!"/>
</form>
<br />
<?php
//NO puede continuar mas.
}
else
{
?>
<h1>Gracias por participar!</h1>
<br />
<a href="score.php?pid=<?=$pid ?>">Go to Score.</a>
<?php
}
?>
</body>
</html>