<?php
session_start();
define("PATH", $_SERVER['DOCUMENT_ROOT']);
include(PATH."/core/func.php");
include(PATH."/core/db.php");
include(PATH."/core/set.php");
include(PATH."/core/define.php");
include(PATH."/core/defineset.php");
db_connect();

db_query("UPDATE `".DBpref."users` SET `bonus_lvl` = 0");


//КНБ 
$knb_top = getKnbTop();
$knb_sum = getKnbSum();

if($knb_top['qt'] != 0)
{
	$user_info = getUserInfo($knb_top[0]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($knb_top['qt'], $knb_sum, 1));
	if(getLvl($user_info['rating']) < 7 && $user_info['knb_bonus'] != 0 && $user_info['bonus_lvl'] < 7)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 7);
	}

	$user_info = getUserInfo($knb_top[1]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($knb_top['qt'], $knb_sum, 2));
	if(getLvl($user_info['rating']) < 6 && $user_info['knb_bonus'] != 0 && $user_info['bonus_lvl'] < 6)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 6);
	}

	$user_info = getUserInfo($knb_top[2]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($knb_top['qt'], $knb_sum, 3));
	if(getLvl($user_info['rating']) < 5 && $user_info['knb_bonus'] != 0 && $user_info['bonus_lvl'] < 5)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 5);
	}
	db_query("UPDATE `".DBpref."users` SET `knb_bonus` = 0");
}

//Орел 
$orel_top = getOrelTop();
$orel_sum = getOrelSum();

if($orel_top['qt'] != 0)
{
	$user_info = getUserInfo($orel_top[0]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($orel_top['qt'], $orel_sum, 1));
	if(getLvl($user_info['rating']) < 7 && $user_info['orel_bonus'] != 0 && $user_info['bonus_lvl'] < 7)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 7);
	}

	$user_info = getUserInfo($orel_top[1]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($orel_top['qt'], $orel_sum, 2));
	if(getLvl($user_info['rating']) < 6 && $user_info['orel_bonus'] != 0 && $user_info['bonus_lvl'] < 6)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 6);
	}

	$user_info = getUserInfo($orel_top[2]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($orel_top['qt'], $orel_sum, 3));
	if(getLvl($user_info['rating']) < 5 && $user_info['orel_bonus'] != 0 && $user_info['bonus_lvl'] < 5)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 5);
	}
	db_query("UPDATE `".DBpref."users` SET `orel_bonus` = 0");
}

//Сапер 
$sapper_top = getSapperTop();
$sapper_sum = getSapperSum();

if($sapper_top['qt'] != 0)
{
	$user_info = getUserInfo($sapper_top[0]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($sapper_top['qt'], $sapper_sum, 1));
	if(getLvl($user_info['rating']) < 7 && $user_info['sapper_bonus'] != 0 && $user_info['bonus_lvl'] < 7)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 7);
	}

	$user_info = getUserInfo($sapper_top[1]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($sapper_top['qt'], $sapper_sum, 2));
	if(getLvl($user_info['rating']) < 6 && $user_info['sapper_bonus'] != 0 && $user_info['bonus_lvl'] < 6)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 6);
	}

	$user_info = getUserInfo($sapper_top[2]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($sapper_top['qt'], $sapper_sum, 3));
	if(getLvl($user_info['rating']) < 5 && $user_info['sapper_bonus'] != 0 && $user_info['bonus_lvl'] < 5)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 5);
	}
	db_query("UPDATE `".DBpref."users` SET `sapper_bonus` = 0");
}

//Партнер 
$partner_top = getPartnerTop();
$partner_sum = getPartnerSum();

if($partner_top['qt'] != 0)
{
	$user_info = getUserInfo($partner_top[0]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($partner_top['qt'], $partner_sum, 1));
	if(getLvl($user_info['rating']) < 7 && $user_info['partner_bonus'] != 0 && $user_info['bonus_lvl'] < 7)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 7);
	}

	$user_info = getUserInfo($partner_top[1]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($partner_top['qt'], $partner_sum, 2));
	if(getLvl($user_info['rating']) < 6 && $user_info['partner_bonus'] != 0 && $user_info['bonus_lvl'] < 6)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 6);
	}

	$user_info = getUserInfo($partner_top[2]['login']);
	setUserInfo($user_info['login'], "balance", $user_info['balance'] + getBonusSum($partner_top['qt'], $partner_sum, 3));
	if(getLvl($user_info['rating']) < 5 && $user_info['partner_bonus'] != 0 && $user_info['bonus_lvl'] < 5)
	{
		setUserInfo($user_info['login'], 'bonus_lvl', 5);
	}
	db_query("UPDATE `".DBpref."users` SET `partner_bonus` = 0");
}

db_close();
?>