]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/ReevaluateChatCommand.php
respond to whispers
[alttp.git] / app / Console / Commands / ReevaluateChatCommand.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\ChatLog;
6 use Illuminate\Console\Command;
7
8 class ReevaluateChatCommand extends Command {
9
10         /**
11          * The name and signature of the console command.
12          *
13          * @var string
14          */
15         protected $signature = 'chat:reevaluate';
16
17         /**
18          * The console command description.
19          *
20          * @var string
21          */
22         protected $description = 'Reevaluate chat log entries';
23
24         /**
25          * Execute the console command.
26          *
27          * @return int
28          */
29         public function handle() {
30                 $good = 0;
31                 $bad = 0;
32                 ChatLog::whereIn('type', ['chat', 'error'])
33                         ->where('banned', false)
34                         ->orderBy('created_at')
35                         ->chunk(5000, function ($logs) use (&$good, &$bad) {
36                                 foreach ($logs as $line) {
37                                         try {
38                                                 $line->evaluate();
39                                                 if ($line->isDirty()) {
40                                                         $line->evaluated_at = now();
41                                                         $line->save();
42                                                 }
43                                                 ++$good;
44                                         } catch (\Exception $e) {
45                                                 ++$bad;
46                                                 $this->error('unable to evaluate line '.$line->id.': '.$e->getMessage());
47                                                 $line->type = 'error';
48                                                 $line->text_content = $e->getMessage();
49                                                 $line->evaluated_at = now();
50                                                 $line->save();
51                                         }
52                                 }
53                                 echo $good;
54                                 if ($bad) {
55                                         echo ' +', $bad, ' errors';
56                                 }
57                                 echo PHP_EOL;
58                         });
59
60                 return Command::SUCCESS;
61         }
62
63 }