<?php 

define('_CONSTANT_', 1);

require_once 'start.php';

$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". USERS ." WHERE `in_battle` = ?", [0]);

if (!empty($all)) 
{
    $sql = DB::$dbs->query("SELECT `id`, `health`, `max_health` FROM ". USERS ." WHERE `in_battle` = ?", [0]);
    
    while ($ank = $sql -> fetch()) 
    {
        if ($ank['health'] < $ank['max_health']) 
        {
            if ($ank['health'] + 60 > $ank['max_health']) 
            {
                $restore = $ank['max_health'] - $ank['health'];
            } 
            else 
            {
                $restore = 60;
            }
            DB::$dbs->query("UPDATE ". USERS ." SET `health` = ? WHERE `id` = ?", [$ank['health'] + $restore, $ank['id']]);
            $_SESSION['restoreHealth'] = $restore;
        } 
        else if ($ank['health'] > $ank['max_health']) 
        {
            DB::$dbs->query("UPDATE ". USERS ." SET `health` = ? WHERE `id` = ?", [$ank['max_health'], $ank['id']]);
        }
    }
} 

header("Location: /");
exit();