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

include_once '../config.php';
include_once '../functions.php';


$r = db::c()->query('SELECT `id`, `win` FROM `battle` WHERE `needbb` = 2 AND `win` = 3');
$r2 = mysql_query("SELECT `id`, `win` FROM `battle` WHERE `needbb` = 1 AND `win` = 3 AND (`timeout`*90+`to1` < ".time()." OR `timeout`*90+`to2` < ".time().")");

if($r->getNumRows() || mysql_num_rows($r2) > 0) { include_once '../fbattle.php'; }
$battles = array();
while($rec = $r->fetch_assoc()) {
  $fbattles = new fbattle($rec['id']);
  foreach($fbattles->battle as $k => $v) {
    if($k > _BOTSEPARATOR_) {
      foreach($v as $k2 => $v2) {
        if($k2 < _BOTSEPARATOR_) continue;
        $hp_1 = mysql_fetch_array(mysql_query('SELECT `hp` FROM `bots` WHERE `id` = "'.$k.'" LIMIT 1'));
        $hp_2 = mysql_fetch_array(mysql_query('SELECT `hp` FROM `bots` WHERE `id` = "'.$k2.'" LIMIT 1'));
        if($fbattles->battle[$k][$k2][0] && $fbattles->battle[$k2][$k][0] && $hp_1['hp'] > 0 && $hp_2['hp'] > 0) {
          $at_1 = $fbattles->battle[$k][$k2][0];
          $def_1 = $fbattles->battle[$k][$k2][1];
          $at_2 = $fbattles->battle[$k2][$k][0];
          $def_2 = $fbattles->battle[$k2][$k][1];
          $fbattles->makechange($k, $k2, $at_1, $at_2, $def_1, $def_2);
          $fbattles->write_log();
          $fbattles->battle[$k][$k2] = array(0, 0, time());
          $fbattles->battle[$k2][$k] = array(0, 0, time());
          $fbattles->needupdate = 1;
          if($cond) { $tocond .= " OR "; }
          $battles[$rec['id']] = 1;
        }
      }
    }
  }
  if($fbattles->needupdate) { $fbattles->UpdateBattle(); }
}

while($rec = mysql_fetch_assoc($r2)) {
  $fbattles = new fbattle($rec['id']);
  foreach($fbattles->battle as $k => $v) {
    if($k > _BOTSEPARATOR_) {
      foreach($v as $k2 => $v2) {
        if($k2 > _BOTSEPARATOR_) continue;
        $hp_1 = mysql_fetch_array(mysql_query('SELECT `hp` FROM `bots` WHERE `id` = "'.$k.'" LIMIT 1'));
        $hp_2 = mysql_fetch_array(mysql_query('SELECT `hp` FROM `bots` WHERE `id` = "'.$k2.'" LIMIT 1'));
        if($hp_1['hp'] > 0 && $hp_2['hp'] > 0) {
          $at_1 = $fbattles->battle[$k][$k2][0];
          $def_1 = $fbattles->battle[$k][$k2][1];
          $at_2 = $fbattles->battle[$k2][$k][0];
          $def_2 = $fbattles->battle[$k2][$k][1];
          $fbattles->makechange($k, $k2, $at_1, $at_2, $def_1, $def_2);
          $fbattles->write_log();
          $fbattles->battle[$k][$k2] = array(0, 0, time());
          $fbattles->battle[$k2][$k] = array(0, 0, time());
          $fbattles->needupdate = 1;
          if($cond) { $tocond .= " or "; }
          $battles[$rec['id']] = 1;
        }
      }
    }
  }
  if($fbattles->needupdate) { $fbattles->UpdateBattle(); }
}

$cond = '';
foreach($battles as $k => $v) {
  if($cond) { $cond .= " OR "; }
  $cond .= " `id` = '$k' ";
}
if($cond) {
  mysql_query("UPDATE `battle` SET `to1` = ".time().", `to2` = ".time()." WHERE $cond");
}