, id?: int} $deck_config * @property string $join_code * @property int $current_round * @property CarbonImmutable|null $revealed_at * @property int $state_version * @property bool $allow_participant_controls * @property CarbonImmutable|null $last_activity_at * @property CarbonImmutable|null $closed_at * @property CarbonImmutable|null $deleted_at * @property int $players_count */ class Room extends Model { use HasUuids, SoftDeletes; protected $fillable = [ 'owner_id', 'title', 'current_round', 'deck_config', 'revealed_at', 'state_version', 'allow_participant_controls', 'last_activity_at', 'closed_at', ]; protected function casts(): array { return [ 'current_round' => 'integer', 'state_version' => 'integer', 'allow_participant_controls' => 'boolean', 'revealed_at' => 'immutable_datetime', 'last_activity_at' => 'immutable_datetime', 'closed_at' => 'immutable_datetime', 'deck_config' => 'array', ]; } protected $attributes = [ 'allow_participant_controls' => false, ]; protected static function booted(): void { static::creating(function (Room $room) { $room->join_code ??= static::generateJoinCode(); $room->last_activity_at ??= now(); }); } protected static function generateJoinCode(): string { $alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; do { $code = ''; for ($i = 0; $i < 6; $i++) { $code .= $alphabet[random_int(0, strlen($alphabet) - 1)]; } } while (static::withTrashed()->where('join_code', $code)->exists()); return $code; } /** @return HasMany */ public function players(): HasMany { return $this->hasMany(Player::class); } /** @return HasMany */ public function activePlayers(): HasMany { return $this->hasMany(Player::class)->whereNull('inactive_at'); } /** @return BelongsTo */ public function owner(): BelongsTo { return $this->belongsTo(User::class, 'owner_id'); } /** @return HasMany */ public function votes(): HasMany { return $this->hasMany(Vote::class); } public function isClosed(): bool { return $this->closed_at !== null; } /** @return BelongsToMany */ public function favoritedBy(): BelongsToMany { return $this->belongsToMany(User::class, 'user_room_favorites')->withTimestamps(); } }