game/_incl_data/class/Present.php

91 lines
3.2 KiB
PHP
Raw Normal View History

<?php
use Core\Db;
use User\Clan;
class Present
{
const EXP_NEEDED_FOR_ADD_PRESENT_TEXT = 500000;
private array $sender;
private array $receiver;
private string $status;
private \DTO\Present $present;
private Clan $senderClan;
/**
* @param \DTO\Present $present
*/
public function __construct(\DTO\Present $present)
{
$check = Db::getValue('select count(id) from users where login in (?,?)', [$present->sender, $present->receiver]);
if ($check === 1) {
$this->status = 'Очень щедро дарить что-то самому себе 😉';
return;
}
if ($check === 0) {
$this->status = 'Ошибка: В городе нет такого персонажа!';
return;
}
$this->sender = User::getInfo($present->sender);
$this->receiver = User::getInfo($present->receiver);
$this->senderClan = new Clan($this->sender);
if ($present->anonymousSender) {
$this->sender['login'] = 'невидимки';
} elseif ($present->clanSender) {
$this->sender['login'] = 'клана ' . $this->senderClan->getName();
}
$this->present = $present;
if ($this->sender['exp'] < self::EXP_NEEDED_FOR_ADD_PRESENT_TEXT) {
$this->present->textTitle = '';
$this->present->text = '';
}
$this->send();
$this->sendResultToChat('Получен подарок от ' . $this->sender['login']);
$this->status = 'Подарок был успешно отправлен персонажу ' . $this->receiver['login'];
}
private function send()
{
$values = [
'textTitle' => strip_tags($this->present->textTitle),
'text' => strip_tags($this->present->text),
'sender' => $this->sender['login'],
'receiver' => $this->receiver['id'],
'presentId' => $this->present->itemId,
];
Db::sql('update items_users set gtxt1 = :textTitle, gtxt2 = :text, gift = :sender, time_create = unix_timestamp(), uid = :receiver where id = :presentId', $values);
$this->addDelo(['id' => $this->present->itemId, 'name' => (new ItemModel($this->present->itemId))->getName()]);
}
private function addDelo(array $item)
{
$from = 'present';
$senderLog = sprintf('Отправлен подарок к %s [id:%s]. Предмет %s [id:%s].', $this->receiver['login'], $this->receiver['id'], $item['name'], $item['id']);
$receiverLog = sprintf('Получен подарок от %s [id:%s]. Предмет %s [id:%s].', $this->sender['login'], $this->sender['id'], $item['name'], $item['id']);
Delo::add(1, $from, $this->receiver['id'], $receiverLog);
Delo::add(1, $from, $this->sender['id'], $senderLog);
}
private function sendResultToChat(string $text)
{
$msg = new ChatMessage();
$msg->setTo($this->receiver['login']);
$msg->setType(6);
$msg->setText($text);
(new Chat())->sendMsg($msg);
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
}