Пример #1
0
function UpdateBots()
{
    $now = time();
    if (read_config('bots') > 0 && read_config('bots_last_update') < $now - 60) {
        include_once XN_ROOT . 'includes/functions/CheckPlanetBuildingQueue.php';
        include_once XN_ROOT . 'includes/functions/GetBuildingPrice.php';
        include_once XN_ROOT . 'includes/functions/IsElementBuyable.php';
        include_once XN_ROOT . 'includes/functions/SetNextQueueElementOnTop.php';
        include_once XN_ROOT . 'includes/functions/UpdatePlanetBatimentQueueList.php';
        include_once XN_ROOT . 'includes/functions/IsTechnologieAccessible.php';
        include_once XN_ROOT . 'includes/functions/GetElementPrice.php';
        include_once XN_ROOT . 'includes/functions/HandleTechnologieBuild.php';
        include_once XN_ROOT . 'includes/functions/CheckPlanetUsedFields.php';
        if (read_config('log_bots')) {
            $BotLog = "\n\n------------------------------------------\n";
        }
        $allbots = doquery("SELECT * FROM {{table}} WHERE `next_time`<" . $now, 'bots');
        $update_bots = array();
        $update_users = array();
        while ($bot = $allbots->fetch_array()) {
            $user = doquery("SELECT * FROM {{table}} WHERE `id` = '" . $bot['user'] . "'", 'users', true);
            $thebot = new Bot($user, $bot);
            $thebot->Play();
            if (isset($BotLog)) {
                $BotLog .= $thebot->log;
            }
            /**
             *	Para calcular la próxima actividad, se genera una función que decrece de
             *	probabilidad casi totalmente en 15 minutos. Luego se calcula la próxima
             *	actividad un poco aleatóriamente, teniendo en cuenta la noche, las horas
             *	de sueño y según los minutos que está conectado.
             **/
            if (date('H', $now) < 8) {
                $max_time = 28800 / (($bot['minutes_per_day'] < 975 ? 15 : $bot['minutes_per_day'] - 960) / 15);
            } elseif ($bot['minutes_per_day'] >= 960) {
                $max_time = 60;
            } else {
                $max_time = 57600 / ($bot['minutes_per_day'] / 15);
            }
            if ($max_time / 60 > 15) {
                $random = mt_rand(1, 100);
                if ($random <= 30) {
                    $next_time = $now + mt_rand(1, 120);
                } elseif ($random <= 45) {
                    $next_time = $now + mt_rand(61, 180);
                } elseif ($random <= 55) {
                    $next_time = $now + mt_rand(121, 240);
                } elseif ($random <= 62) {
                    $next_time = $now + mt_rand(181, 300);
                } elseif ($random <= 68) {
                    $next_time = $now + mt_rand(241, 360);
                } elseif ($random <= 73) {
                    $next_time = $now + mt_rand(301, 420);
                } elseif ($random <= 81) {
                    $next_time = $now + mt_rand(361, 540);
                } elseif ($random <= 90) {
                    $next_time = $now + mt_rand(421, 660);
                } else {
                    $next_time = $now + mt_rand(541, 960);
                }
            }
            if (mt_rand(0, 1) or $max_time / 60 <= 15) {
                $next_time = $now + mt_rand($max_time > 120 ? $max_time - 60 : 60, $max_time + 60);
            }
            if (date('H', $next_time) < 8 && $bot['minutes_per_day'] < 960) {
                $next_time = mktime(8);
            }
            $update_bots[$bot['id']] = array('last_time' => $now, 'next_time' => $next_time, 'minutes_per_day' => !mt_rand(0, 999) ? mt_rand(1, 1440) : $bot['minutes_per_day'], 'last_planet' => $thebot->end_planet);
            $update_users[$bot['user']] = array('onlinetime' => $now, 'user_lastip' => 'BOT', 'user_agent' => 'BOT');
            unset($thebot);
        }
        if (isset($BotLog)) {
            $st = fopen(XN_ROOT . "adm/Log/BotLog.php", "a");
            $BotLog .= 'Bots actualizados a las ' . date('H:i:s - j/n/Y', $now) . "\n";
            $BotLog .= "------------------------------------------";
            fwrite($st, $BotLog);
            fclose($st);
        }
        unset($bot);
        unset($allbots);
        if (!empty($update_bots)) {
            $query = 'UPDATE {{table}}';
            $bot_ids = array();
            $user_ids = array();
            foreach ($update_bots as $id => $values) {
                foreach ($values as $field => $value) {
                    if (!isset($fields[$field])) {
                        $fields[$field] = ' SET `' . $field . '` = CASE';
                    }
                    $fields[$field] .= ' WHEN `id` = \'' . $id . '\' THEN \'' . $value . '\'';
                }
                $bot_ids[] = $id;
            }
            foreach ($fields as $field => $text) {
                $query_bots = $query . $text . ' ELSE `' . $field . '` END,';
            }
            $query_bots = substr($query_bots, 0, -1) . ' WHERE `id` IN (' . implode(',', $bot_ids) . ')';
            foreach ($update_users as $id => $values) {
                foreach ($values as $field => $value) {
                    if (!isset($fields[$field])) {
                        $fields[$field] = ' SET `' . $field . '` = CASE';
                    }
                    $fields[$field] .= ' WHEN `id` = \'' . $id . '\' THEN \'' . $value . '\'';
                }
                $user_ids[] = $id;
            }
            foreach ($fields as $field => $text) {
                $query_users = $query . $text . ' ELSE `' . $field . '` END,';
            }
            $query_users = substr($query_users, 0, -1) . ' WHERE `id` IN (' . implode(',', $user_ids) . ')';
            doquery($query_bots, 'bots');
            doquery($query_users, 'users');
        }
        update_config('bots_last_update', $now);
    }
}