]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
add up to 4 player support for zsr sync
[alttp.git] / app / TwitchBot / TokenizedMessage.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\ChatLog;
6 use Illuminate\Support\Arr;
7 use Illuminate\Support\Str;
8
9 class TokenizedMessage {
10
11         public function __construct($text, $tags = []) {
12                 $this->text = trim($text);
13                 $this->tags = $tags;
14                 $this->raw = strtolower(preg_replace('/[^\w]/u', '', $this->text));
15                 $this->tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->text)))));
16
17                 $this->emoteless = $this->text;
18                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
19                         $emotes = explode('/', $this->tags['emotes']);
20                         foreach ($emotes as $emote) {
21                                 $set = explode(':', $emote);
22                                 $positions = explode(',', $set[1]);
23                                 foreach ($positions as $position) {
24                                         $coords = explode('-', $position);
25                                         $emote_text = mb_substr($this->text, $coords[0], $coords[1] - $coords[0] + 1);
26                                         $this->original_emotes[] = $emote_text;
27                                         $this->emotes[] = preg_replace('/\d+$/', '', strtolower($emote_text));
28                                         $this->emoteless = mb_substr($this->emoteless, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($this->emoteless, $coords[1] + 1);
29                                 }
30                         }
31                         $this->emoteless = trim(preg_replace('/\s+/u', ' ', $this->emoteless));
32                 }
33                 $this->emoteless_raw = strtolower(preg_replace('/[^\w]/u', '', $this->emoteless));
34                 $this->emoteless_tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->emoteless)))));
35         }
36
37         public static function fromIRC(IRCMessage $msg) {
38                 return new self($msg->getText(), $msg->tags);
39         }
40
41         public static function fromLog(ChatLog $log) {
42                 return new self($log->params[1], $log->tags);
43         }
44
45         public static function fromString($text, $tags = []) {
46                 return new self($text, $tags);
47         }
48
49
50         public function contains($text) {
51                 return Str::contains($this->text, $text);
52         }
53
54         public function containsEmoteless($text) {
55                 return Str::contains($this->emoteless, $text);
56         }
57
58         public function containsRaw($text) {
59                 return Str::contains($this->raw, $text);
60         }
61
62         public function endsWith($text) {
63                 return Str::endsWith($this->text, $text);
64         }
65
66         public function endsWithEmoteless($text) {
67                 return Str::endsWith($this->emoteless, $text);
68         }
69
70         public function endsWithEmotelessToken($text) {
71                 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
72         }
73
74         public function endsWithRaw($text) {
75                 return Str::endsWith($this->raw, $text);
76         }
77
78         public function endsWithToken($text) {
79                 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
80         }
81
82         public function getEmotes() {
83                 return $this->emotes;
84         }
85
86         public function getNumericValue() {
87                 return intval($this->text);
88         }
89
90         public function getOriginalEmotes() {
91                 return $this->original_emotes;
92         }
93
94         public function hasConsecutiveTokens($tokens) {
95                 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
96                         for ($j = 0; $j < count($tokens); ++$j) {
97                                 if ($this->tokens[$i + $j] != $tokens[$j]) break;
98                         }
99                         if ($j == count($tokens)) return true;
100                 }
101                 return false;
102         }
103
104         public function hasEmote($text) {
105                 if (is_array($text)) {
106                         foreach ($text as $token) {
107                                 if (in_array($token, $this->emotes)) {
108                                         return true;
109                                 }
110                         }
111                         return false;
112                 }
113                 return in_array($text, $this->emotes);
114         }
115
116         public function hasEmoteThatContains($text) {
117                 foreach ($this->emotes as $emote) {
118                         if (Str::contains($emote, $text)) {
119                                 return true;
120                         }
121                 }
122                 return false;
123         }
124
125         public function hasEmoteThatEndsWith($text) {
126                 foreach ($this->emotes as $emote) {
127                         if (Str::endsWith($emote, $text)) {
128                                 return true;
129                         }
130                 }
131                 return false;
132         }
133
134         public function hasEmoteThatStartsOrEndsWith($text) {
135                 foreach ($this->emotes as $emote) {
136                         if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
137                                 return true;
138                         }
139                 }
140                 return false;
141         }
142
143         public function hasEmoteThatStartsWith($text) {
144                 foreach ($this->emotes as $emote) {
145                         if (Str::startsWith($emote, $text)) {
146                                 return true;
147                         }
148                 }
149                 return false;
150         }
151
152         public function hasToken($text) {
153                 if (is_array($text)) {
154                         foreach ($text as $token) {
155                                 if (in_array($token, $this->tokens)) {
156                                         return true;
157                                 }
158                         }
159                         return false;
160                 }
161                 return in_array($text, $this->tokens);
162         }
163
164         public function hasTokenThatContains($text) {
165                 foreach ($this->tokens as $token) {
166                         if (Str::contains($token, $text)) {
167                                 return true;
168                         }
169                 }
170                 return false;
171         }
172
173         public function hasTokenThatEndsWith($text) {
174                 foreach ($this->tokens as $token) {
175                         if (Str::endsWith($token, $text)) {
176                                 return true;
177                         }
178                 }
179                 return false;
180         }
181
182         public function hasTokenThatStartsOrEndsWith($text) {
183                 foreach ($this->tokens as $token) {
184                         if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
185                                 return true;
186                         }
187                 }
188                 return false;
189         }
190
191         public function hasTokenThatStartsWith($text) {
192                 foreach ($this->tokens as $token) {
193                         if (Str::startsWith($token, $text)) {
194                                 return true;
195                         }
196                 }
197                 return false;
198         }
199
200         public function isLong() {
201                 return strlen($this->emoteless_raw) > 20;
202         }
203
204         public function isShort() {
205                 return strlen($this->emoteless_raw) < 15;
206         }
207
208         public function isVeryLong() {
209                 return strlen($this->emoteless_raw) > 40;
210         }
211
212         public function startsOrEndsWith($text) {
213                 return $this->startsWith($text) || $this->endsWith($text);
214         }
215
216         public function startsOrEndsWithEmotelessToken($text) {
217                 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
218         }
219
220         public function startsOrEndsWithRaw($text) {
221                 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
222         }
223
224         public function startsOrEndsWithToken($text) {
225                 return $this->startsWithToken($text) || $this->endsWithToken($text);
226         }
227
228         public function startsWith($text) {
229                 return Str::startsWith($this->text, $text);
230         }
231
232         public function startsWithEmoteless($text) {
233                 return Str::startsWith($this->emoteless, $text);
234         }
235
236         public function startsWithEmotelessToken($text) {
237                 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
238         }
239
240         public function startsWithRaw($text) {
241                 return Str::startsWith($this->raw, $text);
242         }
243
244         public function startsWithToken($text) {
245                 return isset($this->tokens[0]) && $this->tokens[0] == $text;
246         }
247
248
249         public function isSpammy() {
250                 if ($this->startsWith('!')) {
251                         return true;
252                 }
253                 if ($this->contains(['€', '$', '@', '://'])) {
254                         return true;
255                 }
256                 if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
257                         return true;
258                 }
259                 if ($this->containsRaw('horsti')) {
260                         return true;
261                 }
262                 if ($this->containsRaw([
263                         'folgtjetzt',
264                         'hatdeinenkanalgeraided',
265                         'isnowlivestreaming',
266                         'stürmtdenladenmit',
267                         'thanksfortheraid',
268                         'verschwindetfürneweileindenlurk',
269                         'vielendankfürdenraid',
270                         'willkommenaufstarbase47',
271                 ])) {
272                         return true;
273                 }
274                 return false;
275         }
276
277
278         public function classify() {
279                 if (is_null($this->classification)) {
280                         if (empty($this->text) || $this->isVeryLong()) {
281                                 $this->classification = 'unclassified';
282                         } else if ($this->startsWith('!')) {
283                                 $this->classification = 'cmd';
284                         } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
285                                 $this->classification = 'gg';
286                         } else if ($this->isShort() && $this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück'])) {
287                                 $this->classification = 'gl';
288                         } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
289                                 $this->classification = 'thx';
290                         } 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']))) {
291                                 $this->classification = 'hi';
292                         } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
293                                 $this->classification = 'pog';
294                         } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
295                                 $this->classification = 'hype';
296                         } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
297                                 $this->classification = 'love';
298                         } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
299                                 $this->classification = 'wtf';
300                         } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
301                                 $this->classification = 'eyes';
302                         } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
303                                 $this->classification = 'rage';
304                         } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
305                                 $this->classification = 'sad';
306                         } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
307                                 $this->classification = 'sweat';
308                         } else if ($this->endsWithEmoteless('?')) {
309                                 $this->classification = 'question';
310                         } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
311                                 $this->classification = 'yes';
312                         } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
313                                 $this->classification = 'no';
314                         } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
315                                 $this->classification = 'kappa';
316                         } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
317                                 $this->classification = 'o7';
318                         } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
319                                 $this->classification = 'lol';
320                         } else if (is_numeric($this->raw)) {
321                                 $this->classification = 'number';
322                         } else {
323                                 $this->classification = 'unclassified';
324                         }
325                 }
326                 return $this->classification;
327         }
328
329         public function getResponseCategory() {
330                 switch ($this->classify()) {
331                         case 'gg':
332                                 return ['love', 'eyes', 'thx', 'pog', 'kappa'];
333                         case 'gl':
334                                 return ['love', 'eyes', 'thx'];
335                         case 'hi':
336                                 return ['hi', 'love', 'eyes', 'hype', 'pog'];
337                         case 'kappa':
338                                 return ['kappa', 'lol', 'eyes'];
339                         case 'love':
340                                 return ['hi', 'love', 'eyes', 'thx'];
341                         case 'question':
342                                 if (
343                                         $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
344                                         $this->hasConsecutiveTokens(['how', 'many']) ||
345                                         $this->hasConsecutiveTokens(['how', 'much']) ||
346                                         $this->hasConsecutiveTokens(['wie', 'viele'])
347                                 ) {
348                                         return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
349                                 }
350                                 return ['yes', 'no', 'kappa', 'lol', 'wtf'];
351                         case 'rage':
352                                 return ['kappa', 'lol', 'rage'];
353                         case 'wtf':
354                                 return ['kappa', 'lol', 'rage'];
355                 }
356                 return false;
357         }
358
359
360         private $text;
361         private $tags;
362         private $raw;
363         private $tokens;
364
365         private $emotes = [];
366         private $original_emotes = [];
367         private $emoteless = '';
368         private $emoteless_raw = '';
369         private $emoteless_tokens = [];
370
371         private $classification = null;
372
373 }