<?php
if (empty($_SESSION['uid'])) {
    header("Location: index.php");
    exit;
}

//запрещенная для встройки магия
$forbidden = ["1", "2", "3", "7", "6", "8", "18", "19", "20", "21", "22", "43", "46", "47", "48", "55", "56", "58", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
    "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106",
    "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121",
    "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139",];
// magic
// встраивание магии
if ($user['battle'] > 0) {
    echo "Не в бою...";
} else {
    if ($user['intel'] >= 17) {
        $int = 80 + $user['intel'] - 17;
        if ($int > 100) {
            $int = 100;
        }
    } else {
        $int = 0;
    }
    if (rand(1, 100) <= $int or !$_SESSION['scroll']) {
        if (!$_SESSION['scroll']) {
            $_SESSION['scroll'] = $_POST['target'];
            ?>
            <body
            onload="okno('Название предмета, в который встраивается свиток', 'main.php?edit=1&use=<?= $_GET['use'] ?>','target')"><?
        } else {
            $svitok = mysql_fetch_array(mysql_query("SELECT * FROM `inventory` WHERE `name` = '" . $_SESSION['scroll'] . "' AND `owner` = '{$user['id']}' AND `dressed`=0 LIMIT 1;"));
            $dress = mysql_fetch_array(mysql_query("SELECT * FROM `inventory` WHERE `owner` = '{$user['id']}' AND `name` LIKE '{$_POST['target']}' AND `includemagic` = 0 AND `dressed`=0  LIMIT 1;"));
            //echo "SELECT * FROM `inventory` WHERE `owner` = '{$user['id']}' AND `name` LIKE '{$_REQUEST['target']}' AND `includemagic` = 0  LIMIT 1;";
            $_SESSION['scroll'] = null;
            if (!$svitok) {
                echo "<font color=red><b>У вас нет такого свитка!<b></font>";
            } elseif (!$dress or $dress['type'] >= 12) {
                echo "<font color=red><b>У вас нет такого предмета!<b></font>";
            } else {
                $incmagic = mysql_fetch_array(mysql_query("SELECT * FROM `magic` WHERE `id` = '{$svitok['magic']}' LIMIT 1;"));
                if ((!$incmagic['img']) || in_array($svitok['magic'], $forbidden)) {
                    echo "<font color=red><b>Этот свиток нельзя встраивать в предметы!<b></font>";
                } else {
                    // встраиваем
                    \Battles\InventoryItem::destroyItem($svitok['id']);
                    echo "<font color=red><b>Свиток \"" . $svitok['name'] . "\" удачно встроен в \"" . $dress['name'] . "\"<b></font>";
                    mysql_query("UPDATE `inventory` SET 
						" . ($dress['nintel'] < $svitok['nintel'] ? "`nintel`='" . $svitok['nintel'] . "'," : "") . "
						" . ($dress['nlevel'] < $svitok['nlevel'] ? "`nlevel`='" . $svitok['nlevel'] . "'," : "") . "
						" . ($dress['nmudra'] < $svitok['nmudra'] ? "`nmudra`='" . $svitok['nmudra'] . "'," : "") . "
						" . ($dress['ngray'] < $svitok['ngray'] ? "`ngray`='" . $svitok['ngray'] . "'," : "") . "
						" . ($dress['ndark'] < $svitok['ndark'] ? "`ndark`='" . $svitok['ndark'] . "'," : "") . "
						" . ($dress['nlight'] < $svitok['nlight'] ? "`nlevel`='" . $svitok['nlight'] . "'," : "") . "						
						`massa`=`massa`+1,`cost`=`cost`+'" . $svitok['cost'] . "', `includemagic` = '" . $svitok['magic'] . "', `includemagicdex` = '" . $svitok['maxdur'] . "', `includemagicmax` = '" . $svitok['maxdur'] . "', `includemagicname` = '" . $svitok['name'] . "', `includemagicuses` = '100+" . $user['intel'] . "', `includemagiccost` = '" . ($svitok['cost'] / 2) . "' WHERE `id` = '{$dress['id']}' LIMIT 1;");
                    $bet = 1;
                }
            }
        }
    } else {
        echo "<font color=red><b>Cвиток рассыпался в ваших руках...<b></font>";
        $bet = 1;
        $_SESSION['scroll'] = null;
    }
}