<?php
/**
 * Copyright (c) 2018.
 * Author: Igor Barkov <lopar.4ever@gmail.com>
 * Project name: Battles-Game
 */

use Battles\DressedItems;

require_once '../functions.php';

if (!$tr && $turnirstart[0] <= time() && $dd[0] >= 2) {
    $minroom = 501;
    $maxroom = 560;

    $data = mysql_query("SELECT `dt`.`owner` FROM `deztow_stavka` AS `dt`, `online` AS `o` WHERE (SELECT COUNT(`id`) FROM `effects` WHERE `effects`.`owner` = `dt`.`owner` AND (`type` = 11 OR `type` = 12 OR `type` = 13 OR `type` = 14 OR `type` = 2 OR `type` = 3 OR `type` = 4 OR `type` = 202 OR `type` = 201 OR `type` = 21)) = 0  AND `o`.`id` = `dt`.`owner` AND `room` = 31 AND `o`.login_time >= '" . (time() - 300) . "' ORDER BY `kredit` DESC, `dt`.`time` ASC  LIMIT 50");
    $stavka = mysql_fetch_array(mysql_query("SELECT SUM(`kredit`)*0.7 FROM `deztow_stavka`"));

    while ($row = mysql_fetch_array($data)) {

        $pers = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "' . $row[0] . '" LIMIT 1'));
        DressedItems::undressAllItems($pers['id']);
        ###
        mysql_query('UPDATE `inventory` SET `owner` = "' . ($pers['id'] + _BOTSEPARATOR_) . '" WHERE `owner` = "' . $pers['id'] . '"');
        ###

        ###
        $efs = mysql_query('SELECT * FROM `effects` WHERE `owner` = "' . $pers['id'] . '"');
        $travm = [11, 12, 13, 14];
        while ($row = mysql_fetch_array($efs)) {
            if (in_array($row['type'], $travm)) {
                $pers['sila'] += $row['sila'];
                $pers['lovk'] += $row['lovk'];
                $pers['inta'] += $row['inta'];
                $pers['vinos'] += $row['vinos'];
            } else {
                $pers['sila'] -= $row['sila'];
                $pers['lovk'] -= $row['lovk'];
                $pers['inta'] -= $row['inta'];
                $pers['vinos'] -= $row['vinos'];
                $pers['maxhp'] -= $row['hp'];
            }
            $row['owner'] = ($user['id'] + _BOTSEPARATOR_);
            mysql_query('UPDATE `effects` SET `owner` = "' . $row['owner'] . '" WHERE `id` = "' . $row['id'] . '" LIMIT 1');
            mysql_query('UPDATE `users` SET `sila` = "' . $pers['sila'] . '", `lovk` = "' . $pers['lovk'] . '", `inta` = "' . $pers['inta'] . '", `vinos` = "' . $pers['vinos'] . '", `maxhp` = "' . $pers['maxhp'] . '" WHERE `id` = "' . $pers['id'] . '" LIMIT 1');
        }
        $udata = mysql_fetch_array(mysql_query('SELECT * FROM `deztow_realchars` WHERE `owner` = "' . $pers['id'] . '" LIMIT 1'));
        if (isset($udata['id'])) {
            $udata['sila'] = $pers['sila'];
            $udata['lovk'] = $pers['lovk'];
            $udata['inta'] = $pers['inta'];
            $udata['vinos'] = $pers['vinos'];
            $udata['maxhp'] = $pers['maxhp'];
            $udata['money'] = $pers['money'];
            $udata['level'] = $pers['level'];
            $udata['exp'] = $pers['exp'];
            $udata['nextup'] = $pers['nextup'];
            $udata['align'] = $pers['align'];
            $udata['stats'] = $pers['stats'];
            $udata['master'] = $pers['master'];
            $udata['intel'] = $pers['intel'];
            $udata['noj'] = $pers['noj'];
            $udata['mec'] = $pers['mec'];
            $udata['topor'] = $pers['topor'];
            $udata['dubina'] = $pers['dubina'];
            $udata['mlight'] = $pers['mlight'];
            $udata['mgray'] = $pers['mgray'];
            $udata['mdark'] = $pers['mdark'];
            mysql_query('UPDATE `deztow_realchars` SET `sila` = "' . $udata['sila'] . '", `lovk` = "' . $udata['lovk'] . '", `inta` = "' . $udata['inta'] . '", `vinos` = "' . $udata['vinos'] . '", `maxhp` = "' . $udata['maxhp'] . '", `money` = "' . $udata['money'] . '", `intel` = "' . $udata['intel'] . '", `level` = "' . $udata['level'] . '", `exp` = "' . $udata['exp'] . '", `nextup` = "' . $udata['nextup'] . '", `align` = "' . $udata['align'] . '", `stats` = "' . $udata['stats'] . '", `master` = "' . $udata['master'] . '", `noj` = "' . $udata['noj'] . '", `mec` = "' . $udata['mec'] . '", `topor` = "' . $udata['topor'] . '", `dubina` = "' . $udata['dubina'] . '", `mlight` = "' . $udata['mlight'] . '", `mgray` = "' . $udata['mgray'] . '", `mdark` = "' . $udata['mdark'] . '" WHERE `owner` = "' . $udata['id'] . '" LIMIT 1');
        } else {
            mysql_query('INSERT INTO `deztow_realchars` (`owner`, `sila`, `lovk`, `inta`, `vinos`, `maxhp`, `intel`, `level`, `money`, `exp`, `nextup`, `align`, `stats`, `master`, `noj`, `mec`, `topor`, `dubina`, `mlight`, `mgray`, `mdark`) VALUES ("' . $pers['id'] . '", "' . $pers['sila'] . '", "' . $pers['lovk'] . '", "' . $pers['inta'] . '", "' . $pers['vinos'] . '", "' . $pers['maxhp'] . '", "' . $pers['intel'] . '", "' . $pers['level'] . '", "' . $pers['money'] . '", "' . $pers['exp'] . '", "' . $pers['nextup'] . '", "' . $pers['align'] . '", "' . $pers['stats'] . '", "' . $pers['master'] . '", "' . $pers['noj'] . '", "' . $pers['mec'] . '", "' . $pers['topor'] . '", "' . $pers['dubina'] . '", "' . $pers['mlight'] . '", "' . $pers['mgray'] . '", "' . $pers['mdark'] . '")');
        }

        $tec = mysql_fetch_array(mysql_query("SELECT `id`, `owner`, `name`, `sila`, `lovk`, `inta`, `vinos`, `intel`, `mudra`, `def` FROM `deztow_charstams` WHERE `owner` = '{$row['owner']}' AND `def` = '1' LIMIT 1"));
        $usk = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '" . $row[0] . "' LIMIT 1"));

        if (isset($tec['id'])) {
            mysql_query("UPDATE `users` SET `sila` = '" . $tec['sila'] . "', `lovk` = '" . $tec['lovk'] . "', `inta` = '" . $tec['inta'] . "', `vinos` = '" . $tec['vinos'] . "', `intel` = '" . $tec['intel'] . "', `stats` = 0, `noj` = 0, `mec` = 0, `topor` = 0, `dubina` = 0, `mfire` = 0, `mwater` = 0, `mair` = 0, `mearth` = 0, `mlight` = 0, `mgray` = 0, `mdark` = 0, `master` = '8', `maxhp` = '" . ($tec['vinos'] * 6) . "', `hp` = '" . ($tec['vinos'] * 6) . "', `level` = 7, `nextup` = 999999999 WHERE `id` = '" . $usk['id'] . "' LIMIT 1");
        } else {
            mysql_query("UPDATE `users` SET `sila` = '3', `lovk` = '3', `inta` = '3', `vinos` = '10', `intel` = '0', `stats` = 64, `noj` = 0, `mec` = 0, `topor` = 0, `dubina` = 0, `mfire` = 0, `mwater` = 0, `mair` = 0, `mearth` = 0, `mlight` = 0, `mgray` = 0, `mdark` = 0, `master` = '8', `maxhp` = '60', `hp` = '60', `level` = 7, `nextup` = 999999999 WHERE `id` = '" . $usk['id'] . "' LIMIT 1");
        }

        $rum = rand($minroom, $maxroom);
        $urs[$row['owner']] = $rum;
        $i++;
        if ($i > 1) {
            $lors .= ", ";
        }
        $lors .= Nick::id($row['owner'])->full(1);
    }

    // Боты: Архивариус и два помощника.
    DressedItems::undressAllItems(233);
    DressedItems::undressAllItems(234);
    DressedItems::undressAllItems(235);

    mysql_query("DELETE FROM `inventory` WHERE `owner` = 233");
    mysql_query("DELETE FROM `inventory` WHERE `owner` = 234");
    mysql_query("DELETE FROM `inventory` WHERE `owner` = 235");
    $rums = rand($minroom, $maxroom);
    mysql_query("UPDATE `users` SET `in_tower` = 1, `chattime` = '999999999999', `room` = '" . $rums . "' WHERE `id` = '233' LIMIT 1");
    mysql_query("UPDATE `users` SET `in_tower` = 1, `chattime` = '999999999999', `room` = '" . $rums . "' WHERE `id` = '234' LIMIT 1");
    mysql_query("UPDATE `users` SET `in_tower` = 1, `chattime` = '999999999999', `room` = '" . $rums . "' WHERE `id` = '235' LIMIT 1");
    mysql_query("UPDATE `online` SET `room` = '" . $rums . "', `date` = '999999999999' WHERE `id` = '233' LIMIT 1");
    mysql_query("UPDATE `online` SET `room` = '" . $rums . "', `date` = '999999999999' WHERE `id` = '234' LIMIT 1");
    mysql_query("UPDATE `online` SET `room` = '" . $rums . "', `date` = '999999999999' WHERE `id` = '235' LIMIT 1");
    echo mysql_error();
    $i++;
    $lors .= ", " . Nick::id(233)->full(1);
    $lors .= ", " . Nick::id(234)->full(1);
    $lors .= ", " . Nick::id(235)->full(1);
    $lors .= ltrim(",", $lors);

    mysql_query("TRUNCATE TABLE `deztow_items`");
    $shmots = ["1", "1", "92", "92", "93", "93", "19", "19", "20", "20", "20", "23", "23", "24", "14", "87", "87", "6", "6", "17", "17", "17", "17", "11", "11", "12", "12", "12", "28", "28", "43", "43", "36", "36", "36", "37", "37", "37", "38", "38", "38", "50", "50", "57", "52", "52", "51", "51", "48", "48", "47", "47", "49", "49", "59", "59", "60", "60", "61", "61", "63", "64", "64", "65", "65", "66", "66", "68", "68", "69", "69", "72", "72", "4", "5", "79", "79", "80", "76", "75", "75", "94", "94", "95", "95", "82", "91", "91", "34", "34", "86", "86", "86", "9", "9", "101", "101", "101", "101", "101", "97", "97", "97", "97", "100", "100", "100", "100", "171", "171", "171", "171", "103", "103", "103", "104", "105", "105", "106", "106", "107", "107", "108", "108", "109", "110", "111", "112", "112", "113", "113", "119", "364", "284", "284", "284", "284", "284", "285", "285", "285", "285", "286", "286", "286", "286"];
    while ($sh = array_shift($shmots)) {
        $shopid = mysql_fetch_array(mysql_query("SELECT `id`, `name`, `img` FROM `shop` WHERE `id` = '" . $sh . "' LIMIT 1"));
        if (isset($shopid['id'])) {
            mysql_query("INSERT `deztow_items` (`iteam_id`, `name`, `img`, `room`) VALUES ('" . $shopid['id'] . "', '" . $shopid['name'] . "', '" . $shopid['img'] . "', '" . rand($minroom, $maxroom) . "')");
        }
    }

    $log = '<span class=date>' . date("d.m.y H:i") . '</span> Начало турнира. Участники : ' . $lors . '<br />';
    $end = (time() + 21600);
    mysql_query("INSERT `deztow_turnir` (`type`, `winner`, `coin`, `start_time`, `log`, `endtime`, `active`) VALUES ('" . rand(1, 7) . "', '', '" . $stavka[0] . "', '" . time() . "', '" . $log . "', '" . $end . "', '1')");

    foreach ($urs as $row => $rum) {
        mysql_query("UPDATE `users` SET `in_tower` = 1, `room` = '" . $rum . "' WHERE `id` = '" . $row . "'");
        mysql_query("UPDATE `online` SET `room` = '" . $rum . "' WHERE `id` = '" . $row . "' LIMIT 1");
    }
}
mysql_query('LOCK TABLES `inventory` WRITE, `variables` WRITE, `deztow_turnir` WRITE, `deztow_stavka` WRITE, `effects` WRITE, `deztow_charstams` WRITE, `deztow_gamers_inv` WRITE, `deztow_items` WRITE, `deztow_trap` WRITE, `online` WRITE');
$turnirstart = mysql_fetch_array(mysql_query("SELECT `value`, `bs_art` FROM `variables` WHERE `var` = 'startbs' LIMIT 1"));
$turnirstart['value'] -= 900;

if (date("z-H-i") == date("z-H-i", $turnirstart['value']))
    if ($turnirstart['bs_art'] == 0) {
        file_get_contents(GAMEDOMAIN . '/chats.php?id=4');
    } elseif ($turnirstart['bs_art'] == 1) {
        file_get_contents(GAMEDOMAIN . '/chats.php?id=4');
    }

$tr = mysql_fetch_array(mysql_query("SELECT `id`, `type`, `winner`, `coin`, `start_time`, `log`, `endtime`, `active`, `winnerlog`, `art` FROM `deztow_turnir` WHERE `active` = TRUE LIMIT 1"));
$turnirstart = mysql_fetch_array(mysql_query("SELECT `value`, `bs_art` FROM `variables` WHERE `var` = 'startbs' LIMIT 1"));
$dd = mysql_fetch_array(mysql_query("SELECT COUNT(`kredit`) FROM `deztow_stavka` WHERE `owner` != 1254"));

if ($dd[0] < 2 && $turnirstart[0] <= time()) {
    if ($turnirstart['bs_art'] == 1) {
        $bs_art = 0;
    } elseif ($turnirstart['bs_art'] == 0) {
        $bs_art = 0;
    }
    mysql_query('UPDATE `variables` SET `value` = "' . (time() + 60 * 60) . '", `bs_art` = "' . $bs_art . '" WHERE `var` = "startbs" LIMIT 1');
    mysql_query("INSERT INTO `deztow_stavka` (`owner`, `kredit`, `time`) VALUES ('1254', '30', '" . time() . "')");
    file_get_contents(GAMEDOMAIN . '/chats.php?id=5');
}

$tur_data = mysql_fetch_array(mysql_query("SELECT `id`, `type`, `winner`, `coin`, `start_time`, `log`, `endtime`, `active`, `winnerlog`, `art` FROM `deztow_turnir` WHERE `active` = '1' LIMIT 1"));
if ($tur_data['endtime'] < time() && $tur_data['id']) {
    $list = mysql_query("SELECT * FROM `users` WHERE `in_tower` = 1");

    while ($u = mysql_fetch_array($list)) {
        DressedItems::undressAllItems($u['id']);
        $rep = mysql_query("SELECT * FROM `inventory` WHERE `owner` = '" . $u['id'] . "' AND `bs` = 1");
        while ($r = mysql_fetch_array($rep)) {
            mysql_query("INSERT `deztow_items` (`iteam_id`, `name`, `img`, `room`) VALUES ('" . $r['prototype'] . "', '" . $r['name'] . "', '" . $r['img'] . "', '" . $u['room'] . "')");
        }
        mysql_query("DELETE FROM `inventory` WHERE `owner` = '" . $u['id'] . "' AND `bs` = 1");

        $rep = mysql_query("SELECT `id_item`, `owner` FROM `deztow_gamers_inv` WHERE `owner` = '" . $u['id'] . "'");
        while ($r = mysql_fetch_array($rep)) {
            mysql_query("UPDATE `inventory` SET `owner` = '" . $u['id'] . "' WHERE `owner` = '0' AND `id` = '" . $r['id_item'] . "'");
        }

        $tec = mysql_fetch_array(mysql_query("SELECT `id`, `owner`, `name`, `sila`, `lovk`, `inta`, `vinos`, `intel`, `maxhp`, `money`, `exp`, `mudra`, `stats`, `nextup`, `level`, `align`, `master`, `noj`, `mec`, `topor`, `dubina`, `mlight`, `mgray`, `mdark` FROM `deztow_realchars` WHERE `owner` = '{$u['id']}' LIMIT 1"));
        if (isset($tec['id'])) {
            $u = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '{$u['id']}' LIMIT 1"));
            $stats = $tec['stats'];
            $master = $tec['master'];
            mysql_query("UPDATE `users` SET `sila` = '" . $tec['sila'] . "', `lovk` = '" . $tec['lovk'] . "', `inta` = '" . $tec['inta'] . "', `vinos` = '" . $tec['vinos'] . "', `hp` = '" . ($tec['vinos'] * 6) . "', `maxhp` = '" . ($tec['vinos'] * 6) . "', `intel` = '" . $tec['intel'] . "', `stats` = '" . $tec['stats'] . "', `nextup` = '" . $tec['nextup'] . "', `level` = '" . $tec['level'] . "', `align` = '" . $tec['align'] . "', `noj` = '" . $tec['noj'] . "', `mec` = '" . $tec['mec'] . "', `topor` = '" . $tec['topor'] . "', `dubina` = '" . $tec['dubina'] . "', `mlight` = '" . $tec['mlight'] . "', `mgray` = '" . $tec['mgray'] . "', `mdark` = '" . $tec['mdark'] . "', `master` = '" . $tec['master'] . "' WHERE `id` = '" . $u['id'] . "' LIMIT 1");
            mysql_query("DELETE FROM `deztow_realchars` WHERE `owner` = '{$u['id']}'");

            $eff = mysql_fetch_array(mysql_query("SELECT * FROM `effects` WHERE `owner` = '" . $u['id'] . "' AND (`type` = 1 `type` = 2 `type` = 3 `type` = 4 `type` = 11 OR `type` = 12 OR `type` = 13 OR `type` = 14 `type` = 21 `type` = 201 `type` = 202)"));
            mysql_query("DELETE FROM `effects` WHERE `owner` = '" . $u['id'] . "' AND `type` != 1 AND `type` != 2 AND `type` != 3 AND `type` != 4 AND `type` != 5 AND `type` != 6 AND `type` != 11 AND `type` != 12 AND `type` != 13 AND `type` != 14");

            if (isset($tec['id'])) {
                mysql_query("UPDATE `users` SET `sila` = (`sila`-'" . $eff['sila'] . "'), `lovk` = (`lovk`-'" . $eff['lovk'] . "'), `inta` = (`inta`-'" . $eff['inta'] . "'), `maxhp` = (`maxhp`-'" . $eff['hp'] . "') WHERE `id` = '" . $eff['owner'] . "' LIMIT 1");
            }

            $pers = db::c()->query('SELECT * FROM users WHERE id = ?i', $u['id'])->fetch_assoc_array();
            DressedItems::undressAllItems($pers['id']);
            db::c()->query('UPDATE inventory SET owner = ?i WHERE owner = ?i', $pers['id'], $pers['id'] + _BOTSEPARATOR_);
            ###
            $row = db::c()->query('SELECT * FROM `effects` WHERE `owner` = ?i', $pers['id'] + _BOTSEPARATOR_);
            $travm = [11, 12, 13, 14];
            while ($efs = $row->fetch_assoc()) {
                if (in_array($efs['type'], $travm)) {
                    $pers['sila'] -= $efs['sila'];
                    $pers['lovk'] -= $efs['lovk'];
                    $pers['inta'] -= $efs['inta'];
                    $pers['vinos'] -= $efs['vinos'];
                } else {
                    $pers['sila'] += $efs['sila'];
                    $pers['lovk'] += $efs['lovk'];
                    $pers['inta'] += $efs['inta'];
                    $pers['vinos'] += $efs['vinos'];
                    $pers['maxhp'] += $efs['hp'];
                }
                $efs['owner'] = $_SESSION['uid'];
                mysql_query('UPDATE `effects` SET `owner` = "' . $efs['owner'] . '" WHERE `id` = "' . $efs['id'] . '" LIMIT 1');
                mysql_query('UPDATE `users` SET `sila` = "' . $pers['sila'] . '", `lovk` = "' . $pers['lovk'] . '", `inta` = "' . $pers['inta'] . '", `vinos` = "' . $pers['vinos'] . '", `maxhp` = "' . $pers['maxhp'] . '" WHERE `id` = "' . $pers['id'] . '" LIMIT 1');



                mysql_query("UPDATE `users` SET `in_tower` = 0, `room` = '31' WHERE `id` = '" . $u['id'] . "' LIMIT 1");
            mysql_query("UPDATE `online` SET  `room` = '31' WHERE `id` = '" . $u['id'] . "' LIMIT 1");
        }
    }
    mysql_query('UPDATE `deztow_turnir` SET `winner` = \'NO!\', `winnerlog` = \'<b>Ничья</b>\', `endtime` = ' . time() . ', `active` = 0, `log` = CONCAT(`log`,\'' . "Турнир закончен по тайм-ауту. Победителя нет!<br />" . '\') WHERE `active` = TRUE LIMIT 1');

    $turnirstart = mysql_fetch_array(mysql_query("SELECT `value`, `bs_art` FROM `variables` WHERE `var` = 'startbs' LIMIT 1"));
    if ($turnirstart['bs_art'] == 1) {
        $bs_art = 0;
    } elseif ($turnirstart['bs_art'] == 0) {
        $bs_art = 0; #1
    }
    $times = (time() + 7200);
    mysql_query("UPDATE `variables` SET `value` = '" . $times . "', `bs_art` = '" . $bs_art . "' WHERE `var` = 'startbs' LIMIT 1");
    mysql_query("TRUNCATE TABLE `deztow_stavka`");
    mysql_query("TRUNCATE TABLE `deztow_gamers_inv`");
    mysql_query("TRUNCATE TABLE `deztow_items`");
    mysql_query("TRUNCATE TABLE `deztow_trap`");
}
mysql_query('UNLOCK TABLES');