@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
# Date: 19.02.2022 (18:54)
|
||||
namespace Battles;
|
||||
|
||||
use Battles\Database\Db;
|
||||
|
||||
class UserMoney
|
||||
{
|
||||
private int $uid;
|
||||
private int $wallet_money;
|
||||
private Bank $bank;
|
||||
|
||||
public function __construct(int $uid, int $money)
|
||||
{
|
||||
$this->uid = $uid;
|
||||
$this->wallet_money = $money;
|
||||
$this->initBank();
|
||||
}
|
||||
|
||||
private function initBank()
|
||||
{
|
||||
$this->bank = new Bank($this->uid);
|
||||
}
|
||||
|
||||
public function get(): int
|
||||
{
|
||||
return $this->wallet_money;
|
||||
}
|
||||
|
||||
public function set(int $money)
|
||||
{
|
||||
$this->wallet_money = max($money, 0);
|
||||
}
|
||||
|
||||
public function getBank(): int
|
||||
{
|
||||
return $this->bank->getMoney();
|
||||
}
|
||||
|
||||
public function modifyBank(int $money, string $logType = '')
|
||||
{
|
||||
$this->bank->modify($money, $logType);
|
||||
}
|
||||
|
||||
private function save()
|
||||
{
|
||||
Db::getInstance()->execute('update users set money = ? where id = ?', [$this->wallet_money, $this->uid]);
|
||||
}
|
||||
|
||||
/** Тратим деньги */
|
||||
public function spend(int $value): bool
|
||||
{
|
||||
if ($this->wallet_money > $value && $value > 0) {
|
||||
$this->wallet_money -= $value;
|
||||
$this->save();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Получаем деньги */
|
||||
public function earn(int $value): bool
|
||||
{
|
||||
if ($value <= 0) {
|
||||
return false;
|
||||
}
|
||||
$this->wallet_money += $value;
|
||||
$this->save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user