-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.php
214 lines (182 loc) · 6.88 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
//connection
include("scripts/dbconnect.php");
// if ((isset($_GET['book'])) && (!empty($_GET['book']))) {
// $book = $_POST['book'];
// echo $book;
// }
if(isset($_COOKIE["gameID"])) { //if game cookie exists
$GameID = $_COOKIE["gameID"];
//set cookie, if it does not exist
if(!isset($_COOKIE["nickname"])) {
$nickname = $_POST['nickname'];
setcookie("nickname", $nickname, time() + (86400 * 1), "/"); // 86400 = 1 day
//create player
$sql = "INSERT INTO `$GameID`
(`nickname`, `score`)
VALUES
('$nickname', '0')";
$db_erg = mysqli_query($db, $sql)
or die("Anfrage fehlgeschlagen: " . mysqli_error($db));
}
else
{
$nickname = $_COOKIE['nickname']; //if nickname cookie exists, get it!
}
// $GameID = $_POST['di1'] . $_POST['di2'] . $_POST['di3'] . $_POST['di4'];
//check if game controll file exists
$path = "games/$GameID/control.txt";
if (!file_exists($path)) {
echo "An error Occured. You will be redirectet to the mainpage.";
echo " <script> setInterval(redirect, 2000);
function redirect() {
window.location.replace='index.html';
}
;</script>";
}
// //create Table
// $sql = "
// CREATE TABLE `$GameID` (
// `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
// `nickname` VARCHAR( 150 ) NOT NULL ,
// `score` VARCHAR( 150 ) NULL
// ) ENGINE = MYISAM ;
// ";
// $db_erg = mysqli_query($db, $sql)
// or die("Anfrage fehlgeschlagen: " . mysqli_error($db));
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="style_game.css">
<title>BibleGuess</title>
</head>
</body>
<div id="main">
<h1>BibleGuess</h1>
<h2><?php echo "Hey " . $nickname . ", you joined game " . $GameID?></h2>
<p id="waitMessage">Wait until the game starts...</p>
<!-- spinner -->
<div id="waitingspinner"><div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div></div>
<div id="playField">
<div id="questionContainer">
</div>
<div>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="bookGuess">
<input type="text" class="gameInput" placeholder="Guess the book..." name="book" />
</div>
<input type="submit" class="gameInput" value="Try it!"/>
</form>
</div>
</div>
</div>
<script>
var FORM_Submitted = false;
</script>
<?php
/* checks wether the form was submitted */
if(empty($_POST["book"]))
{
echo "empty";
}
else
{
echo $_POST["book"];
?>
<script>
var FORM_Submitted = true;
</script>
<?php
}
function getControlInput($GID){
$filename = "./games/" . $GID . "/control.txt";
// $handle = fopen($filename, "rb");
// $contents = fread($handle, filesize($filename));
$contents = file_get_contents($filename);
// fclose($handle);
//Clear cache and check filesize again
clearstatcache();
//ugly but works
$ret = json_encode(explode("\n", $contents));
return substr($ret, 2, -2);
}
// echo "\n" . (getControlInput($GameID));
?>
<script src="script_game.js"></script>
<script>
var rule_joinLater;
var game_State;
var game_currQuestion;
var game_timeForNextQuestion;
window.onload = function() {
doGame();
};
setInterval(function() {
doGame();
window.location.reload();
}, 2000);
function doGame() {
var arr = '<?php echo (getControlInput($GameID)); ?>';
arr = arr.split("\",\"");
rule_joinLater = parseInt(arr[0].split("#")[0]); //remove comment
game_State = parseInt(arr[2].split("#")[0]);
game_currQuestion = parseInt(arr[3].split("#")[0]);
game_timeForNextQuestion = arr[4].split("#")[0];
console.log("curr S:" + game_State);
console.log("curr Q:" + game_currQuestion);
switch(game_State){
//waiting until start
case 0:
document.getElementById("waitingspinner").style = "display: block;";
document.getElementById("waitMessage").style = "display: block;";
document.getElementById("questionContainer").style = "display: none;";
document.getElementsByClassName("gameInput")[0].disabled = true;
document.getElementsByClassName("gameInput")[1].disabled = true;
break;
//show question
case 1:
document.getElementById("waitingspinner").style = "display: none;";
document.getElementById("waitMessage").style = "display: none;";
document.getElementById("questionContainer").style = "display: block;";
document.getElementsByClassName("gameInput")[0].disabled = false;
document.getElementsByClassName("gameInput")[1].disabled = false;
document.getElementById("questionContainer").innerHTML = getQuestion(game_currQuestion);//works
///////////////////////
//do validation check//
///////////////////////
if(FORM_Submitted == true){
var guess = "<?php if(!empty($_POST["book"])) echo $_POST["book"];?>";
var answere = getBook(game_currQuestion);
console.log(answere);
//sleep(100);
if(guess == answere)
{
console.log("treffer");
document.getElementById("questionContainer").style = "background-color: #4d4;";
}
else
{
console.log("Falsch");
document.getElementById("questionContainer").style = "background-color: #d44;";
}
}
break;
//wait for next question
case 2:
document.getElementById("waitMessage").style = "display: none;";
document.getElementsByClassName("gameInput")[0].disabled = true;
document.getElementsByClassName("gameInput")[1].disabled = true;
}
}
</script>
</body>
</html>
<!-- control.txt
0#is 1, when started
1#decides, if a player can join later
1#question ID
-->