feat: Show random mobs in UI

This commit is contained in:
overflowerror 2024-07-28 00:03:17 +02:00
parent 1199023daf
commit 8fc7bef7f2
7 changed files with 113 additions and 1 deletions

19
html/index.php Normal file
View file

@ -0,0 +1,19 @@
<?php
require_once __DIR__ . "/../core.php";
require_once __DIR__ . "/../lib/rating.php";
session_start();
$left = getRandomMob();
$right = getRandomMob();
$left["rating"] = getEloForMob($left["id"]);
$right["rating"] = getEloForMob($right["id"]);
$title = "Test";
$content = function() use ($left, $right) {
include __DIR__ . "/../view/fragments/mobSelection.php";
};
include __DIR__ . "/../view/layout.php";

22
html/styles/main.css Normal file
View file

@ -0,0 +1,22 @@
.selection {
display: flex;
flex-direction: row;
justify-content: center;
margin-left: 5%;
margin-right: 5%;
max-width: 800px;
}
.separator {
flex-grow: 1;
}
.mob {
background-color: green;
width: 40%;
flex-grow: 1;
}
.mob img {
width: 80%;
}

18
lib/rating.php Normal file
View file

@ -0,0 +1,18 @@
<?php
require_once __DIR__ . "/database.php";
function getRandomMob(): array {
global $pdo;
$result = $pdo->query("SELECT * FROM mm_mobs WHERE enabled = true");
$result = $result->fetchAll(PDO::FETCH_ASSOC);
return $result[array_rand($result)];
}
function getEloForMob(int $mob): int {
global $pdo;
$query = $pdo->prepare("SELECT * FROM mm_current_rating WHERE mob = ?");
$query->execute([$mob]);
$result = $query->fetch(PDO::FETCH_ASSOC);
return $result["rating"];
}

View file

@ -112,4 +112,24 @@ SELECT mob,
last_update
FROM rating;
CREATE VIEW mm_current_rating(mob, rating, last_update) AS
SELECT mob,
rating,
last_update
FROM mm_rating
WHERE last_update = (
(
SELECT max(matches_with_default.id) AS max
FROM (
SELECT
mm_matches_of_mob.id,
mm_matches_of_mob.mob
FROM mm_matches_of_mob
UNION
SELECT
0,
mm_rating.mob
) matches_with_default
WHERE matches_with_default.mob = mm_rating.mob
)
);

6
view/fragments/mob.php Normal file
View file

@ -0,0 +1,6 @@
<div class="mob">
<img src="/images/mobs/<?php echo $mob["image"] ?? "_placeholder.png"; ?>"><br />
<?php echo $mob["id"]; ?><br />
<?php echo $mob["name"]; ?><br />
<?php echo $mob["rating"]; ?><br />
</div>

View file

@ -0,0 +1,13 @@
<div class="selection">
<?php
$mob = $left ?? [];
include __DIR__ . "/mob.php";
?>
<div class="separator">
OR
</div>
<?php
$mob = $right ?? [];
include __DIR__ . "/mob.php";
?>
</div>

14
view/layout.php Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?? ""; ?></title>
<link rel="stylesheet" type="text/css" href="/styles/main.css" />
</head>
<body>
<?php
if (isset($content)) {
$content();
}
?>
</body>
</html>