]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
listen to round updates
[alttp.git] / app / Models / Protocol.php
1 <?php
2
3 namespace App\Models;
4
5 use App\Events\ProtocolAdded;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8
9 class Protocol extends Model
10 {
11         use HasFactory;
12
13         public static function roundAdded(Tournament $tournament, Round $round, User $user) {
14                 $protocol = static::create([
15                         'tournament_id' => $tournament->id,
16                         'user_id' => $user->id,
17                         'type' => 'round.create',
18                         'details' => [
19                                 'tournament' => static::tournamentMemo($tournament),
20                                 'round' => static::roundMemo($round),
21                         ],
22                 ]);
23                 ProtocolAdded::dispatch($protocol);
24         }
25
26         public static function tournamentCreated(Tournament $tournament, User $user) {
27                 $protocol = static::create([
28                         'tournament_id' => $tournament->id,
29                         'user_id' => $user->id,
30                         'type' => 'tournament.create',
31                         'details' => [
32                                 'tournament' => static::tournamentMemo($tournament),
33                         ],
34                 ]);
35                 ProtocolAdded::dispatch($protocol);
36         }
37
38
39         protected static function roundMemo(Round $round) {
40                 return [
41                         'id' => $round->id,
42                         'seed' => $round->seed,
43                 ];
44         }
45
46         protected static function tournamentMemo(Tournament $tournament) {
47                 return [
48                         'id' => $tournament->id,
49                         'title' => $tournament->title,
50                 ];
51         }
52
53         protected static function userMemo(User $user) {
54                 return [
55                         'id' => $user->id,
56                         'username' => $user->username,
57                         'discriminator' => $user->discriminator,
58                         'avatar' => $user->avatar,
59                 ];
60         }
61
62
63         public function tournament() {
64                 return $this->belongsTo(Tournament::class);
65         }
66
67         public function user() {
68                 return $this->belongsTo(User::class);
69         }
70
71
72         protected $casts = [
73                 'details' => 'array',
74         ];
75
76         protected $fillable = [
77                 'details',
78                 'tournament_id',
79                 'type',
80                 'user_id',
81         ];
82
83 }