From 7fef811dbf43d566131daabc3b8680a17cb9e37a Mon Sep 17 00:00:00 2001 From: "Igor Barkov (iwork)" Date: Mon, 28 Sep 2020 18:59:12 +0300 Subject: [PATCH] =?UTF-8?q?Code=20clean.=20=D0=A2=D0=B2=D0=BE=D1=8E=20?= =?UTF-8?q?=D0=B6=20=D0=BC=D0=B0=D1=82=D1=8C,=20=D0=BF=D1=80=D0=BE=D1=89?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D1=87=D0=B5=D0=BC=20=D0=BA=D0=BE=D0=B2=D1=8B=D1=80?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=D1=81=D1=8F...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zayavka.php | 337 +++++++++++++++++++++++++++++----------------------- 1 file changed, 190 insertions(+), 147 deletions(-) diff --git a/zayavka.php b/zayavka.php index 9149200..0141cbf 100644 --- a/zayavka.php +++ b/zayavka.php @@ -1,13 +1,14 @@ battle != 0) { header('Location: fbattle.php'); - die(); + exit; } db::c()->query('LOCK TABLES `bots` WRITE, `battle` WRITE, `logs` WRITE, `users` WRITE, `inventory` WRITE, `zayavka` WRITE, `effects` WRITE, `online` WRITE, `clans` WRITE'); @@ -16,7 +17,7 @@ if ($do == "clear" && $user->admin == 1) { db::c()->query('UPDATE `zayavka` SET `coment` = "?s" WHERE `id` = ?i', 'Вырезано цензурой!', $_GET['zid']); } -if (!empty($_GET['del']) && $user['admin'] == 1) { //удаление заявки на бой. +if (!empty($_GET['del']) && $user->admin == 1) { //удаление заявки на бой. $zayavka = db::c()->query('SELECT `id` FROM `zayavka` WHERE `id` = ?i', $_GET['zid'])->fetch_assoc(); if (isset($zayavka['id'])) { db::c()->query('UPDATE `users` SET `zayavka` = 0 WHERE `zayavka` = ?i', $zayavka['id']); @@ -26,16 +27,20 @@ if (!empty($_GET['del']) && $user['admin'] == 1) { //удаление заявк } } $_SESSION['view'] = false; -if (isset($_REQUEST['view'])) $_SESSION['view'] = $_REQUEST['view']; +if (isset($_REQUEST['view'])) { + $_SESSION['view'] = $_REQUEST['view']; +} -class zayavka +class Zayavka { - var $mysql; + /** + * @var string + */ + private $errorMessage; - function __construct() + function __construct($user_id) { - global $mysql; - $this->mysql = $mysql; + } function fteam($team) @@ -53,7 +58,7 @@ class zayavka * @return mixed * @throws \Krugozor\Database\Mysql\Exception */ - function getlist($razdel = 4, $level = null, $id = null) + public function getlist($razdel = 4, $level = null, $id = null) { $sql = db::c()->prepare('SELECT * FROM `zayavka` WHERE `level` = ?i', $razdel); if (!is_null($level)) { @@ -86,65 +91,71 @@ class zayavka return $zay; } - function addteam($team, $r) + public function addteam($team, $r) { global $user; $id = $_SESSION['uid']; $zay = $_REQUEST['gocombat']; + $error = null; $owntravma = db::c()->query('SELECT `id`, `type` FROM `effects` WHERE (`type` = 12 OR `type` = 13) AND `owner` = ?i', $id)->fetch_assoc(); $z = $this->getlist($r, null, $zay); - if ($owntravma) { - switch ($owntravma['type']) { - case ($owntravma['type'] == 12 && ($z[$zay]['type'] != 4 && $z[$zay]['type'] != 5)): - return "У Вас средняя травма, поединки с оружием слишком тяжелы для Вас..."; - break; - case 13: - return "У Вас тяжелая травма, Вы не сможете драться..."; - break; - } + if ($owntravma['type'] == 12 && ($z[$zay]['type'] != 4 && $z[$zay]['type'] != 5)) { + $this->errorMessage = "У Вас средняя травма, поединки с оружием слишком тяжелы для Вас..."; + } elseif ($owntravma['type'] == 13) { + $this->errorMessage = "У Вас тяжелая травма, Вы не сможете драться..."; } - - if (!empty($user['zayavka'])) return "Вы находитесь в заявке"; - - if ($user['hp'] < $user['maxhp'] * 0.33) return "Вы слишком ослаблены для боя, восстановитесь."; - - if (!$z) return "Эта заявка не может быть принята Вами."; - - if ($this->user_status($id) != 0) return "Эта заявка не может быть принята Вами."; - - if ($z[$zay]['type'] != 3 OR $z[$zay]['type'] != 5) { - if ($team == 1) $teamz = 2; - else $teamz = 1; + if (!empty($user['zayavka'])) { + $this->errorMessage = "Вы находитесь в заявке"; + } + if ($user['hp'] < $user['maxhp'] * 0.33) { + $this->errorMessage = "Вы слишком ослаблены для боя, восстановитесь."; + } + if (!$z || !empty($this->user_status($id))) { + $this->errorMessage = "Эта заявка не может быть принята Вами."; + } + if ($z[$zay]['type'] != 3 || $z[$zay]['type'] != 5) { + if ($team == 1) { + $teamz = 2; + } else { + $teamz = 1; + } foreach ($z[$zay]['team' . $teamz] as $v) { $enemy = db::c()->query('SELECT `klan` FROM `users` WHERE `id` = ?i', $v)->fetch_assoc(); - if ($user['klan'] == $enemy['klan']) return "Нельзя драться против сокланов!"; + if ($user['klan'] == $enemy['klan']) { + $this->errorMessage = "Нельзя драться против сокланов!"; + } } } if ($z[$zay]['t' . $team . 'min'] == 99) { $toper = db::c()->query('SELECT `klan` FROM `users` WHERE `id` = ?i', $z[$zay]['team' . $team][0])->fetch_assoc(); - if ($user['klan'] != $toper['klan']) return "Эта заявка не может быть принята Вами."; + if ($user['klan'] != $toper['klan']) { + $this->errorMessage = "Эта заявка не может быть принята Вами."; + } } else { if ($user['level'] > 0 && !($z[$zay]['t' . $team . 'min'] <= $user['level'] && $z[$zay]['t' . $team . 'max'] >= $user['level'])) { - return "Эта заявка не может быть принята Вами. - " . $z[$zay]['t' . $team . 'min']; + $this->errorMessage = "Эта заявка не может быть принята Вами. - " . $z[$zay]['t' . $team . 'min']; } } if (count($z[$zay]['team' . $team]) >= $z[$zay]['t' . $team . 'c']) { - return "Группа уже набрана."; + $this->errorMessage = "Группа уже набрана."; } $z[$zay]['team' . $team][] = ''; - if (mysql_query("UPDATE `users`, `zayavka` SET `users`.`zayavka` = {$zay}, `zayavka`.`team{$team}` = '" . implode(";", $z[$zay]['team' . $team]) . "" . $id . ";' WHERE `users`.`id` = {$id} AND `users`.`zayavka` = 0 AND `zayavka`.`id` = {$zay}")) { + if ($this->errorMessage) { + return $this->errorMessage; + } else { + mysql_query("UPDATE `users`, `zayavka` SET `users`.`zayavka` = {$zay}, `zayavka`.`team{$team}` = '" . implode(";", $z[$zay]['team' . $team]) . "" . $id . ";' WHERE `users`.`id` = {$id} AND `users`.`zayavka` = 0 AND `zayavka`.`id` = {$zay}"); return "Вы приняли заявку на бой."; - } else return 'addteam(): database error!'; + } } - function delteam($id, $zay, $r) + public function delteam($id, $zay, $r) { $z = $this->getlist($r, null, $zay); if ($z[$zay]['level'] > 3 || $z[$zay]['level'] == null) { - return "Ай-ай-ай!"; + $this->errorMessage = 'Неизвестная ошибка.'; } foreach ($z[$zay]['team2'] as $v) { @@ -152,73 +163,85 @@ class zayavka $teams[] = $v; } } - - if (mysql_query("UPDATE `users`, `zayavka` SET `users`.`zayavka` = '0', `zayavka`.`team{$team}` = '{$teams}' WHERE `users`.`id` = {$id} AND `zayavka`.`id` = {$zay};")) { + if ($this->errorMessage) { + return $this->errorMessage; + } else { + mysql_query("UPDATE `users`, `zayavka` SET `users`.`zayavka` = '0', `zayavka`.`team{$team}` = '{$teams}' WHERE `users`.`id` = {$id} AND `zayavka`.`id` = {$zay};"); return "Вы отозвали запрос"; - } else return 'delteam(): database error!'; + } } - function addzayavka($start, $timeout, $t1c, $t2c, $type, $t1min, $t2min, $t1max, $t2max, $coment, $level, $blood = 0) + public function addzayavka($start, $timeout, $t1c, $t2c, $type, $t1min, $t2min, $t1max, $t2max, $coment, $level, $blood = 0) { global $user; - if (!in_array($level, [2, 4, 5])) $level = 4; /*2-физ, 4-группа, 5-хаос*/ - - if ($level == 2 AND $type != 1 AND $type != 4 AND $type != 6) $type = 1; - if ($level == 4 AND $type != 2 AND $type != 4) $type = 2; - if ($level == 5 AND $type != 3 AND $type != 5) $type = 3; - - if (!in_array($start, [1, 3, 5, 10, 15, 30, 60])) $start = 1; - if (!in_array($timeout, [1, 3, 5, 10])) $timeout = 1; - - if ($this->user_status($user['id']) != 0) exit(); + if (!in_array($level, [2, 4, 5])) { + $level = 4; + } /*2-физ, 4-группа, 5-хаос*/ + if ($level == 2 && $type != 1 && $type != 4 && $type != 6) { + $type = 1; + } + if ($level == 4 && $type != 2 && $type != 4) { + $type = 2; + } + if ($level == 5 && $type != 3 && $type != 5) { + $type = 3; + } + if (!in_array($start, [1, 3, 5, 10, 15, 30, 60])) { + $start = 1; + } + if (!in_array($timeout, [1, 3, 5, 10])) { + $timeout = 1; + } + if ($this->user_status($user['id']) != 0) { + exit(); + } if (!$user['klan'] && $t1min == 99) { - return "Вы не состоите в клане."; + $this->errorMessage = "Вы не состоите в клане."; } if ($user['hp'] < $user['maxhp'] * 0.33) { - return "Вы слишком ослаблены для боя, восстановитесь."; + $this->errorMessage = "Вы слишком ослаблены для боя, восстановитесь."; } + if ($this->errorMessage) { + return $this->errorMessage; + } else { + if ($type == 3) { + AddChatSystem('Внимание! Подана заявка на хаотический поединок ({$t1min} - {$t1max}), который начнется через {$start} минут.'); + } + $start = time() + $start * 60; - if ($type == 3) { - AddChatSystem('Внимание! Подана заявка на хаотический поединок ({$t1min} - {$t1max}), который начнется через {$start} минут.'); - } - - $start = time() + $start * 60; - - db::c()->query('INSERT INTO `zayavka` + db::c()->query('INSERT INTO `zayavka` (`start`, `timeout`, `t1c`, `t2c`, `type`, `level`, `coment`, `team1`, `t1min`, `t2min`, `t1max`, `t2max`,`podan`,`blood`) VALUES (?i,?i,?i,?i,?i,?i,"?s","?s",?d,?i,?i,?i,?i,"?s",?i)', - $start, $timeout, $t1c, $t2c, $type, $level, $coment, $user['id'], $t1min, $t2min, $t1max, $t2max, date('H:i'), $blood); - db::c()->query('UPDATE `users` SET `zayavka` = ?i WHERE `id` = ?i', db::c()->getLastInsertId(), $user['id']); - - return null; + $start, $timeout, $t1c, $t2c, $type, $level, $coment, $user['id'], $t1min, $t2min, $t1max, $t2max, date('H:i'), $blood); + db::c()->query('UPDATE `users` SET `zayavka` = ?i WHERE `id` = ?i', db::c()->getLastInsertId(), $user['id']); + return 'Заявка подана!'; + } } - function delzayavka($id, $zay, $r, $f = 1) + public function delzayavka($id, $zay, $r, $f = 1) { $z = $this->getlist($r, null, $zay); - if ($f != 1) { - if ($z[$zay]['level'] > 3) { - return 'Ай-ай-ай!'; - } - } - - if (mysql_query("DELETE FROM `zayavka` WHERE `id` = {$zay} AND (`team1` LIKE '{$id};%' OR `team2` LIKE '{$id};%') LIMIT 1")) { + if ($f != 1 && $z[$zay]['level'] > 3) { + return 'Неизвестная ошибка.'; + } else { + mysql_query("DELETE FROM `zayavka` WHERE `id` = {$zay} AND (`team1` LIKE '{$id};%' OR `team2` LIKE '{$id};%') LIMIT 1"); if (count($z[$zay]['team1']) > 0) { - foreach ($z[$zay]['team1'] as $k => $v) + foreach ($z[$zay]['team1'] as $k => $v) { db::c()->query('UPDATE `users` SET `zayavka` = 0 WHERE `id` = ?i', $v); + } } - if (count($z[$zay]['team2']) > 0) { - foreach ($z[$zay]['team2'] as $k => $v) + foreach ($z[$zay]['team2'] as $k => $v) { db::c()->query('UPDATE `users` SET `zayavka` = 0 WHERE `id` = ?i', $v); + } } return 'Вы отозвали заявку.'; - } else return 'delzayavka(): database error!'; + } } - function showfiz($row) + public function showfiz($row) { global $user; $rr = "{$row['podan']} "; @@ -246,7 +269,7 @@ class zayavka return $rr; } - function showgroup($row) + public function showgroup($row) { if ($row['t1min'] == 99) { $range1 = "клан"; @@ -308,7 +331,7 @@ class zayavka return $rr; } - function showhaos($row) + public function showhaos($row) { global $user; $rr = "{$row['podan']} ("; @@ -367,7 +390,7 @@ class zayavka return $rr; } - function user_status($id) + public function user_status($id) { $r = db::c()->query('SELECT `team1`,`team2` FROM `zayavka`, `users` WHERE `users`.`id` = ?i AND `zayavka`.`id` = `users`.`zayavka`', $id)->fetch_assoc(); $t1 = $this->fteam($r['team1']); @@ -381,7 +404,7 @@ class zayavka } } - function battlestart($id, $zay, $r) + public function battlestart($id, $zay, $r) { global $user; $z = $this->getlist($r, null, $zay); @@ -470,7 +493,7 @@ class zayavka } } - while (count($cost) > 0) { + while (!empty($cost)) { if ($power1 <= $power2) { $tmp = array_shift($cost); $power1 += $tmp[1]; @@ -509,7 +532,9 @@ class zayavka die(); } - if (!in_array($z['timeout'], [3, 4, 5, 7, 10])) $z['timeout'] = 3; + if (!in_array($z['timeout'], [3, 4, 5, 7, 10])) { + $z['timeout'] = 3; + } if (count($teams) > 1) { if ($z['type'] == 1 || $z['type'] == 4) { @@ -576,7 +601,7 @@ VALUES ("?s","?s",?i,?i,?i,"?s","?s",?i,?i,?i,?i)', } } -$zay = new zayavka; +$zay = new Zayavka; header("Cache-Control: no-cache"); if (!empty($_POST['open'])) { $f = fopen("/tmp/zayavka/" . $user['id'] . ".txt", "w+"); @@ -609,7 +634,9 @@ if (!empty($_POST['open'])) {
- battle();} ?> + battle(); + } ?> @@ -629,8 +656,12 @@ if (!empty($_POST['open'])) { Выберите раздел

'; + if ($user->room != 1 && $get != 'current') { + exit('В этой комнате невозможно подавать заявки!'); + } + if (!$get) { + echo '

Выберите раздел

'; + } if ($get == 'fiz') { echo ""; @@ -657,7 +688,7 @@ if (!empty($_POST['open'])) { echo $zay->delteam($z[$user['zayavka']]['team2'][0], $user['zayavka'], 2); AddChatSystem('Внимание!' . Nick::id($user['id'])->short() . ' отказался от поединка.'); } - if (!empty($_POST['confirm2']) AND empty($user['zayavka'])) { + if (!empty($_POST['confirm2']) && empty($user['zayavka'])) { $z = $zay->getlist(2, null, $_REQUEST['gocombat']); $toper = db::c()->query('SELECT `klan` FROM `users` WHERE `id` = ?i', $z[$_REQUEST['gocombat']]['team1'][0])->fetch_assoc(); if ($user['klan'] != $toper['klan'] || $user['klan'] == '') { @@ -740,16 +771,16 @@ if (!empty($_POST['open'])) { } } } - if ($zay->user_status($user['id']) == 2) { - if ($z[$user['zayavka']]['level'] == 2) - echo "Ожидаем подтверждения боя. "; + if ($zay->user_status($user['id']) == 2 && $z[$user['zayavka']]['level'] == 2) { + echo "Ожидаем подтверждения боя. "; } echo '

Показывать заявки
моего уровня
все
'; echo '
'; - if ($z = $zay->getlist(2, $_SESSION['view'])) + if ($z = $zay->getlist(2, $_SESSION['view'])) { foreach ($z as $k => $v) { echo $zay->showfiz($v); } + } echo ''; } @@ -767,12 +798,12 @@ if (!empty($_POST['open'])) {

Подать заявку на поединок

- + + + + +

- Ваша команда бойцов
+ Ваша команда бойцов
Уровни союзников   '; - else echo 'Силы Хаоса'; + } else { + echo 'Силы Хаоса'; + } echo ''; die(); @@ -863,16 +901,16 @@ if (!empty($_POST['goconfirm']) AND empty($user['zayavka'])) { echo ""; -if (!empty($_POST['confirm1']) AND !empty($_POST['gocombat']) AND empty($user['zayavka'])) { +if (!empty($_POST['confirm1']) && !empty($_POST['gocombat']) && empty($user['zayavka'])) { echo $zay->addteam(1, 4); } -if (!empty($_POST['confirm2']) AND $_POST['gocombat'] AND empty($user['zayavka'])) { +if (!empty($_POST['confirm2']) && $_POST['gocombat'] && empty($user['zayavka'])) { echo $zay->addteam(2, 4); } -if (!empty($_POST['open']) AND empty($user['zayavka'])) { - //print_r($_REQUEST); +if (!empty($_POST['open']) && empty($user['zayavka'])) { switch ($_POST['levellogin1']) { + default: case 0 : //любой $min1 = 0; $max1 = 21; @@ -907,6 +945,7 @@ if (!empty($_POST['open']) AND empty($user['zayavka'])) { break; } switch ($_POST['levellogin2']) { + default: case 0 : //любой $min1 = 0; $max1 = 21; @@ -940,8 +979,12 @@ if (!empty($_POST['open']) AND empty($user['zayavka'])) { $max1 = 99; break; } - if ($min1 < 0) $min1 = 0; - if ($max1 < 0) $max1 = 0; + if ($min1 < 0) { + $min1 = 0; + } + if ($max1 < 0) { + $max1 = 0; + } if ($_POST['k']) { $_POST['k'] = 4; } else { @@ -952,37 +995,33 @@ if (!empty($_POST['open']) AND empty($user['zayavka'])) { } else { $blood = 0; } - if ($_POST['nlogin1'] == 1 && $_POST['nlogin2'] == 1) { - echo "Не надо превращать групповой бой в физический поединок"; - } else { - echo $zay->addzayavka($_POST['startime'] / 60, $_POST['timeout'], $_POST['nlogin1'], $_POST['nlogin2'], $_POST['k'], $min1, $min2, $max1, $max2, $_POST['cmt'], 4, $blood); - } + + echo $zay->addzayavka($_POST['startime'] / 60, $_POST['timeout'], $_POST['nlogin1'], $_POST['nlogin2'], $_POST['k'], $min1, $min2, $max1, $max2, $_POST['cmt'], 4, $blood); + } -///////////////////////////////// + echo ""; echo '
Показывать заявки
моего уровня
все
'; echo ''; echo '

'; -if ($z = $zay->getlist(4, $_SESSION['view'])) +if ($z = $zay->getlist(4, $_SESSION['view'])) { foreach ($z as $k => $v) { if ((($z[$k]['start'] - time()) < 0) || (($z[$k]['t1c'] == count($z[$k]['team1'])) && ($z[$k]['t2c'] == count($z[$k]['team2'])))) { $zay->battlestart("CHAOS", $k, 4); } echo $zay->showgroup($v); } +} echo ''; } if ($get == 'haos') { - if ($user['level'] < 2) { - die("


Хаотичные бои доступны с 2 уровня.
"); - } - - if (!empty($_POST['open']) AND empty($user['zayavka'])) { + if (!empty($_POST['open']) && empty($user['zayavka'])) { switch ($_POST['levellogin1']) { + default: case 0 : $min1 = 1; $max1 = 21; @@ -996,7 +1035,6 @@ if ($get == 'haos') { $max1 = (int)$user['level'] + 1; break; } - //$_POST['k'] = 3; $blood = 0; if ($_POST['travma']) { @@ -1014,44 +1052,49 @@ if ($get == 'haos') { } if ($zay->user_status($user['id']) != 0) { $z = $zay->getlist(5, null, $user['zayavka']); - if ($z[$user['zayavka']]['level'] == 5) + if ($z[$user['zayavka']]['level'] == 5) { echo "Ожидаем начала группового боя...
Бой начнется через: " . round(($z[$user['zayavka']]['start'] - time()) / 60, 1) . " мин."; + } } echo '
Показывать заявки
моего уровня
все
'; echo '
'; - if ($z = $zay->getlist(5, $_SESSION['view'])) + if ($z = $zay->getlist(5, $_SESSION['view'])) { foreach ($z as $k => $v) { if (($z[$k]['start'] - time()) < 0) { $zay->battlestart("CHAOS", $k, 5); } echo $zay->showhaos($v); } + } echo ''; - //print_r($_POST); } if ($get == 'current') { $t1 = floor(time() - 900); $data = db::c()->query('SELECT * FROM `battle` WHERE `win` = 3 AND `to1` > ?i AND `to2` > ?i ORDER BY `id` ASC', $t1, $t1); - if (empty($data->getNumRows())) echo 'Надо же! Ни одного поединка!'; - else while ($row = $data->fetch_assoc()) { - echo "{$row['date']}"; - $z = explode(";", $row['t1']); - foreach ($z as $k => $v) { - if ($k > 0) { - echo ","; + if (empty($data->getNumRows())) { + echo 'Надо же! Ни одного поединка!'; + } + else { + while ($row = $data->fetch_assoc()) { + echo "{$row['date']}"; + $z = explode(";", $row['t1']); + foreach ($z as $k => $v) { + if ($k > 0) { + echo ","; + } + Nick::id($v)->full(); } - Nick::id($v)->full(); - } - echo " против "; - $z = explode(";", $row['t2']); - foreach ($z as $k => $v) { - if ($k > 0) { - echo ","; + echo " против "; + $z = explode(";", $row['t2']); + foreach ($z as $k => $v) { + if ($k > 0) { + echo ","; + } + Nick::id($v)->full(); } - Nick::id($v)->full(); + echo " »»
"; } - echo " »»
"; } }