2022-08-09 19:57:43 +00:00
|
|
|
<?php
|
|
|
|
# Date: 19.02.2022 (18:54)
|
|
|
|
namespace Battles;
|
|
|
|
|
|
|
|
use Battles\Database\Db;
|
|
|
|
|
|
|
|
class UserMoney
|
|
|
|
{
|
|
|
|
private int $uid;
|
2022-12-16 23:20:43 +00:00
|
|
|
private int $walletMoney;
|
2022-08-09 19:57:43 +00:00
|
|
|
private Bank $bank;
|
|
|
|
|
|
|
|
public function __construct(int $uid, int $money)
|
|
|
|
{
|
|
|
|
$this->uid = $uid;
|
2022-12-16 23:20:43 +00:00
|
|
|
$this->walletMoney = $money;
|
2022-08-09 19:57:43 +00:00
|
|
|
$this->initBank();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function initBank()
|
|
|
|
{
|
|
|
|
$this->bank = new Bank($this->uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(): int
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
return $this->walletMoney;
|
2022-08-09 19:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function set(int $money)
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
$this->walletMoney = max($money, 0);
|
2022-08-09 19:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getBank(): int
|
|
|
|
{
|
|
|
|
return $this->bank->getMoney();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function modifyBank(int $money, string $logType = '')
|
|
|
|
{
|
|
|
|
$this->bank->modify($money, $logType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function save()
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
Db::getInstance()->execute('update users set money = ? where id = ?', [$this->walletMoney, $this->uid]);
|
2022-08-09 19:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Тратим деньги */
|
|
|
|
public function spend(int $value): bool
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
if ($this->walletMoney > $value && $value > 0) {
|
|
|
|
$this->walletMoney -= $value;
|
2022-08-09 19:57:43 +00:00
|
|
|
$this->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Получаем деньги */
|
|
|
|
public function earn(int $value): bool
|
|
|
|
{
|
|
|
|
if ($value <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
$this->walletMoney += $value;
|
2022-08-09 19:57:43 +00:00
|
|
|
$this->save();
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
}
|