]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
tournament settings
[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 roundLocked(Tournament $tournament, Round $round, User $user = null) {
97                 $protocol = static::create([
98                         'tournament_id' => $tournament->id,
99                         'user_id' => $user ? $user->id : null,
100                         'type' => 'round.lock',
101                         'details' => [
102                                 'tournament' => static::tournamentMemo($tournament),
103                                 'round' => static::roundMemo($round),
104                         ],
105                 ]);
106                 ProtocolAdded::dispatch($protocol);
107         }
108
109         public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
110                 $protocol = static::create([
111                         'tournament_id' => $tournament->id,
112                         'user_id' => $user->id,
113                         'type' => 'round.seed',
114                         'details' => [
115                                 'tournament' => static::tournamentMemo($tournament),
116                                 'round' => static::roundMemo($round),
117                         ],
118                 ]);
119                 ProtocolAdded::dispatch($protocol);
120         }
121
122         public static function roundUnlocked(Tournament $tournament, Round $round, User $user = null) {
123                 $protocol = static::create([
124                         'tournament_id' => $tournament->id,
125                         'user_id' => $user ? $user->id : null,
126                         'type' => 'round.unlock',
127                         'details' => [
128                                 'tournament' => static::tournamentMemo($tournament),
129                                 'round' => static::roundMemo($round),
130                         ],
131                 ]);
132                 ProtocolAdded::dispatch($protocol);
133         }
134
135         public static function tournamentClosed(Tournament $tournament, User $user = null) {
136                 $protocol = static::create([
137                         'tournament_id' => $tournament->id,
138                         'user_id' => $user ? $user->id : null,
139                         'type' => 'tournament.close',
140                         'details' => [
141                                 'tournament' => static::tournamentMemo($tournament),
142                         ],
143                 ]);
144                 ProtocolAdded::dispatch($protocol);
145         }
146
147         public static function tournamentCreated(Tournament $tournament, User $user) {
148                 $protocol = static::create([
149                         'tournament_id' => $tournament->id,
150                         'user_id' => $user->id,
151                         'type' => 'tournament.create',
152                         'details' => [
153                                 'tournament' => static::tournamentMemo($tournament),
154                         ],
155                 ]);
156                 ProtocolAdded::dispatch($protocol);
157         }
158
159         public static function tournamentDiscord(Tournament $tournament, User $user = null) {
160                 $protocol = static::create([
161                         'tournament_id' => $tournament->id,
162                         'user_id' => $user ? $user->id : null,
163                         'type' => 'tournament.discord',
164                         'details' => [
165                                 'tournament' => static::tournamentMemo($tournament),
166                         ],
167                 ]);
168                 ProtocolAdded::dispatch($protocol);
169         }
170
171         public static function tournamentDiscordSettings(Tournament $tournament, User $user = null) {
172                 $protocol = static::create([
173                         'tournament_id' => $tournament->id,
174                         'user_id' => $user ? $user->id : null,
175                         'type' => 'tournament.discordSettings',
176                         'details' => [
177                                 'tournament' => static::tournamentMemo($tournament),
178                         ],
179                 ]);
180                 ProtocolAdded::dispatch($protocol);
181         }
182
183         public static function tournamentLocked(Tournament $tournament, User $user = null) {
184                 $protocol = static::create([
185                         'tournament_id' => $tournament->id,
186                         'user_id' => $user ? $user->id : null,
187                         'type' => 'tournament.lock',
188                         'details' => [
189                                 'tournament' => static::tournamentMemo($tournament),
190                         ],
191                 ]);
192                 ProtocolAdded::dispatch($protocol);
193         }
194
195         public static function tournamentOpened(Tournament $tournament, User $user = null) {
196                 $protocol = static::create([
197                         'tournament_id' => $tournament->id,
198                         'user_id' => $user ? $user->id : null,
199                         'type' => 'tournament.open',
200                         'details' => [
201                                 'tournament' => static::tournamentMemo($tournament),
202                         ],
203                 ]);
204                 ProtocolAdded::dispatch($protocol);
205         }
206
207         public static function tournamentUnlocked(Tournament $tournament, User $user = null) {
208                 $protocol = static::create([
209                         'tournament_id' => $tournament->id,
210                         'user_id' => $user ? $user->id : null,
211                         'type' => 'tournament.unlock',
212                         'details' => [
213                                 'tournament' => static::tournamentMemo($tournament),
214                         ],
215                 ]);
216                 ProtocolAdded::dispatch($protocol);
217         }
218
219
220         protected static function applicationMemo(Application $application) {
221                 return [
222                         'id' => $application->id,
223                         'denied' => $application->denied,
224                 ];
225         }
226
227         protected static function resultMemo(Result $result) {
228                 return [
229                         'id' => $result->id,
230                         'comment' => $result->comment,
231                         'forfeit' => $result->forfeit,
232                         'time' => $result->time,
233                 ];
234         }
235
236         protected static function roundMemo(Round $round) {
237                 return [
238                         'id' => $round->id,
239                         'locked' => $round->locked,
240                         'no_record' => $round->no_record,
241                         'number' => $round->number,
242                         'seed' => $round->seed,
243                 ];
244         }
245
246         protected static function tournamentMemo(Tournament $tournament) {
247                 return [
248                         'id' => $tournament->id,
249                         'accept_applications' => $tournament->accept_applications,
250                         'locked' => $tournament->locked,
251                         'no_record' => $tournament->no_record,
252                         'title' => $tournament->title,
253                 ];
254         }
255
256         protected static function userMemo(User $user) {
257                 return [
258                         'id' => $user->id,
259                         'username' => $user->username,
260                         'discriminator' => $user->discriminator,
261                         'avatar' => $user->avatar,
262                         'nickname' => $user->nickname,
263                 ];
264         }
265
266
267         public function tournament() {
268                 return $this->belongsTo(Tournament::class);
269         }
270
271         public function user() {
272                 return $this->belongsTo(User::class);
273         }
274
275
276         protected $casts = [
277                 'details' => 'array',
278         ];
279
280         protected $fillable = [
281                 'details',
282                 'tournament_id',
283                 'type',
284                 'user_id',
285         ];
286
287 }