282 lines
12 KiB
PHP
282 lines
12 KiB
PHP
<?php
|
||
|
||
namespace Insallah;
|
||
|
||
use Core\Db;
|
||
use User;
|
||
|
||
class Achievements
|
||
{
|
||
private User $user;
|
||
private $info;
|
||
private array $data = [
|
||
'zb' => [
|
||
'questname' => 'Ежедн.Задания',
|
||
'quest' => 'Выполнить задания бабки.',
|
||
'check' => ['Заданий', 5, 25, 50, 100, 250],
|
||
'prize' => ['Жетон <br>успеха <br>', 5, 20, 40, 60, 100],
|
||
'startlink' => 11
|
||
],
|
||
'vx' => [
|
||
'questname' => 'Хаотические бои',
|
||
'quest' => 'Победить в хаотических боях.',
|
||
'check' => ['Побед', 50, 300, 400, 500, 1000],
|
||
'prize' => ['екр.', 25, 50, 75, 100, 200],
|
||
'startlink' => 21
|
||
],
|
||
'snt' => [
|
||
'questname' => 'Сундук трупожора',
|
||
'quest' => 'Открыть сундук трупожора.',
|
||
'check' => ['Открытий', 5, 25, 50, 100, 250],
|
||
'prize' => ['екр.', 10, 25, 50, 75, 200],
|
||
'startlink' => 31
|
||
],
|
||
'rp' => [
|
||
'questname' => 'Руны 8 лвл.',
|
||
'quest' => 'Получить руну при плавке плохих заготовок. [S]',
|
||
'check' => ['Рун', 5, 10, 25, 50, 100],
|
||
'prize' => ['Плохие <br> заготовки <br>', 5, 7, 10, 12, 15],
|
||
'startlink' => 41
|
||
],
|
||
'rn' => [
|
||
'questname' => 'Руны 9 лвл.',
|
||
'quest' => 'Получить руну при плавке нормальных заготовок. [M]',
|
||
'check' => ['Рун', 5, 10, 15, 25, 50],
|
||
'prize' => ['Нормальные <br> заготовки <br>', 5, 7, 9, 12, 15],
|
||
'startlink' => 51
|
||
],
|
||
'rb' => [
|
||
'questname' => 'Руны 10 лвл.',
|
||
'quest' => 'Получить руну при плавке идеальных заготовок. [L]',
|
||
'check' => ['Рун', 2, 5, 8, 11, 15],
|
||
'prize' => ['Идеальные <br> заготовки <br>', 2, 4, 6, 10, 15],
|
||
'startlink' => 61
|
||
],
|
||
'pg' => [
|
||
'questname' => 'Героич. Сражения',
|
||
'quest' => 'Поучаствовать в героическом сражении.',
|
||
'check' => ['Участий', 100, 200, 400, 600, 1000],
|
||
'prize' => ['Жетон <br>успеха <br>', 10, 20, 40, 60, 80],
|
||
'startlink' => 71
|
||
],
|
||
'bv' => [
|
||
'questname' => 'Продажи Бабушке',
|
||
'quest' => 'Продать бабушке найденные вещи в пещере.',
|
||
'check' => ['Продаж', 5, 25, 50, 100, 250],
|
||
'prize' => ['кр.', 500, 1000, 2000, 4000, 5000],
|
||
'startlink' => 81
|
||
],
|
||
'kw' => [
|
||
'questname' => 'Клан Поединки',
|
||
'quest' => 'Победить в клановой войне.',
|
||
'check' => ['Побед', 5, 10, 15, 20, 25],
|
||
'prize' => ['Свиток <br> кланового<br> опыта ', 1, 2, 5, 10, 15],
|
||
'startlink' => 91
|
||
],
|
||
'pa' => [ # not implemented!
|
||
'questname' => 'Покровительство',
|
||
'quest' => 'Получить покровительство Ангела.',
|
||
'check' => ['Получено', 5, 25, 50, 100, 250],
|
||
'prize' => null,
|
||
'startlink' => null
|
||
],
|
||
'bs' => [
|
||
'questname' => 'Башня смерти',
|
||
'quest' => 'Победы в башне смерти.',
|
||
'check' => ['Побед', 1, 5, 10, 50, 500],
|
||
'prize' => ['екр.', 1, 5, 10, 50, 200],
|
||
'startlink' => 101
|
||
],
|
||
'trn' => [ # not implemented!
|
||
'questname' => 'Турниры',
|
||
'quest' => 'Поучавствовать в турнирах.',
|
||
'check' => ['Побед', 25, 50, 100, 200, 500],
|
||
'prize' => ['екр.', 25, 25, 25, 25, 50],
|
||
'startlink' => 111
|
||
],
|
||
];
|
||
|
||
public function __construct(User $user)
|
||
{
|
||
$this->user = $user;
|
||
$this->info = Db::getRow('select * from users_achiv where id = ?', [$user->info['id']]);
|
||
}
|
||
|
||
//fixme: сюда иногда приходит (NULL, NULL).
|
||
private function prizeName(?int $int, ?string $string): string
|
||
{
|
||
return in_array($string, ['екр.', 'кр.']) ? $int . ' ' . $string : $string . ' ' . $int;
|
||
}
|
||
|
||
private function drawSunduk($nagr, $whatInside, $tier): string
|
||
{
|
||
return vsprintf(
|
||
'<img onclick="%s" onmouseover="%s" onmouseout="%s" onmousedown="%s" class="cp" src="%s" alt="Сундук">',
|
||
[
|
||
"location.href='\main.php?titul&nagr=$nagr'",
|
||
"top.hi(this, '<b>Сундук $tier ранга<hr>Содержит:<hr> $whatInside</b>', event, 2, 0, 1, 1, 'max-width:307px')",
|
||
"top.hic();",
|
||
"top.hic();",
|
||
"//img.new-combats.tech/i/titul/nagr$tier.gif"
|
||
]
|
||
);
|
||
}
|
||
|
||
public function getList(): string
|
||
{
|
||
$string = '';
|
||
$this->calculateCurrent(1);
|
||
|
||
foreach ($this->data as $key => $value) {
|
||
|
||
//Бля, какую херню приходится городить!
|
||
//Первое число = требование следующего тира минус требование текущего тира (надо набрать)
|
||
//Второе число = количество достижений минус требование текущего тира (набрал)
|
||
//Требования текущего тира отнимаются чтобы каждый тир начинал считать процент с 0.
|
||
//Пизды дать тому, кто так уебански называет переменные, что нужно полдня въезжать что происходит.
|
||
//И хер поменяешь - всё сломается. FFUUUUUUUUUUUUUU!!! Ins.
|
||
|
||
if (!is_numeric($value['check'][$this->info['klas' . $key]])) {
|
||
// Потомы что первый элемент массива - слово. В РНР5.4 прокатывало, в РНР7.4 - нет.
|
||
$value['check'][$this->info['klas' . $key]] = 0;
|
||
}
|
||
|
||
$percentToSuccess = Math::get100Percentage(
|
||
$value['check'][$this->info['klas' . $key] + 1] - $value['check'][$this->info['klas' . $key]],
|
||
$this->info[$key] - $value['check'][$this->info['klas' . $key]]
|
||
);
|
||
$questname = empty($value['questname']) ? $value['quest'] : $value['questname'];
|
||
$string .= '<h4>' . $questname . '</h4>';
|
||
$string .= '<strong>' . $value['quest'] . '</strong><br>';
|
||
$string .= 'Текущий Ранг: <strong>' . $this->info['klas' . $key] . '</strong> ';
|
||
$string .= 'Количество ' . mb_strtolower($value['check'][0]) . ': <strong>' . $this->info[$key] . ' = ' . $percentToSuccess . '%</strong>';
|
||
//$string .= '<progress value="' . $percentToSuccess . '" max="100" style="accent-color:green; appearance: none;">' . $percentToSuccess . '</progress>';
|
||
|
||
$string .= Table::get([
|
||
['Ранги', 1, 2, 3, 4, 5],
|
||
$value['check'],
|
||
[
|
||
'Награда',
|
||
$this->prizeName($value['prize'][1], $value['prize'][0]),
|
||
$this->prizeName($value['prize'][2], $value['prize'][0]),
|
||
$this->prizeName($value['prize'][3], $value['prize'][0]),
|
||
$this->prizeName($value['prize'][4], $value['prize'][0]),
|
||
$this->prizeName($value['prize'][5], $value['prize'][0])
|
||
]
|
||
], 'prizelist', true);
|
||
# If has new tier, AND only first 5 tiers AND prize is set.
|
||
if (
|
||
$this->info['klas' . $key] > $this->info[$key . '_n'] &&
|
||
in_array($this->info[$key . '_n'], [0, 1, 2, 3, 4]) &&
|
||
!empty($value['prize']) &&
|
||
!empty($value['startlink'])
|
||
) {
|
||
$string .= '<br>Награда:<br>';
|
||
$string .= $this->drawSunduk(
|
||
$this->info[$key . '_n'] + $value['startlink'],
|
||
$this->prizeName($value['prize'][$this->info[$key . '_n'] + 1], $value['prize'][0]),
|
||
$this->info[$key . '_n'] + 1
|
||
);
|
||
}
|
||
$string .= '<hr>';
|
||
}
|
||
return $string;
|
||
}
|
||
|
||
private function calculateCurrent($check = null)
|
||
{
|
||
$this->info['bs'] = $this->user->info['win_bs'];
|
||
foreach ($this->info as $k => $v) {
|
||
if (
|
||
empty($this->info[$k]) &&
|
||
is_null($check) ||
|
||
$k === 'id' ||
|
||
!in_array($k, array_keys($this->data))
|
||
) {
|
||
continue;
|
||
}
|
||
|
||
if (is_null($this->info[$k])) {
|
||
$this->info[$k] = 0;
|
||
}
|
||
|
||
$this->info["klas$k"] = 0;
|
||
$this->info["name$k"] = $this->data[$k]['questname'];
|
||
$this->info["klasP$k"] = Math::get100Percentage($this->data[$k]['check'][1], $this->info[$k]);
|
||
|
||
for ($i = 1; $i <= 5; $i++) {
|
||
if ($this->info[$k] >= $this->data[$k]['check'][$i]) {
|
||
$this->info["klas$k"] = $i;
|
||
if ($i === 5) {
|
||
# На 5 ранге должно отдавать максимум.
|
||
$this->info["klasP$k"] = 100;
|
||
} else {
|
||
$this->info["klasP$k"] = Math::get100Percentage(
|
||
$this->data[$k]['check'][$i + 1] - $this->data[$k]['check'][$i],
|
||
$this->info[$k] - $this->data[$k]['check'][$i]
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public function getInfo(?int $uid = null)
|
||
{
|
||
if (!is_null($uid)) {
|
||
$this->info = \Core\Db::getRow('select * from users_achiv where id = ?', [$uid]);
|
||
}
|
||
$this->calculateCurrent();
|
||
return $this->info;
|
||
}
|
||
|
||
public function getCheckedInfo()
|
||
{
|
||
$this->calculateCurrent(1);
|
||
return $this->info;
|
||
}
|
||
|
||
private function addOne($name)
|
||
{
|
||
\Core\Db::sql("update users_achiv set $name = $name + 1 where id = ?", [$this->user->info['id']]);
|
||
}
|
||
|
||
public function updateTier($name)
|
||
{
|
||
if (in_array($name, array_keys($this->data))) {
|
||
$this->addOne($name . '_n');
|
||
}
|
||
}
|
||
|
||
public function updateCounter($name)
|
||
{
|
||
if (in_array($name, array_keys($this->data))) {
|
||
$this->addOne($name);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @return array[]
|
||
*/
|
||
public function getData(): array
|
||
{
|
||
return $this->data;
|
||
}
|
||
|
||
public function hasNoTier4Access($nagr): bool
|
||
{
|
||
return $this->user->info['level'] < 9 && in_array($nagr, [14, 15, 24, 25, 34, 35, 44, 45, 54, 55, 64, 65, 74, 75, 84, 85, 94, 95, 104, 105]);
|
||
}
|
||
|
||
public function givePrizeItems($itemId, $aType)
|
||
{
|
||
for ($i = 1; $i <= $this->data[$aType]['prize'][$this->info[$aType . '_n'] + 1]; $i++) {
|
||
$this->user->addItem($itemId, $this->user->info['id']);
|
||
}
|
||
}
|
||
|
||
public function getTypes(): array
|
||
{
|
||
return array_keys($this->data);
|
||
}
|
||
} |