diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-22 17:19:39 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-22 17:19:39 +0500 |
| commit | 08e8563f94e8b46c08df4c231c838d32365d8f08 (patch) | |
| tree | e13577b2f2d9fbde64203b714516ffd6d78fc780 | |
| parent | 34189e06a88ce4409a2c9652ca26f072c461233e (diff) | |
feat: show emote rating
| -rw-r--r-- | public/emotes/index.php | 59 | ||||
| -rw-r--r-- | public/static/img/icons/ratings/brimstone.webp | bin | 0 -> 8400 bytes | |||
| -rw-r--r-- | public/static/style.css | 28 | ||||
| -rw-r--r-- | src/emote.php | 9 | ||||
| -rw-r--r-- | src/utils.php | 6 |
5 files changed, 94 insertions, 8 deletions
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 "<a href=\"$link\">"; @@ -237,7 +241,48 @@ if ($id == "" || !is_numeric($id)) { </tr> <tr> <th>Rating</th> - <td>Not rated</td> + <?php + if ($emote->get_rating()["total"] < 10) { + echo '<td>Not rated (' . $emote->get_rating()["total"] . ')</td>'; + } 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", + "<img src='/static/img/icons/ratings/1.png'> + <img src='/static/img/icons/ratings/1.png'> + <img src='/static/img/icons/ratings/1.png'> Shiny Gemerald! + <img src='/static/img/icons/ratings/1.png'> + <img src='/static/img/icons/ratings/1.png'> + <img src='/static/img/icons/ratings/1.png'> + " + ], + in_range($rating, 0.25, 0.75) => ["gem", "<img src='/static/img/icons/ratings/1.png'> Gem <img src='/static/img/icons/ratings/1.png'>"], + in_range($rating, -0.25, 0.25) => ["iron", "Iron"], + in_range($rating, -0.75, -0.25) => ["coal", "<img src='/static/img/icons/ratings/-1.png'> Coal <img src='/static/img/icons/ratings/-1.png'>"], + in_range($rating, -1.0, -0.75) => [ + "brimstone", + " + <img src='/static/img/icons/ratings/brimstone.webp'> + <img src='/static/img/icons/ratings/-1.png'> + <img src='/static/img/icons/ratings/brimstone.webp'> + !!!AVOID THIS CANCER-GIVING BRIMSTONE!!! + <img src='/static/img/icons/ratings/brimstone.webp'> + <img src='/static/img/icons/ratings/-1.png'> + <img src='/static/img/icons/ratings/brimstone.webp'> + " + ] + }; + + echo '<td>'; + echo "<span class=\"rating $rating_classname\">$rating_name</span>"; + echo ' (' . $emote->get_rating()["total"] . ')'; + echo '</td>'; + } + ?> </tr> </table> </section> diff --git a/public/static/img/icons/ratings/brimstone.webp b/public/static/img/icons/ratings/brimstone.webp Binary files differnew file mode 100644 index 0000000..98b0d62 --- /dev/null +++ b/public/static/img/icons/ratings/brimstone.webp diff --git a/public/static/style.css b/public/static/style.css index cb9a996..099764f 100644 --- a/public/static/style.css +++ b/public/static/style.css @@ -358,6 +358,34 @@ a.box:hover { margin-top: -15px; } +/** RATINGS */ +.rating.gemerald { + font-weight: bolder; + text-shadow: 0 0 5px blue; + color: #b4dbeb; +} + +.rating.gemerald img { + filter: hue-rotate(50deg) brightness(1.5); +} + +.rating.gem { + font-weight: bold; + color: #2e7b99; +} + +.rating.coal { + font-weight: bold; + color: #2d3335; + text-shadow: 0 0 2px black; +} + +.rating.brimstone { + font-weight: bolder; + color: orange; + text-shadow: 0 0 4px red; +} + /** ------------- ACCOUNTS diff --git a/src/emote.php b/src/emote.php index 2fd30c0..1fa0221 100644 --- a/src/emote.php +++ b/src/emote.php @@ -6,9 +6,10 @@ class Emote public string $ext; public string|null $uploaded_by; public int $created_at; + public mixed $rating; public bool $is_in_user_set; - function __construct($id, $code, $ext, $created_at, $uploaded_by, $is_in_user_set) + function __construct($id, $code, $ext, $created_at, $uploaded_by, $is_in_user_set, $rating) { $this->id = $id; $this->code = $code; @@ -16,6 +17,7 @@ class Emote $this->created_at = $created_at; $this->uploaded_by = $uploaded_by; $this->is_in_user_set = $is_in_user_set; + $this->rating = $rating; } function get_id() @@ -47,4 +49,9 @@ class Emote { return $this->is_in_user_set; } + + function get_rating() + { + return $this->rating; + } } diff --git a/src/utils.php b/src/utils.php index 325603b..a11fa16 100644 --- a/src/utils.php +++ b/src/utils.php @@ -58,7 +58,13 @@ function format_timestamp(int $timestamp_secs) return "$days day" . ($days > 1 ? "s" : ""); } } + function clamp(int $current, int $min, int $max): int { return max($min, min($max, $current)); +} + +function in_range(float $value, float $min, float $max): bool +{ + return $min <= $value && $value <= $max; }
\ No newline at end of file |
