<?php

namespace User;

use Core\Db;

/**
 * Непонятный кусок функции, который лежал внутри системы подсчёта бонусов к параметрам,
 * при этом никак на это не влияя и ни с чем не взаимодействуя. Надо понять что его
 * вызывает и выгнать его из User\Stats.
 */
class WearedScrolls
{

    public static function ScrollsChange(array &$u): void
    {
        //Замена свитков
        if (empty($u['autospell']) || !empty($u['battle'])) {
            return;
        }
        //проверяем свитки
        $sparr = [];
        foreach (Db::getRows('select item_id, inOdet from items_users where inOdet between 40 and 50 and uid = ? order by item_id desc', [$u['id']]) as $scroll) {
            $sparr[] = "{$scroll['item_id']} - {$scroll['inOdet']}";
        }
        $splink = implode(',', $sparr);

        //Запоминаем новый комплект свитков
        if ($u['autospell'] == 1) {
            $u['autospell'] = $splink;
            Db::sql('update users set autospell = ? where id = ?', [$splink, $u['id']]);
        }

        //Выдаем нужный свиток, если он есть в инвентаре
        if ($u['autospell'] != $splink) {
            $spe1 = explode(',', $splink);
            $spe2 = explode(',', $u['autospell']);
            $spe1g = [];
            $spe2g = [];

            for ($i = 0; $i <= 20; $i++) {
                $spe1a = explode('-', $spe1[$i]);
                $spe2a = explode('-', $spe2[$i]);

                if (isset($spe1a[0])) {
                    $spe1g[$spe1a[1]] = $spe1a[0];
                }
                if (isset($spe2a[0])) {
                    $spe2g[$spe2a[1]] = $spe2a[0];
                }
            }

            for ($i = 40; $i <= 50; $i++) {
                if ($spe1g[$i] == $spe2g[$i] || $spe1g[$i] != 0) {
                    continue;
                }
                Db::sql('update items_users set inOdet = ? where inOdet = 0 and `delete` = 0 and inTransfer = 0 and inShop = 0 and inGroup = 0 and item_id = ? and uid = ?',
                    [$i, $spe2g[$i], $u['id']]);
            }
        }

    }
}