battles/classes/Battles/UserMoney.php

72 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?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;
private Bank $bank;
public function __construct(int $uid, int $money)
{
$this->uid = $uid;
2022-12-16 23:20:43 +00:00
$this->walletMoney = $money;
$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;
}
public function set(int $money)
{
2022-12-16 23:20:43 +00:00
$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()
{
2022-12-16 23:20:43 +00:00
Db::getInstance()->execute('update users set money = ? where id = ?', [$this->walletMoney, $this->uid]);
}
/** Тратим деньги */
public function spend(int $value): bool
{
2022-12-16 23:20:43 +00:00
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;
}
2022-12-16 23:20:43 +00:00
$this->walletMoney += $value;
$this->save();
return true;
}
2022-12-16 23:20:43 +00:00
}