-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.php
315 lines (313 loc) · 18 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
require_once 'database.php';
$token=$_COOKIE['cookie_token'];
$query="SELECT login FROM users WHERE token = '$token'";
$result=mysqli_query($link, $query);
$user=mysqli_fetch_row($result);
$user=$user[0];
$query="SELECT id FROM games WHERE user1 = '$user' OR user2 = '$user'";
$result=mysqli_query($link, $query);
$id=mysqli_fetch_row($result);
$id=$id[0];
$query="SELECT status FROM games WHERE id = '$id'";
$result=mysqli_query($link, $query);
$status=mysqli_fetch_row($result);
if ($status[0]==0){
$coin=rand(1,2);
$query="UPDATE games SET status = '$coin' WHERE id = '$id'";
$result=mysqli_query($link, $query);
}
$query="SELECT user1 FROM games WHERE id = '$id'";
$result=mysqli_query($link, $query);
$user1=mysqli_fetch_row($result);
$user1=$user1[0];
?>
<!doctype html>
<html lang="en">
<title> Game </title>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootswatch/4.3.1/litera/bootstrap.min.css" rel="stylesheet" integrity="sha384-D/7uAka7uwterkSxa2LwZR7RJqH2X6jfmhkJ0vFPGUtPyBMF2WMq9S+f9Ik5jJu1" crossorigin="anonymous">
</head>
<style>
body {
background-image: url(https://c.pxhere.com/photos/fe/25/cube_play_random_luck_points_numbers_eyes_magic_cube_craps-1282150.jpg!d);
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
font-size: 200%;
}
div1 {
position: absolute;
top: 20%;
left: 30%;
transform: translate(-50%, -50%);
}
div2 {
position: absolute;
top: 20%;
left: 70%;
transform: translate(-50%, -50%);
}
div3 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<body>
<form method="post">
<div1>
<h2 class="alert alert-success">
<?php
print "$user";
?>
</h2>
<br>
<h3 align = "center" class="alert alert-success">
<?php
if ($user==$user1){
$query="SELECT score1 FROM games WHERE id = '$id'";
$result=mysqli_query($link, $query);
$your_score=mysqli_fetch_row($result);
print "$your_score[0]";
}
else {
$query="SELECT score2 FROM games WHERE id = '$id'";
$result=mysqli_query($link, $query);
$your_score=mysqli_fetch_row($result);
print "$your_score[0]";
}
?>
</h3>
</div1>
<br>
<div2>
<h2 class="alert alert-success">
<?php
if ($user==$user1){
$query="SELECT user2 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$opp_name=mysqli_fetch_row($result);
print "$opp_name[0]";
}
else {
$query="SELECT user1 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$opp_name=mysqli_fetch_row($result);
print "$opp_name[0]";
}
?>
</h2>
<br>
<h3 align = "center" class="alert alert-success">
<?php
if ($user==$user1){
$query="SELECT score2 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$your_opps_score=mysqli_fetch_row($result);
print "$your_opps_score[0]";
}
else {
$query="SELECT score1 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$your_opps_score=mysqli_fetch_row($result);
print "$your_opps_score[0]";
}
?>
</h3>
</div2>
<div3 align="center" >
<h2 class="alert alert-warning">
<?php
$query="SELECT status FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$status=mysqli_fetch_row($result);
if (($status[0]==1 AND $user==$user1) OR ($status[0]==2 AND $user!=$user1)) {
print "It's your turn!";
}
if (($status[0]==1 AND $user!=$user1) OR ($status[0]==2 AND $user==$user1)) {
print "It's opponent's turn, please wait...";
header("Refresh:10");
die();
}
if (($status[0]==3 AND $user==$user1) OR ($status[0]==4 AND $user!=$user1)) {
print "Victory is yours!<br/>";
$query="SELECT games FROM players WHERE login = '$user'";
$result=mysqli_query($link, $query);
$games=mysqli_fetch_row($result);
$games=$games[0];
$query="SELECT wins FROM players WHERE login = '$user'";
$result=mysqli_query($link, $query);
$wins=mysqli_fetch_row($result);
$wins=$wins[0];
if ($games!=0){
$winrate=round(($wins[0]/$games[0])*100);
if ($winrate==100){
$coef=50;
}
else {
$coef=100-$winrate;
}
}
else {
$coef=1;
}
$query="UPDATE players SET rating = rating + '$coef' WHERE login = '$user'";
$result=mysqli_query($link, $query);
$query="UPDATE players SET games = games + 1 WHERE login = '$user'";
$result=mysqli_query($link, $query);
$query="UPDATE players SET wins = wins + 1 WHERE login = '$user'";
$result=mysqli_query($link, $query);
print "You will return to the start page in 10 seconds";
mysqli_close($link);
header ('Refresh:10; URL = index.php');
die();
}
if (($status[0]==3 AND $user!=$user1) OR ($status[0]==4 AND $user==$user1)) {
print "You lose... Good luck next time!<br/>";
$query="SELECT games FROM players WHERE login = '$user'";
$result=mysqli_query($link, $query);
$games=mysqli_fetch_row($result);
$games=$games[0];
$query="SELECT wins FROM players WHERE login = '$user'";
$result=mysqli_query($link, $query);
$wins=mysqli_fetch_row($result);
$wins=$wins[0];
if ($games!=0){
if ($wins!=0){
$winrate=round(($wins[0]/$games[0])*100);
$coef=$winrate;
}
else {
$coef=50;
}
}
else {
$coef=100;
}
$query="UPDATE players SET rating = rating - '$coef' WHERE login = '$user'";
$result=mysqli_query($link, $query);
$query="UPDATE players SET games = games + 1 WHERE login = '$user'";
$result=mysqli_query($link, $query);
$query="UPDATE players SET loses = loses + 1 WHERE login = '$user'";
$result=mysqli_query($link, $query);
print "~You will return to the start page in 10 seconds~";
mysqli_close($link);
header ('Refresh:10; URL = index.php');
die();
}
?>
</h2>
<label><input type="submit" name="drop" class="btn btn-success" value="Drop!"></label>
<label><input type="submit" name="pass" class="btn btn-success" value="Pass!"></label> <br>
<h2 class="alert alert-warning">
<?php
if(array_key_exists('drop',$_POST)){
$query="SELECT total FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$total=mysqli_fetch_row($result);
$bet=rand(1,6);
$total[0]=$total[0]+$bet;
print "Total: $total[0]|";
print "Bet: $bet<br/>";
if ($bet==1){
print "Oops...Total is lost...Switching of turn";
$query="UPDATE games SET bet='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET total='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
if ($user==$user1){
$query="UPDATE games SET status='2' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:5");
die();
}
else {
$query="UPDATE games SET status='1' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:5");
die();
}
}
$query="UPDATE games SET bet='$bet' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="SELECT total FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$total=mysqli_fetch_row($result);
$total[0]=$total[0]+$bet;
$query="UPDATE games SET total='$total[0]' WHERE id='$id'";
$result=mysqli_query($link, $query);
}
if(array_key_exists('pass',$_POST)){
if ($user==$user1){
$query="SELECT score1 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$score1=mysqli_fetch_row($result);
$query="SELECT total FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$total=mysqli_fetch_row($result);
$score1[0]=$score1[0]+$total[0];
$query="UPDATE games SET score1='$score1[0]' WHERE id='$id'";
$result=mysqli_query($link, $query);
if ($score1[0]>=100) {
$query="UPDATE games SET status='3' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET bet='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET total='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:0");
die();
}
$query="UPDATE games SET status='2' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET bet='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET total='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:0");
die();
}
else {
$query="SELECT score2 FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$score2=mysqli_fetch_row($result);
$query="SELECT total FROM games WHERE id='$id'";
$result=mysqli_query($link, $query);
$total=mysqli_fetch_row($result);
$score2[0]=$score2[0]+$total[0];
$query="UPDATE games SET score2='$score2[0]' WHERE id='$id'";
$result=mysqli_query($link, $query);
if ($score2[0]>=100) {
$query="UPDATE games SET status='4' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET bet='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET total='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:0");
die();
}
$query="UPDATE games SET status='1' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET bet='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
$query="UPDATE games SET total='0' WHERE id='$id'";
$result=mysqli_query($link, $query);
header("Refresh:0");
die();
}
}
?>
</h2>
<br>
<h2 class="alert alert-warning">
<br> <a href="index.php"> Give up... </a>
</h2>
</div3>
</form>
</body>
</html> }