改进 扣费机制,优先补充 Drops

This commit is contained in:
iVampireSP.com 2022-09-04 22:18:49 +08:00
parent 792fefc93c
commit fad97f879e
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 14 additions and 4 deletions

View File

@ -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;

View File

@ -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);