]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchChatBot.php
improved responses
[alttp.git] / app / TwitchBot / TwitchChatBot.php
index 420e93704490fe804e037697fb070883547b3846..442173d8c60c82228d66ffdd52e2b1ff5744ef07 100644 (file)
@@ -94,6 +94,7 @@ class TwitchChatBot extends TwitchBot {
                                'last_special' => [],
                                'last_write' => time(),
                                'latest_msgs' => [],
+                               'queued_special' => false,
                                'read_since_last_write' => 0,
                                'wait_msgs' => $this->randomWaitMsgs($channel),
                                'wait_time' => $this->randomWaitTime($channel),
@@ -132,7 +133,20 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function contextualMsg(Channel $channel) {
-               $last = $this->getNote($channel, 'last_special');
+               if ($this->hasQueuedSpecial($channel)) {
+                       $classification = $this->getQueuedSpecial($channel);
+                       if (is_string($classification)) {
+                               $this->tagChannelSpecialSent($channel, $classification);
+                       }
+                       $this->clearQueuedSpecial($channel);
+                       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);
                $count_quotas = [
                        'gg' => 2,
@@ -140,9 +154,11 @@ class TwitchChatBot extends TwitchBot {
                        'hi' => 2,
                        'hype' => 2,
                        'lol' => 2,
+                       'love' => 2,
                        'number' => 2,
                        'pog' => 2,
                        'o7' => 2,
+                       'wtf' => 2,
                ];
                $time_quotas = [
                        'gg' => 600,
@@ -150,28 +166,26 @@ class TwitchChatBot extends TwitchBot {
                        'hi' => 60,
                        'hype' => 60,
                        'lol' => 60,
+                       'love' => 60,
                        'number' => 300,
                        'pog' => 60,
                        'o7' => 300,
+                       'wtf' => 60,
                ];
                foreach ($classifications as $classification => $count) {
+                       if ($classification == $last) continue;
                        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;
        }
 
        private function randomChat(Channel $channel) {
                return $channel->queryChatlog()
-                       ->whereIn('classification', ['hi', 'hype', 'lol', 'pog', 'unclassified'])
+                       ->whereNotIn('classification', ['gg', 'gl', 'number', 'o7'])
                        ->first();
        }
 
@@ -190,6 +204,9 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function randomLaughter(Channel $channel) {
+               if (!random_int(0, 2)) {
+                       return $channel->randomOfClass('lol');
+               }
                return Arr::random([
                        ':tf:',
                        '4Head',
@@ -200,7 +217,6 @@ class TwitchChatBot extends TwitchBot {
                        'GunRun',
                        'heh',
                        'Hhhehehe',
-                       'HypeLUL',
                        'Jebaited',
                        'Jebasted',
                        'KEKW',
@@ -219,7 +235,6 @@ class TwitchChatBot extends TwitchBot {
                        'SUBprise',
                        'xD',
                        'YouDontSay',
-                       $channel->randomOfClass('lol'),
                ]);
        }
 
@@ -239,6 +254,25 @@ class TwitchChatBot extends TwitchBot {
                return random_int($min, $max);
        }
 
+       private function queueSpecial(Channel $channel, $classification) {
+               $this->getNotes($channel);
+               $this->notes[$channel->id]['queued_special'] = $classification;
+       }
+
+       private function hasQueuedSpecial(Channel $channel) {
+               return !!$this->getQueuedSpecial($channel);
+       }
+
+       private function getQueuedSpecial(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               return $notes['queued_special'];
+       }
+
+       private function clearQueuedSpecial(Channel $channel) {
+               $this->getNotes($channel);
+               $this->notes[$channel->id]['queued_special'] = false;
+       }
+
        private function tagChannelRead(Channel $channel, IRCMessage $msg) {
                $this->getNotes($channel);
                $this->notes[$channel->id]['last_read'] = time();
@@ -254,6 +288,10 @@ 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 = $tokenized->getResponseCategory();
+                       if ($response) {
+                               $this->queueSpecial($channel, $response);
+                       }
                }
        }
 
@@ -270,6 +308,27 @@ 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;
+               $max_classification = '';
+               foreach ($notes['last_special'] as $classification => $time) {
+                       if ($time > $max_time) {
+                               $max_time = $time;
+                               $max_classification = $classification;
+                       }
+               }
+               return $max_classification;
+       }
+
        private function getTimeSinceSpecial(Channel $channel, $classification) {
                $notes = $this->getNotes($channel);
                if (isset($notes['last_special'][$classification])) {
@@ -297,6 +356,32 @@ class TwitchChatBot extends TwitchBot {
                return false;
        }
 
+       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'];
+                       case 'hype':
+                               return ['hype', 'love', 'pog'];
+                       case 'lol':
+                               return ['kappa', 'lol'];
+                       case 'pog':
+                               return ['hype', 'pog'];
+                       case 'wtf':
+                               return ['lol', 'wtf'];
+               }
+               return $classification;
+       }
+
        private $channels;
        private $notes = [];