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

feat(achievement): credit users for logic, art, writing, and testing #2683

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
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
48 changes: 48 additions & 0 deletions app/Filament/Resources/AchievementAuthorshipCreditFormSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Models\User;
use App\Platform\Enums\AchievementAuthorTask;
use Filament\Forms;

class AchievementAuthorshipCreditFormSchema
{
public static function getSchema(): array
{
return [
Forms\Components\Select::make('task')
->options(
collect(AchievementAuthorTask::cases())
->mapWithKeys(fn ($enum) => [$enum->value => $enum->label()])
)
->required(),

Forms\Components\Select::make('user_id')
->label('User')
->searchable()
->getSearchResultsUsing(function (string $search): array {
$lowercased = strtolower($search);

return User::whereRaw('LOWER(User) = ?', [$lowercased])
->orWhere(function ($query) use ($lowercased) {
$query->whereRaw('LOWER(display_name) like ?', ["%{$lowercased}%"])
->orWhereRaw('LOWER(User) like ?', ["%{$lowercased}%"]);
})
->orderByRaw('LOWER(User) = ? DESC', [$lowercased])
->limit(50)
->get()
->pluck('display_name', 'id')
->toArray();
})
->getOptionLabelUsing(fn (int $value): string => User::find($value)?->display_name ?? 'Deleted User')
->required(),

Forms\Components\DateTimePicker::make('created_at')
->label('Date Credited')
->default(now()),
];
}
}
22 changes: 19 additions & 3 deletions app/Filament/Resources/AchievementResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Filament\Extensions\Resources\Resource;
use App\Filament\Resources\AchievementResource\Pages;
use App\Filament\Resources\AchievementResource\RelationManagers\AuthorshipCreditsRelationManager;
use App\Models\Achievement;
use App\Models\Game;
use App\Models\User;
Expand Down Expand Up @@ -79,17 +80,22 @@ public static function infolist(Infolist $infolist): Infolist
->label('Badge (locked)')
->size(config('media.icon.lg.width')),
]),

Infolists\Components\Group::make()
->schema([
Infolists\Components\TextEntry::make('Title'),

Infolists\Components\TextEntry::make('Description'),

Infolists\Components\TextEntry::make('game')
->label('Game')
->formatStateUsing(fn (Game $state) => '[' . $state->id . '] ' . $state->title),
Infolists\Components\TextEntry::make('user')

Infolists\Components\TextEntry::make('developer')
->label('Author')
->formatStateUsing(fn (User $state) => $state->User),
->formatStateUsing(fn (User $state) => $state->display_name),
]),

Infolists\Components\Group::make()
->schema([
Infolists\Components\TextEntry::make('canonical_url')
Expand All @@ -99,21 +105,26 @@ public static function infolist(Infolist $infolist): Infolist
->url(fn (Achievement $record): string => $record->getPermalinkAttribute()),
]),
]),

Infolists\Components\Section::make([
Infolists\Components\TextEntry::make('id')
->label('ID'),

Infolists\Components\TextEntry::make('Created')
->label('Created at')
->dateTime()
->hidden(fn ($state) => !$state),

Infolists\Components\TextEntry::make('DateModified')
->label('Modified at')
->dateTime()
->hidden(fn ($state) => !$state),

Infolists\Components\TextEntry::make('Updated')
->label('Updated at')
->dateTime()
->hidden(fn ($state) => !$state),

Infolists\Components\TextEntry::make('Flags')
->badge()
->formatStateUsing(fn (int $state): string => match ($state) {
Expand All @@ -126,9 +137,12 @@ public static function infolist(Infolist $infolist): Infolist
AchievementFlag::Unofficial => 'info',
default => '',
}),

Infolists\Components\TextEntry::make('type')
->badge(),

Infolists\Components\TextEntry::make('Points'),

Infolists\Components\TextEntry::make('DisplayOrder'),
])->grow(false),
])->from('md'),
Expand Down Expand Up @@ -372,7 +386,9 @@ public static function table(Table $table): Table

public static function getRelations(): array
{
return [];
return [
AuthorshipCreditsRelationManager::class,
];
}

public static function getRecordSubNavigation(Page $page): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\AchievementResource\RelationManagers;

use App\Filament\Resources\AchievementAuthorshipCreditFormSchema;
use App\Models\Achievement;
use App\Models\AchievementAuthor;
use App\Platform\Enums\AchievementAuthorTask;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class AuthorshipCreditsRelationManager extends RelationManager
{
protected static string $relationship = 'authorshipCredits';

protected static ?string $title = 'Credits';

public function form(Form $form): Form
{
return $form
->schema(AchievementAuthorshipCreditFormSchema::getSchema());
}

public function table(Table $table): Table
{
$earliestLogicCredit = AchievementAuthor::where('achievement_id', $this->ownerRecord->id)
->where('task', AchievementAuthorTask::Logic->value)
->orderBy('created_at', 'asc')
->first();

return $table
->columns([
Tables\Columns\TextColumn::make('user.display_name')
->label('User')
->url(fn (AchievementAuthor $record): ?string => $record->user ? route('user.show', ['user' => $record->user]) : null),

Tables\Columns\TextColumn::make('task')
->label('Task')
->formatStateUsing(fn ($state) => AchievementAuthorTask::tryFrom($state)?->label() ?? ucfirst($state)),

Tables\Columns\TextColumn::make('created_at')
->label('Date Credited'),
])
->filters([

])
->headerActions([
Tables\Actions\CreateAction::make()
->label('Add achievement credit')
->modalHeading('Add achievement credit')
->using(function (array $data, string $model): Model {
/** @var Achievement $achievement */
$achievement = $this->ownerRecord;

$task = $data['task'];

/**
* An achievement developer doing most tasks is implied. If someone
* new contributes, we'll backfill a credit record for the achievement
* developer.
*
* For example, if someone creates a new badge for the achievement,
* we'll credit the achievement developer for art, then credit the
* new person for art.
*/
$developer = $achievement->developer;
if ($developer) {
$doesDeveloperCreditExist = AchievementAuthor::where('achievement_id', $achievement->id)
->where('user_id', $developer->id)
->where('task', $task)
->exists();

if (!$doesDeveloperCreditExist) {
$backdate = null;

if ($task === AchievementAuthorTask::Artwork->value) {
$firstBadgeComment = $achievement->legacyComments()
->automated()
->where('Payload', 'LIKE', "{$developer->display_name}%")
->where('Payload', 'LIKE', "%badge%")
->first();

if ($firstBadgeComment) {
$backdate = $firstBadgeComment->Submitted;
}
} elseif ($task === AchievementAuthorTask::Writing->value) {
$firstWritingComment = $achievement->legacyComments()
->automated()
->where('Payload', 'LIKE', "{$developer->display_name}%")
->where(function ($query) {
$query->where('Payload', 'LIKE', "%title%")
->orWhere('Payload', 'LIKE', "%description%");
})
->first();

if ($firstWritingComment) {
$backdate = $firstWritingComment->Submitted;
}
}

AchievementAuthor::create([
'user_id' => $developer->id,
'achievement_id' => $achievement->id,
'task' => $task,
'created_at' => $backdate ?? $achievement->DateCreated,
]);
}
}

return AchievementAuthor::create([
'user_id' => (int) $data['user_id'],
'achievement_id' => $achievement->id,
'task' => $task,
]);
}),
])
->actions([
Tables\Actions\EditAction::make()
->modalHeading('Edit achievement credit'),

Tables\Actions\DeleteAction::make()
->modalHeading('Delete achievement credit')
->hidden(fn (AchievementAuthor $record) => $earliestLogicCredit && $earliestLogicCredit->id === $record->id),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Loading