Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(LeaderboardDef): drop Author #2459

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Helpers/database/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ function getGamesListByDev(
$query = "SELECT GameID, COUNT(*) AS NumLBs
FROM LeaderboardDef
WHERE GameID IN ($gameList)
AND Author = :author
AND author_id = :authorId
GROUP BY GameID";
foreach (legacyDBFetchAll($query, ['author' => $dev]) as $row) {
foreach (legacyDBFetchAll($query, ['authorId' => $dev->id]) as $row) {
$games[$row['GameID']]['MyLBs'] = $row['NumLBs'];
}
}
Expand Down
100 changes: 23 additions & 77 deletions app/Helpers/database/leaderboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ function SubmitNewLeaderboard(int $gameID, ?int &$lbIDOut, User $user): bool
}

$defaultMem = "STA:0x0000=h0010_0xhf601=h0c::CAN:0xhfe13<d0xhfe13::SUB:0xf7cc!=0_d0xf7cc=0::VAL:0xhfe24*1_0xhfe25*60_0xhfe22*3600";
$query = "INSERT INTO LeaderboardDef (GameID, Mem, Format, Title, Description, LowerIsBetter, DisplayOrder, Author, author_id, Created)
$query = "INSERT INTO LeaderboardDef (GameID, Mem, Format, Title, Description, LowerIsBetter, DisplayOrder, author_id, Created)
VALUES ($gameID, '$defaultMem', 'SCORE', 'My Leaderboard', 'My Leaderboard Description', 0,
(SELECT * FROM (SELECT COALESCE(Max(DisplayOrder) + 1, 0) FROM LeaderboardDef WHERE GameID = $gameID) AS temp), '{$user->User}', {$user->id}, NOW())";
(SELECT * FROM (SELECT COALESCE(Max(DisplayOrder) + 1, 0) FROM LeaderboardDef WHERE GameID = $gameID) AS temp), {$user->id}, NOW())";

$dbResult = s_mysql_query($query);
if ($dbResult !== false) {
Expand All @@ -477,7 +477,7 @@ function SubmitNewLeaderboard(int $gameID, ?int &$lbIDOut, User $user): bool
}

function UploadNewLeaderboard(
string $author,
string $authorUsername,
int $gameID,
string $title,
string $desc,
Expand All @@ -488,30 +488,32 @@ function UploadNewLeaderboard(
?string &$errorOut
): bool {
$displayOrder = 0;
$originalAuthor = '';
$originalAuthor = null;

if ($idInOut > 0) {
$query = "SELECT DisplayOrder, Author FROM LeaderboardDef WHERE ID='$idInOut'";
$dbResult = s_mysql_query($query);
if ($dbResult !== false && mysqli_num_rows($dbResult) == 1) {
$data = mysqli_fetch_assoc($dbResult);
$displayOrder = $data['DisplayOrder'];
$originalAuthor = $data['Author'] ?? "Unknown";
$displayOrder = (int) $displayOrder;
$foundLeaderboard = Leaderboard::find($idInOut);
if ($foundLeaderboard) {
$displayOrder = $foundLeaderboard->order_column;
$originalAuthor = $foundLeaderboard->authorUser;

$data['DisplayOrder'] = $displayOrder;
$data['Author'] = $originalAuthor?->display_name ?? "Unknown";
} else {
$errorOut = "Unknown leaderboard";

return false;
}
}

$authorModel = User::firstWhere('User', $author);
$authorModel = User::firstWhere('User', $authorUsername);

// Prevent non-developers from uploading or modifying leaderboards
$userPermissions = (int) $authorModel->getAttribute('Permissions');
if ($userPermissions < Permissions::Developer) {
if ($userPermissions < Permissions::JuniorDeveloper
|| (!empty($originalAuthor) && $author !== $originalAuthor)) {
if (
$userPermissions < Permissions::JuniorDeveloper
|| (isset($originalAuthor) && !$authorModel->is($originalAuthor))
) {
$errorOut = "You must be a developer to perform this action! Please drop a message in the forums to apply.";

return false;
Expand All @@ -537,84 +539,28 @@ function UploadNewLeaderboard(
return false;
}

$query = "SELECT DisplayOrder FROM LeaderboardDef WHERE ID='$idInOut'";
$dbResult = s_mysql_query($query);
if ($dbResult !== false && mysqli_num_rows($dbResult) == 1) {
$data = mysqli_fetch_assoc($dbResult);
$displayOrder = $data['DisplayOrder'];
$displayOrder = (int) $displayOrder;
$foundLeaderboard = Leaderboard::find($idInOut);
if ($foundLeaderboard) {
$displayOrder = $foundLeaderboard->order_column;
$data['DisplayOrder'] = $displayOrder;
}
}

if (!submitLBData($author, $idInOut, $mem, $title, $desc, $format, $lowerIsBetter, $displayOrder)) {
if (!submitLBData($authorUsername, $idInOut, $mem, $title, $desc, $format, $lowerIsBetter, $displayOrder)) {
$errorOut = "Internal error updating leaderboard.";

return false;
}

if ($originalAuthor != '') {
if (isset($originalAuthor)) {
addArticleComment("Server", ArticleType::Leaderboard, $idInOut,
"$author edited this leaderboard.", $author
"{$authorModel->display_name} edited this leaderboard.", $authorModel->username
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it matters, but edits from Connect use this message
image

And edits from the webpage use this one
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a huge deal if our long-term goal is to move over to the activitylog, but no harm in getting these consistent. Changed the webpage edits to match "edited this leaderboard".

);
}

return true;
}

/**
* Duplicates a leaderboard a specified number of times.
*/
function duplicateLeaderboard(int $gameID, int $leaderboardID, int $duplicateNumber, string $user): bool
{
if ($gameID == 0) {
return false;
}

// Get the leaderboard info to duplicate
$getQuery = "
SELECT Mem,
Format,
Title,
Description,
LowerIsBetter,
(SELECT Max(DisplayOrder) FROM LeaderboardDef WHERE GameID = $gameID) AS DisplayOrder
FROM LeaderboardDef
WHERE ID = $leaderboardID";

$dbResult = s_mysql_query($getQuery);
if (!$dbResult) {
return false;
}

$db_entry = mysqli_fetch_assoc($dbResult);

if (empty($db_entry)) {
return false;
}

$lbMem = $db_entry['Mem'];
$lbFormat = $db_entry['Format'];
$lbTitle = $db_entry['Title'];
$lbDescription = $db_entry['Description'];
$lbScoreType = $db_entry['LowerIsBetter'];
$lbDisplayOrder = $db_entry['DisplayOrder'];

// Create the duplicate entries
for ($i = 1; $i <= $duplicateNumber; $i++) {
$query = "INSERT INTO LeaderboardDef (GameID, Mem, Format, Title, Description, LowerIsBetter, DisplayOrder, Author, Created)
VALUES ($gameID, '$lbMem', '$lbFormat', '$lbTitle', '$lbDescription', $lbScoreType, ($lbDisplayOrder + $i), '$user', NOW())";
$dbResult = s_mysql_query($query);
if ($dbResult !== false) {
$db = getMysqliConnection();
mysqli_insert_id($db);
} else {
return false;
}
}

return true;
}

function requestResetLB(int $lbID): bool
{
$entries = LeaderboardEntry::where('leaderboard_id', $lbID);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Leaderboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Leaderboard extends BaseModel
// TODO rename Created column to created_at, set to non-nullable, remove getCreatedAtAttribute()
// TODO rename Updated column to updated_at, set to non-nullable, remove getUpdatedAtAttribute()
// TODO drop Mem, migrate to triggerable morph
// TODO drop Author and author_id, migrate to triggerable morph author
// TODO drop author_id, migrate to triggerable morph author
protected $table = 'LeaderboardDef';

protected $primaryKey = 'ID';
Expand Down
3 changes: 1 addition & 2 deletions database/factories/LeaderboardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function definition(): array
'Title' => ucwords(fake()->words(2, true)),
'Description' => fake()->sentence(),
'Mem' => 'STA:0x000000=0::CAN:0x000001=2::SUB:0x000001=1::VAL:0x000002',
'Author' => $author->User,
'author_id' => $author->id,
'author_id' => $author?->id ?? 1,
'Format' => 'VALUE',
'LowerIsBetter' => 0,
'DisplayOrder' => rand(0, 500),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table('LeaderboardDef', function (Blueprint $table) {
$table->dropColumn('Author');
});
}

public function down(): void
{
Schema::table('LeaderboardDef', function (Blueprint $table) {
$table->string('Author', 32)->after('DisplayOrder');
});
}
};
34 changes: 0 additions & 34 deletions public/request/leaderboard/create.php

This file was deleted.

Loading