From 08e8563f94e8b46c08df4c231c838d32365d8f08 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 22 Apr 2025 17:19:39 +0500 Subject: feat: show emote rating --- public/emotes/index.php | 59 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'public/emotes/index.php') diff --git a/public/emotes/index.php b/public/emotes/index.php index de0e6ef..475e119 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -57,7 +57,8 @@ function display_list_emotes(PDO &$db, int $page, int $limit): array $row["ext"], intval(strtotime($row["created_at"])), $row["uploaded_by"], - $row["is_in_user_set"] + $row["is_in_user_set"], + $row["rating"] )); } @@ -66,8 +67,12 @@ function display_list_emotes(PDO &$db, int $page, int $limit): array function display_emote(PDO &$db, int $id) { - $stmt = $db->prepare("SELECT * FROM emotes WHERE id = ?"); - $stmt->execute([$id]); + $stmt = $db->prepare("SELECT e.*, COALESCE(COUNT(r.rate), 0) as total_rating, + COALESCE(ROUND(AVG(r.rate), 2), 0) AS average_rating + FROM emotes e + LEFT JOIN ratings AS r ON r.emote_id = ? + WHERE e.id = ?"); + $stmt->execute([$id, $id]); $emote = null; @@ -78,7 +83,8 @@ function display_emote(PDO &$db, int $id) $row["ext"], intval(strtotime($row["created_at"])), $row["uploaded_by"], - false + false, + ["total" => $row["total_rating"], "average" => $row["average_rating"]] ); } @@ -222,8 +228,6 @@ if ($id == "" || !is_numeric($id)) { $username = $row["username"]; $link = "/users.php?id=" . $emote->get_uploaded_by(); } - - $db = null; } echo ""; @@ -237,7 +241,48 @@ if ($id == "" || !is_numeric($id)) { Rating - Not rated + get_rating()["total"] < 10) { + echo 'Not rated (' . $emote->get_rating()["total"] . ')'; + } else { + + $rating = $emote->get_rating()["average"]; + + // TODO: make it customizable + list($rating_classname, $rating_name) = match (true) { + in_range($rating, 0.75, 1.0) => [ + "gemerald", + " + + Shiny Gemerald! + + + + " + ], + in_range($rating, 0.25, 0.75) => ["gem", " Gem "], + in_range($rating, -0.25, 0.25) => ["iron", "Iron"], + in_range($rating, -0.75, -0.25) => ["coal", " Coal "], + in_range($rating, -1.0, -0.75) => [ + "brimstone", + " + + + + !!!AVOID THIS CANCER-GIVING BRIMSTONE!!! + + + + " + ] + }; + + echo ''; + echo "$rating_name"; + echo ' (' . $emote->get_rating()["total"] . ')'; + echo ''; + } + ?> -- cgit v1.2.3