<?php
include_once '../../sys/inc/start.php';
include_once '../../sys/inc/compress.php';
include_once '../../sys/inc/sess.php';
include_once '../../sys/inc/home.php';
include_once '../../sys/inc/settings.php';
include_once '../../sys/inc/db_connect.php';
include_once '../../sys/inc/ipua.php';
include_once '../../sys/inc/fnc.php';
include_once '../../sys/inc/user.php';

include_once 'online.php';

if (!isset($user) && !isset($_GET['id'])){header("Location: index.php?".SID);exit;}
if (isset($user))$ank['id']=$user['id'];
if (isset($_GET['id']))$ank['id']=intval($_GET['id']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"),0)==0){header("Location: index.php?".SID);exit;}
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $ank[id] LIMIT 1"));
$klubi=mysql_fetch_array(mysql_query("SELECT * FROM `fm_club` WHERE `id_user` = '$ank[id]' LIMIT 1"));
$cena = mysql_fetch_array(mysql_query("SELECT * FROM `fm_admin` WHERE `id` = '1' LIMIT 1"));

$set['title']=''.$klubi['name'].' Игроки';
include_once '../sys/inc/thead.php';
title();
err();
aut();
if (isset($_GET['del']) && $user['id']==$ank['id'])
{
$del=intval($_GET['del']);
$gam=mysql_fetch_array(mysql_query("SELECT * FROM `fm_gamers` WHERE `id` = '$del' LIMIT 1"));
if ($klubi['id']!=$gam['id_club']) if ($klubi['id']!=$gam['id_club'])$err='Это не ваш Игрок!';
mysql_query("DELETE FROM `fm_gamers` WHERE `id` = '$del'");
msg('Успешно удален!');

}

if (isset($_GET['var']) && $user['id']==$ank['id'])
{
$var=intval($_GET['var']);
$gam=mysql_fetch_array(mysql_query("SELECT * FROM `fm_gamers` WHERE `id` = '$var' LIMIT 1"));
if ($klubi['id']!=$gam['id_club']) if ($klubi['id']!=$gam['id_club'])$err='Это не ваш Игрок!';
$gamer=mysql_fetch_array(mysql_query("SELECT * FROM `fm_gamers` WHERE `id` = '$var' LIMIT 1"));
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id` = '$var' and `timevar` > '".($time-3600)."'"),0)==1)$err='Только один раз в час!';
if (!isset($err))
{
mysql_query("UPDATE `fm_gamers` SET `timevar` = '$time' WHERE `id` = '$var'");
mysql_query("UPDATE `fm_club` SET `var` = '".($klubi['var'] + 1)."' WHERE `id_user` = '$user[id]'");
msg('тренировка прошла успешно!');
}
}
$fasi=$cena['gamer'];
if (isset($_GET['set']) && isset($_POST['nick']) && isset($_POST['poz']) && $user['id']==$ank['id'])
{
$poz=intval($_POST['poz']);
if ($user['fm_money']<$fasi)$err="У вас нет $fasi Долларов!";
$motamashe = mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi[id]'"), 0);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `poz` = '0' and `id_club` = '$klubi[id]'"),0)==0 && $poz!=0 && $motamashe==10)$err='вы не купили голкипера!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `poz` = '0' and `id_club` = '$klubi[id]'"),0)==1 && $poz==0)$err='Только 1 голкипер!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `poz` = '1' and `id_club` = '$klubi[id]'"),0)==4 && $poz==1)$err='Не более 4 защитников!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `poz` = '2' and `id_club` = '$klubi[id]'"),0)==4 && $poz==2)$err='Не более 4 полузащитников!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `poz` = '3' and `id_club` = '$klubi[id]'"),0)==4 && $poz==3)$err='Не более 4 нападающих!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `name` = '".mysql_real_escape_string(esc($_POST['nick']))."'"),0)!=0)$err='Имя игрока уже занято!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi[id]'"),0)==11)$err='Не более 11 игроков!';
if (strlen2($_POST['nick'])<3)$err='Короткое Имя!';
if (strlen2($_POST['nick'])>32)$err='Имя не должно быть длиннее 32-х символов!';
if (ereg("\=|\+|\{|\}|\(|\)|\^|\%|\\$|#|@|!|&|\~|'|\"|:|;|`|,|\.|\?|<|>",$_POST['nick']))$err='Имя игрока. присутствуют запрещенные символы!';
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_club` WHERE `id_user` = '$ank[id]'"),0)==0)$err='У вас нет Клубa!';

if (!isset($err))
{
$nick=mysql_real_escape_string(esc($_POST['nick']));
mysql_query("INSERT INTO `fm_gamers` (`id_club`, `name`, `time`, `poz`) VALUES  ('".$klubi['id']."', '$nick', '$time', '$poz')");
mysql_query("UPDATE `user` SET `fm_money` = '".($user['fm_money']-$fasi)."' WHERE `id` = '$user[id]' LIMIT 1");
msg('Вы успешно купили игрока!');
}
}
err();




if ($user['id']==$ank['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi[id]'"),0)!=11)
{
echo "<div class='menu'>Имя игрока:<br />\n";
echo "<form method='post' action='gamers.php?set'><div>\n";
echo "<input type='text' name='nick' value='' maxlength='32' /><br />";
echo "Позиция:<br />\n<select name=\"poz\">\n";
echo "<option value=\"3\">нападающий</option>\n";
echo "<option value=\"2\">полузащитник</option>\n";
echo "<option value=\"1\">защитник</option>\n";
echo "<option value=\"0\">голкипер</option>\n";
echo "</select><br />\n";
echo "<input class='doctor' type='submit' name='save' value='Купить' />\n";
echo "</div>\n";
echo "</form>\n";
echo "<br />*Стоимость $fasi$";
echo "</div>";
}


$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id_club` = '$klubi[id]'"),0);
if ($k_post==0)
{
echo "<div class='menu'>Нет игроков!</div>\n";
}
$q=mysql_query("SELECT * FROM `fm_gamers` WHERE `id_club` = '$klubi[id]' ORDER BY `poz` ASC LIMIT 11");
while ($post = mysql_fetch_array($q))
{
if($num==1){
echo "<div class='f1'>";
$num=0;}
else{
echo "<div class='f2'>";
$num=1;}
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `fm_gamers` WHERE `id` = '$post[id]' and `timevar` > '".($time-3600)."'"),0)==1){$feri='green';}else{$feri='red';}
if ($user['id']==$ank['id'])echo "[<a href='gamers.php?var=$post[id]'><b><span style='color:$feri'>т</span></b></a>]\n";
echo "<a href='gamer.php?id=$post[id]'>$post[name]</a>\n";
if($post['poz']==0){
$statusi='[г]';
}else if($post['poz']==1){
$statusi='[з]';
}else if($post['poz']==2){
$statusi='[п]';
}else if($post['poz']==3){
$statusi='[н]';}
echo " $statusi<br />";
echo'</div>';
}


echo "<div class='foot'>";
echo "&#171; <a href='index.php'>Назад</a><br/>";
echo "</div>";

include_once '../../sys/inc/tfoot.php';

?>
