Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/slutprojekt/

actions.php
administer_users.php
create_code.php
database_include.php
dbconnection.php
index.php
login copy.php
login.php
main.php
password_renewals.php
play copy.php
play.php
setup.php
sign_up.php
statistics.php
verification.php
verify_mail.php

statistics.php

131 lines UTF-8 Windows (CRLF)
<?php
ini_set
('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);
session_start(["gc_maxlifetime" => 86400]);
?>

<!DOCTYPE html>
<html lang="sv">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Statistik</title>
    <style>
        fieldset {
            margin: 5px;
            padding: 5px;
            border: 2px solid black;
        }
    </style>
</head>

<body>
    <a href="main.php">Tillbaka till main</a>
    <?php
    
function getStats($dbconn$user_id): array
    {
        
$statistics = [];

        
// summan blir null om det inte finns något men här ersätts null med 0
        
$sql "SELECT COALESCE(SUM(hit), 0) AS total_hits, count(*) AS total_moves FROM bs_moves";
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute();

        
$row $stmt->fetch(PDO::FETCH_ASSOC);

        
$total_hits $row["total_hits"];
        
$total_moves $row["total_moves"];
        
$avr_accurasy $total_moves round($total_hits $total_moves3) : 0// Snitt-träffsäkerheten
        
$statistics["avr_accurasy"] = $avr_accurasy;

        
$sql "SELECT COALESCE(SUM(hit), 0) AS total_hits, count(*) AS total_moves FROM bs_moves WHERE user_id = ?";
        
$stmt $dbconn->prepare($sql);
        
$data = [$user_id];
        
$stmt->execute($data);

        
$row $stmt->fetch(PDO::FETCH_ASSOC);

        
$hits $row["total_hits"];
        
$moves $row["total_moves"];
        
$accurasy $moves round($hits $moves3) : 0// Ens egna träffsäkerhet
        
$statistics["accurasy"] = $accurasy;

        
$sql "SELECT count(*) AS game_count FROM bs_logs WHERE user_1_id = ? OR user_2_id = ?";
        
$stmt $dbconn->prepare($sql);
        
$data = [$user_id$user_id];
        
$stmt->execute($data);
        
$game_count $stmt->fetch(PDO::FETCH_ASSOC)["game_count"];
        
$statistics["game_count"] = $game_count;

        
$sql "SELECT count(*) AS win_count FROM bs_logs WHERE winner = ?";
        
$stmt $dbconn->prepare($sql);
        
$data = [$user_id];
        
$stmt->execute($data);
        
$win_count $stmt->fetch(PDO::FETCH_ASSOC)["win_count"];
        
$statistics["win_count"] = $win_count;

        
$win_rate $game_count round($win_count $game_count3) : 0;
        
$statistics["win_rate"] = $win_rate;

        
$sql "SELECT 
            bs_logs.winner, 
            bs_users.username, 
            COUNT(*) AS wins
            FROM bs_logs
            JOIN bs_users ON bs_users.id = bs_logs.winner
            GROUP BY bs_logs.winner, bs_users.username
            ORDER BY wins DESC LIMIT 5"
// På något sätt hämtar denna ut de vanligaste id'na i bs_logs. chat-gpt-gjord. Har inte hunnit lära mig hur den fungerar men testning visar iallafall att den gör det.
        
$stmt $dbconn->prepare($sql);
        
$stmt->execute();

        
$leaderboard = [];
        while (
$row $stmt->fetch(PDO::FETCH_ASSOC)) {
            
$leaderboard[] = [
                
"user_id" => $row["winner"],
                
"username" => $row["username"],
                
"wins" => $row["wins"]
            ];
        }
        
$statistics["leaderboard"] = $leaderboard;
        return (
$statistics);
    }
    try {
        
/** @var PDO $dbconn */
        
include("dbconnection.php");
        
$adminrequired FALSE;
        include(
"verification.php");

        
$user_id $_SESSION["user_id"];
        
$statistics getStats($dbconn$user_id);
    } catch (
PDOException $e) {
        echo (
$e->getMessage());
    }

    
// Printar ut leaderboarden ________________________________________________________
    
echo ("<h1>DE BÄSTA</h1>");
    echo (
"<table border='1' cellpadding='5'>");
    echo 
"<tr>
        <th>Username</th>
        <th>Wins</th>
        </tr>"
;
    foreach (
$statistics["leaderboard"] as $row) {
        echo 
"<tr>";
        echo 
"<td>" htmlspecialchars($row["username"]) . "</td>";
        echo 
"<td>" $row["wins"] . "</td>";
        echo 
"</tr>";
    }
    echo 
"</table>";

    echo(
"<h1>Om dina prestationer</h1>");
    echo(
"<p>Spelade matcher: " $statistics["game_count"] . "</p>");
    echo(
"<p>Vunna matcher: " $statistics["win_count"] . "</p>");
    echo(
"<p>Vinstandel: " $statistics["win_rate"]*100 "%</p>");
    echo(
"<p>Träffsäkerhet: " $statistics["accurasy"]*100 "%</p>");
    echo(
"<h1>Globalt:</h1>");
    echo(
"<p>Träffsäkerhet: " $statistics["avr_accurasy"]*100 "%</p>");
    
?>
</body>

</html>