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\Console\Commands;
9
10use App\Models\Multiplayer\DailyChallengeQueueItem;
11use App\Models\Multiplayer\Room;
12use App\Models\User;
13use Illuminate\Console\Command;
14use Illuminate\Support\Facades\DB;
15
16class DailyChallengeCreateNext extends Command
17{
18 protected $description = 'Creates a new "daily challenge" multiplayer room for the first item in queue.';
19
20 protected $signature = 'daily-challenge:create-next';
21
22 public function handle()
23 {
24 $nextQueueItem = DailyChallengeQueueItem::whereNull('multiplayer_room_id')
25 ->orderBy('order', 'asc')
26 ->orderBy('id', 'asc')
27 ->first();
28
29 if ($nextQueueItem === null) {
30 $this->error('"Daily challenge" queue is empty.');
31 return 1;
32 }
33
34 $existing = Room::where('category', 'daily_challenge')->active()->first();
35 if ($existing !== null) {
36 $this->error("Another \"daily challenge\" room is open (id {$existing->getKey()}).");
37 return 1;
38 }
39
40 DB::transaction(function () use ($nextQueueItem) {
41 // matches cron schedule
42 $startTime = today()->addMinutes(5);
43 $hostId = $GLOBALS['cfg']['osu']['legacy']['bancho_bot_user_id'];
44
45 $room = (new Room())->startGame(
46 User::findOrFail($hostId),
47 [
48 'ends_at' => today()->addDay(),
49 'name' => "Daily Challenge: {$startTime->toFormattedDateString()}",
50 'type' => 'playlists',
51 'playlist' => [[
52 'beatmap_id' => $nextQueueItem->beatmap_id,
53 'ruleset_id' => $nextQueueItem->ruleset_id,
54 'allowed_mods' => $nextQueueItem->allowed_mods,
55 'required_mods' => $nextQueueItem->required_mods,
56 ]],
57 ],
58 [
59 'starts_at' => $startTime,
60 ],
61 );
62 $room->update(['category' => 'daily_challenge']);
63
64 $nextQueueItem->multiplayer_room_id = $room->getKey();
65 $nextQueueItem->save();
66
67 $this->info("Created room {$room->name} (id {$room->getKey()})");
68 });
69 }
70}