<?php
define('GAME', true);
include('_incl_data/__config.php');
include('_incl_data/class/__db_connect.php');

if (isset($_GET['test'])) {
    $html = '';
    $sp = mysql_query('SELECT * FROM `aaa_clan_reting_list` WHERE `date` = "' . date('dmY') . '" ORDER BY `pos` ASC LIMIT 1000');
    while ($pl = mysql_fetch_array($sp)) {
        $usr = mysql_fetch_array(mysql_query('SELECT `id`,`name`,`level` FROM `clan` WHERE `id` = "' . $pl['clan'] . '" LIMIT 1'));
        $ret = round($pl['global'] / (15678 + $pl['level']), 3);
        $html .= '(' . $pl['pos'] . ') ' . $usr['name'] . ' [' . $usr['id'] . ']: ' . $ret . '<br>';
    }
    echo $html;
    die();
}

$add_exp_list = [];
$all_exp_list = [];
$exp_list = [];
$win_list = [];
$lose_list = [];
$nich_list = [];
$xu_list = [];
$level_list = [];

$test = mysql_fetch_array(mysql_query('SELECT `id` FROM `aaa_clan_reting_list` WHERE `date` = "' . date('dmY') . '" LIMIT 1'));
if (isset($test['id'])) {
    mysql_query('DELETE FROM `aaa_clan_reting_list` WHERE `date` = "' . date('dmY') . '"');
}
unset($test);

$sp = mysql_query('SELECT `id`,`level` FROM `clan`');
while ($pl = mysql_fetch_array($sp)) {
    $xu = mysql_fetch_array(mysql_query('SELECT COUNT(`id`) FROM `users` WHERE `clan` = "' . $pl['id'] . '" AND `admin` = 0 AND `real` = 1 LIMIT 1'));
    $xu = $xu[0];

    $st = mysql_fetch_array(mysql_query('SELECT SUM(`val`) AS `exp` FROM `users_reting` WHERE `clan` = "' . $pl['id'] . '" LIMIT 1'));

    $wn = mysql_fetch_array(mysql_query('SELECT SUM(`win`) AS `win` , SUM(`lose`) AS `lose` , SUM(`nich`) AS `nich` FROM `users` WHERE `clan` = "' . $pl['id'] . '" LIMIT 1'));

    $rl = mysql_fetch_array(mysql_query('SELECT `exp`,`global`,`exp_real` FROM `aaa_clan_reting_list` WHERE `clan` = "' . $pl['id'] . '" ORDER BY `id` DESC LIMIT 1'));

    $exp_list[$pl['id']] = $st['exp'];

    $level_list[$pl['id']] = $pl['level'];
    $win_list[$pl['id']] = $wn['win'];
    $lose_list[$pl['id']] = $wn['lose'];
    $nich_list[$pl['id']] = $wn['nich'];

    $xu_list[$pl['id']] = $xu;

    $add_exp = $st['exp'] - $rl['exp_real'];

    $global_exp = 0; //значение рейтинга за 100 дней, каждый последующий день -1%
    $cp = mysql_query('SELECT `exp` FROM `aaa_clan_reting_list` WHERE `clan` = "' . $pl['id'] . '" ORDER BY `id` DESC LIMIT 100');
    $dd = 0;
    while ($cl = mysql_fetch_array($cp)) {
        $global_exp += round($cl['exp'] / 100 * (100 - $dd));
        $dd++;
    }

    $add_exp_list[$pl['id']] = $add_exp; //записываем сколько опыта получил за сегодня
    $all_exp_list[$pl['id']] = $global_exp + $add_exp; //записываем сколько опыта получил всего + сегодняшний
}
unset($pl, $sp);

arsort($all_exp_list);

$keys = array_keys($all_exp_list);

$i = 0;
while ($i <= count($keys)) {
    if (isset($keys[$i])) {
        mysql_query('INSERT INTO `aaa_clan_reting_list` ( `level`,`xu`,`win`,`lose`,`nich`,`exp_real`,`clan`,`pos`,`global`,`exp`,`date`,`time` ) VALUES (
				"' . $level_list[$keys[$i]] . '","' . $xu_list[$keys[$i]] . '","' . $win_list[$keys[$i]] . '","' . $lose_list[$keys[$i]] . '","' . $nich_list[$keys[$i]] . '","' . $exp_list[$keys[$i]] . '","' . $keys[$i] . '","' . ($i + 1) . '","' . $all_exp_list[$keys[$i]] . '","' . $add_exp_list[$keys[$i]] . '","' . date('dmY') . '","' . time() . '"
			)');
    }
    $i++;
}
