<?php
if($_SESSION['uid'] == null) { header("Location: index.php"); }
$vip = mysql_fetch_array(mysql_query('SELECT `id`, `klan`, `unclone` FROM `abils_klan` WHERE `klan` = "'.$user['klan'].'" LIMIT 1'));

if($user['battle'] == 0) {
  echo "Это боевая магия...";
} else {
  if($vip['unclone'] >= 1) {
	$bot =  mysql_fetch_array(mysql_query("SELECT * FROM `bots` WHERE `name` = '".mysql_real_escape_string($_POST['target'])."' AND `battle` = '".$user['battle']."' LIMIT 1"));
	
    if($bot) {
      $prototype = mysql_fetch_array(mysql_query('SELECT `id`, `intel` FROM `users` WHERE `id` = "'.$bot['prototype'].'" LIMIT 1'));
    }
    if($prototype['intel'] > $user['intel']) {
      echo 'Ваш противник имеет более высокий интелект ...';
    } elseif($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] = array(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);

	  addlog($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'].' LIMIT 1');
	  mysql_query("UPDATE `battle` SET `to1` = '".time()."', `to2` = '".time()."' WHERE `id` = ".$user['battle']." LIMIT 1");							
      echo "Вы переманили клона";
      $vip['unclone'] -= 1;
      mysql_query('UPDATE `abils_klan` SET `unclone` = "'.$vip['unclone'].'" WHERE `klan` = "'.$vip['klan'].'" LIMIT 1');
    } else {
	  echo "Нет такого клона";
	}
  } else {
	echo "Свиток рассыпался в ваших руках...";
  }
}