]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
result comments
[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 resultCommented(Tournament $tournament, Result $result, User $user) {
14                 $protocol = static::create([
15                         'tournament_id' => $tournament->id,
16                         'user_id' => $user->id,
17                         'type' => 'result.comment',
18                         'details' => [
19                                 'tournament' => static::tournamentMemo($tournament),
20                                 'result' => static::resultMemo($result),
21                                 'round' => static::roundMemo($result->round),
22                         ],
23                 ]);
24                 ProtocolAdded::dispatch($protocol);
25         }
26
27         public static function resultReported(Tournament $tournament, Result $result, User $user) {
28                 $protocol = static::create([
29                         'tournament_id' => $tournament->id,
30                         'user_id' => $user->id,
31                         'type' => 'result.report',
32                         'details' => [
33                                 'tournament' => static::tournamentMemo($tournament),
34                                 'result' => static::resultMemo($result),
35                                 'round' => static::roundMemo($result->round),
36                         ],
37                 ]);
38                 ProtocolAdded::dispatch($protocol);
39         }
40
41         public static function roundAdded(Tournament $tournament, Round $round, User $user) {
42                 $protocol = static::create([
43                         'tournament_id' => $tournament->id,
44                         'user_id' => $user->id,
45                         'type' => 'round.create',
46                         'details' => [
47                                 'tournament' => static::tournamentMemo($tournament),
48                                 'round' => static::roundMemo($round),
49                         ],
50                 ]);
51                 ProtocolAdded::dispatch($protocol);
52         }
53
54         public static function roundLocked(Tournament $tournament, Round $round, User $user = null) {
55                 $protocol = static::create([
56                         'tournament_id' => $tournament->id,
57                         'user_id' => $user ? $user->id : null,
58                         'type' => 'round.lock',
59                         'details' => [
60                                 'tournament' => static::tournamentMemo($tournament),
61                                 'round' => static::roundMemo($round),
62                         ],
63                 ]);
64                 ProtocolAdded::dispatch($protocol);
65         }
66
67         public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
68                 $protocol = static::create([
69                         'tournament_id' => $tournament->id,
70                         'user_id' => $user->id,
71                         'type' => 'round.create',
72                         'details' => [
73                                 'tournament' => static::tournamentMemo($tournament),
74                                 'round' => static::roundMemo($round),
75                         ],
76                 ]);
77                 ProtocolAdded::dispatch($protocol);
78         }
79
80         public static function roundUnlocked(Tournament $tournament, Round $round, User $user = null) {
81                 $protocol = static::create([
82                         'tournament_id' => $tournament->id,
83                         'user_id' => $user ? $user->id : null,
84                         'type' => 'round.unlock',
85                         'details' => [
86                                 'tournament' => static::tournamentMemo($tournament),
87                                 'round' => static::roundMemo($round),
88                         ],
89                 ]);
90                 ProtocolAdded::dispatch($protocol);
91         }
92
93         public static function tournamentCreated(Tournament $tournament, User $user) {
94                 $protocol = static::create([
95                         'tournament_id' => $tournament->id,
96                         'user_id' => $user->id,
97                         'type' => 'tournament.create',
98                         'details' => [
99                                 'tournament' => static::tournamentMemo($tournament),
100                         ],
101                 ]);
102                 ProtocolAdded::dispatch($protocol);
103         }
104
105         public static function tournamentLocked(Tournament $tournament, User $user = null) {
106                 $protocol = static::create([
107                         'tournament_id' => $tournament->id,
108                         'user_id' => $user ? $user->id : null,
109                         'type' => 'tournament.lock',
110                         'details' => [
111                                 'tournament' => static::tournamentMemo($tournament),
112                         ],
113                 ]);
114                 ProtocolAdded::dispatch($protocol);
115         }
116
117
118         protected static function resultMemo(Result $result) {
119                 return [
120                         'id' => $result->id,
121                         'comment' => $result->comment,
122                         'forfeit' => $result->forfeit,
123                         'time' => $result->time,
124                 ];
125         }
126
127         protected static function roundMemo(Round $round) {
128                 return [
129                         'id' => $round->id,
130                         'locked' => $round->locked,
131                         'no_record' => $round->no_record,
132                         'number' => $round->number,
133                         'seed' => $round->seed,
134                 ];
135         }
136
137         protected static function tournamentMemo(Tournament $tournament) {
138                 return [
139                         'id' => $tournament->id,
140                         'locked' => $tournament->locked,
141                         'no_record' => $tournament->no_record,
142                         'title' => $tournament->title,
143                 ];
144         }
145
146         protected static function userMemo(User $user) {
147                 return [
148                         'id' => $user->id,
149                         'username' => $user->username,
150                         'discriminator' => $user->discriminator,
151                         'avatar' => $user->avatar,
152                 ];
153         }
154
155
156         public function tournament() {
157                 return $this->belongsTo(Tournament::class);
158         }
159
160         public function user() {
161                 return $this->belongsTo(User::class);
162         }
163
164
165         protected $casts = [
166                 'details' => 'array',
167         ];
168
169         protected $fillable = [
170                 'details',
171                 'tournament_id',
172                 'type',
173                 'user_id',
174         ];
175
176 }