]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Protocol.php
option to hide round numbers
[alttp.git] / app / Models / Protocol.php
index fb93a374d744668941056175dcf5c8c383eb6428..60b25f2ad8cd6cd21c4003fc747e40c808e1b132 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\Events\ProtocolAdded;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
@@ -9,6 +10,285 @@ class Protocol extends Model
 {
        use HasFactory;
 
+       public static function applicationAccepted(Tournament $tournament, Application $application, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'application.accepted',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'application' => static::applicationMemo($application),
+                               'user' => static::userMemo($application->user),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function applicationReceived(Tournament $tournament, Application $application, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'application.received',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'application' => static::applicationMemo($application),
+                               'user' => static::userMemo($application->user),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function applicationRejected(Tournament $tournament, Application $application, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'application.rejected',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'application' => static::applicationMemo($application),
+                               'user' => static::userMemo($application->user),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function resultCommented(Tournament $tournament, Result $result, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'result.comment',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'result' => static::resultMemo($result),
+                               'round' => static::roundMemo($result->round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function resultReported(Tournament $tournament, Result $result, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'result.report',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'result' => static::resultMemo($result),
+                               'round' => static::roundMemo($result->round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function roundAdded(Tournament $tournament, Round $round, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'round.create',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function roundEdited(Tournament $tournament, Round $round, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'round.edit',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function roundLocked(Tournament $tournament, Round $round, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'round.lock',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'round.seed',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function roundUnlocked(Tournament $tournament, Round $round, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'round.unlock',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentClosed(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.close',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentCreated(Tournament $tournament, User $user) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user->id,
+                       'type' => 'tournament.create',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentDiscord(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.discord',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentDiscordSettings(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.discordSettings',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentLocked(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.lock',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentOpened(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.open',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentSettings(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.settings',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+       public static function tournamentUnlocked(Tournament $tournament, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'tournament.unlock',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
+
+       protected static function applicationMemo(Application $application) {
+               return [
+                       'id' => $application->id,
+                       'denied' => $application->denied,
+               ];
+       }
+
+       protected static function resultMemo(Result $result) {
+               return [
+                       'id' => $result->id,
+                       'comment' => $result->comment,
+                       'forfeit' => $result->forfeit,
+                       'time' => $result->time,
+               ];
+       }
+
+       protected static function roundMemo(Round $round) {
+               return [
+                       'id' => $round->id,
+                       'locked' => $round->locked,
+                       'no_record' => $round->no_record,
+                       'number' => $round->number,
+                       'seed' => $round->seed,
+               ];
+       }
+
+       protected static function tournamentMemo(Tournament $tournament) {
+               return [
+                       'id' => $tournament->id,
+                       'accept_applications' => $tournament->accept_applications,
+                       'locked' => $tournament->locked,
+                       'no_record' => $tournament->no_record,
+                       'title' => $tournament->title,
+               ];
+       }
+
+       protected static function userMemo(User $user) {
+               return [
+                       'id' => $user->id,
+                       'username' => $user->username,
+                       'discriminator' => $user->discriminator,
+                       'avatar' => $user->avatar,
+                       'nickname' => $user->nickname,
+               ];
+       }
+
+
        public function tournament() {
                return $this->belongsTo(Tournament::class);
        }
@@ -17,4 +297,17 @@ class Protocol extends Model
                return $this->belongsTo(User::class);
        }
 
+
+       protected $casts = [
+               'details' => 'array',
+               'user_id' => 'string',
+       ];
+
+       protected $fillable = [
+               'details',
+               'tournament_id',
+               'type',
+               'user_id',
+       ];
+
 }