diff --git a/app/Filament/Resources/System/MedalResource.php b/app/Filament/Resources/System/MedalResource.php
index 73a316e4..a621ef9a 100644
--- a/app/Filament/Resources/System/MedalResource.php
+++ b/app/Filament/Resources/System/MedalResource.php
@@ -76,6 +76,12 @@ class MedalResource extends Resource
->numeric()
->default(0)
,
+ Forms\Components\TextInput::make('gift_fee_factor')
+ ->label(__('medal.fields.gift_fee_factor'))
+ ->helperText(__('medal.fields.gift_fee_factor_help'))
+ ->numeric()
+ ->default(0)
+ ,
Forms\Components\Textarea::make('description')
->label(__('label.description'))
,
@@ -96,6 +102,7 @@ class MedalResource extends Resource
->formatStateUsing(fn ($record) => new HtmlString(sprintf('%s ~
%s', $record->sale_begin_time ?? '--', $record->sale_end_time ?? '--')))
,
Tables\Columns\TextColumn::make('bonus_addition_factor')->label(__('medal.fields.bonus_addition_factor')),
+ Tables\Columns\TextColumn::make('gift_fee_factor')->label(__('medal.fields.gift_fee_factor')),
Tables\Columns\TextColumn::make('price')->label(__('label.price')),
Tables\Columns\TextColumn::make('duration')->label(__('label.medal.duration')),
diff --git a/app/Models/BonusLogs.php b/app/Models/BonusLogs.php
index 588b09f1..37109ce4 100644
--- a/app/Models/BonusLogs.php
+++ b/app/Models/BonusLogs.php
@@ -32,6 +32,7 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_BUY_TEMPORARY_INVITE = 15;
const BUSINESS_TYPE_BUY_RAINBOW_ID = 16;
const BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD = 17;
+ const BUSINESS_TYPE_GIFT_MEDAL = 18;
public static array $businessTypes = [
self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'],
@@ -51,6 +52,7 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_BUY_TEMPORARY_INVITE => ['text' => 'Buy temporary invite'],
self::BUSINESS_TYPE_BUY_RAINBOW_ID => ['text' => 'Buy rainbow ID'],
self::BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD => ['text' => 'Buy change username card'],
+ self::BUSINESS_TYPE_GIFT_MEDAL => ['text' => 'Gift medal to someone'],
];
public static function getBonusForCancelHitAndRun()
diff --git a/app/Models/Medal.php b/app/Models/Medal.php
index a76dfca7..c72faa33 100644
--- a/app/Models/Medal.php
+++ b/app/Models/Medal.php
@@ -18,6 +18,7 @@ class Medal extends NexusModel
protected $fillable = [
'name', 'description', 'image_large', 'image_small', 'price', 'duration', 'get_type',
'display_on_medal_page', 'sale_begin_time', 'sale_end_time', 'inventory', 'bonus_addition_factor',
+ 'gift_fee_factor',
];
public $timestamps = true;
@@ -55,6 +56,23 @@ class Medal extends NexusModel
return nexus_trans("label.permanent");
}
+ public function checkCanBeBuy()
+ {
+ if ($this->get_type == self::GET_TYPE_GRANT) {
+ throw new \RuntimeException(nexus_trans('medal.grant_only'));
+ }
+ $now = now();
+ if ($this->sale_begin_time && $this->sale_begin_time->gt($now)) {
+ throw new \RuntimeException(nexus_trans('medal.before_sale_begin_time'));
+ }
+ if ($this->sale_end_time && $this->sale_end_time->lt($now)) {
+ throw new \RuntimeException(nexus_trans('medal.after_sale_end_time'));
+ }
+ if ($this->inventory !== null && $this->inventory <= 0) {
+ throw new \RuntimeException(nexus_trans('medal.inventory_empty'));
+ }
+ }
+
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(User::class, 'user_medals', 'medal_id', 'uid')->withTimestamps();
diff --git a/app/Models/User.php b/app/Models/User.php
index a660d641..be4685e5 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -459,9 +459,9 @@ class User extends Authenticatable implements FilamentUser, HasName
public function medals(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Medal::class, 'user_medals', 'uid', 'medal_id')
- ->withPivot(['id', 'expire_at', 'status'])
+ ->withPivot(['id', 'expire_at', 'status', 'priority'])
->withTimestamps()
- ->orderByPivot('id', 'desc')
+ ->orderByPivot('priority', 'desc')
;
}
diff --git a/app/Repositories/BonusRepository.php b/app/Repositories/BonusRepository.php
index 1945a0a0..93391652 100644
--- a/app/Repositories/BonusRepository.php
+++ b/app/Repositories/BonusRepository.php
@@ -6,6 +6,7 @@ use App\Models\BonusLogs;
use App\Models\HitAndRun;
use App\Models\Invite;
use App\Models\Medal;
+use App\Models\Message;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserMedal;
@@ -58,19 +59,7 @@ class BonusRepository extends BaseRepository
if ($exists) {
throw new \LogicException("user: $uid already own this medal: $medalId.");
}
- if ($medal->get_type != Medal::GET_TYPE_EXCHANGE) {
- throw new \LogicException("This medal can not be buy.");
- }
- if ($medal->inventory !== null && $medal->inventory <= 0) {
- throw new \LogicException("Inventory empty.");
- }
- $now = now();
- if ($medal->sale_begin_time && $medal->sale_begin_time->gt($now)) {
- throw new \LogicException("Before sale begin time.");
- }
- if ($medal->sale_end_time && $medal->sale_end_time->lt($now)) {
- throw new \LogicException("After sale end time.");
- }
+ $medal->checkCanBeBuy();
$requireBonus = $medal->price;
NexusDB::transaction(function () use ($user, $medal, $requireBonus) {
$comment = nexus_trans('bonus.comment_buy_medal', [
@@ -101,6 +90,67 @@ class BonusRepository extends BaseRepository
}
+ public function consumeToGiftMedal($uid, $medalId, $toUid): bool
+ {
+ $user = User::query()->findOrFail($uid);
+ $toUser = User::query()->findOrFail($toUid);
+ $medal = Medal::query()->findOrFail($medalId);
+ $exists = $toUser->valid_medals()->where('medal_id', $medalId)->exists();
+ do_log(last_query());
+ if ($exists) {
+ throw new \LogicException("user: $toUid already own this medal: $medalId.");
+ }
+ $medal->checkCanBeBuy();
+ $giftFee = $medal->price * ($medal->gift_fee_factor ?? 0);
+ $requireBonus = $medal->price + $giftFee;
+ NexusDB::transaction(function () use ($user, $toUser, $medal, $requireBonus, $giftFee) {
+ $comment = nexus_trans('bonus.comment_gift_medal', [
+ 'bonus' => $requireBonus,
+ 'medal_name' => $medal->name,
+ 'to_username' => $toUser->username,
+ ], $user->locale);
+ do_log("comment: $comment");
+ $this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_GIFT_MEDAL, "$comment(medal ID: {$medal->id})");
+
+ $expireAt = null;
+ if ($medal->duration > 0) {
+ $expireAt = Carbon::now()->addDays($medal->duration)->toDateTimeString();
+ }
+ $msg = [
+ 'sender' => 0,
+ 'receiver' => $toUser->id,
+ 'subject' => nexus_trans('message.receive_medal.subject', [], $toUser->locale),
+ 'msg' => nexus_trans('message.receive_medal.body', [
+ 'username' => $user->username,
+ 'cost_bonus' => $requireBonus,
+ 'medal_name' => $medal->name,
+ 'price' => $medal->price,
+ 'gift_fee_total' => $giftFee,
+ 'gift_fee_factor' => $medal->gift_fee_factor ?? 0,
+ 'expire_at' => $expireAt ?? nexus_trans('label.permanent'),
+ 'bonus_addition_factor' => $medal->bonus_addition_factor ?? 0,
+ ], $toUser->locale),
+ 'added' => now()
+ ];
+ Message::add($msg);
+ $toUser->medals()->attach([$medal->id => ['expire_at' => $expireAt, 'status' => UserMedal::STATUS_NOT_WEARING]]);
+ if ($medal->inventory !== null) {
+ $affectedRows = NexusDB::table('medals')
+ ->where('id', $medal->id)
+ ->where('inventory', $medal->inventory)
+ ->decrement('inventory')
+ ;
+ if ($affectedRows != 1) {
+ throw new \RuntimeException("Decrement medal({$medal->id}) inventory affected rows != 1($affectedRows)");
+ }
+ }
+
+ });
+
+ return true;
+
+ }
+
public function consumeToBuyAttendanceCard($uid): bool
{
$user = User::query()->findOrFail($uid);
diff --git a/app/Repositories/MedalRepository.php b/app/Repositories/MedalRepository.php
index 45050372..ea2641fb 100644
--- a/app/Repositories/MedalRepository.php
+++ b/app/Repositories/MedalRepository.php
@@ -74,7 +74,7 @@ class MedalRepository extends BaseRepository
return $user->medals()->attach([$medal->id => ['expire_at' => $expireAt, 'status' => UserMedal::STATUS_NOT_WEARING]]);
}
- function toggleUserMedalStatus($id, $userId)
+ public function toggleUserMedalStatus($id, $userId)
{
$userMedal = UserMedal::query()->findOrFail($id);
if ($userMedal->uid != $userId) {
@@ -91,4 +91,32 @@ class MedalRepository extends BaseRepository
return $userMedal;
}
+ public function saveUserMedal(int $userId, array $userMedalData)
+ {
+ $user = User::query()->findOrFail($userId);
+ $validMedals = $user->valid_medals;
+ if ($validMedals->isEmpty()) {
+ return true;
+ }
+ $statusCaseWhens = $priorityCaseWhens = $idArr = [];
+ foreach ($validMedals as $medal) {
+ $id = $medal->pivot->id;
+ $idArr[] = $id;
+ if (isset($userMedalData[$id]['status'])) {
+ $status = UserMedal::STATUS_WEARING;
+ } else {
+ $status = UserMedal::STATUS_NOT_WEARING;
+ }
+ $statusCaseWhens[] = sprintf('when `id` = %s then %s', $id, $status);
+ $priorityCaseWhens[] = sprintf('when `id` = %s then %s', $id, $userMedalData[$id]['priority'] ?? 0);
+ }
+ $sql = sprintf(
+ 'update user_medals set `status` = case %s end, `priority` = case %s end where id in (%s)',
+ implode(' ', $statusCaseWhens), implode(' ', $priorityCaseWhens), implode(',', $idArr)
+ );
+ do_log("sql: $sql");
+ clear_user_cache($userId);
+ return NexusDB::statement($sql);
+ }
+
}
diff --git a/database/migrations/2023_01_27_143831_add_gift_fee_factor_to_medals_table.php b/database/migrations/2023_01_27_143831_add_gift_fee_factor_to_medals_table.php
new file mode 100644
index 00000000..85f52f34
--- /dev/null
+++ b/database/migrations/2023_01_27_143831_add_gift_fee_factor_to_medals_table.php
@@ -0,0 +1,32 @@
+float('gift_fee_factor', 10, 6)->default(0);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('medals', function (Blueprint $table) {
+ $table->dropColumn(['gift_fee_factor']);
+ });
+ }
+};
diff --git a/database/migrations/2023_01_28_170836_add_priority_to_user_medals_table.php b/database/migrations/2023_01_28_170836_add_priority_to_user_medals_table.php
new file mode 100644
index 00000000..9dd0a2f2
--- /dev/null
+++ b/database/migrations/2023_01_28_170836_add_priority_to_user_medals_table.php
@@ -0,0 +1,32 @@
+integer('priority')->default(0);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('user_medals', function (Blueprint $table) {
+ $table->dropColumn(['priority']);
+ });
+ }
+};
diff --git a/include/constants.php b/include/constants.php
index 261c281c..1c598408 100644
--- a/include/constants.php
+++ b/include/constants.php
@@ -1,6 +1,6 @@
';
- $wrapAfter = '';
+ $wrapBefore = '