2022-08-09 19:57:43 +00:00
|
|
|
|
<?php
|
|
|
|
|
# Date: 19.02.2022 (2:33)
|
|
|
|
|
namespace Battles;
|
|
|
|
|
|
|
|
|
|
use Battles\Database\Db;
|
|
|
|
|
|
|
|
|
|
class Hostel
|
|
|
|
|
{
|
|
|
|
|
private array $status = [];
|
|
|
|
|
private int $uid;
|
|
|
|
|
private int $hid;
|
|
|
|
|
private int $type;
|
|
|
|
|
private int $time;
|
|
|
|
|
|
|
|
|
|
public const PRICEPERTYPE = [
|
|
|
|
|
1 => [8, 16, 24, 32],
|
|
|
|
|
2 => [15, 30, 45, 60],
|
|
|
|
|
3 => [25, 50, 75, 100],
|
|
|
|
|
4 => [40, 80, 120, 160]
|
|
|
|
|
];
|
|
|
|
|
public const BASENAME = [
|
|
|
|
|
1 => 'Сумка',
|
|
|
|
|
2 => 'Сундук',
|
|
|
|
|
3 => 'Комната',
|
|
|
|
|
4 => 'Амбар',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->uid = User::getInstance()->getId();
|
|
|
|
|
$data = Db::getInstance()->ofetch('select id, type, time from hostel where uid = ?', $this->uid);
|
|
|
|
|
$this->hid = $data->id ?? null;
|
|
|
|
|
$this->type = $data->type ?? null;
|
|
|
|
|
$this->time = $data->time ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function add7DayRent($type)
|
|
|
|
|
{
|
|
|
|
|
Db::getInstance()->execute('insert into hostel (uid, type, time) values (?,?,?)', [$this->uid, $type, time() + 60 * 60 * 24 * 7]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
|
private function addRentTime($hostelId, $time)
|
2022-08-09 19:57:43 +00:00
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
|
Db::getInstance()->execute('update hostel set time = ? where id = ? and uid = ?', [$time, $hostelId, $this->uid]);
|
2022-08-09 19:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function removeItems()
|
|
|
|
|
{
|
|
|
|
|
Db::getInstance()->execute('update inventory set in_hostel = 0 where owner_id = ? and in_hostel = 1', $this->uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function removeRent()
|
|
|
|
|
{
|
|
|
|
|
Db::getInstance()->execute('delete from hostel where id = ? and uid = ?', [$this->hid, $this->uid]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function pay($amount): bool
|
|
|
|
|
{
|
|
|
|
|
if (!User::getInstance()->money()->spend($amount)) {
|
|
|
|
|
$this->setError('Недостаточно денег!');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function hasNoRents(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->hid) {
|
|
|
|
|
$this->setError('Не более 1 арендованного места!');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function typeIsAllowed($type): bool
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($type, [1, 2, 3, 4])) {
|
|
|
|
|
$this->setError('Неверный тип аренды!');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setError(string $message)
|
|
|
|
|
{
|
|
|
|
|
$this->status = ['type' => 'error', 'message' => $message];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function setSuccess(string $message)
|
|
|
|
|
{
|
|
|
|
|
$this->status = ['type' => 'success', 'message' => $message];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function newRent($type): bool
|
|
|
|
|
{
|
|
|
|
|
if (
|
|
|
|
|
!$this->typeIsAllowed($type) ||
|
|
|
|
|
!$this->hasNoRents() ||
|
|
|
|
|
!$this->pay(self::PRICEPERTYPE[$type][0])
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->add7DayRent($type);
|
|
|
|
|
$this->setSuccess('Поздравляем с успешной арендой!');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function remove(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->time < time()) {
|
|
|
|
|
$this->setError('Нельзя отказаться от услуг если имеется задолежнность!');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->removeRent();
|
|
|
|
|
$this->removeItems();
|
|
|
|
|
$this->setSuccess('Вы успешно отказались от аренды!');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function changeType(int $type)
|
|
|
|
|
{
|
|
|
|
|
$this->remove();
|
|
|
|
|
$this->newRent($type);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
|
public function changeTime(int $orderedTime): bool
|
2022-08-09 19:57:43 +00:00
|
|
|
|
{
|
|
|
|
|
$daysByOrder = [
|
|
|
|
|
1 => 7,
|
|
|
|
|
2 => 14,
|
|
|
|
|
3 => 21,
|
|
|
|
|
4 => 28,
|
|
|
|
|
];
|
|
|
|
|
if (
|
2022-12-16 23:20:43 +00:00
|
|
|
|
!$this->typeIsAllowed($orderedTime) ||
|
|
|
|
|
!$this->pay(self::PRICEPERTYPE[$this->type][$orderedTime - 1])
|
2022-08-09 19:57:43 +00:00
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$this->time += 60 * 60 * 24 * $daysByOrder[$orderedTime];
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->addRentTime($this->hid, $this->time);
|
|
|
|
|
$this->setSuccess('Всё прошло успешно!');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function getHid(): ?int
|
|
|
|
|
{
|
|
|
|
|
return $this->hid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function getType(): ?int
|
|
|
|
|
{
|
|
|
|
|
return $this->type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function getTime(): ?int
|
|
|
|
|
{
|
|
|
|
|
return $this->time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getStatus(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->status;
|
|
|
|
|
}
|
|
|
|
|
}
|