改进 扣费机制,优先补充 Drops
This commit is contained in:
parent
792fefc93c
commit
fad97f879e
@ -78,11 +78,10 @@ public function scopeThisUser($query, $module = null)
|
||||
// cost
|
||||
public function cost($price = null)
|
||||
{
|
||||
$price = abs($price);
|
||||
|
||||
$this->load('user');
|
||||
|
||||
$price = abs($price);
|
||||
|
||||
$cache_key = 'user_drops_' . $this->user_id;
|
||||
|
||||
$drops = Cache::get($cache_key);
|
||||
@ -116,6 +115,8 @@ public function cost($price = null)
|
||||
]);
|
||||
}
|
||||
|
||||
$this->price = abs($this->price);
|
||||
|
||||
Cache::decrement($cache_key, $this->price);
|
||||
|
||||
return true;
|
||||
|
@ -51,10 +51,19 @@ class User extends Authenticatable
|
||||
public function toDrops($amount = 1)
|
||||
{
|
||||
$rate = Cache::get('drops_rate', 100);
|
||||
$total = $amount * $rate;
|
||||
|
||||
$cache_key = 'user_drops_' . $this->id;
|
||||
|
||||
$drops = Cache::get($cache_key, 0);
|
||||
|
||||
$total = 0;
|
||||
|
||||
if ($drops < 0) {
|
||||
$amount += abs($drops) / $rate;
|
||||
}
|
||||
|
||||
$total += $amount * $rate;
|
||||
|
||||
|
||||
$lock = Cache::lock("lock_" . $cache_key, 5);
|
||||
try {
|
||||
$lock->block(5);
|
||||
|
Loading…
Reference in New Issue
Block a user