<?php

use Battles\GameLogs;
use Battles\Nick;

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

if ($user['battle'] == 0) {
    echo "ќто боеваЯ магиЯ...";
} else {
    $magic = mysql_fetch_array(mysql_query("SELECT `chanse` FROM `magic` WHERE `id` = '17' ;"));

    if ($user['intel'] >= 3) {
        $int = $magic['chanse'] + ($user['intel'] - 3) * 3;
        if ($int > 98) {
            $int = 99;
        }
    } else {
        $int = 0;
    }

    if (rand(1, 100) < $int) {
        $bot = mysql_fetch_array(mysql_query("SELECT * FROM `bots` WHERE `name` = '" . $_POST['target'] . "';"));
        if ($bot && strpos($_POST['target'], "(клон")) {
            $bot = $bot[0];

            $bd = mysql_fetch_array(mysql_query('SELECT * FROM `battle` WHERE `id` = ' . $user['battle'] . ' LIMIT 1;'));
            $battle = unserialize($bd['teams']);
            $battle[$bot] = $battle[$user['id']];
            foreach ($battle as $k => $v) {
                unset($battle[$k][$bot]);
            }

            foreach ($battle[$user['id']] as $k => $v) {
                $battle[$k][$bot] = [0, 0, time()];
            }
            $t1 = explode(";", $bd['t1']);
            $t2 = explode(";", $bd['t2']);
            foreach ($t1 as $k => $v) {
                if ($v == $bot) {
                    unset($t1[$k]);
                }
            }
            foreach ($t2 as $k => $v) {
                if ($v == $bot) {
                    unset($t2[$k]);
                }
            }
            // проставлЯем кто-где
            if (in_array($user['id'], $t1)) {
                $ttt = 1;
                $t1[] = $bot;
            } else {
                $ttt = 2;
                $t2[] = $bot;
            }

            $t1 = implode(";", $t1);
            $t2 = implode(";", $t2);

            GameLogs::addBattleLog($user['battle'], '<span class=date>' . date("H:i") . '</span> ' . Nick::id($user['id'])->short() . ' переманил клона ' . Nick::id($bot)->short() . ' на свою сторону<BR>');
            mysql_query('UPDATE `battle` SET `teams` = \'' . serialize($battle) . '\', `t1` = \'' . $t1 . '\', `t2` = \'' . $t2 . '\'  WHERE `id` = ' . $user['battle'] . ' ;');

            mysql_query("UPDATE `battle` SET `to1` = '" . time() . "', `to2` = '" . time() . "' WHERE `id` = " . $user['battle'] . " LIMIT 1;");
            mysql_query("UPDATE bots set 'owner'='" . $user['id'] . "' WHERE `name` = '" . $_POST['target'] . "';");

            $bet = 1;
            echo "‚ы переманили клона";
        } else {
            echo "Ќет такого клона";
        }
    } else {
        echo "свиток рассыпалсЯ в ваших руках...";
        $bet = 1;
    }
}

