]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
allow admins to lock/unlock rounds
[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 roundLocked(Tournament $tournament, Round $round, User $user = null) {
40                 $protocol = static::create([
41                         'tournament_id' => $tournament->id,
42                         'user_id' => $user ? $user->id : null,
43                         'type' => 'round.lock',
44                         'details' => [
45                                 'tournament' => static::tournamentMemo($tournament),
46                                 'round' => static::roundMemo($round),
47                         ],
48                 ]);
49                 ProtocolAdded::dispatch($protocol);
50         }
51
52         public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
53                 $protocol = static::create([
54                         'tournament_id' => $tournament->id,
55                         'user_id' => $user->id,
56                         'type' => 'round.create',
57                         'details' => [
58                                 'tournament' => static::tournamentMemo($tournament),
59                                 'round' => static::roundMemo($round),
60                         ],
61                 ]);
62                 ProtocolAdded::dispatch($protocol);
63         }
64
65         public static function roundUnlocked(Tournament $tournament, Round $round, User $user = null) {
66                 $protocol = static::create([
67                         'tournament_id' => $tournament->id,
68                         'user_id' => $user ? $user->id : null,
69                         'type' => 'round.unlock',
70                         'details' => [
71                                 'tournament' => static::tournamentMemo($tournament),
72                                 'round' => static::roundMemo($round),
73                         ],
74                 ]);
75                 ProtocolAdded::dispatch($protocol);
76         }
77
78         public static function tournamentCreated(Tournament $tournament, User $user) {
79                 $protocol = static::create([
80                         'tournament_id' => $tournament->id,
81                         'user_id' => $user->id,
82                         'type' => 'tournament.create',
83                         'details' => [
84                                 'tournament' => static::tournamentMemo($tournament),
85                         ],
86                 ]);
87                 ProtocolAdded::dispatch($protocol);
88         }
89
90         public static function tournamentLocked(Tournament $tournament, User $user = null) {
91                 $protocol = static::create([
92                         'tournament_id' => $tournament->id,
93                         'user_id' => $user ? $user->id : null,
94                         'type' => 'tournament.lock',
95                         'details' => [
96                                 'tournament' => static::tournamentMemo($tournament),
97                         ],
98                 ]);
99                 ProtocolAdded::dispatch($protocol);
100         }
101
102
103         protected static function resultMemo(Result $result) {
104                 return [
105                         'id' => $result->id,
106                         'forfeit' => $result->forfeit,
107                         'time' => $result->time,
108                 ];
109         }
110
111         protected static function roundMemo(Round $round) {
112                 return [
113                         'id' => $round->id,
114                         'number' => $round->number,
115                         'seed' => $round->seed,
116                 ];
117         }
118
119         protected static function tournamentMemo(Tournament $tournament) {
120                 return [
121                         'id' => $tournament->id,
122                         'title' => $tournament->title,
123                 ];
124         }
125
126         protected static function userMemo(User $user) {
127                 return [
128                         'id' => $user->id,
129                         'username' => $user->username,
130                         'discriminator' => $user->discriminator,
131                         'avatar' => $user->avatar,
132                 ];
133         }
134
135
136         public function tournament() {
137                 return $this->belongsTo(Tournament::class);
138         }
139
140         public function user() {
141                 return $this->belongsTo(User::class);
142         }
143
144
145         protected $casts = [
146                 'details' => 'array',
147         ];
148
149         protected $fillable = [
150                 'details',
151                 'tournament_id',
152                 'type',
153                 'user_id',
154         ];
155
156 }