]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
e182c0885ae6e07e2f8bb12698b8c51adda64424
[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 resultReported(Tournament $tournament, Result $result, User $user) {
14                 $protocol = static::create([
15                         'tournament_id' => $tournament->id,
16                         'user_id' => $user->id,
17                         'type' => 'result.report',
18                         'details' => [
19                                 'tournament' => static::tournamentMemo($tournament),
20                                 'result' => static::resultMemo($result),
21                         ],
22                 ]);
23                 ProtocolAdded::dispatch($protocol);
24         }
25
26         public static function roundAdded(Tournament $tournament, Round $round, User $user) {
27                 $protocol = static::create([
28                         'tournament_id' => $tournament->id,
29                         'user_id' => $user->id,
30                         'type' => 'round.create',
31                         'details' => [
32                                 'tournament' => static::tournamentMemo($tournament),
33                                 'round' => static::roundMemo($round),
34                         ],
35                 ]);
36                 ProtocolAdded::dispatch($protocol);
37         }
38
39         public static function tournamentCreated(Tournament $tournament, User $user) {
40                 $protocol = static::create([
41                         'tournament_id' => $tournament->id,
42                         'user_id' => $user->id,
43                         'type' => 'tournament.create',
44                         'details' => [
45                                 'tournament' => static::tournamentMemo($tournament),
46                         ],
47                 ]);
48                 ProtocolAdded::dispatch($protocol);
49         }
50
51
52         protected static function resultMemo(Result $result) {
53                 return [
54                         'id' => $result->id,
55                         'time' => $result->time,
56                 ];
57         }
58
59         protected static function roundMemo(Round $round) {
60                 return [
61                         'id' => $round->id,
62                         'seed' => $round->seed,
63                 ];
64         }
65
66         protected static function tournamentMemo(Tournament $tournament) {
67                 return [
68                         'id' => $tournament->id,
69                         'title' => $tournament->title,
70                 ];
71         }
72
73         protected static function userMemo(User $user) {
74                 return [
75                         'id' => $user->id,
76                         'username' => $user->username,
77                         'discriminator' => $user->discriminator,
78                         'avatar' => $user->avatar,
79                 ];
80         }
81
82
83         public function tournament() {
84                 return $this->belongsTo(Tournament::class);
85         }
86
87         public function user() {
88                 return $this->belongsTo(User::class);
89         }
90
91
92         protected $casts = [
93                 'details' => 'array',
94         ];
95
96         protected $fillable = [
97                 'details',
98                 'tournament_id',
99                 'type',
100                 'user_id',
101         ];
102
103 }