72 lines
1.4 KiB
PHP
72 lines
1.4 KiB
PHP
<?php
|
|
# Date: 19.02.2022 (18:54)
|
|
namespace Battles;
|
|
|
|
use Battles\Database\Db;
|
|
|
|
class UserMoney
|
|
{
|
|
private int $uid;
|
|
private int $walletMoney;
|
|
private Bank $bank;
|
|
|
|
public function __construct(int $uid, int $money)
|
|
{
|
|
$this->uid = $uid;
|
|
$this->walletMoney = $money;
|
|
$this->initBank();
|
|
}
|
|
|
|
private function initBank()
|
|
{
|
|
$this->bank = new Bank($this->uid);
|
|
}
|
|
|
|
public function get(): int
|
|
{
|
|
return $this->walletMoney;
|
|
}
|
|
|
|
public function set(int $money)
|
|
{
|
|
$this->walletMoney = 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->walletMoney, $this->uid]);
|
|
}
|
|
|
|
/** Тратим деньги */
|
|
public function spend(int $value): bool
|
|
{
|
|
if ($this->walletMoney > $value && $value > 0) {
|
|
$this->walletMoney -= $value;
|
|
$this->save();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/** Получаем деньги */
|
|
public function earn(int $value): bool
|
|
{
|
|
if ($value <= 0) {
|
|
return false;
|
|
}
|
|
$this->walletMoney += $value;
|
|
$this->save();
|
|
return true;
|
|
}
|
|
}
|