mobmash.click/html/index.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2024-07-27 22:03:17 +00:00
<?php
require_once __DIR__ . "/../core.php";
2024-07-30 21:14:08 +00:00
require_once __DIR__ . "/../lib/pairing.php";
2024-08-01 18:50:08 +00:00
require_once __DIR__ . "/../lib/rating.php";
2024-08-04 10:46:27 +00:00
ensureSession();
2024-07-27 22:03:17 +00:00
2024-08-01 18:50:08 +00:00
function renderChoice(): void {
[$left, $right] = [$_SESSION["left"], $_SESSION["right"]];
2024-08-01 19:36:58 +00:00
$csrfToken = $_SESSION["csrfToken"];
2024-08-01 18:50:08 +00:00
2024-08-02 19:45:51 +00:00
$ajax = isset($_GET["ajax"]);
if ($ajax) {
include __DIR__ . "/../view/fragments/mobSelection.php";
} else {
2024-08-03 17:41:12 +00:00
$title = "MobMash";
$content = function() use ($left, $right, $csrfToken) {
2024-08-02 19:45:51 +00:00
include __DIR__ . "/../view/pages/mobSelection.php";
};
include __DIR__ . "/../view/layout.php";
}
2024-08-01 18:50:08 +00:00
}
function reload(): void {
if (isset($_GET["ajax"])) {
header("LOCATION: ?ajax");
} else {
header("LOCATION: /");
}
2024-08-01 18:50:08 +00:00
http_send_status(303);
}
function newPairing(): array {
2024-08-04 12:08:50 +00:00
if ($_REQUEST["csrfToken"] != $_SESSION["csrfToken"]) {
return [$_SESSION["left"], $_SESSION["right"]];
}
2024-08-01 18:50:08 +00:00
return makeInitialPairing(session_id());
}
2024-07-27 22:03:17 +00:00
const LEFT = 1;
const RIGHT = 2;
2024-07-27 22:03:17 +00:00
2024-08-01 18:50:08 +00:00
function voteAndNextPairing(int $winner): array {
2024-08-01 19:36:58 +00:00
if ($_POST["csrfToken"] != $_SESSION["csrfToken"]) {
return [$_SESSION["left"], $_SESSION["right"]];
}
2024-08-01 18:50:08 +00:00
addMatch($_SESSION["left"]["id"], $_SESSION["right"]["id"], $winner, session_id());
$winnerMob = ($winner == LEFT) ? $_SESSION["left"] : $_SESSION["right"];
2024-08-01 18:50:08 +00:00
[$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["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],
2024-07-27 22:03:17 +00:00
};
2024-08-01 18:50:08 +00:00
if ($render) {
renderChoice();
} else {
2024-08-01 19:36:58 +00:00
$_SESSION["csrfToken"] = makeCcrfToken();
2024-08-01 18:50:08 +00:00
reload();
}