<?php
include_once 'session_start.php';
$query_rebot = $pdo->query("Select id, hp, max_hp, last_active, time_p, yron_p,	tochn_p, speed_p, gruppa, regen from users where location = '$location' and gruppa != 'dolg' and gruppa != 'renegat' and gruppa != 'chistoenebo' and gruppa != 'svoboda' and gruppa != 'naemniki'");
$now = (date("Y-m-d H:i:s"));
$now = strtotime("$now");
while($row_rebot = $query_rebot->fetch()){
$hp_rebot = abs(intval($row_rebot['hp']));
$id_rebot = abs(intval($row_rebot['id']));
  if ($hp_rebot == 0) {
$max_hp_rebot = abs(intval($row_rebot['max_hp']));
$last_active_rebot = filter($row_rebot['last_active']);
    $last_active_rebot = strtotime("$last_active_rebot");
    $razn_rebot_t = ($now- $last_active_rebot);
	if ($razn_rebot_t >= 7) {
$pdo->exec("update users set hp = '$max_hp_rebot', last_active=NOW()  where  id = '$id_rebot' limit 1");
	}
  }
  else {
$last_active=filter($row_rebot['last_active']);
$regen_us=filter($row_rebot['regen']);
$hp_us=abs(intval($row_rebot['hp']));
$max_hp_us=abs(intval($row_rebot['max_hp']));
   $last_active_us = strtotime("$last_active");
   $hp_time_us = ($now - $last_active_us);
   if ($hp_time_us !=0) {
	$hp_up_us = ($hp_time_us * $regen_us);
    $hp_up_us = ($hp_us + $hp_up_us);
    if (($max_hp_us > $hp_up_us) and ($hp_up_us > 0)) {
	  $hp_us = $hp_up_us;
    }
    if ($hp_up_us > $max_hp_us) {
      $hp_us = $max_hp_us;
    }
  }
$time_p_abot = filter($row_rebot['time_p']);
	$time_p_abot = strtotime("$time_p_abot");
	$razn_abot = ($now - $time_p_abot);
	if ($razn_abot > $row_rebot['speed_p']) {
	  $query_abot = "Select id, hp, bronya, sost_cl, razriv_cl, sost_cl from users where location = '$location' and hp > 0 and last_active > NOW() - (60*5) and ban != 1 and id != '$id_rebot'";
      $result_abot = mysqli_query($dbc, $query_abot) or die ('Ошибка передачи запроса к БД7'); 
      $count_users = mysqli_num_rows($result_abot);
	  $row_count = mysqli_fetch_array($result_abot);
      $s=1;
      $rand_number = rand(1,$count_users);
      while ($s<>$rand_number) {
		$row_count = mysqli_fetch_array($result_abot);
        $s=$s+1;

      }
$id_set_abot = abs(intval($row_count['id']));
	  if ($row_count['sost_cl'] > 0) {
	    if ($row_count['gruppa'] == 'mytants') {
$bron = filter($row_count['razriv_cl']);
	    }
	    else {
$bron = filter($row_count['bronya']);
	    }
	  } 
	  else {
	    $bron = 0;
	  }
	  $p = rand(245,275);
      $p = ($p/1000);
	  if ($bron <> 0) {
	    $attack = ((($row_rebot['yron_p'] * $row_rebot['tochn_p']) / $bron) * $p);
	  }
	  else {
	    $attack = ((($row_rebot['yron_p'] * $row_rebot['tochn_p'])/(300)) * $p);
	  }
$id_abot = abs(intval($row_count['id']));
	  $hp_abot = ($row_count['hp'] - $attack);
	  if ($hp_abot < 0) {
	    $hp_abot = 0;
	  }
$pdo->exec("insert into log (`time`, `user_id`, `sboitie`, `thing` , `yron`) values (NOW(), '$id_rebot', 1 , '$id_abot', '$attack')");
$pdo->exec("update users set hp = '$hp_abot' where  id = '$id_abot' limit 1");
$pdo->exec("update users set last_active=NOW(), hp='$hp_us',  time_p=NOW() where  id = '$id_rebot' limit 1");
	  
	}
  }
}
?>