Show sourcecode
The following files exists in this folder. Click to view.
bacon-egg-pizza.php
cquiz.php
create-account.php
create-account111.php
create-quiz.php
create-quiz2.php
createaccount.php
delete-quiz.php
drop-rps-tables.php
legacy-code-rps.php
legacy-index.php
login.php
logincheck.php
newpassword-verify.php
newpassword.php
potential-code.txt
quiz-db-init.php
quizhub.php
quizhub2.php
results.php
rpsaccount-ajax-user-search.php
rpsaccount.php
rpsbetting-ajax-match-finder.php
rpsbetting.php
rpsbettingleaderboard.php
rpschangepassword.php
rpschangepassword.txt
rpsgame-ajax.php
rpsgame.php
rpsgameresults.php
rpshostnewgame.php
rpshub.php
rpsleaderboard.php
rpslib.php
rpslogin.php
rpsmaininclude.php
rpsproject/
rpsusersearch.php
table-init.php
verification.php
rpslib.php
560 lines UTF-8 Windows (CRLF)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
function getUserInfo($user){
require("../incl/dbconnection.php");
$userInfo=[];
$userinfocolumns = ['username','pass','email','wins','losses','ratio','bettingpoints'];
$sql = "SELECT * FROM rpsusers WHERE username = ?";
$stmt = $dbconn -> prepare($sql);
$stmt -> execute([$user]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
foreach($userinfocolumns as $type){
array_push($userInfo,$res["$type"]);
}
}
return $userInfo;
}
function getMatches($dbconn){
$sql = "SELECT * FROM matches WHERE player2id IS NULL";
$stmt = $dbconn -> prepare($sql);
$stmt -> execute();
echo "<h2>matcher:</h2>";
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
$key = $res['matchid'];
echo '<a href="rpsgame.php?matchid='.$key.'">gå med!</a><br>';
}
//ADD ASYNC
/*sleep(1);
getMatches($dbconn);
*/
}
function matchActiveCheck($dbconn, $matchid){
$sql = "SELECT isactive FROM matches WHERE matchid = ?";
$stmt = $dbconn -> prepare($sql);
$stmt -> execute([$matchid]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['isactive']==0){
return false;
}
else{
return true;
}
}
}
class RPSgame{
}
function joinGame($dbconn, $matchkey){
$_SESSION['hasMadeMove'] = false;
$sql = "SELECT * FROM matches WHERE matchid = ?";
$stmt = $dbconn -> prepare($sql);
$stmt -> execute([$matchkey]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['player2id']==null AND $res['player1id']!=$_SESSION['userid']){
$ukey = $_SESSION['userid'];
$sql = "UPDATE matches SET player2id = ? WHERE matchid = ?";
$stmt = $dbconn -> prepare($sql);
$stmt -> execute([$ukey, $matchkey]);
}
}
}
function sendMove($dbconn, $matchkey, $move) {
// Securely obtain the current user from session
$userkey = $_SESSION['userid'];
// Retrieve the current round (default is 1 if no moves exist)
$sql = "SELECT round FROM moves WHERE matchkey = ? ORDER BY round DESC LIMIT 1";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
$currentround = 1;
if ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$currentround = $res['round'];
echo "Round number acquired: " . $currentround."<br>";
}
// Check how many moves have been made in the current round
$sql = "SELECT userid FROM moves WHERE matchkey = ? AND round = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $currentround]);
$useridArrayCurrentRound = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
// If both players have moved, start a new round
if (count($useridArrayCurrentRound) == 2) {
$currentround++;
}
// Check if this user has already made a move in the current round
$sql = "SELECT COUNT(*) AS total FROM moves WHERE matchkey = ? AND round = ? AND userid = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $currentround, $userkey]);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
if ($res['total'] == 0) {
// Insert the move if not already made
$sql = "INSERT INTO moves (matchkey, userid, round, rps) VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $userkey, $currentround, $move]);
} else {
echo "move already made<br>";
}
checkMoveStatus($dbconn,$matchkey,$currentround);
}
function sendMoveV2($dbconn, $matchkey, $move){
if($_SESSION['hasMadeMove']==false){
//get user id
$userkey = $_SESSION['userid'];
//get current round number
$sql = "SELECT * FROM moves WHERE matchkey = ? ORDER BY round DESC LIMIT 1";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
if ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$_SESSION['currentround'] = $res['round'];
echo "Round number acquired: " . $_SESSION['currentround']."<br>";
}
$sql = "INSERT INTO moves (matchkey, userid, round, rps) VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $userkey, $_SESSION['currentround'], $move]);
/*
// Check how many moves have been made in the current round
$sql = "SELECT userid FROM moves WHERE matchkey = ? AND round = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $_SESSION['currentround']]);
$useridArrayCurrentRound = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
// If both players have moved, start a new round
if (count($useridArrayCurrentRound) == 2) {
$_SESSION['currentround']++;
}
*/
$_SESSION['hasMadeMove']=true;
checkMoveStatusV1V2($dbconn,$matchkey,$_SESSION['currentround']);
}
else{
echo "wait for opponent";
}
}
function creatematch($dbconn,$roundnum){
$userid = $_SESSION['userid'];
//creates match
$sql= "INSERT INTO matches (roundAmount, player1id, p1points, p2points, isactive, datestarted) VALUES (?,?,?,?,?,?)";
$stmt = $dbconn->prepare($sql);
$data = [$roundnum,$userid,0,0,1,time()];
$stmt->execute($data);
//finds created match
$sql = "SELECT matchid FROM matches WHERE player1id = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$userid]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
$matchkey=$res['matchid'];
}
//sends match creator to created match
header("Location:rpsgame.php?matchid=$matchkey");
}
function checkMoveStatus($dbconn,$matchkey,$currentround){
$sql = "SELECT COUNT(*) AS total FROM moves WHERE matchkey = ? AND round = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $currentround]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['total']==2){
$sql="SELECT * FROM moves WHERE matchkey=? AND round=? /*AND NOT userid=?*/";
$stmt = $dbconn->prepare($sql);
$data=[$matchkey,$currentround/*,$_SESSION['userid']*/];
$stmt->execute($data);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['userid']!=$_SESSION['userid']){
echo "opponent: ".$res['rps'].'<br>';
}
else if($res['userid']==$_SESSION['userid']){
echo "your move: ".$res['rps']."<br>";
}
}
}
else if($res['total']==1){
echo "waiting for opponent";
}
else if($res['total']==0){
echo $_SESSION['rpsmove']."<br>";
}
$sql="SELECT roundAmount FROM matches WHERE matchid = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['roundAmount']==$currentround){
header("Location:rpsgameresults.php?matchkey=$matchkey");
$_SESSION['isInGame'] = false;
}
}
}
}
function checkMoveStatusV2($dbconn,$matchkey,$currentround){
$sql = "SELECT COUNT(*) AS total FROM moves WHERE matchkey = ? AND round = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $currentround]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['total']==2){
$sql="SELECT * FROM moves WHERE matchkey=? AND round=? /*AND NOT userid=?*/";
$stmt = $dbconn->prepare($sql);
$data=[$matchkey,$currentround/*,$_SESSION['userid']*/];
$stmt->execute($data);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['userid']!=$_SESSION['userid']){
echo "opponent: ".$res['rps'].'<br>';
}
else if($res['userid']==$_SESSION['userid']){
echo "your move: ".$res['rps']."<br>";
}
}
$_SESSION['currentround']++;
$_SESSION['hasMadeMove']=false;
}
else if($res['total']==1){
echo "waiting for opponent";
}
else if($res['total']==0){
echo $_SESSION['rpsmove']."<br>";
}
}
$sql="SELECT roundAmount FROM matches WHERE matchid = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['roundAmount']==$_SESSION['currentround']){
header("Location:rpsgameresults.php?matchkey=$matchkey");
$_SESSION['isInGame'] = false;
}
}
}
function checkMoveStatusV1V2($dbconn,$matchkey,$currentround){
$sql = "SELECT
m.matchid,
m.player1id,
m.player2id,
u1.userid AS p1id,
u2.userid AS p2id,
u1.rps AS u1move,
u2.rps AS u2move
FROM matches AS m
INNER JOIN moves AS u1 ON m.player1id = u1.userid
INNER JOIN moves AS u2 ON m.player2id = u2.userid
WHERE matchid = ?";
$stmt = $dbconn->prepare($sql);
$data=[$matchkey];
$stmt->execute($data);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
//echo $res[];
}
$sql = "SELECT COUNT(*) AS total FROM moves WHERE matchkey = ? AND round = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey, $currentround]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['total']==2){
$sql="SELECT * FROM moves WHERE matchkey=? AND round=? /*AND NOT userid=?*/";
$stmt = $dbconn->prepare($sql);
$data=[$matchkey,$currentround/*,$_SESSION['userid']*/];
$stmt->execute($data);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['userid']!=$_SESSION['userid']){
echo "opponent: ".$res['rps'].'<br>';
}
else if($res['userid']==$_SESSION['userid']){
echo "your move: ".$res['rps']."<br>";
}
}
}
else if($res['total']==1){
echo "waiting for opponent or you";
}
else if($res['total']==0){
echo $_SESSION['rpsmove']."<br>";
}
$sql="SELECT roundAmount FROM matches WHERE matchid = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['roundAmount']==$currentround){
header("Location:rpsgameresults.php?matchkey=$matchkey");
$_SESSION['isInGame'] = false;
exit;
}
}
echo "<hr><strong>All Moves in Match:</strong><br>";
$sql = "SELECT round, userid, rps FROM moves WHERE matchkey = ? ORDER BY round ASC, userid ASC";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$matchkey]);
$currentRound = -1;
while ($move = $stmt->fetch(PDO::FETCH_ASSOC)) {
// New round header
if ($currentRound != $move['round']) {
$currentRound = $move['round'];
echo "<br><strong>Round " . $currentRound . ":</strong><br>";
}
// Identify if it's the current user or opponent
$label = ($move['userid'] == $_SESSION['userid']) ? "You" : "Opponent";
echo $label . ": " . htmlspecialchars($move['rps']) . "<br>";
}
}
}
function changePassword($dbconn,$oldpass,$newpass){
$sql = "SELECT * FROM rpsusers WHERE userid = ?";
$stmt = $dbconn->prepare($sql);
$stmt->execute([$_SESSION['userid']]);
while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
if($res['lastpasswordchangedtime']+60*15<=time()){
if(password_verify($oldpass,$res['pass'])){
$sql ="UPDATE rpsusers SET pass=?, lastpasswordchangedtime=? WHERE userid = ?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([password_hash($newpass,PASSWORD_DEFAULT),time(),$_SESSION['userid']]);
header("Location:rpshub.php");
}
else{
echo "fel lösenord";
}
}
else{
echo "vänta lite till innan du ändrar lösenord";
}
}
}
function getMatchResult($dbconn,$matchkey){
//define variables
$roundamount=null;
$player1id = null;
$player2id = null;
$player1name=null;
$player2name=null;
//get match info
$sql = "SELECT
m.matchid,
m.roundAmount,
m.player1id,
m.player2id,
u1.userid AS p1id,
u1.username AS player1name,
u2.userid AS p2id,
u2.username AS player2name
FROM matches AS m
INNER JOIN rpsusers AS u1 ON m.player1id = u1.userid
INNER JOIN rpsusers AS u2 ON m.player2id = u2.userid
WHERE m.matchid=?";
$stmt = $dbconn -> prepare($sql);
$stmt->execute([$matchkey]);
while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
$roundamount=$res['roundAmount'];
$player1id=$res['p1id'];
$player1name=$res['player1name'];
$player2id=$res['p2id'];
$player2name=$res['player2name'];
}
//remove match from active matches
$sql="UPDATE matches SET isactive=0 WHERE matchid=?";
$stmt = $dbconn -> prepare($sql);
if(isset($_GET['matchkey'])){
$stmt -> execute([$_GET['matchkey']]);
}
else{
$stmt -> execute([$matchkey]);
}
//get number of moves
$sql = "SELECT count(*) AS total FROM moves WHERE matchkey=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$_GET['matchkey']]);
//create associative array
$matchResultsArray = [];
$p1moves=[];
$p1score=[];
$p2moves=[];
$p2score=[];
//check if correct number of moves have been made
if($res= $stmt->fetch(PDO::FETCH_ASSOC)){
if($res['total']==$roundamount*2){
//get all moves from match
$sql = "SELECT * FROM moves WHERE matchkey=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$matchkey]);
while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
/*
$matchResultsArray[]=$res['round'];
$matchResultsArray[$res['round']][]=$res['userid'];
$matchResultsArray[$res['round']][$res['userid']][] = $res['rps'];
*/
//add moves to array
if($res['userid']==$player1id){
array_push($p1moves,$res['rps']);
}
else if($res['userid']==$player2id){
array_push($p2moves,$res['rps']);
}
}
//calculate scores
$x=0;
while($x<$roundamount){
if($p1moves[$x]=="rock"){
if($p2moves[$x]=="rock"){
//does nothing
}
else if($p2moves[$x]=="paper"){
array_push($p2score,1);
}
else if($p2moves[$x]=="scissors"){
array_push($p1score,1);
}
}
else if($p1moves[$x]=="paper"){
if($p2moves[$x]=="rock"){
array_push($p1score,1);
}
else if($p2moves[$x]=="paper"){
//does nothing
}
else if($p2moves[$x]=="scissors"){
array_push($p2score,1);
}
}
else if($p1moves[$x]=="scissors"){
if($p2moves[$x]=="rock"){
array_push($p2score,1);
}
else if($p2moves[$x]=="paper"){
array_push($p1score,1);
}
else if($p2moves[$x]=="scissors"){
//does nothing
}
}
$x++;
}
$p1score=count($p1score);
$p2score=count($p2score);
//show scores
echo "<br>$player1name: $p1score poäng<br>";
echo "$player2name: $p2score poäng<br>";
if($p1score>$p2score){
echo "$player1name vann";
$sql = "UPDATE matches SET p1points=? WHERE matchid=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$p1score,$matchkey]);
//update bet with p1 win
updateBets($dbconn,$player1id,$matchkey);
}
else if($p1score==$p2score){
echo "oavgjort";
}
else if($p1score<$p2score){
echo "$player2name vann";
$sql = "UPDATE matches SET p2points=? WHERE matchid=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$p2score,$matchkey]);
//update bet with p2 win
updateBets($dbconn,$player2id,$matchkey);
}
updateWinLoss($dbconn);
}
else{
//echo "wait";
//sleep(1);
getMatchResult($dbconn,$matchkey);
}
}
}
function updateWinLoss($dbconn){
$uid=$_SESSION['userid'];
$sql="SELECT * FROM matches WHERE player1id=? OR player2id=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$uid,$uid]);
$tempWins=0;
$tempLosses=0;
$ratio=0;
while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
if($res['player1id']==$uid){
if($res['p1points']>$res['p2points']){
$tempWins++;
}
else if($res['p1points']<$res['p2points']){
$tempLosses++;
}
}
else if($res['player2id']==$uid){
if($res['p2points']>$res['p1points']){
$tempWins++;
}
else if($res['p2points']<$res['p1points']){
$tempLosses++;
}
}
}
if($tempWins != 0 AND $tempLosses != 0){
$ratio = $tempWins/$tempLosses;
}
$sql="UPDATE rpsusers SET wins=?, losses=?, ratio=? WHERE userid=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$tempWins,$tempLosses,$ratio,$uid]);
}
function updateBets($dbconn,$winningplayerid,$matchid){
$sql = "SELECT
b.beton, b.totalbet, b.userid AS buid,
u.bettingpoints, u.userid AS nuid
FROM bets AS b
INNER JOIN rpsusers AS u ON b.userid = u.userid
WHERE matchid = ?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$matchid]);
while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
$bet=$res['totalbet'];
$betwinnings=$bet*2;
$currentuserid = $res['buid'];
$bettingpoints=$res['bettingpoints'];
$newBettingPoints = $bettingpoints+$betwinnings;
if($res['beton']==$winningplayerid){
$sql="UPDATE rpsusers SET bettingpoints=? WHERE userid=?";
$stmt=$dbconn->prepare($sql);
$stmt->execute([$newBettingPoints,$currentuserid]);
}
}
}
?>