<?php

   #####################################
   ##                                 ##
 ####       Powered by (GENERAL)      ####
 ####         UZMASTER.RU             ####
   ##                                 ##
   #####################################

define('_IN_JOHNCMS', 1);
$headmod = 'balls';
$textl = 'Ball o\'g\'irlash';
require('../incfiles/core.php');
require('../incfiles/head.php');

$error = '';
if (!$user_id)
    $error = 'Siz <a href="../login.php">saytga</a> kirmagansiz';
if ($error) {
    require_once ("../incfiles/head.php");
    echo '<div class="rmenu"><p>' . $error . '</p></div>';
    require_once ("../incfiles/end.php");
    exit;
}

$cost = 3; // Bal minimalkasi

switch ($act) {
    default:
		$req = mysql_query("SELECT * FROM `garem_stat` WHERE `user_id` = '".$user_id."'");
		$count = mysql_num_rows($req);
		if (!$count) {
			mysql_query("INSERT INTO `garem_stat` SET
								`user_id`='" . $user_id . "',
								`stolen`='0',
								`stole`='0',
								`mod`='1',
								`cost`='" . $cost. "'		
								");
			mysql_query("UPDATE `users` SET
								`balans`= '" . $datauser['balans'] . "' + '3'
								 WHERE `id` = '".$user_id."'
								 ");
			echo '<div class="gmenu">Tabriklaymiz,siz 3 ballga erishdingiz!</div>';
								
		}
		else {
			$res = mysql_fetch_array($req);
			if ($res['mod'] == 0) {
						mysql_query("UPDATE `garem_stat` SET
									`mod`='1'		
								");
						mysql_query("UPDATE `users` SET
											`balans`= '" . $datauser['balans'] . "' + '3'
											 WHERE `id` = '".$user_id."'
											 ");
						echo '<div class="gmenu"> Tabriklaymiz,siz 3 ballga erishdingiz!</div>';
						
			}
		}
		echo '<div class="menu">';
		if ($id) {
			$garem = mysql_fetch_array(mysql_query("SELECT `cost` FROM `garem_stat` WHERE `user_id` = '".$id."'"));
			$balans = mysql_fetch_array(mysql_query("SELECT `balans` FROM `users` WHERE `id` = '".$user_id."'"));

			if ($id == $user_id) {
				// Выкуп
				echo 'SIz do\'stigiz ballaridan o\'g\'irladingiz <br /><a href="../users/profile.php"><b>Anketine bax</b></a> qazandigi xal '.$garem['cost'].'  <br /> Cemi balansi: '.$balans['balans'].' bal <br />';
				if (($balans['balans'] + 3) >= 0) {
					echo '<form action="index.php?id='.$id.'&amp;act=do"  method="post">';
					echo '<input type="submit" name = "submit" value="Davom etish" /></form>';
				}
				else echo 'O\'g\'irlashga ballariz yatarli emas... ';
			}
			else {
				// Кража
				$us = mysql_fetch_array(mysql_query("SELECT `name` FROM `users` WHERE `id` = '".$id."'"));
				if (!$garem['cost']) $garem['cost'] = 3;
				echo 'Siz ushbu <a href="../users/profile.php?user='.$id.'"><b>'. functions::checkout($us['name']) .'</b></a> do\'stingizdan <b> '.$garem['cost'].'</b> ball o\'g\'irlayapsiz <br /> Sizda '.$balans['balans'].' ball bor!<br />';
				if (($balans['balans'] + 3) >= 0) {
					echo '<form action="index.php?id='.$id.'&amp;act=do"  method="post">';
					echo '<input type="submit" name = "submit" value="Davom etamiz" /></form>';
				}
				else echo 'O\'g\'irlashga ballariz yatarli emas... ';
			}
		}
		else {
			echo '<a href="index.php?act=top&amp;cost">Ball olganlar ro\'yxati</a><br />';
			echo '<a href="index.php?act=top&amp;exp">Ball o\'g\'irlaganlar ro\'yxati</a><br />';
			echo '<a href="index.php?act=top&amp;rich">Ball o\'g\'irlatganlar ro\'yxati</a>';
			
		}
		echo '</div>';
	break;
	
	case "do":
	if (!$id) {
		header("location: ../users/profile.php");
		exit;
	}
	$req = mysql_query("SELECT * FROM `garem_stat` WHERE `user_id` = '".$id."'");
	$count2 = mysql_num_rows($req);
	if (!$count2) 
		mysql_query("INSERT INTO `garem_stat` SET
								`user_id`='" . $id . "',
								`stolen`='0',
								`stole`='0',
								`mod`='0',
								`cost`='" . $cost. "'		
								");
	$garem = mysql_fetch_array(mysql_query("SELECT `cost` FROM `garem_stat` WHERE `user_id` = '".$id."'"));
	$balans = mysql_fetch_array(mysql_query("SELECT `balans` FROM `users` WHERE `id` = '".$user_id."'"));
	if (($balans['balans'] + 3) < 0) {
		header("location: ../users/profile.php");
		exit;
	}
	if ($id == $user_id) {
			// Выкуп
			mysql_query("UPDATE `users` SET
									`balans`= '" . $datauser['balans'] . "' + '3'
									 WHERE `id` = '".$user_id."'
									 ");
			mysql_unbuffered_query ( "delete from `garem` where `user_id` = '".$user_id."'");
		echo 'En tecrubeliler!';	
		}
		else {
			// Кража
			$count3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `garem` WHERE `user_id` = '".$id."' and `thief_id` = '".$user_id."'"),0);
			if ($count3) echo 'Siz bu xizmatdan foydalanib bo\'lgansiz';
			else {
				$bal = $datauser['balans'] + 3;
				mysql_query("UPDATE `users` SET
										`balans`= '" . $bal ."'
										 WHERE `id` = '".$user_id."'
										 ");
				mysql_query("INSERT INTO `garem` SET
										`user_id`='" . $id . "',
										`thief_id`='" . $user_id . "',
										`time`='" . time(). "',
										`cost`='" . $garem['cost']. "'		
										");
				mysql_query("UPDATE `garem_stat` SET
										`cost`= '" . intval($garem['cost'] + 0.25 * $garem['cost']) . "',
										`stolen` = `stolen` + 1
										 WHERE `user_id` = '".$id."'
										 ");
				mysql_query("UPDATE `garem_stat` SET
										`stole`= `stole` + 1
										 WHERE `user_id` = '".$user_id."'
										 ");
				echo 'Muvaffaqiyatli bajarildi va sizga +3 ball qo\'shildi!';
										
			}
		}
	
	break;
	

	case "garem":
		if (isset($_GET['gar1'])) {
			$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `garem` WHERE `thief_id` = '".$id."'"),0);
			echo '<div class="phdr">O\'g\'irlagan  ['.$count.'] kishidan.</div>';
			echo '<div class="menu">';
			if ($count) {
				$req = mysql_query("select `garem`.`user_id`, `users`.`name` FROM `garem` LEFT JOIN `users` on `garem`.`user_id` = `users`.`id` where `garem`.`thief_id` = '".$id."'");
					while ($res = mysql_fetch_array($req)) {
						echo '<a href="../users/profile.php?user=' .($res['user_id']). '">' .functions::checkout($res['name']). '</a><br/>, ';
					}
			}
			
			echo '</div><div class="gmenu"><a href="../users/profile.php?user='.$id.'">Foydalanuvchi anketasi</a></div>';
		} 
		elseif (isset($_GET['gar2'])) {
			$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `garem` WHERE `user_id` = '".$id."'"),0);
			echo '<div class="phdr">O\'g\'irlaganlar ['.$count.'] kishi.</div>';
			echo '<div class="menu">';
			if ($count) {
				$req = mysql_query("SELECT `garem`.`thief_id`, `users`.`name` FROM `garem` LEFT JOIN `users` on `garem`.`thief_id` = `users`.`id` where `garem`.`user_id` = '".$id."'");
				while ($res = mysql_fetch_array($req)) {
					echo '<a href="../users/profile.php?user=' .($res['thief_id']). '">' .functions::checkout($res['name']). '</a>, ';
				}
			}
			echo '</div><div class="gmenu"><a href="../users/profile.php?user='.$id.'"> Foydalanuvchi anketasi</a></div>';
		
		}
		else header("location: index.php");
	
	break;
	
	case "top":
		if (isset($_GET['cost'])) {
				echo '<div class="phdr">Ball olganlar ro\'yxati</div><div class="menu">';
				$req = mysql_query("SELECT `garem_stat`.*, `users`.`name` FROM `garem_stat` LEFT JOIN `users` on `garem_stat`.`user_id` = `users`.`id` ORDER BY `garem_stat`.`cost` DESC LIMIT 10");
				while ($res = mysql_fetch_array($req)) {
					echo '<a href="../users/profile.php?user=' .($res['user_id']). '">' .functions::checkout($res['name']). '</a> +  ' . $res['cost'] . ' ball olgan<br />';
				}
				echo '</div><div class="gmenu"><a href="index.php">Ball ishlash</a></div>';
		}
		elseif (isset($_GET['rich'])) {
				echo '<div class="phdr">Ball o\'g\'irlaganlar ro\'yxati</div><div class="menu">';
				$req = mysql_query("SELECT `garem_stat`.*, `users`.`name` FROM `garem_stat` LEFT JOIN `users` on `garem_stat`.`user_id` = `users`.`id` ORDER BY `garem_stat`.`stole` DESC LIMIT 10");
				while ($res = mysql_fetch_array($req)) {
					echo '<a href="../users/profile.php?user=' .($res['user_id']). '">' .functions::checkout($res['name']). '</a> + 3 ball o\'g\'irlagan.<br />';
				}
				echo '</div><div class="gmenu"><a href="index.php">Ball ishlash</a></div>';
		}
		elseif (isset($_GET['exp'])) {
				echo '<div class="phdr">Ball o\'g\'irlatganlar ro\'yxati</div><div class="menu">';
				$req = mysql_query("SELECT `garem_stat`.*, `users`.`name` FROM `garem_stat` LEFT JOIN `users` on `garem_stat`.`user_id` = `users`.`id` ORDER BY `garem_stat`.`stolen` DESC LIMIT 10");
				while ($res = mysql_fetch_array($req)) {
					echo '<a href="../users/profile.php?user=' .($res['user_id']). '">' .functions::checkout($res['name']). '</a> - 3 ball o\'g\'irlatgan<br />';
				}
				echo '</div><div class="gmenu"><a href="index.php">Ball ishlash</a></div>';
		}
		else header("location: index.php");
	
	break;
	
}
require('../incfiles/end.php');
?>