the browser-facing portion of osu!
at master 117 lines 3.4 kB view raw
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 6namespace App\Transformers\Multiplayer; 7 8use App\Models\Multiplayer\Room; 9use App\Models\Multiplayer\UserScoreAggregate; 10use App\Transformers\TransformerAbstract; 11use App\Transformers\UserCompactTransformer; 12use Carbon\Carbon; 13 14class RoomTransformer extends TransformerAbstract 15{ 16 protected array $availableIncludes = [ 17 'current_playlist_item', 18 'current_user_score', 19 'difficulty_range', 20 'host', 21 'playlist', 22 'playlist_item_stats', 23 'recent_participants', 24 ]; 25 26 public static function createShowResponse(Room $room): array 27 { 28 return json_item( 29 $room->loadMissing([ 30 'host', 31 'playlist.beatmap.baseMaxCombo', 32 'playlist.beatmap.beatmapset', 33 ]), 34 new static(), 35 [ 36 'current_user_score.playlist_item_attempts', 37 'host.country', 38 'playlist.beatmap.beatmapset', 39 'playlist.beatmap.checksum', 40 'playlist.beatmap.max_combo', 41 'recent_participants', 42 ], 43 ); 44 } 45 46 public function transform(Room $room) 47 { 48 return [ 49 'id' => $room->id, 50 'name' => $room->name, 51 'category' => $room->category, 52 'status' => $room->status, 53 'type' => $room->type, 54 'user_id' => $room->user_id, 55 'starts_at' => json_time($room->starts_at), 56 'ends_at' => json_time($room->ends_at), 57 'max_attempts' => $room->max_attempts, 58 'participant_count' => $room->participant_count, 59 'channel_id' => $room->channel_id, 60 'active' => $room->ends_at === null || Carbon::now()->between($room->starts_at, $room->ends_at), 61 'has_password' => $room->password !== null, 62 'queue_mode' => $room->queue_mode, 63 'auto_skip' => $room->auto_skip, 64 ]; 65 } 66 67 public function includeCurrentPlaylistItem(Room $room) 68 { 69 return $room->currentPlaylistItem === null 70 ? $this->null() 71 : $this->item($room->currentPlaylistItem, new PlaylistItemTransformer()); 72 } 73 74 public function includeCurrentUserScore(Room $room) 75 { 76 $user = auth()->user(); 77 78 if ($user === null) { 79 return; 80 } 81 82 $score = UserScoreAggregate::lookupOrDefault($user, $room); 83 84 return $this->item($score, new UserScoreAggregateTransformer()); 85 } 86 87 public function includeDifficultyRange(Room $room) 88 { 89 return $this->primitive($room->difficultyRange()); 90 } 91 92 public function includeHost(Room $room) 93 { 94 return $this->item( 95 $room->host, 96 new UserCompactTransformer() 97 ); 98 } 99 100 public function includeRecentParticipants(Room $room) 101 { 102 return $this->collection($room->recentParticipants(), new UserCompactTransformer()); 103 } 104 105 public function includePlaylist(Room $room) 106 { 107 return $this->collection( 108 $room->playlist, 109 new PlaylistItemTransformer() 110 ); 111 } 112 113 public function includePlaylistItemStats(Room $room) 114 { 115 return $this->primitive($room->playlistItemStats()); 116 } 117}