Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/projects/

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

legacy-code-rps.php

231 lines UTF-8 Windows (CRLF)
<?php
function sendMove($dbconn$matchkey$move){

$userkey=$_SESSION['userid'];

//get current round number 
$sql"SELECT round FROM moves WHERE EXISTS (SELECT round FROM moves WHERE matchkey='$matchkey' ORDER BY round DESC)";
$stmt $dbconn -> prepare($sql);
$stmt -> execute();
$currentround=1;
while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
    
$currentround $res['round'];
    GLOBAL 
$currentround;
    echo 
"round number acquired".$currentround;
    break;
}


//check if both users have made moves
$sql"SELECT * FROM moves WHERE matchkey='$matchkey' AND round = '$currentround'";
$stmt $dbconn -> prepare($sql);
$stmt -> execute();
$useridArrayCurrentRound = [];
while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
    
array_push($useridArrayCurrentRound$res['userid']);
    if(
$useridArrayCurrentRound==2){
        
$currentround+=1;
    }
}


//check if user has made a move this round
//$sql= "SELECT EXISTS (SELECT * FROM moves WHERE matchkey='$matchkey' AND round='$currentround' AND  userid='$userkey')";
$sql ="SELECT COUNT(*) AS total FROM moves WHERE matchkey='$matchkey' AND round='$currentround' AND  userid='$userkey'";
$stmt $dbconn -> prepare($sql);
$stmt -> execute();
while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){;
    if(
$res['total']==/*count($currentRoundMoveArray)==2||count($currentRoundMoveArray)==0*/){
        
/*if(count($currentRoundMoveArray)==2){
            $currentround+=1;
        }*/
        
$sql "INSERT INTO moves (matchkey, userid, round, rps) VALUES (?,?,?,?)";
        
$stmt $dbconn -> prepare($sql);
        
$data=[$matchkey$_SESSION['userid'], $currentround$move];
        
$stmt -> execute($data);
    }
    else{
        echo 
"move already made";
    }
}
}

function 
getMatchesBettingVersion($dbconn){
    
$sql="SELECT * FROM matches /*LEFT JOIN rpsusers ON matches*/";
    
$stmt=$dbconn->prepare($sql);
    
$stmt->execute();
    while(
$res=$stmt->fetch(PDO::FETCH_ASSOC)){
        if(
$res['player2id']!=null AND $res['player1id']!=null AND $res['isactive']==1){
            
$player1id=$res['player1id'];
            
$player2id=$res['player2id'];
            
$matchid=$res['matchid'];

            
/*
            $sql="SELECT * FROM rpsusers WHERE userid=? OR userid=?";
            $stmt=$dbconn->prepare($sql);
            $stmt->execute([$player1,$player2]);
            $x=0;
            while($res=$stmt->fetch(PDO::FETCH_ASSOC)){
                echo $res['username'];
                if($x==0){
                    echo " vs ";
                    $x++;
                }
            }
            */

            
$u1n null;
            
$u2n null;

            
$sql="SELECT * FROM rpsusers";
            
$stmt=$dbconn->prepare($sql);
            
$stmt->execute();
            while(
$res=$stmt->fetch(PDO::FETCH_ASSOC)){
                if(
$player1id==$res['userid']){
                    
$u1n=$res['username'];
                    echo 
$u1n." vs ";
                } 
                else if(
$player2id==$res['userid']){
                    
$u2n=$res['username'];
                    echo 
$u2n;
                }
            }

            echo 
'<html><form action="" method="POST">
            <button type radio name="chosenplayer" value="'
.$player1id.'>'.$u1n.'</button>
            <button type radio name="chosenplayer" value="'
.$player2id.'>'.$u2n.'</button>
            <input type="text" name="betamount">test</input>
            <input type="submit">'
;
        }
    }
}

function 
getMatchResult($dbconn,$matchkey){
    
//get roundamount
    
$roundamount=null;
    
$sql ="SELECT * FROM matches WHERE matchid=?";
    
$stmt $dbconn -> prepare($sql);
    
$stmt->execute([$matchkey]);
    while(
$res=$stmt->fetch(PDO::FETCH_ASSOC)){
        
$roundamount=$res['roundAmount'];
    }


    
$sql="UPDATE matches SET isactive=0 WHERE matchid=?";
    
$stmt $dbconn -> prepare($sql);
    if(isset(
$_GET['matchkey'])){
        
$stmt -> execute([$_GET['matchkey']]);
    }
    else{
        
$stmt -> execute([$matchkey]);
    }


    
$sql "SELECT count(*) AS total FROM moves WHERE matchkey=?";
    
$stmt=$dbconn->prepare($sql);
    
$stmt->execute([$_GET['matchkey']]);

    
//create associative array
    
$matchResultsArray = [];

    
$yourmoves=[];
    
$yourscore=[];

    
$opponentmoves=[];
    
$opponentscore=[];

    if(
$res$stmt->fetch(PDO::FETCH_ASSOC)){
        if(
$res['total']==$roundamount*2){

            
$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']==$_SESSION['userid']){
                    
array_push($yourmoves,$res['rps']);
                }
                else if(
$res['userid']!=$_SESSION['userid']){
                    
array_push($opponentmoves,$res['rps']);
                } 
            }

            
//calculate scores
            
$x=0;
            while(
$x<$roundamount){
                if(
$yourmoves[$x]=="rock"){
                    if(
$opponentmoves[$x]=="rock"){
                        
//does nothing
                    
}
                    else if(
$opponentmoves[$x]=="paper"){
                        
array_push($opponentscore,1);
                    }
                    else if(
$opponentmoves[$x]=="scissors"){
                        
array_push($yourscore,1);
                    }
                }
                else if(
$yourmoves[$x]=="paper"){
                    if(
$opponentmoves[$x]=="rock"){
                        
array_push($yourscore,1);
                    }
                    else if(
$opponentmoves[$x]=="paper"){
                        
//does nothing
                    
}
                    else if(
$opponentmoves[$x]=="scissors"){
                        
array_push($opponentscore,1);
                    }
                }
                else if(
$yourmoves[$x]=="scissors"){
                    if(
$opponentmoves[$x]=="rock"){
                        
array_push($opponentscore,1);
                    }
                    else if(
$opponentmoves[$x]=="paper"){
                        
array_push($yourscore,1);
                    }
                    else if(
$opponentmoves[$x]=="scissors"){
                        
//does nothing
                    
}
                }
                
$x++;
            }

           
$yscore=count($yourscore);
           
$oscore=count($opponentscore);

            
//show scores
            
echo "<br>dina poäng: ".$yscore."<br>";
            echo 
"motståndarens poäng: ".$oscore."<br>";
            
            if(
$yscore>=$oscore){
                echo 
"du vann";
                
$sql "UPDATE matches SET p1points=? WHERE matchid=?";
                
$stmt=$dbconn->prepare($sql);
                
$stmt->execute([$yscore,$matchkey]);
            }
            else if(
$yscore==$oscore){
                echo 
"oavgjort";
            }
            else if(
$yscore<=$oscore){
                echo 
"motståndaren vann";
                
$sql "UPDATE matches SET p1points=? WHERE matchid=?";
                
$stmt=$dbconn->prepare($sql);
                
$stmt->execute([$oscore,$matchkey]);
            }
        }
        
        else{
            echo 
"wait";
            
//sleep(1);
            
getMatchResult($dbconn,$matchkey); 
        }
        
    }
}
?>