/database/seeds/ModelSeeders/SpotlightSeeder.php

https://github.com/ppy/osu-web · PHP · 131 lines · 97 code · 22 blank · 12 comment · 2 complexity · f241edb288a1b936d7a493c3d263897e MD5 · raw file

  1. <?php
  2. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
  3. // See the LICENCE file in the repository root for full licence text.
  4. use App\Models\Beatmap;
  5. use App\Models\Score\Best\Model as ScoresBestModel;
  6. use App\Models\Spotlight;
  7. use App\Models\UserStatistics\Spotlight\Model as UserStatisticsModel;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\Seeder;
  10. class SpotlightSeeder extends Seeder
  11. {
  12. /**
  13. * Run the database seeds.
  14. *
  15. * @return void
  16. */
  17. public function run()
  18. {
  19. $date = Carbon::parse('3 years ago')->startOfMonth();
  20. $now = Carbon::now()->startOfMonth();
  21. while ($date <= $now) {
  22. $this->seedMonthly($date);
  23. if ($date->month === 12) {
  24. $this->seedBestOf($date);
  25. }
  26. $date->addMonth(1);
  27. }
  28. collect(range(1, 10))->each(function () {
  29. $this->seedNonPeriodic();
  30. });
  31. }
  32. public function seedMonthly($date)
  33. {
  34. // note: this does result in spotlights with beatmaps from the future.
  35. DB::transaction(function () use ($date) {
  36. $spotlight = factory(Spotlight::class, 'monthly')->make([
  37. 'chart_month' => $date,
  38. ]);
  39. $spotlight->saveOrExplode();
  40. static::seedData($spotlight);
  41. });
  42. }
  43. public function seedBestOf($date)
  44. {
  45. DB::transaction(function () use ($date) {
  46. $spotlight = factory(Spotlight::class, 'bestof')->make([
  47. 'chart_month' => $date->copy()->endOfYear(),
  48. ]);
  49. $spotlight->saveOrExplode();
  50. static::seedData($spotlight);
  51. });
  52. }
  53. public function seedNonPeriodic()
  54. {
  55. DB::transaction(function () {
  56. $spotlight = factory(Spotlight::class)->make();
  57. $spotlight->saveOrExplode();
  58. static::seedData($spotlight);
  59. });
  60. }
  61. private static function seedData($spotlight)
  62. {
  63. DB::connection('mysql-charts')->transaction(function () use ($spotlight) {
  64. $spotlight->createTables();
  65. // users
  66. $users = User::orderByRaw('RAND()')->limit(1000)->get();
  67. foreach (Beatmap::MODES as $mode => $v) {
  68. // beatmaps
  69. $beatmaps = Beatmap::where('playmode', $v)->orderByRaw('RAND()')->limit(rand(4, 10))->get();
  70. $beatmapsetIds = array_unique($beatmaps->pluck('beatmapset_id')->toArray());
  71. DB::connection('mysql-charts')->table($spotlight->beatmapsetsTableName($mode))->insert(
  72. array_map(function ($id) {
  73. return ['beatmapset_id' => $id];
  74. }, $beatmapsetIds)
  75. );
  76. foreach ($users as $user) {
  77. // user_stats
  78. $stats = factory(UserStatisticsModel::getClass($mode))->make(['user_id' => $user->user_id]);
  79. $stats->setTable($spotlight->userStatsTableName($mode));
  80. $stats->save();
  81. // scores
  82. $scoresClass = ScoresBestModel::getClass($v);
  83. $possible_ranks = ['A', 'S', 'B', 'SH', 'XH', 'X'];
  84. foreach ($beatmaps as $beatmap) {
  85. $maxcombo = rand(1, $beatmap->countNormal);
  86. $score = new $scoresClass([
  87. 'user_id' => $user->user_id,
  88. 'beatmap_id' => $beatmap->beatmap_id,
  89. 'beatmapset_id' => $beatmap->beatmapset_id,
  90. 'score' => rand(50000, 100000000),
  91. 'maxcombo' => $maxcombo,
  92. 'rank' => array_rand_val($possible_ranks),
  93. 'count300' => round($maxcombo * 0.8),
  94. 'count100' => rand(0, round($maxcombo * 0.15)),
  95. 'count50' => rand(0, round($maxcombo * 0.05)),
  96. 'countgeki' => round($maxcombo * 0.3),
  97. 'countmiss' => round($maxcombo * 0.05),
  98. 'countkatu' => round($maxcombo * 0.05),
  99. 'date' => rand($spotlight->start_date->timestamp, $spotlight->end_date->timestamp),
  100. ]);
  101. $score->setConnection('mysql-charts');
  102. $score->setTable($spotlight->bestScoresTableName($mode));
  103. $score->save();
  104. }
  105. }
  106. }
  107. });
  108. }
  109. }