game/_incl_data/crons/undone/cron_dungeon_goBots.php

493 lines
19 KiB
PHP

<?php
/**
* Ñêðèïò îòâå÷àåò çà ïåðåìåùåíèÿ áîòîâ ïî ïåùåðå è íàïåäåíèÿ, êîãäà èãðîê ðÿäîì.
*
* Çàïóñê ðàç â ìèíèòó
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'autoload.php';
//todo: IN PROGRESS
# Ñîâåðøàåì äåéñòâèå -> Íàïàäåíèå íà èãðîêà.
function botAttack($bot, $user)
{
if ($user['userBattle'] > 0) {
$battleID = mysql_fetch_array(
mysql_query(
'SELECT `id` FROM `battle` WHERE `id` = "' . $user['userBattle'] . '" AND `team_win` = "-1" LIMIT 1'
)
);
}
if (!isset($battleID['id'])) { //Ñîçäàåì ïîåäèíîê
$btl_id = 0;
$expB = 0;
$btl = ['players' => '', 'timeout' => 180, 'type' => 0, 'invis' => 0, 'noinc' => 0, 'travmChance' => 0, 'typeBattle' => 0, 'addExp' => $expB, 'money' => 0];
$ins = mysql_query(
'INSERT INTO `battle`
(`dungeon`,`dn_id`,`x`,`y`,`city`,`time_start`,`players`,`timeout`,`type`,`invis`,`noinc`,`travmChance`,`typeBattle`,`addExp`,`money`)
VALUES (
"' . $bot['dn_id'] . '",
"' . $bot['this_dn'] . '",
"' . $bot['x'] . '",
"' . $bot['y'] . '",
"' . $bot['userCity'] . '",
"' . time() . '",
"' . $btl['players'] . '",
"' . $btl['timeout'] . '",
"' . $btl['type'] . '",
"' . $btl['invis'] . '",
"' . $btl['noinc'] . '",
"' . $btl['travmChance'] . '",
"' . $btl['typeBattle'] . '",
"' . $btl['addExp'] . '",
"' . $btl['money'] . '"
)'
);
$btl_id = mysql_insert_id();
if ($btl_id > 0) { //Äîáàâëÿåì áîòîâ
$j = 0;
$logins_bot = [];
mysql_query(
'UPDATE `dungeon_bots` SET `inBattle` = "' . $btl_id . '" WHERE `id2` = "' . $bot['id2'] . '" LIMIT 1'
);
$jui = 1;
while ($jui <= $bot['colvo']) {
$k = botAddBattle($bot, $logins_bot);
$logins_bot = $k['logins_bot'];
if ($k != false) {
$upd = mysql_query(
'UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $k['id'] . '" LIMIT 1'
);
if ($upd) {
$upd = mysql_query('UPDATE `stats` SET `team` = "2" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
if ($upd) {
$j++;
}
}
}
$jui++;
}
unset($logins_bot);
if ($j > 0) {
mysql_query(
'UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $user['userId'] . '" LIMIT 1'
);
mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $user['userId'] . '" LIMIT 1');
}
}
} else { # Âìåøèâàåìñÿ â ïîåäèíîê.
$j = 0;
$logins_bot = [];
$logins_bot_text = [];
$logins_bot_vars = ['time1=' . time() . ''];
$logins_bot_inBattle = mysql_query(
'SELECT SUBSTRING_INDEX(`login`, " (", 1) as login2, count(`login`) as count, `login` FROM `battle_users` WHERE `battle` = "' . $battleID['id'] . '" AND `team`=2 GROUP BY `login2`'
);
while ($row = mysql_fetch_array($logins_bot_inBattle)) {
$logins_bot[$row['login2']] = (int)$row['count'];
}
mysql_query(
'UPDATE `dungeon_bots` SET `inBattle` = "' . $battleID['id'] . '" WHERE `id2` = "' . $bot['id2'] . '" LIMIT 1'
);
$jui = 1;
while ($jui <= $bot['colvo']) {
$k = botAddBattle($bot, $logins_bot);
$logins_bot = $k['logins_bot'];
$logins_bot_text[] = ' <strong>' . $k['login'] . '</strong>';
if ($k != false) {
$upd = mysql_query(
'UPDATE `users` SET `battle` = "' . $battleID['id'] . '" WHERE `id` = "' . $k['id'] . '" LIMIT 1'
);
if ($upd) {
$upd = mysql_query('UPDATE `stats` SET `team` = "2" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
if ($upd) {
$j++;
}
}
}
$jui++;
}
if ($j > 0) {
$logins_bot_text = '{tm1} Â ïîåäèíîê âìåøàëèñü: ' . implode(', ', $logins_bot_text) . '.';
$logins_bot_vars = implode('||', $logins_bot_vars);
$battle_log = mysql_fetch_array(
mysql_query(
'SELECT * FROM `battle_logs` WHERE `battle`=' . $battleID['id'] . ' ORDER BY `id_hod` DESC LIMIT 1'
)
);
if ($battle_log['id_hod'] > 0) {
mysql_query(
'INSERT INTO `battle_logs` (`time`,`battle`,`id_hod`,`text`,`vars`,`zona1`,`zonb1`,`zona2`,`zonb2`,`type`) VALUES ("' . time(
) . '","' . $battleID['id'] . '","' . ($battle_log['id_hod'] + 1) . '","' . $logins_bot_text . '","' . $logins_bot_vars . '","","","","",1)'
);
}
}
unset($logins_bot);
if ($j > 0) {
mysql_query(
'UPDATE `users` SET `battle` = "' . $battleID['id'] . '" WHERE `id` = "' . $user['id'] . '" LIMIT 1'
);
mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
}
unset($logins_bot_inBattle);
}
}
# Ñîâåðøàåì íàïàäåíèå -> Äîáàâëÿåì Áîòîâ â ïîåäèíîê
function botAddBattle($bot, $logins_bot)
{
$add_bot = mysql_fetch_array(
mysql_query(
'SELECT
`id`, `login`, `stats`, `obraz`, `level`, `sex`, `name`, `hobby`, `type`, `itemsUse`, `priemUse`, `align`, `clan`, `align_zvanie`, `bonus`, `clan_zvanie`, `time_reg`, `city_reg`, `upLevel`, `active`, `expB`, `p_items`, `agressor`, `priems`, `priems_z`, `award`
FROM `test_bot`
WHERE `id` = "' . $bot['id_bot'] . '"
LIMIT 1'
)
);
if (isset($add_bot['id'])) {
if (isset($logins_bot[$add_bot['login']])) {
$logins_bot[$add_bot['login']]++;
$add_bot['login'] = $add_bot['login'] . ' (' . $logins_bot[$add_bot['login']] . ')';
} else {
$logins_bot[$add_bot['login']] = 1;
}
$ret = true;
if ($add_bot['time_reg'] == 100) {
$add_bot['time_reg'] = time();
}
if ($add_bot['city_reg'] == '{thiscity}') {
$add_bot['city_reg'] = $bot['userCity'];
}
$ins1 = mysql_query(
'INSERT INTO `users` (
`align`,
`login`,
`level`,
`pass`,
`city`,
`cityreg`,
`name`,
`sex`,
`hobby`,
`timereg`,
`obraz`,
`bot_id`
) VALUES (
"' . $add_bot['align'] . '",
"' . $add_bot['login'] . '",
"' . $add_bot['level'] . '",
"' . md5('bot_pass_' . $add_bot['login'] . '_') . '",
"' . $bot['userCity'] . '",
"' . $add_bot['city_reg'] . '",
"' . $add_bot['name'] . '",
"' . $add_bot['sex'] . '",
"' . $add_bot['hobby'] . '",
"' . $add_bot['time_reg'] . '",
"' . $add_bot['obraz'] . '",
"' . $bot['id_bot'] . '"
)'
);
# Åñëè áîò óñïåøíî ñîçäàí.
if ($ins1) {
$uid = mysql_insert_id();
$ins2 = mysql_query(
'INSERT INTO `stats` (`id`,`stats`,`hpNow`,`upLevel`,`bot`) VALUES ("' . $uid . '","' . $add_bot['stats'] . '","1000000","' . $add_bot['upLevel'] . '","1")'
);
if ($ins2) {
$add_bot['id'] = $uid;
$add_bot['logins_bot'] = $logins_bot;
$ret = $add_bot;
//Âûäàåì ïðåäìåòû
//$this->addItem($item_id,$uid);
$iu = explode(',', $add_bot['itemsUse']);
$i = 0;
$w3b = 0;
while ($i < count($iu)) {
if ($iu[$i] > 0) {
$idiu = botAddItem($iu[$i], $add_bot['id'], $bot['userCity']);
$islot = mysql_fetch_array(
mysql_query('SELECT `id`,`inslot` FROM `items_main` WHERE `id` = "' . $iu[$i] . '" LIMIT 1')
);
if (isset($islot['id'])) {
if ($islot['inslot'] == 3) {
if ($w3b == 1) {
$islot = 14;
} else {
$islot = 3;
$w3b = 1;
}
} else {
$islot = $islot['inslot'];
}
} else {
$islot = 2000;
}
if (isset($idiu, $islot)) {
mysql_query(
'UPDATE `items_users` SET `inOdet` = "' . $islot . '" WHERE `id` = "' . $idiu . '" LIMIT 1'
);
}
}
$i++;
}
} else {
$ret = false;
}
} else {
$ret = false;
}
return $ret;
}
}
#Âûäàåì ïðåäìåòû Áîòó.
function botAddItem($item_id, $bot_uid, $city)
{
$i = mysql_fetch_array(
mysql_query(
'SELECT `im`.`id`,`im`.`name`,`im`.`img`,`im`.`type`,`im`.`inslot`,`im`.`2h`,`im`.`2too`,`im`.`iznosMAXi`,`im`.`inRazdel`,`im`.`price1`,`im`.`price2`,`im`.`pricerep`,`im`.`magic_chance`,`im`.`info`,`im`.`massa`,`im`.`level`,`im`.`magic_inci`,`im`.`overTypei`,`im`.`group`,`im`.`group_max`,`im`.`geni`,`im`.`ts`,`im`.`srok`,`im`.`class`,`im`.`class_point`,`im`.`anti_class`,`im`.`anti_class_point`,`im`.`max_text`,`im`.`useInBattle`,`im`.`lbtl`,`im`.`lvl_itm`,`im`.`lvl_exp`,`im`.`lvl_aexp` FROM `items_main` AS `im` WHERE `im`.`id` = "' . mysql_real_escape_string(
$item_id
) . '" LIMIT 1'
)
);
if (isset($i['id'])) {
$d = mysql_fetch_array(
mysql_query(
'SELECT `id`,`items_id`,`data` FROM `items_main_data` WHERE `items_id` = "' . $i['id'] . '" LIMIT 1'
)
);
//íîâàÿ äàòà
$data = $d['data'];
$ins = mysql_query(
'
INSERT INTO `items_users` (
`overType`,`item_id`,`uid`,`data`,`iznosMAX`,`geniration`,`magic_inc`,`maidin`,`lastUPD`,`time_create`,`dn_delete`
) VALUES (
"' . $i['overTypei'] . '",
"' . $i['id'] . '",
"' . $bot_uid . '",
"' . $data . '",
"' . $i['iznosMAXi'] . '",
"' . $i['geni'] . '",
"' . $i['magic_inci'] . '",
"' . $city . '",
"' . time() . '",
"' . time() . '",
"' . $i['dn_delete'] . '"
)'
);
# Åñëè ïðåäìåò óñïåøíî äîáàâëåí â áàçó äàííûõ.
if ($ins) {
$rt = mysql_insert_id();
# îòêëþ÷åíà çàïèñü ïîëó÷åíèÿ ïðåäìåòà â Äåëî.
} else {
$rt = 0;
}
}
return $rt;
}
function moveBots($direction, $b)
{
$toGoX = 0;
$toGoY = 0;
if (isset($b['noBot']) && $b['noBot'] != '0000') {
if ($b['noBot'][0] != '0') {
$b['goTop'] = 0;
}
if ($b['noBot'][1] != '0') {
$b['goLeft'] = 0;
}
if ($b['noBot'][2] != '0') {
$b['goBottom'] = 0;
}
if ($b['noBot'][3] != '0') {
$b['goRight'] = 0;
}
}
$go = [
1 => ['d' => (int)$b['goTop'], 'go1' => (int)$b['goLeft'], 'go2' => (int)$b['goRight'], 'x' => (int)$b['x'], 'y' => (int)$b['y'], 's' => (int)$b['s']],
2 => ['d' => (int)$b['goLeft'], 'go1' => (int)$b['goBottom'], 'go2' => (int)$b['goTop'], 'x' => (int)$b['x'], 'y' => (int)$b['y'], 's' => (int)$b['s']],
3 => ['d' => (int)$b['goBottom'], 'go1' => (int)$b['goRight'], 'go2' => (int)$b['goLeft'], 'x' => (int)$b['x'], 'y' => (int)$b['y'], 's' => (int)$b['s']],
4 => ['d' => (int)$b['goRight'], 'go1' => (int)$b['goTop'], 'go2' => (int)$b['goBottom'], 'x' => (int)$b['x'], 'y' => (int)$b['y'], 's' => (int)$b['s']],
];
$dir = [
1 => ['moveForward' => ['x' => '0', 'y' => '1'], 'moveBack' => ['x' => '0', 'y' => '-1'], 'moveGo1' => ['x' => '-1', 'y' => '0'], 'moveGo2' => ['x' => '1', 'y' => '0']],
2 => ['moveForward' => ['x' => '-1', 'y' => '0'], 'moveBack' => ['x' => '1', 'y' => '0'], 'moveGo1' => ['x' => '0', 'y' => '-1'], 'moveGo2' => ['x' => '0', 'y' => '1']],
3 => ['moveForward' => ['x' => '0', 'y' => '-1'], 'moveBack' => ['x' => '0', 'y' => '1'], 'moveGo1' => ['x' => '1', 'y' => '0'], 'moveGo2' => ['x' => '-1', 'y' => '0']],
4 => ['moveForward' => ['x' => '1', 'y' => '0'], 'moveBack' => ['x' => '-1', 'y' => '0'], 'moveGo1' => ['x' => '0', 'y' => '1'], 'moveGo2' => ['x' => '0', 'y' => '-1']],
];
$go = $go[$direction];
$dir = $dir[$direction];
if ($go['d'] == 1) {
$toGoY = $dir['moveForward']['y'];
$toGoX = $dir['moveForward']['x'];
if (rand(1, 100) > 66) {
if ($go['go1'] == 1 && $go['go2'] == 0) {
$toGoY = $dir['moveGo1']['y'];
$toGoX = $dir['moveGo1']['x'];
} elseif ($go['go1'] == 0 && $go['go2'] == 1) {
$toGoY = $dir['moveGo2']['y'];
$toGoX = $dir['moveGo2']['x'];
} elseif ($go['go1'] == 1 && $go['go2'] == 1) {
$a = rand(1, 2);
$toGoY = $dir['moveGo' . $a]['y'];
$toGoX = $dir['moveGo' . $a]['x'];
}
} elseif (rand(1, 100) > 96) {
$toGoY = $dir['moveBack']['y'];
$toGoX = $dir['moveBack']['x'];
}
} elseif ($go['d'] == 0) {
if ($go['go1'] == 1 && $go['go2'] == 1) {
if (rand(0, 1) == 1) {
$toGoY = $dir['moveGo1']['y'];
$toGoX = $dir['moveGo1']['x'];
} else {
$toGoY = $dir['moveGo2']['y'];
$toGoX = $dir['moveGo2']['x'];
}
} elseif ($go['go1'] == 1 && $go['go2'] == 0) {
$toGoY = $dir['moveGo1']['y'];
$toGoX = $dir['moveGo1']['x'];
} elseif ($go['go1'] == 0 && $go['go2'] == 1) {
$toGoY = $dir['moveGo2']['y'];
$toGoX = $dir['moveGo2']['x'];
} elseif ($go['go1'] == 0 && $go['go2'] == 0) {
$toGoY = $dir['moveBack']['y'];
$toGoX = $dir['moveBack']['x'];
}
}
unset($dir, $go, $direction, $a);
return ['x' => (int)$toGoX, 'y' => (int)$toGoY];
}
# çàïóñê ñêðèïòà.
function start()
{
# Ñòðàíèöà ñîçäàíà 0.0000
$mtime = microtime();
$mtime = explode(" ", $mtime);
$tstart = $mtime[1] + $mtime[0];
# Âûáèðàåì âñåõ áîòîâ.
#  âûáîðêó âêëþ÷åíî: Ïîçèöèÿ áîòà, Íàïðàâëåíèå êóäà îí ìîæåò èäòè, Ñóùåñòâóåò ëè ðÿäîì Èãðîê, åãî êîîðäèíàòû è â ïîåäèíêå ëè îí.
$query = mysql_query(
"SELECT
`dn`.`id` as `this_dn`, `dn`.`id2` as `dn_id`, `db`.`id2`, `db`.`id_bot`, `tb`.`login` as login, `db`.`colvo`, `db`.`go_bot`, `db`.`x`, `db`.`y`, `db`.`s`, `db`.`atack`, `tb`.`agressor`,
`dm`.`go_1` as `goRight`, `dm`.`go_2` as `goLeft`, `dm`.`go_3` as `goTop`, `dm`.`go_4` as `goBottom`, `dm`.`no_bot` as `noBot`,
`user_info`.`id` as `userId`,
`user_info`.`login` as `userLogin`,
`user_stats`.`hpNow` as `userHP`,
`user_stats`.`x` as `userPosX`,
`user_stats`.`y` as `userPosY`,
`user_info`.`battle` as `userBattle`
FROM `dungeon_now` as `dn`
LEFT JOIN `dungeon_bots` as `db` ON `db`.`dn` = `dn`.`id`
LEFT JOIN `dungeon_map` as `dm` ON ( `dm`.`x` = `db`.`x` AND `dm`.`y` = `db`.`y` AND `dm`.`id_dng` = `dn`.`id2` )
LEFT JOIN `test_bot` AS `tb` ON `db`.`id_bot` = `tb`.`id`
LEFT JOIN `stats` AS `user_stats` ON ( ( `user_stats`.`x`+1 >= `db`.`x` AND `user_stats`.`x`-1 <= `db`.`x` ) AND ( `user_stats`.`y`+1 >= `db`.`y` AND `user_stats`.`y`-1 <= `db`.`y`) AND `user_stats`.`dnow` = `dn`.`id` )
LEFT JOIN `users` AS `user_info` ON ( `user_stats`.`id` = `user_info`.`id` )
WHERE
`dn`.`time_finish` = '0' AND
`db`.`atack` = '0' AND
`db`.`delete` = '0' AND
`db`.`for_dn` = '0' AND
( ( `db`.`go_bot` > '" . (time() - 32400) . "' AND `db`.`go_bot` < '" . (time()) . "') OR `db`.`go_bot`='1') AND
`db`.`inBattle`='0'
GROUP BY `db`.`id2`
ORDER BY `db`.`go_bot` ASC"
);
while ($bot = mysql_fetch_array($query)) {
if ($bot['go_bot'] > 0 && $bot['go_bot'] <= time()) {
$sNext = true;
$sTo = $bot['s'];
$xFrom = $bot['x']; # òåêóùèå êîîðäèíàòû X
$yFrom = $bot['y']; # òåêóùèå êîîðäèíàòû Y
$return = moveBots($bot['s'], $bot);
$xTo = $bot['x'] + $return['x'];
$yTo = $bot['y'] + $return['y'];
# Ïðîâåðÿåì, èìååòñÿ ëè ïåðåõîä ïî êëåòêå.
$inSight = 0;
if (isset($bot['id2'])) {
if ($xFrom == $xTo && $yFrom == $yTo) { # Åñëè îñòàåìñÿ íà ìåñòå.
$inSight = 1;
} elseif (isset($bot['goRight']) && $xFrom == $xTo - 1 && $bot['goRight'] == 1) { //ïðàâî
$sTo = 4;
$inSight = 1;
} elseif (isset($bot['goLeft']) && $xFrom == $xTo + 1 && $bot['goLeft'] == 1) { //ëåâî
$sTo = 2;
$inSight = 1;
} elseif (isset($bot['goTop']) && $yFrom == $yTo - 1 && $bot['goTop'] == 1) { //âåðõ
$sTo = 1;
$inSight = 1;
} elseif (isset($bot['goBottom']) && $yFrom == $yTo + 1 && $bot['goBottom'] == 1) { //íèç
$sTo = 3;
$inSight = 1;
}
}
if (isset($bot['userId'], $bot['userPosY'], $bot['userPosX']) && $bot['userId'] != '' && $bot['agressor'] == 1 && (
($bot['userPosY'] == $bot['y'] + 1 && $bot['userPosX'] == $bot['x']) or
($bot['userPosY'] == $bot['y'] - 1 && $bot['userPosX'] == $bot['x']) or
($bot['userPosY'] == $bot['y'] && $bot['userPosX'] == $bot['x'] - 1) or
($bot['userPosY'] == $bot['y'] && $bot['userPosX'] == $bot['x'] + 1)
)) {
bot['userPosY'] = $yFrom;
$bot['userPosX'] = $xFrom;
mysql_query(
'UPDATE `stats` SET `x` = "' . $xFrom . '" , `y` = "' . $yFrom . '" WHERE `id` = "' . $bot['userId'] . '" LIMIT 1'
);
botAttack($bot, $bot);
} elseif (isset($bot['userId'], $bot['userPosY'], $bot['userPosX']) && $bot['userId'] != '' && $inSight == 1 && $yTo == $bot['userPosY'] && $xTo == $bot['userPosX'] && $bot['atack'] == 1) {
botAttack($bot, $bot);
} elseif ($inSight == 1) { // Ïåðåäâèæåíèå áîòîâ.
$bot['go_bot'] = time() + rand(7, 15);
mysql_query(
'UPDATE `dungeon_bots` SET `x` = "' . $xTo . '",`y` = "' . $yTo . '", `s` = "' . $sTo . '", `go_bot` = "' . $bot['go_bot'] . '" WHERE `id2` = "' . $bot['id2'] . '" LIMIT 1 '
);
}
unset($xFrom, $yFrom, $xTo, $yTo, $inSight, $sNext, $sTo);
}
unset($bot);
}
unset($query, $bot);
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$totaltime = ($mtime - $tstart);
printf("Ñòðàíèöà ñãåíåðèðîâàíà çà %f ñåêóíä !", $totaltime);
}
# Çàïóñêàåì âûïîëíåíèå ïðîöåññà.
start();
/* Äëÿ îïòèìèçàöèè çàïðîñà îáíîâëåíèé ïîçèöèé
UPDATE dungeon_bots SET
x = CASE
WHEN id2 = '.$bot['id2'].' THEN "test2"
WHEN id2 = '.$bot['id2'].' THEN "test1" END
WHERE id2 IN ('.$bot['id2'].', 2, 3, 4)
*/