battles/classes/Battles/Nick.php

102 lines
3.1 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
namespace Battles;
/**
* Разные способы отображения строки с логином персонажа.
*/
const INVIS = '<i>невидимка</i>';
class Nick
{
private User $user;
private function __construct(int $userid)
{
$this->user = User::getInstance($userid);
}
/**
* Отображение иконки склонности.
* @return string
*/
private function getAlignImage(): ?string
{
return $this->getImage($this->user->getAlign(), '/i/align_');
}
/**
* Отображение иконки клана.
* @return string
*/
private function getClanImage(): string
{
return $this->getImage($this->user->getClan(), '/i/clan/');
}
private function getImage($name, $path): string
{
if (empty($name)) {
return '';
}
$file = $path . $name . '.png';
$alt = '';
if (strpos($path, 'align')) {
$alt = '{a:' . $name . '}';
} elseif (strpos($path, 'clan')) {
$alt = '{c:' . $name . '}';
}
return file_exists($file) ? "<img src='$file' alt='$alt'>" : $alt;
}
private function getInfolinkImage(): string
{
return "<a href='inf.php?" . $this->user->getLogin() . "' target='_blank'><img src='i/inf.gif' alt='Ссылка на профиль'></a>";
}
/**
* Вызов класса из самого себя. Читать про обратное связывание и пытаться что-то понять.
*
* @param $playerId
*
* @return Nick
*/
public static function id($playerId): self
{
return new self($playerId);
}
/**
* Возвращает строку со склонностью, кланом, логином, уровнем, ссылкой на профиль.
*
* @param int $showInvisibility отображать логин даже если персонаж невидимка.
*
* @return string
*/
public function full(int $showInvisibility = 0): string
{
if ($showInvisibility === 0 && UserEffect::isInvisible($this->user->getId())) {
return INVIS;
}
return $this->getAlignImage() . '&nbsp;' . $this->getClanImage() . '&nbsp;' . $this->user->getLogin() . " [" . $this->user->getLevel() . "] " . $this->getInfolinkImage();
}
/**
* Возвращает строку с логином.
* Избавиться от этого! Оставлено для совместимости.
* @return string
*/
public function short(): string
{
return $this->user->getLogin();
}
/**
* Возвращает строку со склонностью, кланом, логином, уровнем, ссылкой на профиль, здоровьем.
* @return string
*/
public function battle(): string
{
return $this->full() . "<img src='i/herz.gif' alt='HP'> [" . $this->user->stats()->getHealth() . "/" . $this->user->stats()->getMaxHealth() . "]";
}
}