feat: Add voting logic

This commit is contained in:
overflowerror 2024-08-01 20:50:08 +02:00
parent 9295d632a9
commit b4cedafa27
4 changed files with 63 additions and 13 deletions

View file

@ -2,17 +2,57 @@
require_once __DIR__ . "/../core.php";
require_once __DIR__ . "/../lib/pairing.php";
require_once __DIR__ . "/../lib/rating.php";
function renderChoice(): void {
[$left, $right] = [$_SESSION["left"], $_SESSION["right"]];
$title = "Test";
$content = function() use ($left, $right) {
include __DIR__ . "/../view/fragments/mobSelection.php";
};
include __DIR__ . "/../view/layout.php";
}
function reload(): void {
header("LOCATION: /");
http_send_status(303);
}
function newPairing(): array {
return makeInitialPairing(session_id());
}
const LEFT = 0;
const RIGHT = 1;
function voteAndNextPairing(int $winner): array {
addMatch($_SESSION["left"]["id"], $_SESSION["right"]["id"], $winner, session_id());
$winnerMob = ($winner == 0) ? $_SESSION["left"] : $_SESSION["right"];
[$left, $right] = makeFollowUpPairing(session_id(), $winnerMob["id"]);
if (($winner == LEFT && $left["id"] != $winnerMob["id"]) ||
($winner == RIGHT && $right["id"] != $winnerMob["id"])
) {
[$left, $right] = [$right, $left];
}
return [$left, $right];
}
session_start();
$pairing = makeInitialPairing(session_id());
$left = $pairing[0];
$right = $pairing[1];
$title = "Test";
$content = function() use ($left, $right) {
include __DIR__ . "/../view/fragments/mobSelection.php";
[$_SESSION["left"], $_SESSION["right"], $render] = match (true) {
isset($_GET["new"]), !isset($_SESSION["left"]) => [...newPairing(), false],
isset($_GET["left"]) => [...voteAndNextPairing(LEFT), false],
isset($_GET["right"]) => [...voteAndNextPairing(RIGHT), false],
default => [$_SESSION["left"], $_SESSION["right"], true],
};
include __DIR__ . "/../view/layout.php";
if ($render) {
renderChoice();
} else {
reload();
}

View file

@ -8,4 +8,10 @@ function getEloForMob(int $mob): int {
$query->execute([$mob]);
$result = $query->fetch(PDO::FETCH_ASSOC);
return $result["rating"];
}
function addMatch(int $mob1, int $mob2, int $winner, string $session): void {
global $pdo;
$query = $pdo->prepare("INSERT INTO mm_matches (mob1fk, mob2fk, winner, session) VALUES (?, ?, ?, ?)");
$query->execute([$mob1, $mob2, $winner, $session]);
}

View file

@ -1,4 +1,6 @@
<div class="mob">
<h2><?= $mob["name"]; ?></h2>
<img alt="<?= $mob["name"]; ?>" src="/images/mobs/<?= $mob["image"] ?? "_placeholder.png"; ?>">
</div>
<form action="?<?= $side ?>" method="POST" name="<?= $side ?>" id="form-<?= $side ?>">
<div class="mob" onclick="document.forms['<?= $side ?>'].submit()">
<h2><?= $mob["name"]; ?></h2>
<img alt="<?= $mob["name"]; ?>" src="/images/mobs/<?= $mob["image"] ?? "_placeholder.png"; ?>">
</div>
</form>

View file

@ -4,6 +4,7 @@
<div class="selection">
<?php
$mob = $left ?? [];
$side = "left";
include __DIR__ . "/mob.php";
?>
<div class="separator">
@ -13,6 +14,7 @@
</div>
<?php
$mob = $right ?? [];
$side = "right";
include __DIR__ . "/mob.php";
?>
</div>