mirror of
https://github.com/sigmasternchen/mobmash.click
synced 2025-03-15 08:09:02 +00:00
feat: Basics for more advanced pairing
This commit is contained in:
parent
7b025f9ca3
commit
7186ed25e9
3 changed files with 107 additions and 12 deletions
|
@ -1,15 +1,14 @@
|
|||
<?php
|
||||
|
||||
require_once __DIR__ . "/../core.php";
|
||||
require_once __DIR__ . "/../lib/rating.php";
|
||||
require_once __DIR__ . "/../lib/pairing.php";
|
||||
|
||||
session_start();
|
||||
|
||||
$left = getRandomMob();
|
||||
$right = getRandomMob();
|
||||
$pairing = makeInitialPairing(session_id());
|
||||
|
||||
$left["rating"] = getEloForMob($left["id"]);
|
||||
$right["rating"] = getEloForMob($right["id"]);
|
||||
$left = $pairing[0];
|
||||
$right = $pairing[1];
|
||||
|
||||
$title = "Test";
|
||||
$content = function() use ($left, $right) {
|
||||
|
|
103
lib/pairing.php
Normal file
103
lib/pairing.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?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 findPair(string $session, int $current): array|false {
|
||||
global $pdo;
|
||||
|
||||
error_log($current);
|
||||
|
||||
// language=sql
|
||||
$query = $pdo->prepare(<<<EOF
|
||||
WITH possible_pairings AS (
|
||||
SELECT
|
||||
pairings.mob,
|
||||
mob_rating.rating AS mob_rating,
|
||||
pairings.opponent,
|
||||
opponent_rating.rating AS opponent_rating
|
||||
FROM (
|
||||
(
|
||||
SELECT
|
||||
mob.id AS mob,
|
||||
opponent.id AS opponent
|
||||
FROM mm_mobs mob
|
||||
CROSS JOIN mm_mobs opponent
|
||||
WHERE
|
||||
mob.id != opponent.id
|
||||
AND mob.enabled
|
||||
AND opponent.enabled
|
||||
)
|
||||
EXCEPT
|
||||
SELECT
|
||||
mob,
|
||||
opponent
|
||||
FROM mm_matches_of_mob
|
||||
WHERE session = ?
|
||||
) AS pairings
|
||||
INNER JOIN mm_current_rating mob_rating
|
||||
ON mob_rating.mob = pairings.mob
|
||||
INNER JOIN mm_current_rating opponent_rating
|
||||
ON opponent_rating.mob = pairings.opponent
|
||||
)
|
||||
SELECT
|
||||
mob.id AS mob,
|
||||
mob.name AS mob_name,
|
||||
mob.image AS mob_image,
|
||||
pairings_with_difference.mob_rating AS mob_rating,
|
||||
opponent.id AS opponent,
|
||||
opponent.name AS opponent_name,
|
||||
opponent.image AS opponent_image,
|
||||
pairings_with_difference.opponent_rating AS opponent_rating,
|
||||
pairings_with_difference.difference AS rating_difference
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
*,
|
||||
abs(mob_rating - opponent_rating) AS difference
|
||||
FROM possible_pairings
|
||||
) AS pairings_with_difference
|
||||
INNER JOIN mm_mobs AS mob
|
||||
ON pairings_with_difference.mob = mob.id
|
||||
INNER JOIN mm_mobs AS opponent
|
||||
ON pairings_with_difference.opponent = opponent.id
|
||||
ORDER BY
|
||||
(pairings_with_difference.mob = ?) DESC,
|
||||
pairings_with_difference.difference ASC
|
||||
LIMIT 1;
|
||||
EOF);
|
||||
$query->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);
|
||||
}
|
|
@ -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 = ?");
|
||||
|
|
Loading…
Reference in a new issue