<?php
//
//
try{ 
$BaseConnect = new PDO("mysql:host=localhost;dbname=79910259319_test",'79910259319','JxU75k6dTHE');
} 
catch(PDOException $e){ 
die('Нет соединения с базой данных!');   
} 
$BaseConnect->exec("set names utf8"); 
// 
//фильтрация текста 
function checking($text) { 
$text = trim(htmlspecialchars($text)); 
return $text; 
} 
//фильтрация цифр 
function number($num) { 
$num = abs(intval($num)); 
return $num; 
} 
// 
//Для обычного запроса 
function cnt($sql, $params = array()) { 
    global $BaseConnect; 
$stmt = $BaseConnect -> prepare($sql); 
    $stmt -> execute($params); 
    $stmt = $stmt->rowCount(); 
    return $stmt; 
} 
// 
//Работаем с базой данных 
function qry($sql, $params = array()) { 
global $BaseConnect; 
$stmt = $BaseConnect -> prepare($sql); 
    $stmt -> execute($params); 
    return $stmt; 
}  
// 
//Для вывода ассоциотивного массива  
function fch($sql, $params = array()) { 
    global $BaseConnect; 
    $stmt = $BaseConnect -> prepare($sql); 
    $stmt -> execute($params); 
    $stmt = $stmt->fetch(); 
    return $stmt; 
}  
//
//
qry("UPDATE `users` SET `key2` = ?, `sun2` = ?", array(0,0));  //обновляем ключи и редкий сундук 
//
//
$m1 = fch("SELECT * FROM `users` WHERE `tour` >= ? ORDER BY `tour` DESC LIMIT 1", array(1)); 
//
//
if($m1['tour'] >= 1){
//
//
$coll1 = intval($m1['tour']*15/300);
//
//
if($m1){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll1-50,$m1['id'])); 
}
//
//
$m2 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m1['tour'],$m1['id'])); 
//
//
$coll2 = intval($m2['tour']*15/300);
//
//
if($m2){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll2-50,$m2['id'])); 
}
//
//
$m3 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m2['tour'],$m1['id'],$m2['id']));
//
//
$coll3 = intval($m3['tour']*15/300);
//
//
if($m3){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll3-50,$m3['id'])); 
} 
//
//
$m4 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m3['tour'],$m1['id'],$m2['id'],$m3['id'])); 
//
//
$coll4 = intval($m4['tour']*15/300);
//
//
if($m4){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll4-50,$m4['id'])); 
}
//
//
$m5 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m4['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'])); 
//
//
$coll5 = intval($m5['tour']*15/300);
//
//
if($m5){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll5-50,$m5['id'])); 
}
//
//
$m6 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ?  AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m5['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'],$m5['id'])); 
//
//
$coll6 = intval($m6['tour']*15/300);
//
//
if($m6){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll6-50,$m6['id'])); 
}
//
//
$m7 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ?  AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m6['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'],$m5['id'],$m6['id'])); 
//
//
$coll7 = intval($m7['tour']*15/300);
//
//
if($m7){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll7-50,$m7['id'])); 
}
//
//
$m8 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ?  AND `id` != ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m7['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'],$m5['id'],$m6['id'],$m7['id'])); 
//
//
$coll8 = intval($m8['tour']*15/300);
//
//
if($m8){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll8-50,$m8['id'])); 
}
//
//
$m9 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ?  AND `id` != ? AND `id` != ?  AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m8['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'],$m5['id'],$m6['id'],$m7['id'],$m8['id'])); 
//
//
$coll9 = intval($m8['tour']*15/300);
//
//
if($m9){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll9-50,$m9['id'])); 
}
//
//
$m10 = fch("SELECT * FROM `users` WHERE `tour` <= ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ?  AND `id` != ? AND `id` != ? AND `id` != ? AND `id` != ? ORDER BY `tour` DESC LIMIT 1", array($m9['tour'],$m1['id'],$m2['id'],$m3['id'],$m4['id'],$m5['id'],$m6['id'],$m7['id'],$m8['id'],$m9['id'])); 
//
//
$coll10 = intval($m10['tour']*15/300);
//
//
if($m10){
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array($coll10-50,$m10['id'])); 
}
//
//
$text = "Подведены итоги турнира:[br][br]

".($m1 ? "1. [img=http://mhol.ru/style/$m1[sex].png]16[/img] [color=#F2C234]$m1[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m1['tour']*15/300)." [/color]" : "")."

".($m2 ? "[br]2. [img=http://mhol.ru/style/$m2[sex].png]16[/img] [color=#F2C234]$m2[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m2['tour']*15/300)." [/color]" : "")."

".($m3 ? "[br]3. [img=http://mhol.ru/style/$m3[sex].png]16[/img] [color=#F2C234]$m3[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m3['tour']*15/300)." [/color]" : "")."

".($m4 ? "[br]4. [img=http://mhol.ru/style/$m4[sex].png]16[/img] [color=#F2C234]$m4[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m4['tour']*15/300)." [/color]" : "")."

".($m5 ? "[br]5. [img=http://mhol.ru/style/$m5[sex].png]16[/img] [color=#F2C234]$m5[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m5['tour']*15/300)." [/color]" : "")."

".($m6 ? "[br]6. [img=http://mhol.ru/style/$m6[sex].png]16[/img] [color=#F2C234]$m6[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m6['tour']*15/300)." [/color]" : "")."

".($m7 ? "[br]7. [img=http://mhol.ru/style/$m7[sex].png]16[/img] [color=#F2C234]$m7[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m7['tour']*15/300)." [/color]" : "")."

".($m8 ? "[br]8. [img=http://mhol.ru/style/$m8[sex].png]16[/img] [color=#F2C234]$m8[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m8['tour']*15/300)." [/color]" : "")."

".($m9 ? "[br]9. [img=http://mhol.ru/style/$m9[sex].png]16[/img] [color=#F2C234]$m9[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m9['tour']*15/300)." [/color]" : "")."

".($m10 ? "[br]10. [img=http://mhol.ru/style/$m10[sex].png]16[/img] [color=#F2C234]$m10[login][/color] - [img=http://mhol.ru/images/ruby.png]16[/img] [color=tomato]".intval($m10['tour']*15/300)." [/color]" : "")."

[br][br]
Поздравляем всех победителей! 
[br]
Остальные игроки набравшие 250 [img=http://mhol.ru/images/tour.png]16[/img] получили поощрительные призы.
";
//
//
$name = "Королевский турнир";
//
//
qry("INSERT INTO `news` SET `time` = ?, `name` = ?, `text` = ?", array(time(),$name,$text));
//
//
qry("UPDATE `users` SET `tour` = ?, `news` = ?", array(0,1)); 
//
//
$user_q = $BaseConnect->query("SELECT * FROM `users` WHERE `tour` >= '250'");
//
while($user = $user_q->fetch(PDO::FETCH_ASSOC)){
//
//
qry("UPDATE `users` SET `gold` = `gold` + ? WHERE `id` = ? LIMIT 1", array(50,$user['id']));
//
//
}
//
//
}
//
//
?>