1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Models;
9
10use Carbon\Carbon;
11use Illuminate\Database\Eloquent\ModelNotFoundException;
12use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
14/**
15 * @property bool $finalised
16 * @property string $name
17 * @property-read Collection<Multiplayer\Room> $rooms
18 * @property float[]|null $score_factors
19 * @property string|null $url
20 */
21class Season extends Model
22{
23 protected $casts = [
24 'finalised' => 'boolean',
25 'score_factors' => 'array',
26 ];
27
28 public function scopeActive($query)
29 {
30 return $query->where('finalised', false);
31 }
32
33 public static function latestOrId($id)
34 {
35 if ($id === 'latest') {
36 $season = static::last();
37
38 if ($season === null) {
39 throw new ModelNotFoundException();
40 }
41 } else {
42 $season = static::findOrFail($id);
43 }
44
45 return $season;
46 }
47
48 public function endDate(): ?Carbon
49 {
50 return $this->finalised
51 ? $this->rooms->max('ends_at')
52 : null;
53 }
54
55 public function startDate(): ?Carbon
56 {
57 return $this->rooms->min('starts_at');
58 }
59
60 public function rooms(): BelongsToMany
61 {
62 return $this->belongsToMany(Multiplayer\Room::class, SeasonRoom::class);
63 }
64}