View sourcecode

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

ladder.php

142 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
<?php
require "database.php";
require 
"auth.php";
requireLogin();

$ladderID $_GET["id"] ?? null;
$playerID $_SESSION["player_id"];

if (!
$ladderID) {
    die(
"No ladder selected");
}

/* Ladder info */
$stmt $conn->prepare(
    
"SELECT L.*, P.Username AS OwnerName
     FROM Ladder L
     JOIN Players P ON L.OwnerPlayerID = P.PlayerID
     WHERE L.LadderID = ?"
);
$stmt->execute([$ladderID]);
$ladder $stmt->fetch();

if (!
$ladder) {
    die(
"Ladder not found");
}

$isOwner = ($ladder["OwnerPlayerID"] == $playerID);

/* Players in ladder */
$stmt $conn->prepare(
    
"SELECT P.PlayerID, P.Username, LP.CurrentScore
     FROM LadderPlayers LP
     JOIN Players P ON LP.PlayerID = P.PlayerID
     WHERE LP.LadderID = ?
     ORDER BY LP.CurrentScore DESC"
);
$stmt->execute([$ladderID]);
$players $stmt->fetchAll();

/* Recent matches */
$stmt $conn->prepare(
    
"SELECT m.PlayedAt,
            p1.Username AS P1,
            p2.Username AS P2,
            w.Username AS Winner
     FROM Matches m
     JOIN Players p1 ON m.Player1ID = p1.PlayerID
     JOIN Players p2 ON m.Player2ID = p2.PlayerID
     JOIN Players w  ON m.WinnerID  = w.PlayerID
     WHERE m.LadderID = ?
     ORDER BY m.PlayedAt DESC
     LIMIT 5"
);
$stmt->execute([$ladderID]);
$matches $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title><?= htmlspecialchars($ladder["LadderName"]) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">

    <!-- Ladder header -->
    <h2><?= htmlspecialchars($ladder["LadderName"]) ?></h2>

    <p>
        Owner: <strong><?= htmlspecialchars($ladder["OwnerName"]) ?></strong><br>
        Invite Code:
        <span class="badge bg-secondary fs-6">
            <?= htmlspecialchars($ladder["InviteCode"]) ?>
        </span>
    </p>

    <!-- buttons -->
    <div class="mb-3">
        <a href="match_create.php?ladder_id=<?= $ladderID ?>"
           class="btn btn-success">
            Submit Match
        </a>

        <a href="match_history.php?ladder_id=<?= $ladderID ?>"
           class="btn btn-outline-primary">
            Match History
        </a>

        <a href="dashboard.php" class="btn btn-secondary">
            Back
        </a>
    </div>

    <!-- Players table -->
    <table class="table table-striped mt-4">
        <thead>
        <tr>
            <th>#</th>
            <th>Player</th>
            <th>Score</th>
        </tr>
        </thead>
        <tbody>
        <?php $i 1; foreach ($players as $player): ?>
            <tr>
                <td><?= $i++ ?></td>
                <td><?= htmlspecialchars($player["Username"]) ?></td>
                <td><?= $player["CurrentScore"?></td>

            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Recent matches -->
    <h4 class="mt-5">Recent Matches</h4>

    <ul class="list-group">
        <?php if (!$matches): ?>
            <li class="list-group-item text-muted">
                No matches played yet
            </li>
        <?php endif; ?>

        <?php foreach ($matches as $m): ?>
            <li class="list-group-item">
                <?= htmlspecialchars($m["P1"]) ?>
                vs
                <?= htmlspecialchars($m["P2"]) ?>
                —
                Winner:
                <strong><?= htmlspecialchars($m["Winner"]) ?></strong>
            </li>
        <?php endforeach; ?>
    </ul>

</div>
</body>
</html>