<?
  ##########################################
## Автор: Говно кодырь -=ШАХТЕР=-           #  
## Сайт: http://masteram.us ICQ  : 46-92-90 #
### Версия 4.0 Лицензионная                 #
## Данная версия скрипта являлась ПЛАТНОЙ,  #
## вы НЕ ИМЕЛИ ПРАВА распрострянять данный  #
## скрипт или какие-либо части его кода...  #
## А ТЕПЕРЬ СВСЕМ НА ЭТО ПОХУЙ!!!)))        #
  #########################################
require'../config.php';

aut();

$rmmaf  = check(intval($_GET['rmmaf']));

$page = check(intval($_GET['page']));
who_add(0,'enter'); // Занисываем Кто онлайн...


if (!ctype_digit($rmmaf)) { header("Location: index.php"); die; }
$time0 = (time()+0);

## Файл Мафия
$us = $user['user'];
$mafiozi = $mafiozi;
$mafid = 0;
$rmafaction = "mafaction".$rmmaf;
$rscores = "scores".$rmmaf;
$ringame = "ingame".$rmmaf;
$room="maf".$rmmaf;

require'configurations.php';
## В игре человек или нет: ##
$db->sql_query ("select * from $ringame WHERE gamer_id = '".$user['id']."'");
if (mysql_affected_rows()!=0) $gmes = 1;
else $gmes = 0;

## Проверка, на связи ли человек: ##
$tm = time()-300;
$r = $db->sql_query ("select * from $ringame WHERE (onl<'".$tm."')");
if (mysql_affected_rows() != 0){
$mmsg = "";

while (($a = $db->sql_fetchrow($r))!==false){
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$gname = get_name_by_id($a["id_in_game"], $rmmaf);
$gid = $a["gamer_id"];
$mmsg = $mmsg."$grole $gname покидает игру (превышено время ожидания ответа)! ";

## Вычитаем очки: ##
$db->sql_query("update users set creditsingame = creditsingame - 100 WHERE id = '".$gid."'");
$r = $db->sql_query("select creditsingame from users WHERE id = '".$gid."'");
$a = $db->sql_fetchrow($r);
$cring = $a["creditsingame"];
## Записываем для дальнейшего вывода, кто сколько очков набрал ##
$db->sql_query("insert into $rscores set gamer_id = '".$gid."', score = '".$cring."'");
$db->sql_query("delete from $ringame WHERE gamer_id = '".$gid."'");
}
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$vktr = false;

$r = $db->sql_query("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];

$db->sql_query ("select * from $ringame WHERE role = 'главарь мафии'");
if (mysql_affected_rows()==0&&$sm!=0){
$r = $db->sql_query ("select * from $ringame WHERE role = 'мафиози'");
if (mysql_affected_rows()==0){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");

$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "обыватель") $db->sql_query("update users set creditsingame = creditsingame + 100 WHERE id = '".$gamer_id."'");
else $db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}
$mmsg = "Игра окончена, победа мирных жителей! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
}else{
$a = $db->sql_fetchrow($r);
$id_in_game = $a["id_in_game"];
$gamer_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gamer_id."' LIMIT 1;"));
$db->sql_query ("update $ringame set role = 'главарь мафии' WHERE id_in_game = '".$id_in_game."'");
$mmsg = "Теперь вы главарь мафии!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$gamer_id."', gamemes='1', komu='".$igrok[0]."'");
}
}

if ($vktr!==true){

$r = $db->sql_query ("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);

if ($a["sm"]==2){
$db->sql_query("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");

if (mysql_affected_rows()==2) $vk = "maf";
if (mysql_affected_rows()==1){
$db->sql_query("select * from $ringame WHERE (role = 'доктор')or(role = 'комиссар катани')or(role = 'путана')or(role = 'маньяк')");
if (mysql_affected_rows() != 0) $vk = "ni4"; else $vk = "maf";
}
}
if ($a["sm"]==1){
$db->sql_query ("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");
if (mysql_affected_rows() != 0) $vk = "maf";
}
if ($a["sm"]==0) $vk = "ni4";

$db->sql_query("select * from $ringame");
$nm = round(mysql_affected_rows()/2-0.5);
$r = $db->sql_query("Select count(*) as sm from $ringame WHERE (role='мафиози')or(role='главарь мафии')");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];
if ($sm>$nm) $vk = "maf";

if (isset($vk)){
$vktr = true;
if ($vk == "ni4"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gamer_id = $a["gamer_id"];
$db->sql_query("update users set creditsingame = creditsingame + 25 WHERE id = '".$gamer_id."'");
}
$mmsg = "Силы Мафии и мирных жителей равны, ничья! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
}
if ($vk == "maf"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "мафиози"||$role == "главарь мафии")
$db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}
$mmsg = "Всем хана, победа Мафов! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
}
}
}
}
$tm = time();
$db->sql_query ("update users set onl = '".$tm."' WHERE id = '".$user['id']."'");
if ($gmes == 1) $db->sql_query ("update $ringame set onl = '".$tm."' WHERE gamer_id = '".$user['id']."'");
$r = $db->sql_query ("select * from $rmafaction");
$a = $db->sql_fetchrow($r);
$action = $a["action"];
$nexttime = $a["nexttime"];

if (($action == "game_night"&&$tm>$nexttime)||($action == "dvote"&&$tm>$nexttime)){
$tm = time()+120;
$db->sql_query ("update $rmafaction set action = 'game_day', nexttime = '".$tm."', kiked = '0', cround = cround + 1");
$kiked = $a["kiked"];
if ($kiked==1) $mmsg = "Наступает ночь, все жители засыпают, кроме некоторых...";
else $mmsg = "Жители, прогалдев целый день, так ничего и не решили. Наступает ночь...";

$db->sql_query("update $ringame set dvote = '0', wholin = '0'");

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$db->sql_query("select gamer_id from $ringame WHERE role = 'комиссар катани'");
if(mysql_affected_rows()==0){
$r = $db->sql_query("select state from $ringame WHERE role = 'жена комиссара'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
if ($a["state"]==0) $db->sql_query ("update $ringame set state = '1' WHERE role = 'жена комиссара'");
}
}

$db->sql_query("update $ringame set gamer_act = ''");
$r = $db->sql_query("select * from $ringame WHERE role = 'мафиози'");
if (mysql_affected_rows()!=0){
while (($a = $db->sql_fetchrow($r))!==false){
$thg = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$thg."' LIMIT 1;"));
$re = $db->sql_query("select * from $ringame WHERE ((role = 'мафиози')or(role = 'главарь мафии'))and(gamer_id != '".$thg."')");
$so = "";
while (($b = $db->sql_fetchrow($re))!==false){
$sname = get_name_by_id($b["id_in_game"], $rmmaf);
if (get_role_by_id($b["id_in_game"], $rmmaf)!="главарь мафии") $so = $so."$sname, "; else $so = $so."$sname(главарь), ";
}
$so = substr($so,0,strlen($so)-2);
$mmsg = "Сообщники: $so";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$thg."', gamemes='1', komu='".$igrok[0]."'");
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'главарь мафии'");
$a = $db->sql_fetchrow($r);
$glmaf_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$glmaf_id."' LIMIT 1;"));
$mmsg = "Выбирайте, кому сегодня не поздоровится(!номер, в приват Мафу): ";
$r = $db->sql_query("select * from $ringame");
$kol = mysql_affected_rows();
for ($i=1;$i<=$kol;$i++){
$a = $db->sql_fetchrow($r);
$gamer_id = $a["gamer_id"];
$r2 = $db->sql_query ("select * from users where id = '".$gamer_id."'");
$b = $db->sql_fetchrow($r2);
$gname = $b["user"];
$id_in_game = $a["id_in_game"];
$s = $s."$id_in_game - $gname, ";
}
$mmsg = $mmsg.$s;
$mmsg = substr($mmsg,0,strlen($mmsg)-2);

$r = $db->sql_query ("select * from $ringame WHERE role = 'мафиози'");
if (mysql_affected_rows()!=0){
$sg = "";
while (($a = $db->sql_fetchrow($r))!==false){
$sname = get_name_by_id($a["id_in_game"], $rmmaf);
$sg = $sg."$sname, ";
}
$sg = substr($sg,0,strlen($sg)-2);
$mmsg = $mmsg."<br/>"."(ваши сообщники: $sg)";
}

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$glmaf_id."', gamemes='1', komu='".$igrok[0]."'");

$r = $db->sql_query("select * from $ringame WHERE role = 'комиссар катани'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$kom_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$kom_id."' LIMIT 1;"));
$mmsg = "Выбирайте, кого проверить (!номер, в приват Мафу) или убить (!!номер): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$kom_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'маньяк'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$man_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$man_id."' LIMIT 1;"));
$mmsg = "Кто же будет жертвой маньяка? (!номер, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$man_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'путана'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$put_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$put_id."' LIMIT 1;"));
$mmsg = "К кому же шлюха направится сегодня? (!номер, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$put_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'бомж'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$bomj_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$bomj_id."' LIMIT 1;"));
$mmsg = "У кого роемся в карманах? (!номер, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$bomj_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'доктор'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$dok_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$dok_id."' LIMIT 1;"));
$mmsg = "Кого лечим? (!номер, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$dok_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'клоун'");
$a = $db->sql_fetchrow($r);
$state = $a["state"];
if (mysql_affected_rows()!=0&&$state==0){
$kloun_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$kloun_id."' LIMIT 1;"));
$mmsg = "Выберите, чьи роли поменять местами(''!номер !номер'', через пробел, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$kloun_id."', gamemes='1', komu='".$igrok[0]."'");
}

$r = $db->sql_query("select * from $ringame WHERE role = 'жена комиссара'");
$a = $db->sql_fetchrow($r);
$state = $a["state"];
if (mysql_affected_rows()!=0&&$state==1){
$jena_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$jena_id."' LIMIT 1;"));
$mmsg = "На кого выпустим нашу месть? (!номер, в приват Мафу): ".$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$jena_id."', gamemes='1', komu='".$igrok[0]."'");
}
}

if (($action=="game_day"&&$tm>$nexttime)||$action == "allvoted"){
$tm = time()+180;
$db->sql_query ("update $rmafaction set action = 'game_night', nexttime = '".$tm."'");
$kom_kil = 0;
$maf_kil = 0;
$man_kil = 0;
$jena_kil = 0;
$mmsg = "Вот и день наступил. Но все ли дожили до него?";
$r = $db->sql_query("select * from $ringame WHERE role = 'путана'");
$a = $db->sql_fetchrow($r);
$put_act = 0;
$put_act = $a["gamer_act"];
$put_id = $a["gamer_id"];

$r = $db->sql_query("select * from $ringame WHERE role = 'горец'");
$a = $db->sql_fetchrow($r);
$gor_id_in_game = $a["id_in_game"];

if ($put_act != 0) $db->sql_query("update users set creditsingame = creditsingame + 5 WHERE id = '".$put_id."'");

$r = $db->sql_query("select * from $ringame WHERE role = 'доктор'");
$a = $db->sql_fetchrow($r);
$doknotact = false;
$dok_act = 0;
if ($put_act != $a["id_in_game"]) $dok_act = $a["gamer_act"];

$r = $db->sql_query("select * from $ringame WHERE role = 'комиссар катани'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$kom_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$kom_id."' LIMIT 1;"));
if ($a["gamer_act"]!=""){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Инспектор катани всю ночь был занят шлюхой, и не успел отловить бандитов.";
else if (strpos($a["gamer_act"],"!")===false){
$db->sql_query("update users set creditsingame = creditsingame + 5 WHERE id = '".$kom_id."'");
$mmsg = $mmsg." Инспектор катани времени зря не терял. Проведя тщательное расследование, он наконец-то выяснил, кто такой $wh!";
if (get_role_by_id($a["gamer_act"], $rmmaf)=="мафиози"||get_role_by_id($a["gamer_act"], $rmmaf)=="главарь мафии")$tokom = "$wh - мафиози";
else $tokom = "$wh - мирный житель";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$tokom."', id='".$tm."', towhom='".$kom_id."', gamemes='1', komu='".$igrok[0]."'");
}else{
$a["gamer_act"] = str_replace("!","",$a["gamer_act"]);
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
$kto = role_to_rpadej(get_role_by_id($a["gamer_act"], $rmmaf));
if ($gor_id_in_game!=$a["gamer_act"]){
if ($a["gamer_act"] != $dok_act){
if (get_role_by_id($a["gamer_act"], $rmmaf)=="мафиози"||get_role_by_id($a["gamer_act"], $rmmaf)=="главарь мафии") {$mmsg = $mmsg." Проведя сложнейшую операцию, инспектор катани наконец-то вычислил и убил $kto $wh!!!"; $db->sql_query("update users set creditsingame = creditsingame + 20 WHERE id = '".$kom_id."'");}
else {$mmsg = $mmsg." Инспектор катани так спятил, что казнил ни в чем не повинного $kto $wh!"; $db->sql_query("update users set creditsingame = creditsingame - 20 WHERE id = '".$kom_id."'");}
$kom_kil = $a["gamer_act"];
}else {
$mmsg = $mmsg." Комиссар катани почти расстрелял $wh, но добрый доктор буквально достал того из гроба.";
$doknotact = true;
}
}else {
$mmsg = $mmsg." Комиссар катани расстрелял ночью горца, и был очень удивлен, когда тот не умер.";
}                                                                                                                                      }
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'главарь мафии'");
$a = $db->sql_fetchrow($r);
$maf_id = $a["gamer_id"];
if ($a["gamer_act"]!=""){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Шлюха направилась в логово к мафии и всю ночь пробыла с главарем мафии, отвлекая его от дел.";
else{
if ($gor_id_in_game != $a["gamer_act"]){
$db->sql_query("update users set creditsingame = creditsingame + 10 WHERE id = '".$maf_id."'");
$kto = role_to_tpadej(get_role_by_id($a["gamer_act"], $rmmaf));
if ($dok_act != $a["gamer_act"]){
$mmsg = $mmsg." Мафия сегодня зверски расправилась с $kto $wh!";
$maf_kil = $a["gamer_act"];
}else{
$mmsg = $mmsg." Мафы убили $wh, но доктор сгонял на кладбище и пришил ему все обратно";
$doknotact = true;
}
}else{
$mmsg = $mmsg." Мафия ночью нарвалась на горца, он ей так накостылял, что мафы еле ноги унесли.";
}
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'маньяк'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$man_id = $a["gamer_id"];
$man_act = $a["gamer_act"];
if ($a["gamer_act"]!=""){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Маньяк так хотел намотать кишки на руку кому-нибудь этой ночью, но встретил шлюху и решил отложить это дело.";
else{
$kto = get_role_by_id($a["gamer_act"], $rmmaf);
if ($dok_act != $a["gamer_act"]){
$db->sql_query("select * from $ringame WHERE (id_in_game = '".$man_act."')and((role = 'мафиози')or(role = 'главарь мафии'))");
if (mysql_affected_rows()!=0) $db->sql_query("update users set creditsingame = creditsingame + 20 WHERE id = '".$man_id."'");
else $db->sql_query("update users set creditsingame = creditsingame - 5 WHERE id = '".$man_id."'");
$mmsg = $mmsg." $kto $wh был наден утром в мусорном баке с топором в жопе. Похоже, тут не обошлось без маньяка...";
$man_kil = $a["gamer_act"];
}else{
$mmsg = $mmsg." Маньяк вставил топор в жопу $wh, но операция была успешной, док его вытащил";
$doknotact = true;
}
}
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'бомж'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$bomj_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$bomj_id."' LIMIT 1;"));
if ($a["gamer_act"]!=""){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Нахальный бомж поймал шлюху на улице и затащил к подвал. Там они и провели всю ночь.";
else{
$db->sql_query("update users set creditsingame = creditsingame + 5 WHERE id = '".$bomj_id."'");
$mmsg = $mmsg." Бомж ночью рылся в карманах у $wh!";
$kto = get_role_by_id($a["gamer_act"], $rmmaf);
$mmsgtob = "$wh это $kto!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsgtob."', id='".$tm."', towhom='".$bomj_id."', gamemes='1', komu='".$igrok[0]."'");
}
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'жена комиссара'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$jena_id = $a["gamer_id"];
$state = $a["state"];
if ($a["gamer_act"]!=""){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Шлюха сегодня направилась к жене комиссара. Камасутра отдыхает.";
else{
if ($gor_id_in_game != $a["gamer_act"]){
$db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$jena_id."'");
$kto = role_to_rpadej(get_role_by_id($a["gamer_act"], $rmmaf));
if ($dok_act != $a["gamer_act"]){
$mmsg = $mmsg." Жена комиссара отравила ночью $kto $wh, отомстив за смерть мужа.!";
$jena_kil = $a["gamer_act"];
}else{
$mmsg = $mmsg." Жена комиссара почти зарезала $wh, но добрый док за небольшую плату все восстановил.";
$doknotact = true;
}
}else{
$mmsg = $mmsg." Жена комиссара продырявила ножом горца, а он даже не заметил. Теперь ходит с ножом в жопе.";
}
}
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'доктор'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$dok_id = $a["gamer_id"];
if ($doknoact==true) $db->sql_query("update users set creditsingame = creditsingame + 20 WHERE id = '".$dok_id."'");
else if ($put_act!=$a["id_in_game"]) $db->sql_query("update users set creditsingame = creditsingame + 5 WHERE id = '".$dok_id."'");

if ($a["gamer_act"]!=""&&$doknotact===false){
$wh = get_name_by_id($a["gamer_act"], $rmmaf);
if ($put_act==$a["id_in_game"]) $mmsg = $mmsg." Доктор целую ночь прокувыркался с путаной, так никого и не вылечив";
else{
if ($a["gamer_id"]==$a["gamer_act"]) $mmsg = $mmsg." Наш доктор эгоистично лечил сам себя.";
else $mmsg = $mmsg." Доктор накачал лекарствами $wh";
}
}
}

$r = $db->sql_query("select * from $ringame WHERE role = 'клоун'");
if (mysql_affected_rows()!=0){
$a = $db->sql_fetchrow($r);
$state = $a["state"];
$kloun_id = $a["gamer_id"];
if ($a["gamer_act"]!=""){
list($g1,$g2) = explode (" ",$a["gamer_act"]);
$db->sql_query("select gamer_id from $ringame WHERE (id_in_game = '".$g1."')or(id_in_game = '".$g2."')");
if (mysql_affected_rows()==2){
$r = $db->sql_query("select gamer_id, role, state from $ringame WHERE id_in_game = '".$g1."'");
$b = $db->sql_fetchrow($r);
$gid1 = $b["gamer_id"];
$igrok1 = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gid1."' LIMIT 1;"));
$role1 = $b["role"];
$state1 = $b["state"];
$r = $db->sql_query("select gamer_id, role, state from $ringame WHERE id_in_game = '".$g2."'");
$b = $db->sql_fetchrow($r);
$gid2 = $b["gamer_id"];
$igrok2 = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gid2."' LIMIT 1;"));
$role2 = $b["role"];
$state2 = $b["state"];
$mmsg1 = "Ваша новая роль: $role2!";
$mmsg2 = "Ваша новая роль: $role1!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg1."', id='".$tm."', towhom='".$gid1."', gamemes='1', komu='".$igrok1[0]."'");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg2."', id='".$tm."', towhom='".$gid2."', gamemes='1', komu='".$igrok2[0]."'");
$db->sql_query("update $ringame set role = '".$role2."', state = '".$state2."' WHERE id_in_game = '".$g1."'");
$db->sql_query("update $ringame set role = '".$role1."', state = '".$state1."' WHERE id_in_game = '".$g2."'");
$mmsg = $mmsg." Жители! В городе завелся клоун, и этой ночью он поменял роли двух игроков!!!";
$r = $db->sql_query("select cround from $rmafaction");
$a = $db->sql_fetchrow($r);
$sc = $a["cround"]*30;
$db->sql_query("update users set creditsingame = creditsingame + '".$sc."' WHERE id = '".$kloun_id."'");
$db->sql_query("update $ringame set state = '1' WHERE role = 'клоун'");
}
}
}

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE ((id_in_game = '".$kom_kil."')or(id_in_game = '".$maf_kil."')or(id_in_game = '".$man_kil."')or(id_in_game = '".$jena_kil."'))and(users.id = $ringame.gamer_id)");
$db->sql_query ("delete from $ringame WHERE (id_in_game = '".$kom_kil."')or(id_in_game = '".$maf_kil."')or(id_in_game = '".$man_kil."')or(id_in_game='".$jena_kil."')");
$vktr = false;
$r = $db->sql_query("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];
$r = $db->sql_query("select current,prev,prevprev from $rmafaction");
$a = $db->sql_fetchrow($r);
$current = $a["current"];
$prev = $a["prev"];
## $prevprev = $a["prevprev"];
$prevprev = $prev;
$prev = $current;
$current = $sm;
$db->sql_query("update $rmafaction set prevprev = '".$prevprev."', prev = '".$prev."', current = '".$current."'");
if ($current == $prevprev){

$db->sql_query ("update $rmafaction set action = 'nogame'");
$vktr = true;
$mmsg = "Два раунда жители тормозят, и никто не умирает. Поэтому объявляется ничья! А роли были такие: ";
$r = $db->sql_query("select gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gamer_id = $a["gamer_id"];
$db->sql_query("update users set creditsingame = creditsingame + 25 WHERE id = '".$gamer_id."'");
}
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query("delete from $ringame");
$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");
}
$db->sql_query ("select * from $ringame WHERE role = 'главарь мафии'");
if (mysql_affected_rows()==0&&$sm!=0&&$vktr!==true){
$r = $db->sql_query ("select * from $ringame WHERE role = 'мафиози'");
if (mysql_affected_rows()==0){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "обыватель") $db->sql_query("update users set creditsingame = creditsingame + 100 WHERE id = '".$gamer_id."'");
else $db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}
$mmsg = "Игра окончена, победа мирных жителей! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}else{
$a = $db->sql_fetchrow($r);
$id_in_game = $a["id_in_game"];
$gamer_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gamer_id."' LIMIT 1;"));
$db->sql_query ("update $ringame set role = 'главарь мафии' WHERE id_in_game = '".$id_in_game."'");
$mmsg = "Теперь вы главарь мафии!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$gamer_id."', gamemes='1', komu='".$igrok[0]."'");

}
}

if ($vktr!==true){
$r = $db->sql_query ("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);

if ($a["sm"]==2){
$db->sql_query("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");

if (mysql_affected_rows()==2) $vk = "maf";
if (mysql_affected_rows()==1){
$db->sql_query("select * from $ringame WHERE (role = 'доктор')or(role = 'комиссар катани')or(role = 'путана')or(role = 'маньяк')");
if (mysql_affected_rows() != 0) $vk = "ni4";
else $vk = "maf";
}
}
if ($a["sm"]==1){
$db->sql_query ("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");
if (mysql_affected_rows() != 0) $vk = "maf";
}
if ($a["sm"]==0) $vk = "ni4";
$db->sql_query("select * from $ringame");
$nm = round(mysql_affected_rows()/2-0.5);
$r = $db->sql_query("Select count(*) as sm from $ringame WHERE (role='мафиози')or(role='главарь мафии')");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];
if ($sm>$nm) $vk = "maf";

if (isset($vk)){
$vktr = true;
if ($vk == "ni4"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");

$r = $db->sql_query("select gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gamer_id = $a["gamer_id"];
$db->sql_query("update users set creditsingame = creditsingame + 25 WHERE id = '".$gamer_id."'");
}
$mmsg = "Силы Мафии и мирных жителей равны, ничья! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}

if ($vk == "maf"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "мафиози"||$role == "главарь мафии") $db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}

$mmsg = "Всем хана, победа Мафов! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}
}

$db->sql_query ("update $ringame set gamer_act=''");
if ($vktr===false){
$mmsg = "Кого же залинчуем сегодня днем? День длится 3 минуты. Голосуем.(!номер) ";
$r = $db->sql_query("select * from $ringame");
$kol = mysql_affected_rows();
for ($i=1;$i<=$kol;$i++){
$a = $db->sql_fetchrow($r);
$gamer_id = $a["gamer_id"];
$r2 = $db->sql_query ("select * from users where id = '".$gamer_id."'");
$b = $db->sql_fetchrow($r2);
$gname = $b["user"];
$id_in_game = $a["id_in_game"];
$s = $s."<b>$id_in_game</b> - $gname, ";
}
$mmsg = $mmsg.$s;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}

if ($action=="registration"&&$tm>$nexttime){
$db->sql_query("select * from $ringame");
if (mysql_affected_rows()<3){
$db->sql_query ("update $rmafaction set action = 'nogame'");
$mmsg = "Игра не начнется, пока не будет хотя бы 3 игроков!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$db->sql_query ("delete from $ringame");
}else{
$db->sql_query("delete from $rscores");
$tm = time()+25;
$db->sql_query ("update $rmafaction set action = 'game_night', nexttime = '".$tm."', kiked = '1', cround = '0'");
$r = $db->sql_query ("select * from $ringame");
$roles = array("обыватель","главарь мафии","комиссар катани");
if (mysql_affected_rows()>3) array_push ($roles,"доктор");
if (mysql_affected_rows()>4) array_push ($roles,"обыватель");
if (mysql_affected_rows()>5) array_push ($roles,"мафиози");
if (mysql_affected_rows()>6) array_push ($roles,"маньяк");
if (mysql_affected_rows()>7) array_push ($roles,"путана");
if (mysql_affected_rows()>8) array_push ($roles,"обыватель");
if (mysql_affected_rows()>9) array_push ($roles,"мафиози");
if (mysql_affected_rows()>10) array_push ($roles,"бомж");
if (mysql_affected_rows()>11) array_push ($roles,"мафиози");
if (mysql_affected_rows()>12) array_push ($roles,"горец");
if (mysql_affected_rows()>13) array_push ($roles,"клоун");
if (mysql_affected_rows()>14) array_push ($roles,"мафиози");
if (mysql_affected_rows()>15) array_push ($roles,"жена комиссара");

for ($i=1;$i<=((mysql_affected_rows()-15)/3);$i++) array_push ($roles,"мафиози");
$j = count ($roles);
for ($i=1;$i<=mysql_affected_rows()-$j;$i++) array_push ($roles,"обыватель");

mt_srand(time()*100000);
shuffle($roles);
$i = 0;
while (($a = $db->sql_fetchrow($r))!==false){
$gamer_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gamer_id."' LIMIT 1;"));
$db->sql_query("update users set creditsingame = '0' WHERE id = '".$gamer_id."'");
$id_in_game = $i+1;
$role = $roles[$i];
$mmsg = "Вы $role!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$gamer_id."', gamemes='1', komu='".$igrok[0]."'");


$db->sql_query("update $ringame set role = '".$role."', id_in_game = '".$id_in_game."' WHERE gamer_id = '".$gamer_id."'");
$i++;
}
$mmsg = "Раздача ролей окончена! Ночь начнется через 20 секунд!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$db->sql_query ("update $rmafaction set prevprev = '199', prev = '199', current = '200'");
}
}

$smthwr = 0;

if (empty($user['max'])) $user['max']=10;
$max = $user['max'];

$k_post=$db->sql_fetchfield($db->sql_query("select COUNT(*) from $room;"),0);
$k_page=k_page($k_post,$max);
$page=page($k_page);
$start=$max*$page-$max;

$res = $db->sql_query ("Select * from $room order by id desc limit $start, $max;");
$kol = mysql_affected_rows();



if(isset($_POST['msg'])){
$msg  = check($_POST['msg']);
if (strlen2($msg)<1){header ('Location: ?rmmaf='.$rmmaf.'&'.SID);  exit;}
$msg = substr($msg,0,400);

$komu = check($_POST['komu']);

$prvt  = check($_POST['prvt']);

if (!isset($prvt)) $prvt = 0;



if ($prvt == 0 && $komu != $mafiozi) $towhom = "";
else if ($prvt == 0 && $komu == $mafiozi) $towhom = 0;
else $towhom = $prvt;

if ($user["translit"]==1) $msg = trun_to_rus($msg);






$r = $db->sql_query("SELECT * FROM $room WHERE usid = '".$user['id']."' order by id desc LIMIT 1");
$a = $db->sql_fetchrow($r);
if ($a["message"] !== $msg)
{
$time = $time0-1;

$today=date ("H:i", $time);



$hid = $user["inv"];
$kol++;
$rnd = rand(0,99999999);

$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$us."', message='".$msg."', messagewosm = '".$msg_wosm."', messagewoasm = '".$msg_woasm."', id='".$time."', towhom='".$towhom."', hid='".$hid."', usid='".$user['id']."', gamemes = '".$gmes."', komu='".$komu."'");
$usmes["komu"] = $komu;
$usmes["time"] = $today;
$usmes["who"] = $us;
$usmes["usid"] = $user['id'];
$usmes["message"] = $msg;
$usmes["messagewosm"] = $msg_wosm;
$usmes["messagewoasm"] = $msg_woasm;
$usmes["id"] = $time;
$usmes["towhom"] = $towhom;
$usmes["gamemes"] = $gmes;
$smthwr = 1;

$r = $db->sql_query ("select * from $rmafaction");
$a = $db->sql_fetchrow($r);
$action = $a["action"];

if ($action == "game_night"&&$gmes==1){
$s = str_replace("!","",$msg);
$r = $db->sql_query("select * from $ringame WHERE id_in_game = '".$s."'");
if (mysql_affected_rows()!==0){
$a = $db->sql_fetchrow($r);
$id_in_game = $a["id_in_game"];
$db->sql_query ("update $ringame set gamer_act = '".$s."' WHERE gamer_id = '".$user['id']."'");
$wh = get_name_by_id($id_in_game, $rmmaf);

$db->sql_query("select * from $ringame WHERE gamer_act = '".$s."'");
$sm = mysql_affected_rows();
$mmsg = "$us голосует за $wh!(<b>$sm</b>)";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}

$db->sql_query ("select * from $ringame");
$nm = round(mysql_affected_rows()/3);
$r = $db->sql_query ("select *,count(gamer_act) from $ringame WHERE gamer_act != '' group by gamer_act having count(gamer_act)>'".$nm."'");
$a = $db->sql_fetchrow($r);
if (mysql_affected_rows() != 0){
$w = $a["gamer_act"];
$db->sql_query ("update $rmafaction set action = 'dvote'");
$db->sql_query ("update $ringame set dvote = '1' WHERE id_in_game = '".$w."'");
$whk = get_name_by_id($w, $rmmaf);
$mmsg = "Жители, вы уверены, что хотите казнить $whk?(!да или !нет) $whk, у вас есть время оправдаться.";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}

if ($gmes==1&&$action == "dvote"&&($msg == "!da"||$msg == "!net"||$msg == "!да"||$msg == "!нет")){
$r = $db->sql_query("select * from $ringame WHERE dvote = '1'");
$a = $db->sql_fetchrow($r);
$w = $a["id_in_game"];
$whk = get_name_by_id($w, $rmmaf);
if ($msg=="!da"||$msg == "!да"){
$db->sql_query("update $ringame set wholin = '1' WHERE gamer_id = '".$user['id']."'");
$mmsg = "$us голосует за казнь $whk!";
}else{
$db->sql_query("update $ringame set wholin = '-1' WHERE gamer_id = '".$user['id']."'");
$mmsg = "$us голосует против казни $whk!";
}
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$db->sql_query ("select * from $ringame");
$nm = round(mysql_affected_rows()/2-0.5);

$db->sql_query("select * from $ringame WHERE wholin = '1'");
if (mysql_affected_rows()>$nm){
$tm = time();
$db->sql_query("update $rmafaction set action = 'game_night', nexttime = '".$tm."', kiked = '1'");
$krole = get_role_by_id($w, $rmmaf);
if ($krole=="мафиози"||$krole=="главарь мафии"){
$krole = role_to_tpadej($krole);
$mmsg = "Наконец-то мирные жители расправились с $krole $whk!!!";
}else{
$krole = role_to_rpadej($krole);
$mmsg = "Жители сами усложнили себе задачу, казнив ни в чем не повинного $krole $whk!";
}
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE (users.id = $ringame.gamer_id)and(id_in_game = '".$w."')");
$db->sql_query ("delete from $ringame WHERE id_in_game = '".$w."'");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$vktr = false;

$r = $db->sql_query("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];

$db->sql_query ("select * from $ringame WHERE role = 'главарь мафии'");
if (mysql_affected_rows()==0&&$sm!=0){
$r = $db->sql_query ("select * from $ringame WHERE role = 'мафиози'");
if (mysql_affected_rows()==0){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "обыватель") $db->sql_query("update users set creditsingame = creditsingame + 100 WHERE id = '".$gamer_id."'");
else $db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}
$mmsg = "Игра окончена, победа мирных жителей! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}else{
$a = $db->sql_fetchrow($r);
$id_in_game = $a["id_in_game"];
$gamer_id = $a["gamer_id"];
$igrok = @$db->sql_fetchrow(@$db->sql_query ("Select user from users where id='".$gamer_id."' LIMIT 1;"));
$db->sql_query ("update $ringame set role = 'главарь мафии' WHERE id_in_game = '".$id_in_game."'");
$mmsg = "Теперь вы главарь мафии!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$gamer_id."', gamemes='1', komu='".$igrok[0]."'");

}
}

if ($vktr!==true){
$r = $db->sql_query ("select count(*) as sm from $ringame");
$a = $db->sql_fetchrow($r);

if ($a["sm"]==2){
$db->sql_query("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");
if (mysql_affected_rows()==2) $vk = "maf";
if (mysql_affected_rows()==1){
$db->sql_query("select * from $ringame WHERE (role = 'доктор')or(role = 'комиссар катани')or(role = 'путана')or(role = 'маньяк')");
if (mysql_affected_rows() != 0) $vk = "ni4";
else $vk = "maf";
}
}
if ($a["sm"]==1){
$db->sql_query ("select * from $ringame WHERE (role = 'главарь мафии')or(role = 'мафиози')");
if (mysql_affected_rows() != 0) $vk = "maf";
}
if ($a["sm"]==0) $vk = "ni4";

$db->sql_query("select * from $ringame");
$nm = round(mysql_affected_rows()/2-0.5);
$r = $db->sql_query("Select count(*) as sm from $ringame WHERE (role='мафиози')or(role='главарь мафии')");
$a = $db->sql_fetchrow($r);
$sm = $a["sm"];
if ($sm>$nm) $vk = "maf";

if (isset($vk)){
if ($vk == "ni4"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gamer_id = $a["gamer_id"];
$db->sql_query("update users set creditsingame = creditsingame + 25 WHERE id = '".$gamer_id."'");
}
$mmsg = "Силы Мафии и мирных жителей равны, ничья! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
if ($vk == "maf"){
$vktr = true;
$db->sql_query ("update $rmafaction set action = 'nogame'");
$r = $db->sql_query("select role, gamer_id from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$role = $a["role"];
$gamer_id = $a["gamer_id"];
if ($role == "мафиози"||$role == "главарь мафии") $db->sql_query("update users set creditsingame = creditsingame + 50 WHERE id = '".$gamer_id."'");
}
$mmsg = "Всем хана, победа Мафов! А роли были такие: ";
$r = $db->sql_query("Select * from $ringame");
while (($a = $db->sql_fetchrow($r))!==false){
$gnick = get_name_by_id($a["id_in_game"], $rmmaf);
$grole = get_role_by_id($a["id_in_game"], $rmmaf);
$mmsg = $mmsg."<b>$gnick</b> - $grole, ";
}
$mmsg = substr($mmsg,0,strlen($mmsg)-2);
$db->sql_query("insert into $rscores select gamer_id, creditsingame from $ringame, users WHERE users.id = $ringame.gamer_id");
$db->sql_query ("delete from $ringame");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

$mmsg = "За эту игру заработали столько очков: ";
$mmes = scores($rmmaf);
$mmsg = $mmsg.$mmes;
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}
}
}

$db->sql_query("select * from $ringame WHERE wholin = '-1'");
if (mysql_affected_rows()>$nm){
$db->sql_query("update $rmafaction set action = 'game_night'");
$db->sql_query("update $ringame set dvote = '0', wholin = '0', gamer_act = ''");
$mmsg = "$whk оправдан!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid = '".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}

if ($gmes==1&&$action == "game_day"&&$towhom == "".$mafid.""){
$r = $db->sql_query("select * from $ringame WHERE gamer_id = '".$user['id']."'");
$a = $db->sql_fetchrow($r);
$role = $a ["role"];
$gamer_act = $a["gamer_act"];
$state = $a["state"];
if ($role!="обыватель"&&$role!="мафиози"&&$role!="клоун"&&$role!="жена комиссара"){
$s = str_replace("!","",$msg);
$r2 = $db->sql_query ("select * from $ringame WHERE id_in_game = '".$s."'");
if (mysql_affected_rows()==0){
$mmsg = "Что ж ты мне пишешь!?!?";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

}else{
if ($gamer_act==""){
$b = $db->sql_fetchrow($r2);
$hid = $b["gamer_id"];
$r3 = $db->sql_query("select * from users WHERE id = '".$hid."'");
$c = $db->sql_fetchrow($r3);
$gname = $c ["user"];
unset ($c);
unset ($r3);
if ($role == "главарь мафии") {
$mmsg = "Заказ на убийство $gname принят!";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from mafia WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "комиссар катани"&&strpos($msg,"!!")!==false) {
$mmsg = "Заказ на убийство $gname принят!";
$s="!".$s;
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from komissar WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "комиссар катани"&&strpos($msg,"!!")===false) {
$mmsg = "Заказ на проверку $gname принят!";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from komissar WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "доктор") {
$mmsg = "Заказ на лечение $gname принят!";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from doktor WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "маньяк") {
$mmsg = "Изнасилуем $gname =)";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from maniac WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "путана") {
$mmsg = "$gname сегодня ночью будет занят)";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from wluha WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
if ($role == "бомж") {
$mmsg = "Копаемся в карманах $gname";
$rnd = rand(1,3);
$re = $db->sql_query("select night_act from bomj WHERE klu4 = '".$rnd."'");
$b = $db->sql_fetchrow($re);
$mmsg1=$b["night_act"];
}
$db->sql_query ("update $ringame set gamer_act='".$s."' WHERE gamer_id = '".$user['id']."'");
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");


$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg1."', id='".$tm."', towhom='', gamemes='1'");

}else {
$mmsg = "Ты уже выбрал жертву!";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

}
}

}else if ($role=="клоун"&&$state==0){
$s = str_replace("!","",$msg);
list($g1,$g2) = explode(" ",$s);
$db->sql_query("select gamer_id from $ringame WHERE (id_in_game='".$g1."')or(id_in_game='".$g2."')");
if (mysql_affected_rows()==2){
$db->sql_query("update $ringame set gamer_act='".$s."' WHERE gamer_id = '".$user['id']."'");
$r = $db->sql_query("select id_in_game from $ringame WHERE id_in_game = '".$g1."'");
$a = $db->sql_fetchrow($r);
$gn1 = get_name_by_id($a["id_in_game"], $rmmaf);
$r = $db->sql_query("select id_in_game from $ringame WHERE id_in_game = '".$g2."'");
$a = $db->sql_fetchrow($r);
$gn2 = get_name_by_id($a["id_in_game"], $rmmaf);
$mmsg = "Меняем ролями $gn1 и $gn2!";
$mmsg1 = "Клоун ходит ночью по улицам и запутывает и без того сложное дело...";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg1."', id='".$tm."', towhom='', gamemes='1', komu=''");

}else{
$mmsg = "Что ж ты мне пишешь!?!?!?";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

}

}else if ($role=="жена комиссара"&&$state==1){
$s = str_replace("!","",$msg);
$r = $db->sql_query("select * from $ringame WHERE id_in_game = '".$s."'");
if (mysql_affected_rows()!=0){
$db->sql_query("update $ringame set state = '2', gamer_act = '".$s."' WHERE gamer_id = '".$user['id']."'");
$a = $db->sql_fetchrow($r);
$gn = get_name_by_id($a["id_in_game"], $rmmaf);
$mmsg = "$gn поплатится за смерть комиссара!";
$mmsg1 = "Жена комиссара вышла из дома мстить за смерть мужа...";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg1."', id='".$tm."', towhom='', gamemes='1', komu=''");

}else{
$mmsg = "Что ж ты мне пишешь!?!?!?";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='".$user['id']."', gamemes='1', komu='".$us."'");

}
}

$r = $db->sql_query ("select count(*) as nv from $ringame WHERE (role != 'обыватель')and(gamer_act='')");
$a = $db->sql_fetchrow($r);
if ($a["nv"]==0){
$db->sql_query("update $rmafaction set action = 'allvoted'");
$mmsg = "Все, что могло свершиться ночью, свершилось.";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}

if ($action == "nogame"&&($msg=="!start"||$msg=="!старт")){
$tm = time()+180;
$db->sql_query ("update $rmafaction set action = 'registration', nexttime = '".$tm."'");
$mmsg = "Начинается игра \"Мафия\"! Набор игроков будет продолжаться 3 мин. Для участия введите !reg.";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}

if ($action == "registration"&&($msg=="!reg"||$msg=="!рег")){
$db->sql_query ("select * from $ringame WHERE gamer_id = '".$user['id']."'");
if (mysql_affected_rows()==0){
$mmsg = "$us присоединяется к игре!";
$rnd = rand(10000,99999999);
$tm = time();
$db->sql_query("insert into $ringame set gamer_id = '".$user['id']."', onl = '".$tm."'");
}else{
$mmsg = "$us выходит из игры!";
$db->sql_query("delete from $ringame WHERE gamer_id = '".$user['id']."'");
}
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='1', komu=''");

}
}
header ('Location: ?rmmaf='.$rmmaf.'&'.SID);  exit;
}

if ($msg == "!stats"||$msg == "!статс"){
$r = $db->sql_query("select mafcredits from users WHERE id = '".$user['id']."'");
$a = $db->sql_fetchrow($r);
$cr = $a["mafcredits"];
$mmsg = "$us, у вас на счету $cr кредитов";
$tm = $time0;
$today=date ("H:i", $time0);
$rnd = rand(0,99999999);
$db->sql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$mafiozi."', usid='".$mafid."', message='".$mmsg."', id='".$tm."', towhom='', gamemes='".$gmes."', komu=''");

}
unset($msg);


$max = $user["max"];
$avr = $user["avr"];
$time=date ("H:i", $time0);
$avr2 = $avr/10;

$r = $db->sql_query ("select count(readd) as num from zapiski WHERE (idtowhom = '".$user['id']."')and(readd = '0')and(ininc = '1')");
$a = $db->sql_fetchrow($r);
$inb = $a["num"];

$takep="";



ob_start();
$tyu="left";
$title="Мафия-(".$time.")";
head();
$bs=explode('|',$user['binds']);
$tm = time()-300;
$inr=$db->sql_query("SELECT id FROM $room WHERE id >'".$tm."' group by who order by id desc");
$kola = mysql_affected_rows();
echo "<a href=\"help?rmmaf=$rmmaf\">Что делать?</a>|<a href=\"mafia_rules.php?rmmaf=$rmmaf\">Правила игры</a>|<a href=\"whoroom.php?rmmaf=$rmmaf$takep\">Кто тут?(".($kola).")</a>";
echo gbinroom.'<a href="add?rmmaf='.$rmmaf.'" accesskey="'.$bs[0].'">Сказать ['.$bs[0].']</a>'.div;
echo gbinroom.'<a href="?rmmaf='.$rmmaf.'&amp;obn" accesskey="'.$bs[1].'">Обновить ['.$bs[1].']</a> '.div;

if (isset($_GET['obn'])){header ('Location: ?rmmaf='.$rmmaf.'&'.SID);  exit;}

@$total=$kol;
$mread = 0;
$i = 0;


while ($lines = $db->sql_fetchrow ($res)){

$komu = $lines["komu"];
$date = $lines["time"];
$name = $lines["who"];
$usid = $lines["usid"];
$msg = text($lines["message"]);
$gm = $lines["gamemes"];
if ($smset==0&&$lines["messagewosm"]!="") $msg = $lines["messagewosm"];
if ($smset==2&&$lines["messagewoasm"]!="") $msg = $lines["messagewoasm"];
$time = $lines["id"];
$th = $lines["towhom"];
$hid = $lines["hid"];
$i++;

if ($th == "") {
if (!empty($komu)) {
if ($us==$komu) $komu = "<b>".$komu."</b>,";
else $komu = "".$komu.",";
}
echo '<br/>';
echo nik($name,'mafia/inside?rmmaf='.$rmmaf.'&amp;nk='.$usid.'');
echo "(".$date.")".$komu."".$msg.""; $mread++;}
else if (($th == $user['id'])||($user['id'] == $usid)){
if (!empty($komu)) {
if ($us==$komu) $komu = "<b>".$komu."</b>,";
else $komu = "".$komu.",";
}
echo '<br/>';
echo nik($name,'mafia/inside?rmmaf='.$rmmaf.'&amp;nk='.$usid.'');
echo "(".$date.")<b>[P!]</b>".$komu."".$msg."";$mread++;}

}
$s_next = $max;

echo "<br/><br/>";



if ($ver!='wml'){
echo "<form method=\"post\" action=\"maf.php?rmmaf=$rmmaf$takep\" name=\"auth\">";

echo "<br/><div class=\"c3\"> Сказать:</div>";

echo'<textarea cols="20" rows="1" name="msg" maxlength="200" ></textarea><br />';echo "<input class=\"ibutton\" type=\"submit\" value=\"Сказать\" name=\"enter\"></form>";
}else{

echo "<br/>Сказать:<br/>";

echo'<textarea cols="20" rows="1" name="msg" maxlength="200" ></textarea><br />';
echo "<anchor title=\"send\">Сказать<go href=\"maf.php?rmmaf=$rmmaf$takep\" method=\"post\">";
echo "<postfield name=\"msg\" value=\"$(msg$ref)\"/>";
echo "<postfield name=\"shrift\" value=\"$(shrift)\"/>";
echo "<postfield name=\"cveti_shrift\" value=\"$(cveti_shrift)\"/>";
echo "</go></anchor><br/>";
}

echo gbinroom;
if(isset($_GET['histiri'])){
str('?rmmaf='.$rmmaf.'&amp;histiri&amp;',$k_page,$page);
}else{
echo '<a href="?rmmaf='.$rmmaf.'&amp;histiri" accesskey="'.$bs[6].'">История ['.$bs[6].']</a>';
}
echo div;


echo gbinroom.'<a href="/enter" accesskey="'.$bs[7].'">Выйти из игры ['.$bs[7].']</a>'.div;
foot();
?>