battles/classes/Battles/Hostel.php

178 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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]);
}
private function addRentTime($hostelId, $time)
{
Db::getInstance()->execute('update hostel set time = ? where id = ? and uid = ?', [$time, $hostelId, $this->uid]);
}
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);
}
public function changeTime(int $orderedTime): bool
{
$daysByOrder = [
1 => 7,
2 => 14,
3 => 21,
4 => 28,
];
if (
!$this->typeIsAllowed($orderedTime) ||
!$this->pay(self::PRICEPERTYPE[$this->type][$orderedTime - 1])
) {
return false;
}
$this->time += 60 * 60 * 24 * $daysByOrder[$orderedTime];
$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;
}
}