]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
slightly improved message generation
[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 applicationAccepted(Tournament $tournament, Application $application, User $user) {
14                 $protocol = static::create([
15                         'tournament_id' => $tournament->id,
16                         'user_id' => $user->id,
17                         'type' => 'application.accepted',
18                         'details' => [
19                                 'tournament' => static::tournamentMemo($tournament),
20                                 'application' => static::applicationMemo($application),
21                                 'user' => static::userMemo($application->user),
22                         ],
23                 ]);
24                 ProtocolAdded::dispatch($protocol);
25         }
26
27         public static function applicationReceived(Tournament $tournament, Application $application, User $user) {
28                 $protocol = static::create([
29                         'tournament_id' => $tournament->id,
30                         'user_id' => $user->id,
31                         'type' => 'application.received',
32                         'details' => [
33                                 'tournament' => static::tournamentMemo($tournament),
34                                 'application' => static::applicationMemo($application),
35                                 'user' => static::userMemo($application->user),
36                         ],
37                 ]);
38                 ProtocolAdded::dispatch($protocol);
39         }
40
41         public static function applicationRejected(Tournament $tournament, Application $application, User $user) {
42                 $protocol = static::create([
43                         'tournament_id' => $tournament->id,
44                         'user_id' => $user->id,
45                         'type' => 'application.rejected',
46                         'details' => [
47                                 'tournament' => static::tournamentMemo($tournament),
48                                 'application' => static::applicationMemo($application),
49                                 'user' => static::userMemo($application->user),
50                         ],
51                 ]);
52                 ProtocolAdded::dispatch($protocol);
53         }
54
55         public static function resultCommented(Tournament $tournament, Result $result, User $user) {
56                 $protocol = static::create([
57                         'tournament_id' => $tournament->id,
58                         'user_id' => $user->id,
59                         'type' => 'result.comment',
60                         'details' => [
61                                 'tournament' => static::tournamentMemo($tournament),
62                                 'result' => static::resultMemo($result),
63                                 'round' => static::roundMemo($result->round),
64                         ],
65                 ]);
66                 ProtocolAdded::dispatch($protocol);
67         }
68
69         public static function resultReported(Tournament $tournament, Result $result, User $user) {
70                 $protocol = static::create([
71                         'tournament_id' => $tournament->id,
72                         'user_id' => $user->id,
73                         'type' => 'result.report',
74                         'details' => [
75                                 'tournament' => static::tournamentMemo($tournament),
76                                 'result' => static::resultMemo($result),
77                                 'round' => static::roundMemo($result->round),
78                         ],
79                 ]);
80                 ProtocolAdded::dispatch($protocol);
81         }
82
83         public static function roundAdded(Tournament $tournament, Round $round, User $user) {
84                 $protocol = static::create([
85                         'tournament_id' => $tournament->id,
86                         'user_id' => $user->id,
87                         'type' => 'round.create',
88                         'details' => [
89                                 'tournament' => static::tournamentMemo($tournament),
90                                 'round' => static::roundMemo($round),
91                         ],
92                 ]);
93                 ProtocolAdded::dispatch($protocol);
94         }
95
96         public static function roundEdited(Tournament $tournament, Round $round, User $user) {
97                 $protocol = static::create([
98                         'tournament_id' => $tournament->id,
99                         'user_id' => $user->id,
100                         'type' => 'round.edit',
101                         'details' => [
102                                 'tournament' => static::tournamentMemo($tournament),
103                                 'round' => static::roundMemo($round),
104                         ],
105                 ]);
106                 ProtocolAdded::dispatch($protocol);
107         }
108
109         public static function roundLocked(Tournament $tournament, Round $round, User $user = null) {
110                 $protocol = static::create([
111                         'tournament_id' => $tournament->id,
112                         'user_id' => $user ? $user->id : null,
113                         'type' => 'round.lock',
114                         'details' => [
115                                 'tournament' => static::tournamentMemo($tournament),
116                                 'round' => static::roundMemo($round),
117                         ],
118                 ]);
119                 ProtocolAdded::dispatch($protocol);
120         }
121
122         public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
123                 $protocol = static::create([
124                         'tournament_id' => $tournament->id,
125                         'user_id' => $user->id,
126                         'type' => 'round.seed',
127                         'details' => [
128                                 'tournament' => static::tournamentMemo($tournament),
129                                 'round' => static::roundMemo($round),
130                         ],
131                 ]);
132                 ProtocolAdded::dispatch($protocol);
133         }
134
135         public static function roundUnlocked(Tournament $tournament, Round $round, User $user = null) {
136                 $protocol = static::create([
137                         'tournament_id' => $tournament->id,
138                         'user_id' => $user ? $user->id : null,
139                         'type' => 'round.unlock',
140                         'details' => [
141                                 'tournament' => static::tournamentMemo($tournament),
142                                 'round' => static::roundMemo($round),
143                         ],
144                 ]);
145                 ProtocolAdded::dispatch($protocol);
146         }
147
148         public static function tournamentClosed(Tournament $tournament, User $user = null) {
149                 $protocol = static::create([
150                         'tournament_id' => $tournament->id,
151                         'user_id' => $user ? $user->id : null,
152                         'type' => 'tournament.close',
153                         'details' => [
154                                 'tournament' => static::tournamentMemo($tournament),
155                         ],
156                 ]);
157                 ProtocolAdded::dispatch($protocol);
158         }
159
160         public static function tournamentCreated(Tournament $tournament, User $user) {
161                 $protocol = static::create([
162                         'tournament_id' => $tournament->id,
163                         'user_id' => $user->id,
164                         'type' => 'tournament.create',
165                         'details' => [
166                                 'tournament' => static::tournamentMemo($tournament),
167                         ],
168                 ]);
169                 ProtocolAdded::dispatch($protocol);
170         }
171
172         public static function tournamentDiscord(Tournament $tournament, User $user = null) {
173                 $protocol = static::create([
174                         'tournament_id' => $tournament->id,
175                         'user_id' => $user ? $user->id : null,
176                         'type' => 'tournament.discord',
177                         'details' => [
178                                 'tournament' => static::tournamentMemo($tournament),
179                         ],
180                 ]);
181                 ProtocolAdded::dispatch($protocol);
182         }
183
184         public static function tournamentDiscordSettings(Tournament $tournament, User $user = null) {
185                 $protocol = static::create([
186                         'tournament_id' => $tournament->id,
187                         'user_id' => $user ? $user->id : null,
188                         'type' => 'tournament.discordSettings',
189                         'details' => [
190                                 'tournament' => static::tournamentMemo($tournament),
191                         ],
192                 ]);
193                 ProtocolAdded::dispatch($protocol);
194         }
195
196         public static function tournamentLocked(Tournament $tournament, User $user = null) {
197                 $protocol = static::create([
198                         'tournament_id' => $tournament->id,
199                         'user_id' => $user ? $user->id : null,
200                         'type' => 'tournament.lock',
201                         'details' => [
202                                 'tournament' => static::tournamentMemo($tournament),
203                         ],
204                 ]);
205                 ProtocolAdded::dispatch($protocol);
206         }
207
208         public static function tournamentOpened(Tournament $tournament, User $user = null) {
209                 $protocol = static::create([
210                         'tournament_id' => $tournament->id,
211                         'user_id' => $user ? $user->id : null,
212                         'type' => 'tournament.open',
213                         'details' => [
214                                 'tournament' => static::tournamentMemo($tournament),
215                         ],
216                 ]);
217                 ProtocolAdded::dispatch($protocol);
218         }
219
220         public static function tournamentSettings(Tournament $tournament, User $user = null) {
221                 $protocol = static::create([
222                         'tournament_id' => $tournament->id,
223                         'user_id' => $user ? $user->id : null,
224                         'type' => 'tournament.settings',
225                         'details' => [
226                                 'tournament' => static::tournamentMemo($tournament),
227                         ],
228                 ]);
229                 ProtocolAdded::dispatch($protocol);
230         }
231
232         public static function tournamentUnlocked(Tournament $tournament, User $user = null) {
233                 $protocol = static::create([
234                         'tournament_id' => $tournament->id,
235                         'user_id' => $user ? $user->id : null,
236                         'type' => 'tournament.unlock',
237                         'details' => [
238                                 'tournament' => static::tournamentMemo($tournament),
239                         ],
240                 ]);
241                 ProtocolAdded::dispatch($protocol);
242         }
243
244
245         protected static function applicationMemo(Application $application) {
246                 return [
247                         'id' => $application->id,
248                         'denied' => $application->denied,
249                 ];
250         }
251
252         protected static function resultMemo(Result $result) {
253                 return [
254                         'id' => $result->id,
255                         'comment' => $result->comment,
256                         'forfeit' => $result->forfeit,
257                         'time' => $result->time,
258                 ];
259         }
260
261         protected static function roundMemo(Round $round) {
262                 return [
263                         'id' => $round->id,
264                         'locked' => $round->locked,
265                         'no_record' => $round->no_record,
266                         'number' => $round->number,
267                         'seed' => $round->seed,
268                 ];
269         }
270
271         protected static function tournamentMemo(Tournament $tournament) {
272                 return [
273                         'id' => $tournament->id,
274                         'accept_applications' => $tournament->accept_applications,
275                         'locked' => $tournament->locked,
276                         'no_record' => $tournament->no_record,
277                         'title' => $tournament->title,
278                 ];
279         }
280
281         protected static function userMemo(User $user) {
282                 return [
283                         'id' => $user->id,
284                         'username' => $user->username,
285                         'discriminator' => $user->discriminator,
286                         'avatar' => $user->avatar,
287                         'nickname' => $user->nickname,
288                 ];
289         }
290
291
292         public function tournament() {
293                 return $this->belongsTo(Tournament::class);
294         }
295
296         public function user() {
297                 return $this->belongsTo(User::class);
298         }
299
300
301         protected $casts = [
302                 'details' => 'array',
303                 'user_id' => 'string',
304         ];
305
306         protected $fillable = [
307                 'details',
308                 'tournament_id',
309                 'type',
310                 'user_id',
311         ];
312
313 }