feat: Add statistics to about page

This commit is contained in:
overflowerror 2024-08-06 23:03:22 +02:00
parent 9fea53c099
commit 4fe068cd57
3 changed files with 144 additions and 1 deletions

View file

@ -2,8 +2,14 @@
require_once __DIR__ . '/../../core.php'; require_once __DIR__ . '/../../core.php';
require_once __DIR__ . '/../../lib/stats.php';
$stats = getStatistics();
$mobStats = getMobStats();
$title = "MobMash - About"; $title = "MobMash - About";
$content = function () { $content = function () use ($stats, $mobStats) {
require __DIR__ . '/../../view/pages/about.php'; require __DIR__ . '/../../view/pages/about.php';
}; };

86
lib/stats.php Normal file
View file

@ -0,0 +1,86 @@
<?php
require_once __DIR__ . '/rating.php';
function getStatistics(): array {
global $pdo;
$query = $pdo->query(<<<EOF
SELECT
max(c) AS max,
min(c) AS min,
avg(c) AS avg,
count(c) AS voters
FROM (
SELECT
count(*) AS c
FROM mm_matches
WHERE session IS NOT NULL GROUP BY session
) AS c;
EOF
);
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetch();
$query = $pdo->query("SELECT count(*) AS votes FROM mm_matches");
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = array_merge($result, $query->fetch());
$query = $pdo->query("SELECT count(*) AS mobs FROM mm_mobs");
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = array_merge($result, $query->fetch());
$query = $pdo->query(<<<EOF
SELECT
count(*) AS maxed_out
FROM (
SELECT
count(*) AS c
FROM mm_matches
WHERE session IS NOT NULL
GROUP BY session
) AS c
WHERE c.c >= power((SELECT count(*) FROM mm_mobs), 2);
EOF
);
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = array_merge($result, $query->fetch());
return $result;
}
function getMobStats(): array {
$mobs = getMobsWithMetaData();
return [
"highest_rating" => array_reduce($mobs,
fn($current, $mob) => $mob["rating"] > $current["rating"] ? $mob : $current,
$mobs[0]
),
"lowest_rating" => array_reduce($mobs,
fn($current, $mob) => $mob["rating"] < $current["rating"] ? $mob : $current,
$mobs[0]
),
"most_matches" => array_reduce($mobs,
fn($current, $mob) => $mob["matches"] > $current["matches"] ? $mob : $current,
$mobs[0]
),
"least_matches" => array_reduce($mobs,
fn($current, $mob) => $mob["matches"] < $current["matches"] ? $mob : $current,
$mobs[0]
),
"most_wins" => array_reduce($mobs,
fn($current, $mob) => $mob["wins"] > $current["wins"] ? $mob : $current,
$mobs[0]
),
"least_wins" => array_reduce($mobs,
fn($current, $mob) => $mob["losses"] > $current["losses"] ? $mob : $current,
$mobs[0]
),
];
}

View file

@ -1,3 +1,8 @@
<?php
$stats ??= [];
$mobStats ??= [];
?>
<h1>About</h1> <h1>About</h1>
<div class="text-container"> <div class="text-container">
@ -63,6 +68,52 @@
<a href="https://github.com/overflowerror/mobmash.click">Github</a>. Pull Requests are welcome! <a href="https://github.com/overflowerror/mobmash.click">Github</a>. Pull Requests are welcome!
</p> </p>
<h2 id="statistics">Statistics</h2>
<p>
There are currently <?= $stats["mobs"] ?> mobs in the system.
The top ranked mob is "<?= $mobStats["highest_rating"]["name"] ?>" with a rating of
<?= number_format($mobStats["highest_rating"]["rating"]) ?>. Last place is
"<?= $mobStats["lowest_rating"]["name"] ?>" with a rating of
<?= number_format($mobStats["lowest_rating"]["rating"]) ?>.
</p>
<p>
"<?= $mobStats["most_matches"]["name"] ?>" has fought the most matches:
<?= $mobStats["most_matches"]["matches"] ?>, out of which it won <?= $mobStats["most_matches"]["wins"] ?>.
<?php if ($mobStats["most_matches"]["id"] == $mobStats["most_wins"]["id"]): ?>
Which also makes it the mob with the most wins.
<?php else: ?>
Speaking of wins: The mob with the most wins is "<?= $mobStats["most_wins"]["name"] ?>" with
<?= $mobStats["most_wins"]["wins"] ?> wins out of <?= $mobStats["most_wins"]["matches"] ?> matches.
<?php endif ?>
</p>
<p>
Until now, there have been <?= $stats["votes"] ?> votes.
</p>
<p>
Over the past 6 months, there have been <?= $stats["voters"] ?> unique voters. On average, each one voted
<?= number_format($stats["avg"], 1) ?> times with <?= $stats["max"] ?> <?= $stats["max"] > 80 ? "(You people are mad!)" : "" ?>
as the maximum.
</p>
<p>
<?php
if ($stats["maxed_out"] == 0) {
?>
So far, none have voted for all <?= $stats["mobs"] * $stats["mobs"] ?> pairings yet. : (
<?php
} else if ($stats["maxed_out"] == 1) {
?>
Coincidentally, that maximum is the highest possible number - this one person voted for every single paring. ^^
<?php
} else {
?>
Coincidentally, that maximum is the highest possible number - <?= $stats["maxed_out"] ?> visitors voted for every
single paring. D:
<?php
}
?>
</p>
<h2 id="contact">Contact</h2> <h2 id="contact">Contact</h2>
<p> <p>