diff --git a/html/index.php b/html/index.php index 2fe4998..18b1d88 100644 --- a/html/index.php +++ b/html/index.php @@ -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"; \ No newline at end of file +if ($render) { + renderChoice(); +} else { + reload(); +} diff --git a/lib/rating.php b/lib/rating.php index 5eb50dd..2e1db21 100644 --- a/lib/rating.php +++ b/lib/rating.php @@ -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]); } \ No newline at end of file diff --git a/view/fragments/mob.php b/view/fragments/mob.php index 67ecafd..f5ea0fc 100644 --- a/view/fragments/mob.php +++ b/view/fragments/mob.php @@ -1,4 +1,6 @@ -
-

- <?= $mob[" src="/images/mobs/"> -
\ No newline at end of file +
+
+

+ <?= $mob[" src="/images/mobs/"> +
+
\ No newline at end of file diff --git a/view/fragments/mobSelection.php b/view/fragments/mobSelection.php index 336c2c4..a65003b 100644 --- a/view/fragments/mobSelection.php +++ b/view/fragments/mobSelection.php @@ -4,6 +4,7 @@
@@ -13,6 +14,7 @@