]> git.localhorst.tv Git - alttp.git/commitdiff
improved responses
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Apr 2024 11:23:22 +0000 (13:23 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Apr 2024 11:23:22 +0000 (13:23 +0200)
app/TwitchBot/TokenizedMessage.php
app/TwitchBot/TwitchChatBot.php

index e35299803c4753f369c5942c3d3c914b05f5208e..668634af6ef45f887d3360997d7fb85e1e1ee0c4 100644 (file)
@@ -189,6 +189,18 @@ class TokenizedMessage {
                return false;
        }
 
+       public function isLong() {
+               return strlen($this->emoteless_raw) > 20;
+       }
+
+       public function isShort() {
+               return strlen($this->emoteless_raw) < 15;
+       }
+
+       public function isVeryLong() {
+               return strlen($this->emoteless_raw) > 40;
+       }
+
        public function startsOrEndsWith($text) {
                return $this->startsWith($text) || $this->endsWith($text);
        }
@@ -236,7 +248,7 @@ class TokenizedMessage {
                if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
                        return true;
                }
-               if ($this->containsRaw('horstie')) {
+               if ($this->containsRaw('horsti')) {
                        return true;
                }
                if ($this->containsRaw(['folgtjetzt', 'vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
@@ -248,21 +260,21 @@ class TokenizedMessage {
 
        public function classify() {
                if (is_null($this->classification)) {
-                       if (empty($this->text)) {
+                       if (empty($this->text) || $this->isVeryLong()) {
                                $this->classification = 'unclassified';
                        } else if ($this->startsWith('!')) {
                                $this->classification = 'cmd';
-                       } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
+                       } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
                                $this->classification = 'gg';
-                       } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
+                       } else if ($this->isShort() && $this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück'])) {
                                $this->classification = 'gl';
                        } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
                                $this->classification = 'thx';
-                       } else if ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'vohiyo', 'wave']) || $this->hasToken(['hi', 'hey', 'yo']) || $this->containsRaw(['gutenmorgen', 'gutenabend'])) {
+                       } else if (!$this->isLong() && ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'vohiyo', 'wave']) || $this->hasToken(['hi', 'hey', 'yo']) || $this->containsRaw(['gutenmorgen', 'gutenabend']))) {
                                $this->classification = 'hi';
-                       } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
+                       } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
                                $this->classification = 'pog';
-                       } else if ($this->containsRaw(['hype']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
+                       } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
                                $this->classification = 'hype';
                        } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
                                $this->classification = 'love';
@@ -286,7 +298,7 @@ class TokenizedMessage {
                                $this->classification = 'kappa';
                        } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
                                $this->classification = 'o7';
-                       } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd'])) {
+                       } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
                                $this->classification = 'lol';
                        } else if (is_numeric($this->raw)) {
                                $this->classification = 'number';
@@ -297,6 +309,36 @@ class TokenizedMessage {
                return $this->classification;
        }
 
+       public function getResponseCategory() {
+               switch ($this->classify()) {
+                       case 'gg':
+                               return ['love', 'eyes', 'thx', 'pog', 'kappa'];
+                       case 'gl':
+                               return ['love', 'eyes', 'thx'];
+                       case 'hi':
+                               return ['hi', 'love', 'eyes', 'hype', 'pog'];
+                       case 'kappa':
+                               return ['kappa', 'lol', 'eyes'];
+                       case 'love':
+                               return ['hi', 'love', 'eyes', 'thx'];
+                       case 'question':
+                               if (
+                                       $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
+                                       $this->hasConsecutiveTokens(['how', 'many']) ||
+                                       $this->hasConsecutiveTokens(['how', 'much']) ||
+                                       $this->hasConsecutiveTokens(['wie', 'viele'])
+                               ) {
+                                       return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
+                               }
+                               return ['yes', 'no', 'kappa', 'lol', 'wtf'];
+                       case 'rage':
+                               return ['kappa', 'lol', 'rage'];
+                       case 'wtf':
+                               return ['kappa', 'lol', 'rage'];
+               }
+               return false;
+       }
+
 
        private $text;
        private $tags;
index a403b0b57b716ae488c1056e8f3e6d8be53573f8..442173d8c60c82228d66ffdd52e2b1ff5744ef07 100644 (file)
@@ -139,13 +139,12 @@ class TwitchChatBot extends TwitchBot {
                                $this->tagChannelSpecialSent($channel, $classification);
                        }
                        $this->clearQueuedSpecial($channel);
-                       if ($classification == 'number') {
-                               return $this->randomContextualNumber($channel);
-                       }
-                       if ($classification == 'lol') {
-                               return $this->randomLaughter($channel);
-                       }
-                       return $channel->randomOfClass($classification);
+                       return $this->getRandomOfClass($channel, $classification);
+               }
+               $latest_msg = $this->getLatestMessage($channel);
+               if ($latest_msg->classify() == 'question') {
+                       $response = $latest_msg->getResponseCategory();
+                       return $this->getRandomOfClass($channel, $response);
                }
                $last = $this->getLastSpecialSent($channel);
                $classifications = $this->collectClassifications($channel);
@@ -178,13 +177,8 @@ class TwitchChatBot extends TwitchBot {
                        if (!isset($count_quotas[$classification]) || $count < $count_quotas[$classification]) continue;
                        if (!isset($time_quotas[$classification]) || $this->getTimeSinceSpecial($channel, $classification) < $time_quotas[$classification]) continue;
                        $this->tagChannelSpecialSent($channel, $classification);
-                       if ($classification == 'number') {
-                               return $this->randomContextualNumber($channel);
-                       }
-                       if ($classification == 'lol') {
-                               return $this->randomLaughter($channel);
-                       }
-                       return $channel->randomOfClass($classification);
+                       $reaction = $this->getChimeInReaction($channel, $classification);
+                       return $this->getRandomOfClass($channel, $reaction);
                }
                return false;
        }
@@ -294,7 +288,7 @@ class TwitchChatBot extends TwitchBot {
                if ($this->isDirectedAtMe($msg->getText()) && $this->shouldRespond($channel)) {
                        $this->notes[$channel->id]['wait_msgs'] = 0;
                        $this->notes[$channel->id]['wait_time'] = 0;
-                       $response = $this->getResponseTo($tokenized);
+                       $response = $tokenized->getResponseCategory();
                        if ($response) {
                                $this->queueSpecial($channel, $response);
                        }
@@ -314,6 +308,14 @@ class TwitchChatBot extends TwitchBot {
                $this->notes[$channel->id]['last_special'][$classification] = time();
        }
 
+       private function getLatestMessage(Channel $channel) {
+               $this->getNotes($channel);
+               if (!empty($notes['latest_msgs'])) {
+                       return $notes['latest_msgs'][count($notes['latest_msgs']) - 1];
+               }
+               return TokenizedMessage::fromString('');
+       }
+
        private function getLastSpecialSent(Channel $channel) {
                $notes = $this->getNotes($channel);
                $max_time = 0;
@@ -354,26 +356,30 @@ class TwitchChatBot extends TwitchBot {
                return false;
        }
 
-       private function getResponseTo(TokenizedMessage $msg) {
-               switch ($msg->classify()) {
-                       case 'gg':
-                               return ['love', 'eyes', 'thx', 'pog', 'kappa'];
-                       case 'gl':
-                               return ['love', 'eyes', 'thx'];
+       private function getRandomOfClass(Channel $channel, $classification) {
+               if ($classification == 'number') {
+                       return $this->randomContextualNumber($channel);
+               }
+               if ($classification == 'lol') {
+                       return $this->randomLaughter($channel);
+               }
+               return $channel->randomOfClass($classification);
+       }
+
+       private function getChimeInReaction(Channel $channel, $classification) {
+               switch ($classification) {
                        case 'hi':
-                               return ['hi', 'love', 'eyes', 'hype', 'pog'];
-                       case 'kappa':
-                               return ['kappa', 'lol', 'eyes'];
-                       case 'love':
-                               return ['hi', 'love', 'eyes', 'thx'];
-                       case 'question':
-                               return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
-                       case 'rage':
-                               return ['kappa', 'lol', 'rage'];
+                               return ['hi', 'love'];
+                       case 'hype':
+                               return ['hype', 'love', 'pog'];
+                       case 'lol':
+                               return ['kappa', 'lol'];
+                       case 'pog':
+                               return ['hype', 'pog'];
                        case 'wtf':
-                               return ['kappa', 'lol', 'rage'];
+                               return ['lol', 'wtf'];
                }
-               return false;
+               return $classification;
        }
 
        private $channels;