Skip to content

Commit

Permalink
Merge branch 'master' into team-disband-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko authored Jan 23, 2025
2 parents 72a5d0e + 89b2f71 commit 02bd1e7
Show file tree
Hide file tree
Showing 28 changed files with 523 additions and 192 deletions.
12 changes: 6 additions & 6 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\NewsPost;
use App\Models\UserDonation;
use App\Transformers\MenuImageTransformer;
use App\Transformers\UserCompactTransformer;
use Auth;
use Jenssegers\Agent\Agent;
use Request;
Expand Down Expand Up @@ -142,12 +143,11 @@ public function quickSearch()
$result[$mode]['total'] = $search->count();
}

$result['user']['users'] = json_collection($searches['user']->data(), 'UserCompact', [
'country',
'cover',
'groups',
'support_level',
]);
$result['user']['users'] = json_collection(
$searches['user']->data(),
new UserCompactTransformer(),
[...UserCompactTransformer::CARD_INCLUDES, 'support_level'],
);
$result['beatmapset']['beatmapsets'] = json_collection($searches['beatmapset']->data(), 'Beatmapset', ['beatmaps']);
}

Expand Down
35 changes: 35 additions & 0 deletions app/Http/Controllers/InterOp/Multiplayer/RoomsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers\InterOp\Multiplayer;

use App\Http\Controllers\Controller;
use App\Models\Multiplayer\Room;
use App\Models\User;
use App\Transformers\Multiplayer\RoomTransformer;

class RoomsController extends Controller
{
public function join(string $id, string $userId)
{
$user = User::findOrFail($userId);
$room = Room::findOrFail($id);

$room->assertCorrectPassword(get_string(request('password')));
$room->join($user);

return RoomTransformer::createShowResponse($room);
}

public function store()
{
$params = \Request::all();
$user = User::findOrFail(get_int($params['user_id'] ?? null));

$room = (new Room())->startGame($user, $params);

return RoomTransformer::createShowResponse($room);
}
}
47 changes: 8 additions & 39 deletions app/Http/Controllers/Multiplayer/RoomsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace App\Http\Controllers\Multiplayer;

use App\Exceptions\InvariantException;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Ranking\DailyChallengeController;
use App\Models\Model;
Expand Down Expand Up @@ -101,24 +100,18 @@ public function index()

public function join($roomId, $userId)
{
$currentUser = \Auth::user();
// this allows admins/whatever to add users to games in the future
if (get_int($userId) !== auth()->user()->user_id) {
if (get_int($userId) !== $currentUser->getKey()) {
abort(403);
}

$room = Room::findOrFail($roomId);
$room->assertCorrectPassword(get_string(request('password')));

if ($room->password !== null) {
$password = get_param_value(request('password'), null);

if ($password === null || !hash_equals(hash('sha256', $room->password), hash('sha256', $password))) {
abort(403, osu_trans('multiplayer.room.invalid_password'));
}
}

$room->join(auth()->user());
$room->join($currentUser);

return $this->createJoinedRoomResponse($room);
return RoomTransformer::createShowResponse($room);
}

public function leaderboard($roomId)
Expand Down Expand Up @@ -168,7 +161,7 @@ public function show($id)
}

if (is_api_request()) {
return $this->createJoinedRoomResponse($room);
return RoomTransformer::createShowResponse($room);
}

if ($room->category === 'daily_challenge') {
Expand Down Expand Up @@ -200,32 +193,8 @@ public function show($id)

public function store()
{
try {
$room = (new Room())->startGame(auth()->user(), request()->all());

return $this->createJoinedRoomResponse($room);
} catch (InvariantException $e) {
return error_popup($e->getMessage(), $e->getStatusCode());
}
}
$room = (new Room())->startGame(\Auth::user(), \Request::all());

private function createJoinedRoomResponse($room)
{
return json_item(
$room->loadMissing([
'host',
'playlist.beatmap.beatmapset',
'playlist.beatmap.baseMaxCombo',
]),
'Multiplayer\Room',
[
'current_user_score.playlist_item_attempts',
'host.country',
'playlist.beatmap.beatmapset',
'playlist.beatmap.checksum',
'playlist.beatmap.max_combo',
'recent_participants',
]
);
return RoomTransformer::createShowResponse($room);
}
}
57 changes: 32 additions & 25 deletions app/Models/ChangelogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class ChangelogEntry extends Model

public static function convertLegacy($changelog)
{
$message = $changelog->message;
$splitMessage = static::splitMessage($message);
$title = $splitMessage[0];
$splitMessage = static::splitMessage($changelog->message);
$title = presence($splitMessage[0]);
$message = $splitMessage[1];

if ($title === null) {
$title = $splitMessage[1];
Expand All @@ -65,6 +65,13 @@ public static function convertLegacy($changelog)
]);
}

public static function getDisplayMessage(?string $origMessage): string
{
$split = static::splitMessage($origMessage);

return $split[1] === null ? '' : $split[0];
}

public static function guessCategory($data)
{
static $ignored = [
Expand Down Expand Up @@ -125,7 +132,7 @@ public static function importFromGithub($data)
'category' => static::guessCategory($data),
'created_at' => Carbon::parse($data['pull_request']['merged_at']),
'github_pull_request_id' => $data['pull_request']['number'],
'message' => $data['pull_request']['body'],
'message' => static::getDisplayMessage($data['pull_request']['body']),
'private' => static::isPrivate($data),
'title' => $data['pull_request']['title'],
'type' => static::guessType($data),
Expand Down Expand Up @@ -180,21 +187,30 @@ public static function placeholder()
]);
}

public static function splitMessage($message)
/**
* Returns array of message split by thematic break (`---`)
*
* The array length is always two.
* If the message is empty, both values will be null.
* If there's no thematic break, the second value will be null.
*/
public static function splitMessage($message): array
{
if (!present($message)) {
return [null, null];
}

static $separator = "\n\n---\n";
// prepended with \n\n just in case the message starts with ---\n (blank first part).
$message = "\n\n".trim(str_replace("\r\n", "\n", $message));
$splitPos = null_if_false(strpos($message, $separator)) ?? strlen($message);

return [
presence(trim(substr($message, 0, $splitPos))),
presence(trim(substr($message, $splitPos + strlen($separator)))),
];
// Surround with newlines to handle separator at the start/end.
$message = "\n\n".trim(strtr($message, ["\r\n" => "\n"]))."\n";
$splitPos = strpos($message, $separator);

return $splitPos === false
? [trim($message), null]
: [
trim(substr($message, 0, $splitPos)),
trim(substr($message, $splitPos + strlen($separator))),
];
}

public function builds()
Expand Down Expand Up @@ -253,21 +269,12 @@ public function githubUrl()
}
}

public function publicMessageHtml()
public function messageHtml(): ?string
{
return $this->memoize(__FUNCTION__, function () {
$message = $this->publicMessage();
$message = $this->message;

if ($message !== null) {
return markdown($message, 'changelog_entry');
}
});
}

public function publicMessage()
{
return $this->memoize(__FUNCTION__, function () {
return static::splitMessage($this->message)[1];
return present($message) ? markdown($message, 'changelog_entry') : null;
});
}
}
12 changes: 12 additions & 0 deletions app/Models/Multiplayer/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace App\Models\Multiplayer;

use App\Casts\PresentString;
use App\Exceptions\AuthorizationException;
use App\Exceptions\InvariantException;
use App\Models\Beatmap;
use App\Models\Chat\Channel;
Expand Down Expand Up @@ -332,6 +333,17 @@ public function scopeWithRecentParticipantIds($query, ?int $limit = null)
", 'recent_participant_ids');
}

public function assertCorrectPassword(?string $password): void
{
if ($this->password === null) {
return;
}

if ($password === null || !hash_equals(hash('sha256', $this->password), hash('sha256', $password))) {
throw new AuthorizationException(osu_trans('multiplayer.room.invalid_password'));
}
}

public function difficultyRange()
{
$extraQuery = true;
Expand Down
31 changes: 15 additions & 16 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,24 +317,23 @@ public function getAuthPassword()

public function usernameChangeCost()
{
$changesToDate = $this->usernameChangeHistory()
->whereIn('type', ['support', 'paid'])
->count();
$tier = min($this->usernameChangeHistory()->paid()->count(), 5);

switch ($changesToDate) {
case 0:
return 0;
case 1:
return 8;
case 2:
return 16;
case 3:
return 32;
case 4:
return 64;
default:
return 100;
if ($tier > 1) {
$lastChange = $this->usernameChangeHistory()->paid()->last()?->timestamp;
if ($lastChange !== null) {
$tier = max($tier - $lastChange->diffInYears(Carbon::now(), false), 1);
}
}

return match ($tier) {
0 => 0,
1 => 8,
2 => 16,
3 => 32,
4 => 64,
default => 100,
};
}

public function revertUsername($type = 'revert'): UsernameChangeHistory
Expand Down
5 changes: 5 additions & 0 deletions app/Models/UsernameChangeHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class UsernameChangeHistory extends Model
protected $table = 'osu_username_change_history';
protected $primaryKey = 'change_id';

public function scopePaid($query)
{
$query->whereIn('type', ['support', 'paid']); // changed by support counts as paid.
}

public function scopeVisible($query)
{
$query->whereIn('type', ['support', 'paid', 'admin']);
Expand Down
4 changes: 2 additions & 2 deletions app/Transformers/ChangelogEntryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function includeGithubUser(ChangelogEntry $entry)

public function includeMessage(ChangelogEntry $entry)
{
return $this->primitive($entry->publicMessage());
return $this->primitive($entry->message);
}

public function includeMessageHtml(ChangelogEntry $entry)
{
return $this->primitive($entry->publicMessageHtml());
return $this->primitive($entry->messageHtml());
}
}
20 changes: 20 additions & 0 deletions app/Transformers/Multiplayer/RoomTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ class RoomTransformer extends TransformerAbstract
'recent_participants',
];

public static function createShowResponse(Room $room): array
{
return json_item(
$room->loadMissing([
'host',
'playlist.beatmap.baseMaxCombo',
'playlist.beatmap.beatmapset',
]),
new static(),
[
'current_user_score.playlist_item_attempts',
'host.country',
'playlist.beatmap.beatmapset',
'playlist.beatmap.checksum',
'playlist.beatmap.max_combo',
'recent_participants',
],
);
}

public function transform(Room $room)
{
return [
Expand Down
Loading

0 comments on commit 02bd1e7

Please sign in to comment.