game/_incl_data/crons/rating_clans.php
2022-12-30 21:03:50 +02:00

127 lines
3.6 KiB
PHP

<?php
use Core\Db;
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'autoload.php';
/**
* Îáíîâèòü ðåéòèíã êëàíîâ.
* Ðàç â äåíü.
*/
$add_exp_list = [];
$all_exp_list = [];
$exp_list = [];
$win_list = [];
$lose_list = [];
$nich_list = [];
$xu_list = [];
$level_list = [];
$log_list = [];
$clans = Db::getRows(
'
select
clan.id as cid,
clan.level,
clan.name,
count(*) as ucount,
clan.exp,
stats.id as sid,
sum(users.win) as win,
sum(users.lose) as lose,
sum(users.nich) as nich,
aaa_clan_reting_list.global
from
clan
left join users on clan.id = users.clan
left join stats on users.id = stats.id
left join aaa_clan_reting_list on aaa_clan_reting_list.clan = clan.id
where clan.id not in (62, 93) and users.admin = 0 and users.`real` = 1
'
);
foreach ($clans as $clan) {
$add_exp = 0;
$add_exp_list[$clan['cid']] = 0;
$all_exp_list[$clan['cid']] = 0;
$log_list[$clan['cid']] = $clan['name'];
$level_list[$clan['cid']] = $clan['level'];
$xu_list[$clan['cid']] = $clan['ucount'];
$exp_list[$clan['cid']] = $clan['exp'];
$win_list[$clan['cid']] = $clan['win'];
$lose_list[$clan['cid']] = $clan['lose'];
$nich_list[$clan['cid']] = $clan['nich'];
$clanMemberStats = Db::getRows(
'select
stats.id,
repexp
from
stats
left join users on users.id = stats.id
where clan = ?',
[$clan['cid']]
);
foreach ($clanMemberStats as $stat) {
$add_exp += $stat['repexp'];
Db::sql('update stats set repexp = 0 where id = ?', [$stat['id']]);
}
$global_exp = idate('d') != 1 ? round($clan['global']) : 0;
if ($clan['ucount'] > 0) {
$add_exp_list[$clan['cid']] = $add_exp; //çàïèñûâàåì ñêîëüêî îïûòà ïîëó÷èë çà ñåãîäíÿ
$all_exp_list[$clan['cid']] = $global_exp + $add_exp; //çàïèñûâàåì ñêîëüêî îïûòà ïîëó÷èë âñåãî + ñåãîäíÿøíèé
}
}
arsort($all_exp_list);
$keys = array_keys($all_exp_list);
$i = 0;
foreach ($keys as $key) {
$i++;
if (!$key) {
continue;
}
Db::sql(
'insert into aaa_clan_reting_list (level, xu, win, lose, nich, exp_real, clan, pos, global, exp, date, time) values (?,?,?,?,?,?,?,?,?,?,?,unix_timestamp())',
[
$level_list[$key],
$xu_list[$key],
$win_list[$key],
$lose_list[$key],
$nich_list[$key],
$exp_list[$key],
$key,
$i,
$all_exp_list[$key],
$add_exp_list[$key],
date('dmY'),
]
);
}
$stmt = Db::prepare('update clan set money2 = money2 + ? where id = ?');
$stmt->execute([$xu_list[0] * 0.3, $keys[0]]);
$stmt->execute([$xu_list[1] * 0.2, $keys[1]]);
$stmt->execute([$xu_list[2] * 0.1, $keys[2]]);
Db::sql(
'insert into chat (text, city, type, new, time) values
(?,\'capitalcity\',6,1,unix_timestamp()),
(?,\'capitalcity\',6,1,unix_timestamp()),
(?,\'capitalcity\',6,1,unix_timestamp()),
(?,\'capitalcity\',6,1,unix_timestamp())',
[
'<span style="color: red; font-weight: bold;">Ðåéòèíã êëàíîâ ' . date('d-m-Y') . ' </span>',
'Êëàí ' . $log_list[0] . ' ïîëó÷àåò ' . $xu_list[0] * 0.3 . ' Åêð. â êàçíó êëàíà çà 1-å ìåñòî â ðåéòèíãå!',
'Êëàí ' . $log_list[1] . ' ïîëó÷àåò ' . $xu_list[1] * 0.2 . ' Åêð. â êàçíó êëàíà çà 2-å ìåñòî â ðåéòèíãå!',
'Êëàí ' . $log_list[2] . ' ïîëó÷àåò ' . $xu_list[2] * 0.1 . ' Åêð. â êàçíó êëàíà çà 3-å ìåñòî â ðåéòèíãå!',
]
);
Db::sql('update stats set repexp = 0 where repexp > 0');