From 0b25d0e8a22a13f09efbeab94d0b353ae603bc16 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 29 Feb 2024 17:01:37 +0100 Subject: [PATCH] guessing game leaderboard --- app/Models/Channel.php | 8 +++++++ app/TwitchBot/ChatCommand.php | 3 +++ app/TwitchBot/GuessingLeaderboardCommand.php | 21 +++++++++++++++++++ .../js/components/twitch-bot/CommandForm.js | 1 + resources/js/i18n/de.js | 1 + resources/js/i18n/en.js | 1 + 6 files changed, 35 insertions(+) create mode 100644 app/TwitchBot/GuessingLeaderboardCommand.php diff --git a/app/Models/Channel.php b/app/Models/Channel.php index 738312d..edc2ba8 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -16,6 +16,10 @@ class Channel extends Model ->first(); } + public function getGuessingLeaderboard() { + return $this->winners()->selectRaw('(select t2.uname from guessing_winners t2 where t2.uid = guessing_winners.uid order by created_at desc limit 1) as name, sum(score) as score')->groupBy('uid')->orderBy('score', 'desc')->limit(10)->get(); + } + public function hasActiveGuessing() { return !is_null($this->guessing_start); } @@ -149,6 +153,10 @@ class Channel extends Model return $this->belongsTo(Organization::class); } + public function winners() { + return $this->hasMany(GuessingWinner::class); + } + protected $casts = [ 'chat' => 'boolean', 'chat_commands' => 'array', diff --git a/app/TwitchBot/ChatCommand.php b/app/TwitchBot/ChatCommand.php index 8e6fbe1..b00f0cf 100644 --- a/app/TwitchBot/ChatCommand.php +++ b/app/TwitchBot/ChatCommand.php @@ -16,6 +16,9 @@ abstract class ChatCommand { case 'guessing-cancel': $cmd = new GuessingCancelCommand(); break; + case 'guessing-leaderboard': + $cmd = new GuessingLeaderboardCommand(); + break; case 'guessing-solve': $cmd = new GuessingSolveCommand(); break; diff --git a/app/TwitchBot/GuessingLeaderboardCommand.php b/app/TwitchBot/GuessingLeaderboardCommand.php new file mode 100644 index 0000000..2b12109 --- /dev/null +++ b/app/TwitchBot/GuessingLeaderboardCommand.php @@ -0,0 +1,21 @@ +channel->getGuessingLeaderboard(); + $msg = ''; + foreach ($leaderboard as $entry) { + if (!empty($msg)) { + $msg .= ', '; + } + $msg .= $entry->name.' ('.$entry->score.')'; + } + $this->messageChannel($msg); + } + +} + +?> diff --git a/resources/js/components/twitch-bot/CommandForm.js b/resources/js/components/twitch-bot/CommandForm.js index 9e9586a..d922170 100644 --- a/resources/js/components/twitch-bot/CommandForm.js +++ b/resources/js/components/twitch-bot/CommandForm.js @@ -27,6 +27,7 @@ const CommandForm = ({ 'guessing-stop', 'guessing-solve', 'guessing-cancel', + 'guessing-leaderboard', ]; const RESTRICTIONS = [ 'none', diff --git a/resources/js/i18n/de.js b/resources/js/i18n/de.js index b949695..9b7a0f3 100644 --- a/resources/js/i18n/de.js +++ b/resources/js/i18n/de.js @@ -516,6 +516,7 @@ export default { commandTypes: { crew: 'Crew Liste', 'guessing-cancel': 'Guessing Game abbrechen', + 'guessing-leaderboard': 'Guessing Game Leaderboard', 'guessing-solve': 'Guessing Game auflösen', 'guessing-start': 'Guessing Game starten', 'guessing-stop': 'Guessing Game stoppen', diff --git a/resources/js/i18n/en.js b/resources/js/i18n/en.js index 695530d..2bcbe91 100644 --- a/resources/js/i18n/en.js +++ b/resources/js/i18n/en.js @@ -516,6 +516,7 @@ export default { commandTypes: { crew: 'Crew list', 'guessing-cancel': 'Cancel guessing game', + 'guessing-leaderboard': 'Guessing game leaderboard', 'guessing-solve': 'Solve guessing game', 'guessing-start': 'Start guessing game', 'guessing-stop': 'Stop guessing game', -- 2.39.2