改进 Drops 计算
This commit is contained in:
parent
2a4a9f4b1a
commit
8521234bb2
@ -1,8 +1,67 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
function now($timezone = null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,8 @@ public function index(Request $request)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
@ -78,19 +78,13 @@ public function cost($price = null)
|
||||
{
|
||||
$this->load('user');
|
||||
|
||||
$cache_key = 'user_drops_' . $this->user_id;
|
||||
|
||||
$drops = Cache::get($cache_key);
|
||||
|
||||
$drops = getDrops($this->user_id);
|
||||
|
||||
if ($price !== null) {
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
|
||||
$amount = $price / Cache::get('drops_rate', 100) + 1;
|
||||
$amount = intval(log10(abs($amount)) / 3);
|
||||
|
||||
$amount = $price / config('drops.rate') + 1;
|
||||
|
||||
// if drops <= price
|
||||
if ($drops < $this->price) {
|
||||
@ -99,7 +93,7 @@ public function cost($price = null)
|
||||
$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);
|
||||
} catch (BalanceNotEnoughException) {
|
||||
@ -115,11 +109,7 @@ public function cost($price = null)
|
||||
]);
|
||||
}
|
||||
|
||||
$this->price = intval(log10(abs($this->price)) / 3);
|
||||
|
||||
echo $this->price;
|
||||
|
||||
Cache::decrement($cache_key, $this->price);
|
||||
reduceDrops($this->user_id, $this->price);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -46,10 +46,11 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
|
||||
|
||||
public function toDrops($amount = 1)
|
||||
{
|
||||
$rate = Cache::get('drops_rate', 100);
|
||||
$rate = config('drops.rate');
|
||||
|
||||
$cache_key = 'user_drops_' . $this->id;
|
||||
|
||||
$drops = Cache::get($cache_key, 0);
|
||||
$drops = getDrops($this->id);
|
||||
|
||||
$total = 0;
|
||||
|
||||
@ -59,7 +60,6 @@ public function toDrops($amount = 1)
|
||||
|
||||
$total += $amount * $rate;
|
||||
|
||||
|
||||
$lock = Cache::lock("lock_" . $cache_key, 5);
|
||||
try {
|
||||
$lock->block(5);
|
||||
@ -67,8 +67,7 @@ public function toDrops($amount = 1)
|
||||
$this->balance -= $amount;
|
||||
$this->save();
|
||||
|
||||
// increment user drops
|
||||
Cache::increment($cache_key, $total);
|
||||
addDrops($this->id, $total);
|
||||
|
||||
// if user balance <= 0
|
||||
if ($this->balance < $amount) {
|
||||
|
@ -44,7 +44,7 @@ public static function boot()
|
||||
}
|
||||
|
||||
|
||||
$rate = Cache::get('drops_rate', 100);
|
||||
$rate = config('drops.rate') ;
|
||||
$drops->total = $drops->amount * $rate;
|
||||
|
||||
$this->cache_key = 'user_' . $drops->user_id;
|
||||
|
@ -70,6 +70,7 @@
|
||||
$app->configure('queue');
|
||||
$app->configure('session');
|
||||
$app->configure('alipay');
|
||||
$app->configure('drops');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
6
config/drops.php
Normal file
6
config/drops.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'rate' => 1000,
|
||||
'decimal' => 5,
|
||||
];
|
Loading…
Reference in New Issue
Block a user