From 7186ed25e96f459449363d8af53759d4ab985546 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Tue, 30 Jul 2024 23:14:08 +0200 Subject: [PATCH] feat: Basics for more advanced pairing --- html/index.php | 9 ++--- lib/pairing.php | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/rating.php | 7 ---- 3 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 lib/pairing.php diff --git a/html/index.php b/html/index.php index 6c5033d..2fe4998 100644 --- a/html/index.php +++ b/html/index.php @@ -1,15 +1,14 @@ query("SELECT * FROM mm_mobs WHERE enabled = true"); + $result = $result->fetchAll(PDO::FETCH_ASSOC); + return $result[array_rand($result)]; +} + +function findPair(string $session, int $current): array|false { + global $pdo; + + error_log($current); + + // language=sql + $query = $pdo->prepare(<<execute([$session, $current]); + $result = $query->fetch(PDO::FETCH_ASSOC); + + if ($result === false) { + return false; + } else { + return [ + [ + "id" => $result["mob"], + "name" => $result["mob_name"], + "image" => $result["mob_image"], + "rating" => $result["mob_rating"], + ], + [ + "id" => $result["opponent"], + "name" => $result["opponent_name"], + "image" => $result["opponent_image"], + "rating" => $result["opponent_rating"], + ] + ]; + } +} + +function makeInitialPairing(string $session): array { + $current = getRandomMob()["id"]; + return findPair($session, $current); +} + +function makeFollowUpPairing(string $session, int $winner): array { + return findPair($session, $winner); +} \ No newline at end of file diff --git a/lib/rating.php b/lib/rating.php index d92fdf8..5eb50dd 100644 --- a/lib/rating.php +++ b/lib/rating.php @@ -2,13 +2,6 @@ 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 = ?");