]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Channel.php
chat adlib prototype
[alttp.git] / app / Models / Channel.php
index 500f2e33e6f9fb3456694d7dc403159dd7205260..f50c267eb8a3cae89bfbba84bada33f682a86862 100644 (file)
@@ -31,6 +31,55 @@ class Channel extends Model {
                        ->first();
        }
 
+       public function randomOfClass($class) {
+               if (is_array($class)) {
+                       return $this->queryChatlog()
+                               ->whereIn('classification', $class)
+                               ->first();
+               }
+               return $this->queryChatlog()
+                       ->where('classification', '=', $class)
+                       ->first();
+       }
+
+       public function queryChatlog() {
+               $min_age = $this->getChatSetting('min_age', 1);
+               $query = ChatLog::where('type', '=', 'chat')
+                       ->where('banned', '=', false)
+                       ->where('created_at', '<', now()->sub($min_age, 'day'))
+                       ->where(function ($query) {
+                               $query->whereNull('detected_language');
+                               $query->orWhereIn('detected_language', $this->getPreferredLanguages());
+                       })
+                       ->inRandomOrder();
+               $source = $this->getChatSetting('source', 'any');
+               if (in_array($source, ['catchan', 'chan'])) {
+                       $query->where('channel_id', $this->id);
+               }
+               if (in_array($source, ['cat', 'catchan'])) {
+                       $query->where('twitch_category', $this->twitch_category);
+               }
+               return $query;
+       }
+
+       public function getPreferredLanguages() {
+               $setting = $this->getChatSetting('language');
+               if ($setting) {
+                       return [$setting];
+               }
+               if (!empty($this->languages)) {
+                       return $this->languages;
+               }
+               return ['de'];
+       }
+
+       public function getChatSetting($name, $default = null) {
+               if (array_key_exists($name, $this->chat_settings)) {
+                       return $this->chat_settings[$name];
+               }
+               return $default;
+       }
+
        public function getGuessingLeaderboard() {
                $query = $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')
@@ -173,7 +222,7 @@ class Channel extends Model {
                $transformed = $this->transformGuess($solution);
                if ($this->guessing_type == 'gtbk') {
                        $int_solution = intval($transformed);
-                       return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
+                       return is_numeric($transformed) && $int_solution >= 0 && $int_solution <= 23;
                }
                return false;
        }
@@ -205,6 +254,10 @@ class Channel extends Model {
                return Arr::join($entries, ', ', ' and ');
        }
 
+       public function chat_bot_logs() {
+               return $this->hasMany(ChatBotLog::class);
+       }
+
        public function crews() {
                return $this->hasMany(ChannelCrew::class);
        }