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 Database\Factories;
9
10use App\Models\Beatmap;
11use App\Models\BeatmapOwner;
12use App\Models\Beatmapset;
13use App\Models\User;
14use Carbon\Carbon;
15
16class BeatmapFactory extends Factory
17{
18 protected $model = Beatmap::class;
19
20 public function definition(): array
21 {
22 return [
23 'beatmapset_id' => fn () => Beatmapset::factory(),
24 'filename' => fn () => $this->faker->sentence(3),
25 'last_update' => Carbon::now(),
26 'checksum' => md5((string) rand()),
27 'version' => fn () => $this->faker->domainWord(),
28 'total_length' => rand(30, 200),
29 'hit_length' => fn (array $attr) => $attr['total_length'] - rand(0, 20),
30 'countSpinner' => rand(0, 5),
31 'countNormal' => rand(100, 2000),
32 'bpm' => rand(100, 200),
33 'diff_drain' => rand(0, 10),
34 'diff_size' => rand(0, 10),
35 'diff_overall' => rand(0, 10),
36 'diff_approach' => rand(0, 10),
37 'playmode' => array_rand_val(Beatmap::MODES),
38 'approved' => array_rand_val(Beatmapset::STATES),
39 'difficultyrating' => rand(0, 5000) / 1000,
40 'playcount' => rand(0, 50000),
41
42 // depends on countNormal
43 'countSlider' => fn (array $attr) => round($attr['countNormal'] / 9),
44
45 // depends on playcount
46 'passcount' => fn (array $attr) => round($attr['playcount'] * 0.7),
47 ];
48 }
49
50 public function deleted(): static
51 {
52 return $this->state(['deleted_at' => now()]);
53 }
54
55 public function deletedBeatmapset(): static
56 {
57 return $this->state([
58 'beatmapset_id' => Beatmapset::factory()->deleted(),
59 ]);
60 }
61
62 public function inactive(): static
63 {
64 return $this->state([
65 'beatmapset_id' => Beatmapset::factory()->state(['active' => false]),
66 ]);
67 }
68
69 public function owner(User $user): static
70 {
71 return $this
72 ->state(['user_id' => $user])
73 ->has(BeatmapOwner::factory()->state(fn (array $attr, Beatmap $beatmap) => [
74 'user_id' => $beatmap->user_id,
75 ]));
76 }
77
78 public function qualified(): static
79 {
80 return $this->state(['approved' => Beatmapset::STATES['qualified']]);
81 }
82
83 public function ranked(): static
84 {
85 return $this->state(['approved' => Beatmapset::STATES['ranked']]);
86 }
87
88 public function ruleset(string $ruleset): static
89 {
90 return $this->state(['playmode' => Beatmap::modeInt($ruleset)]);
91 }
92
93 public function wip(): static
94 {
95 return $this->state(['approved' => Beatmapset::STATES['wip']]);
96 }
97}