Skip to content

Commit

Permalink
Merge branch 'main' into fix/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw authored Nov 1, 2024
2 parents 044250d + b336c9d commit e82dcc2
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 152 deletions.
13 changes: 4 additions & 9 deletions app/Domains/Badges/UseCases/GetBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

namespace App\Domains\Badges\UseCases;

use App\Core\Domains\MinecraftUUID\Data\MinecraftUUID;
use App\Models\Badge;
use App\Models\MinecraftPlayer;
use Illuminate\Support\Collection;

final class GetBadges
{
public function execute(MinecraftUUID $uuid): array
public function execute(MinecraftPlayer $player): Collection
{
$player = MinecraftPlayer::whereUuid($uuid)->first();
if ($player === null) {
return [];
}

$account = $player->account;
if ($account === null) {
return [];
return collect();
}

$badges = $account->badges;
Expand All @@ -29,6 +24,6 @@ public function execute(MinecraftUUID $uuid): array
$badge->unicode_icon = '';
$badges->add($badge);

return $badges->toArray();
return $badges;
}
}
22 changes: 0 additions & 22 deletions app/Domains/Bans/UseCases/GetActivePlayerBan.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function execute(): void
return;
}

$donorGroup = Group::where('name', Group::DONOR_GROUP_NAME)->first();
$donorGroup = Group::whereDonor()->first();
if ($donorGroup === null) {
throw new \Exception('Could not find donor group');
}
Expand Down
41 changes: 0 additions & 41 deletions app/Domains/Donations/UseCases/GetDonationTiers.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Domains/Donations/UseCases/ProcessPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function execute(
throw new BadRequestException(id: 'invalid_quantity', message: 'Quantity purchased was zero');
}

$donorGroup = Group::where('name', Group::DONOR_GROUP_NAME)->first();
$donorGroup = Group::whereDonor()->first();
if ($donorGroup === null) {
throw new \Exception('Could not find donor group');
}
Expand Down
55 changes: 30 additions & 25 deletions app/Http/Controllers/Api/v2/Minecraft/MinecraftPlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Core\Domains\MinecraftUUID\Data\MinecraftUUID;
use App\Domains\Badges\UseCases\GetBadges;
use App\Domains\Bans\UseCases\GetActiveIPBan;
use App\Domains\Bans\UseCases\GetActivePlayerBan;
use App\Domains\Donations\UseCases\GetDonationTiers;
use App\Http\Controllers\ApiController;
use App\Models\GamePlayerBan;
use App\Models\Group;
use App\Models\MinecraftPlayer;
use Illuminate\Http\JsonResponse;
Expand All @@ -18,45 +18,50 @@ final class MinecraftPlayerController extends ApiController
public function __invoke(
Request $request,
MinecraftUUID $uuid,
GetActivePlayerBan $getBan,
GetBadges $getBadges,
GetDonationTiers $getDonationTier,
GetActiveIPBan $getActiveIPBan,
): JsonResponse {
$request->validate([
'ip' => 'ip',
]);

$ban = $getBan->execute(uuid: $uuid);
$badges = $getBadges->execute(uuid: $uuid);
$player = MinecraftPlayer::whereUuid($uuid)
->with(['account.groups', 'account.donationPerks.donationTier.group'])
->first();

$donationTiers = rescue(
callback: fn () => $getDonationTier->execute(uuid: $uuid),
rescue: [],
report: false,
);

$ipBan = null;
if ($request->has('ip')) {
$ipBan = $getActiveIPBan->execute(ip: $request->get('ip'));
}

$player = MinecraftPlayer::whereUuid($uuid)->first();
$player?->touchLastSyncedAt();

$account = $player?->account;

$donationTiers = optional($account, function ($account) {
return $account->donationPerks
->where('is_active', true)
->where('expires_at', '>', now())
->map(fn ($it) => $it->donationTier);
}) ?: collect();

$groups = $account?->groups ?: collect();
if ($account !== null && $account->groups->isEmpty()) {
// TODO: change this to model attribute
$account->groups->push(Group::whereDefault()->first());
$groups->add(Group::whereDefault()->first());
}

$donorGroups = $donationTiers->map(fn ($tier) => $tier->group);
if (!$donorGroups->isEmpty()) {
$groups = $groups->merge($donorGroups);
}

return response()->json([
'account' => $account,
'account' => $account?->withoutRelations(),
'player' => $player?->withoutRelations(),
'ban' => $ban,
'badges' => $badges,
'donation_tiers' => $donationTiers,
'ip_ban' => $ipBan,
'groups' => $groups,
'ban' => optional($player, function ($player) {
return GamePlayerBan::where('banned_player_id', $player->getKey())
->active()
->first();
}),
'badges' => optional($player, fn ($player) => $getBadges->execute($player)),
'ip_ban' => $request->has('ip')
? $getActiveIPBan->execute(ip: $request->get('ip'))
: null,
]);
}
}
1 change: 0 additions & 1 deletion app/Models/DonationPerk.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ final class DonationPerk extends Model implements LinkableAuditModel
'expires_at',
'created_at',
'updated_at',
'last_currency_reward_at',
];

protected $casts = [
Expand Down
10 changes: 10 additions & 0 deletions app/Models/DonationTier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Core\Utilities\Traits\HasStaticTable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class DonationTier extends Model implements LinkableAuditModel
{
Expand All @@ -26,6 +27,15 @@ final class DonationTier extends Model implements LinkableAuditModel

public $timestamps = false;

public function group(): BelongsTo
{
return $this->belongsTo(
related: Group::class,
foreignKey: 'group_id',
ownerKey: 'group_id',
);
}

public function getActivitySubjectLink(): ?string
{
return null;
Expand Down
8 changes: 5 additions & 3 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ final class Group extends Model implements LinkableAuditModel
use HasFactory;
use HasStaticTable;

public const DONOR_GROUP_NAME = 'donator';

protected $table = 'groups';

protected $primaryKey = 'group_id';
Expand All @@ -27,7 +25,6 @@ final class Group extends Model implements LinkableAuditModel
'is_default',
'is_staff',
'is_admin',
'discourse_name',
'minecraft_name',
'minecraft_display_name',
'minecraft_hover_text',
Expand Down Expand Up @@ -71,6 +68,11 @@ public function scopeWhereDefault(Builder $query)
$query->where('is_default', true);
}

public function scopeWhereDonor(Builder $query)
{
$query->where('name', 'donator');
}

public function getActivitySubjectLink(): ?string
{
return route('front.panel.groups.index').'#group-'.$this->getKey();
Expand Down
10 changes: 6 additions & 4 deletions database/factories/GroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function definition(): array
'is_default' => false,
'is_staff' => false,
'is_admin' => false,
'discourse_name' => $this->faker->randomLetter(),
'discord_name' => $this->faker->name(),
'minecraft_display_name' => $this->faker->name(),
'minecraft_hover_text' => $this->faker->name(),
];
}

Expand All @@ -36,7 +38,7 @@ public function member(): Factory
return $this->state(function (array $attributes) {
return [
'name' => 'member',
'discourse_name' => 'member',
'discord_name' => 'member',
'is_default' => true,
];
});
Expand All @@ -50,7 +52,7 @@ public function donor(): Factory
return $this->state(function (array $attributes) {
return [
'name' => 'donator',
'discourse_name' => 'donator',
'discord_name' => 'donator',
];
});
}
Expand All @@ -63,7 +65,7 @@ public function administrator(): Factory
return $this->state(function (array $attributes) {
return [
'name' => 'Administrator',
'discourse_name' => 'administrator',
'discord_name' => 'administrator',
'is_staff' => true,
'is_admin' => true,
'can_access_panel' => true,
Expand Down
7 changes: 4 additions & 3 deletions database/factories/MinecraftConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public function definition(): array
{
return [
'version' => 1,
'config' => json_encode([
'config' => [
'localization' => [
'time_zone' => 'UTC',
'locale' => 'en-us',
],
'chat' => [
'badge_icon' => '<color:yellow>★</color>',
'badge_icon' => '<gold>★</gold>',
'staff_channel' => '<yellow>(Staff) <name>:</yellow> <message>',
],
'warps' => [
'items_per_page' => 15,
Expand All @@ -50,7 +51,7 @@ public function definition(): array
'first_time_join' => '<light_purple>✦ Welcome <white><name></white> <light_purple>to the server!',
'welcome' => '<gray>Welcome to </gray><white><bold>PROJECT </bold></white><gold><bold>CITY </bold></gold><blue><bold>BUILD</bold></blue><newline><gray>| </gray><newline><gray>| </gray><white>Type <red><bold>/menu </bold></red><white>to access most server features, including rank<newline><gray>| </gray><white>applications, warps, player reporting, and other information.<newline><gray>| </gray><gray>Hold down the <red><bold>TAB </bold><gray>key to see who else is online.<newline><gray>| </gray><white>Ask our staff if you have any questions.</white>',
],
]),
],
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\MinecraftWarp;

class WarpFactory extends Factory
class MinecraftWarpFactory extends Factory
{
protected $model = MinecraftWarp::class;

Expand Down
56 changes: 56 additions & 0 deletions database/migrations/2024_10_30_110257_add_minecraft_group_cols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Models\DonationTier;
use App\Models\Group;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('groups', function (Blueprint $table) {
$table->dropColumn('discourse_name');
$table->integer('display_priority')->nullable()->after('minecraft_name');
$table->string('minecraft_hover_text')->after('minecraft_name');
$table->string('minecraft_display_name')->after('minecraft_name');
$table->string('group_type')->nullable();
});

Schema::table('donation_perks', function (Blueprint $table) {
$table->dropColumn('last_currency_reward_at');
});

Schema::table('donation_tiers', function (Blueprint $table) {
$table->dropColumn('currency_reward');
$table->unsignedInteger('group_id')->nullable();

$table->foreign('group_id')->references('group_id')->on('groups');
});

$group = Group::first();
if ($group === null) {
$group = Group::factory()->create();
}
DonationTier::get()->each(function ($tier) use($group) {
$tier->group_id = $group->getKey();
$tier->save();
});

Schema::table('donation_tiers', function (Blueprint $table) {
$table->unsignedInteger('group_id')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Loading

0 comments on commit e82dcc2

Please sign in to comment.