diff --git a/app/Manager/Bonus.php b/app/Manager/Bonus.php index cb6a92ad2..616f0224a 100644 --- a/app/Manager/Bonus.php +++ b/app/Manager/Bonus.php @@ -232,11 +232,14 @@ class Bonus extends \Gazelle\Base { /** * return array */ - public function offerTokenOther(User $user): array { - $balance = $user->bonusPointsTotal(); + public function offerTokenOther(User $giver, User $receiver): array { + $balance = $giver->bonusPointsTotal(); $offer = []; foreach ($this->itemList() as $item) { - if (str_starts_with($item->label(), 'other-') && $balance >= $item->price()) { + if ( + str_starts_with($item->label(), 'other-') + && $balance >= $item->priceForTokenOther($giver, $receiver) + ) { $offer[] = $item; } } @@ -253,9 +256,13 @@ class Bonus extends \Gazelle\Base { if (is_null($item)) { return BonusItemPurchaseStatus::incomplete; } + $price = $item->priceForTokenOther($giver, $receiver); + if ($price === false) { + return BonusItemPurchaseStatus::insufficientFunds; + } return $item->purchase( $giver, - $item->price(), + $price, [ 'message' => $message, 'receiver' => $receiver, diff --git a/public/static/functions/bonus.js b/public/static/functions/bonus.js index 92d3abd09..21f3c7654 100644 --- a/public/static/functions/bonus.js +++ b/public/static/functions/bonus.js @@ -42,7 +42,8 @@ async function validateBonusUsername() { if (data.status !== 'success') { message = status; } else { - const info = data.response; + const info = data.response; + const price = new Intl.NumberFormat('en').format(info.price); if (!info.found) { message = '⛔️ ' + 'user not found'; } else if (info.id == document.body.dataset.id) { @@ -51,8 +52,10 @@ async function validateBonusUsername() { message = '⛔️ ' + info.username + ' is currently not enabled'; } else if (!info.accept) { message = '🚫 ' + info.username + ' does not wish to receive tokens'; + } else if (info.percent5 >= 100) { + message = '⛔️ you do not have enough bonus points (' + price + ' needed)'; } else { - message = '✅ This will cost ' + info.price + ' bonus points'; + message = '✅ This will cost ' + price + ' bonus points'; if (info.percent5 > 0) { message = message + ' (which is more than ' + info.percent5 + '% of your balance)'; } diff --git a/sections/user/user.php b/sections/user/user.php index f4d36fe5e..ce57dd95e 100644 --- a/sections/user/user.php +++ b/sections/user/user.php @@ -101,7 +101,7 @@ echo $Twig->render('user/header.twig', [ 'bonus' => $userBonus, 'donor' => $donor, 'freeleech' => [ - 'offer' => $OwnProfile ? [] : $bonusMan->offerTokenOther($Viewer), + 'offer' => $OwnProfile ? [] : $bonusMan->offerTokenOther($Viewer, $user), 'latest' => $viewerBonus->otherLatest($user), ], 'friend' => new User\Friend($Viewer), diff --git a/templates/user/header.twig b/templates/user/header.twig index e46cbc7e1..d35bc767b 100644 --- a/templates/user/header.twig +++ b/templates/user/header.twig @@ -89,10 +89,11 @@