]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchChatBot.php
better quota system for contextual messages
[alttp.git] / app / TwitchBot / TwitchChatBot.php
index 7cdc704bedfcd4e6c25a40298929430405070690..aacc56fc11f2aa0e410787be739eab776136aacf 100644 (file)
@@ -82,7 +82,7 @@ class TwitchChatBot extends TwitchBot {
                if (!isset($this->notes[$channel->id])) {
                        $this->notes[$channel->id] = [
                                'last_read' => 0,
-                               'last_special' => '',
+                               'last_special' => [],
                                'last_write' => time(),
                                'latest_msgs' => [],
                                'read_since_last_write' => 0,
@@ -106,127 +106,57 @@ class TwitchChatBot extends TwitchBot {
                $this->notes[$channel->id][$name] = $value;
        }
 
-       private function checkForGG(Channel $channel) {
+       private function collectClassifications(Channel $channel) {
+               $classifications = [];
                $notes = $this->getNotes($channel);
-               $ggs = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'gg') {
-                               ++$ggs;
+               foreach ($notes['latest_msgs'] as $msg) {
+                       $classification = $msg->classify();
+                       if ($classification == 'unclassified') continue;
+                       if (isset($classifications[$classification])) {
+                               ++$classifications[$classification];
+                       } else {
+                               $classifications[$classification] = 1;
                        }
                }
-               return $ggs > 2;
-       }
-
-       private function checkForGLHF(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $gls = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'gl') {
-                               ++$gls;
-                       }
-               }
-               return $gls > 2;
-       }
-
-       private function checkForGreeting(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $his = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'hi') {
-                               ++$his;
-                       }
-               }
-               return $his > 2;
-       }
-
-       private function checkForHype(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $hypes = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'hype') {
-                               ++$hypes;
-                       }
-               }
-               return $hypes > 2;
-       }
-
-       private function checkForLaughter(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $lulz = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'lol') {
-                               ++$lulz;
-                       }
-               }
-               return $lulz > 2;
-       }
-
-       private function checkForNumbers(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $numbers = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (is_numeric(trim($text))) {
-                               ++$numbers;
-                       }
-               }
-               return $numbers > 2;
-       }
-
-       private function checkForPog(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $pogs = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'pog') {
-                               ++$pogs;
-                       }
-               }
-               return $pogs > 2;
-       }
-
-       private function checkForSalute(Channel $channel) {
-               $notes = $this->getNotes($channel);
-               $o7s = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (ChatLog::classify($text) == 'o7') {
-                               ++$o7s;
-                       }
-               }
-               return $o7s > 2;
+               arsort($classifications);
+               return $classifications;
        }
 
        private function contextualMsg(Channel $channel) {
                $last = $this->getNote($channel, 'last_special');
-               if ($last != 'gg' && $this->checkForGG($channel)) {
-                       $this->setNote($channel, 'last_special', 'gg');
-                       return $channel->randomOfClass('gg');
-               }
-               if ($last != 'number' && $this->checkForNumbers($channel)) {
-                       $this->setNote($channel, 'last_special', 'number');
-                       return $this->randomContextualNumber($channel);
-               }
-               if ($last != 'lol' && $this->checkForLaughter($channel)) {
-                       $this->setNote($channel, 'last_special', 'lol');
-                       return $this->randomLaughter($channel);
-               }
-               if ($last != 'glhf' && $this->checkForGLHF($channel)) {
-                       $this->setNote($channel, 'last_special', 'glhf');
-                       return $channel->randomOfClass('gl');
-               }
-               if ($last != 'hi' && $this->checkForGreeting($channel)) {
-                       $this->setNote($channel, 'last_special', 'hi');
-                       return $channel->randomOfClass('hi');
-               }
-               if ($last != 'hype' && $this->checkForHype($channel)) {
-                       $this->setNote($channel, 'last_special', 'hype');
-                       return $channel->randomOfClass('hype');
-               }
-               if ($last != 'pog' && $this->checkForPog($channel)) {
-                       $this->setNote($channel, 'last_special', 'pog');
-                       return $channel->randomOfClass('pog');
-               }
-               if ($last != 'o7' && $this->checkForSalute($channel)) {
-                       $this->setNote($channel, 'last_special', 'o7');
-                       return $channel->randomOfClass('o7');
+               $classifications = $this->collectClassifications($channel);
+               $count_quotas = [
+                       'gg' => 2,
+                       'gl' => 2,
+                       'hi' => 2,
+                       'hype' => 2,
+                       'lol' => 2,
+                       'number' => 2,
+                       'pog' => 2,
+                       'o7' => 2,
+               ];
+               $time_quotas = [
+                       'gg' => 300,
+                       'gl' => 900,
+                       'hi' => 60,
+                       'hype' => 60,
+                       'lol' => 60,
+                       'number' => 300,
+                       'pog' => 60,
+                       'o7' => 300,
+               ];
+               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);
                }
                return false;
        }
@@ -242,9 +172,9 @@ class TwitchChatBot extends TwitchBot {
                $notes = $this->getNotes($channel);
                $min = 100000;
                $max = 0;
-               foreach ($notes['latest_msgs'] as $text) {
-                       if (is_numeric(trim($text))) {
-                               $number = intval(trim($text));
+               foreach ($notes['latest_msgs'] as $msg) {
+                       if ($msg->classify() == 'number') {
+                               $number = $msg->getNumericValue();
                                $min = min($min, $number);
                                $max = max($max, $number);
                        }
@@ -307,8 +237,10 @@ class TwitchChatBot extends TwitchBot {
                $this->getNotes($channel);
                $this->notes[$channel->id]['last_read'] = time();
                ++$this->notes[$channel->id]['read_since_last_write'];
-               if (!ChatLog::isKnownBot($msg->nick) && !ChatLog::spammyText($msg->getText())) {
-                       $this->notes[$channel->id]['latest_msgs'][] = $msg->getText();
+
+               $tokenized = $msg->tokenize();
+               if (!ChatLog::isKnownBot($msg->nick) && !$tokenized->isSpammy()) {
+                       $this->notes[$channel->id]['latest_msgs'][] = $tokenized;
                        if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
                                array_shift($this->notes[$channel->id]['latest_msgs']);
                        }
@@ -327,9 +259,22 @@ class TwitchChatBot extends TwitchBot {
                $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel);
        }
 
+       private function tagChannelSpecialSent(Channel $channel, $classification) {
+               $this->getNotes($channel);
+               $this->notes[$channel->id]['last_special'][$classification] = time();
+       }
+
+       private function getTimeSinceSpecial(Channel $channel, $classification) {
+               $notes = $this->getNotes($channel);
+               if (isset($notes['last_special'][$classification])) {
+                       return time() - $notes['last_special'][$classification];
+               }
+               return 999999;
+       }
+
        private function isDirectedAtMe($raw_text) {
                $text = strtolower($raw_text);
-               if (strpos($text, 'horstie') !== false) {
+               if (strpos($text, 'horsti') !== false) {
                        return true;
                }
                return false;