改进 Drops 计算

This commit is contained in:
iVampireSP.com 2022-09-09 21:04:09 +08:00
parent 2a4a9f4b1a
commit 8521234bb2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 77 additions and 21 deletions

View File

@ -1,8 +1,67 @@
<?php <?php
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
function now($timezone = null) function now($timezone = null)
{ {
return Carbon::now($timezone); return Carbon::now($timezone);
} }
function getDrops($user_id)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$drops = Cache::get($cache_key) / (config('drops.decimal') * $multiple);
return $drops;
}
function reduceDrops($user_id, $amount = 0)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$amount = $amount * $multiple;
$drops = Cache::decrement($cache_key, $amount);
return $drops;
}
function addDrops($user_id, $amount = 0)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$amount = $amount * $multiple;
$drops = Cache::increment($cache_key, $amount);
return $drops;
}

View File

@ -12,7 +12,8 @@ public function index(Request $request)
{ {
$user = $request->user(); $user = $request->user();
$user['drops'] = (float) Cache::get('user_drops_' . $user['id'], 0); $user['drops'] = getDrops($user['id']);
$user['drops_rate'] = config('drops.rate');
return $this->success($user); return $this->success($user);
} }

View File

@ -78,19 +78,13 @@ public function cost($price = null)
{ {
$this->load('user'); $this->load('user');
$cache_key = 'user_drops_' . $this->user_id; $drops = getDrops($this->user_id);
$drops = Cache::get($cache_key);
if ($price !== null) { if ($price !== null) {
$this->price = $price; $this->price = $price;
} }
$amount = $price / config('drops.rate') + 1;
$amount = $price / Cache::get('drops_rate', 100) + 1;
$amount = intval(log10(abs($amount)) / 3);
// if drops <= price // if drops <= price
if ($drops < $this->price) { if ($drops < $this->price) {
@ -99,7 +93,7 @@ public function cost($price = null)
$need = $this->price - $drops; $need = $this->price - $drops;
// 算出需要补充多少余额 // 算出需要补充多少余额
$need_amount = $need / Cache::get('drops_rate', 100) + 1; $need_amount = $need / config('drops.rate') + 1;
$this->user->toDrops($amount + $need_amount); $this->user->toDrops($amount + $need_amount);
} catch (BalanceNotEnoughException) { } catch (BalanceNotEnoughException) {
@ -115,11 +109,7 @@ public function cost($price = null)
]); ]);
} }
$this->price = intval(log10(abs($this->price)) / 3); reduceDrops($this->user_id, $this->price);
echo $this->price;
Cache::decrement($cache_key, $this->price);
return true; return true;
} }

View File

@ -46,10 +46,11 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
public function toDrops($amount = 1) public function toDrops($amount = 1)
{ {
$rate = Cache::get('drops_rate', 100); $rate = config('drops.rate');
$cache_key = 'user_drops_' . $this->id; $cache_key = 'user_drops_' . $this->id;
$drops = Cache::get($cache_key, 0); $drops = getDrops($this->id);
$total = 0; $total = 0;
@ -59,7 +60,6 @@ public function toDrops($amount = 1)
$total += $amount * $rate; $total += $amount * $rate;
$lock = Cache::lock("lock_" . $cache_key, 5); $lock = Cache::lock("lock_" . $cache_key, 5);
try { try {
$lock->block(5); $lock->block(5);
@ -67,8 +67,7 @@ public function toDrops($amount = 1)
$this->balance -= $amount; $this->balance -= $amount;
$this->save(); $this->save();
// increment user drops addDrops($this->id, $total);
Cache::increment($cache_key, $total);
// if user balance <= 0 // if user balance <= 0
if ($this->balance < $amount) { if ($this->balance < $amount) {

View File

@ -44,7 +44,7 @@ public static function boot()
} }
$rate = Cache::get('drops_rate', 100); $rate = config('drops.rate') ;
$drops->total = $drops->amount * $rate; $drops->total = $drops->amount * $rate;
$this->cache_key = 'user_' . $drops->user_id; $this->cache_key = 'user_' . $drops->user_id;

View File

@ -70,6 +70,7 @@
$app->configure('queue'); $app->configure('queue');
$app->configure('session'); $app->configure('session');
$app->configure('alipay'); $app->configure('alipay');
$app->configure('drops');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

6
config/drops.php Normal file
View File

@ -0,0 +1,6 @@
<?php
return [
'rate' => 1000,
'decimal' => 5,
];