]> git.localhorst.tv Git - alttp.git/blob - app/Models/Protocol.php
allow setting seeds
[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 roundSeedSet(Tournament $tournament, Round $round, User $user) {
40                 $protocol = static::create([
41                         'tournament_id' => $tournament->id,
42                         'user_id' => $user->id,
43                         'type' => 'round.create',
44                         'details' => [
45                                 'tournament' => static::tournamentMemo($tournament),
46                                 'round' => static::roundMemo($round),
47                         ],
48                 ]);
49                 ProtocolAdded::dispatch($protocol);
50         }
51
52         public static function tournamentCreated(Tournament $tournament, User $user) {
53                 $protocol = static::create([
54                         'tournament_id' => $tournament->id,
55                         'user_id' => $user->id,
56                         'type' => 'tournament.create',
57                         'details' => [
58                                 'tournament' => static::tournamentMemo($tournament),
59                         ],
60                 ]);
61                 ProtocolAdded::dispatch($protocol);
62         }
63
64
65         protected static function resultMemo(Result $result) {
66                 return [
67                         'id' => $result->id,
68                         'time' => $result->time,
69                 ];
70         }
71
72         protected static function roundMemo(Round $round) {
73                 return [
74                         'id' => $round->id,
75                         'seed' => $round->seed,
76                 ];
77         }
78
79         protected static function tournamentMemo(Tournament $tournament) {
80                 return [
81                         'id' => $tournament->id,
82                         'title' => $tournament->title,
83                 ];
84         }
85
86         protected static function userMemo(User $user) {
87                 return [
88                         'id' => $user->id,
89                         'username' => $user->username,
90                         'discriminator' => $user->discriminator,
91                         'avatar' => $user->avatar,
92                 ];
93         }
94
95
96         public function tournament() {
97                 return $this->belongsTo(Tournament::class);
98         }
99
100         public function user() {
101                 return $this->belongsTo(User::class);
102         }
103
104
105         protected $casts = [
106                 'details' => 'array',
107         ];
108
109         protected $fillable = [
110                 'details',
111                 'tournament_id',
112                 'type',
113                 'user_id',
114         ];
115
116 }