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

102 lines
2.5 KiB
PHP

<?php
use Core\Config;
use Core\Db;
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'autoload.php';
/**
* Îáíîâëåíèå äàííûõ î êóðñàõ âàëþò èç Öåíòðîáàíêà ÐÔ.
* Ðàç â äåíü â ÷àñ íî÷è.
*/
// Îáíóëåíèå(?!) ïåðåäà÷ ðàç â ñóòêè
Db::sql('update stats set transfers = 100 where transfers < 200');
function getCurs()
{
// ñîçäàåì îáúåêò äëÿ ðàáîòû ñ XML
$xml = new DOMDocument();
// ññûëêà íà ñàéò áàíêà
$url = 'https://www.cbr.ru/scripts/XML_daily.asp?date_req=' . date('d.m.Y');
// ïîëó÷àåì xml ñ êóðñàìè âñåõ âàëþò
if (!$xml->load($url)) {
// åñëè íå ïîëó÷èëè xml âîçâðàùàåì false
return false;
}
// ìàññèâ äëÿ õðàíåíèÿ êóðñîâ âàëþò
$result = [];
// ðàçáèðàåì xml
$root = $xml->documentElement;
// áåðåì âñå òåãè 'Valute' è èõ ñîäåðæèìîå
$items = $root->getElementsByTagName('Valute');
// ïåðåáåðåì òåãè 'Valute' ïî îäíîìó
foreach ($items as $item) {
// ïîëó÷àåì êîä âàëþòû
$code = $item->getElementsByTagName('CharCode')->item(0)->nodeValue;
// ïîëó÷àåì çíà÷åíèå êóðñà âàëþòû, îòíîñèòåëüíî ðóáëÿ
$value = $item->getElementsByTagName('Value')->item(0)->nodeValue;
// íîìèíàë
$nominal = $item->getElementsByTagName('Nominal')->item(0)->nodeValue;
// çàïèñûâàåì â ìàññèâ, ïðåäâàðèòåëüíî çàìåíèâ çàïÿòóþ íà òî÷êó
$result[$code] = round(str_replace(',', '.', $value), 5) / $nominal;
}// âîçâðàùàåì çíà÷åíèå êóðñà, äëÿ çàïðîøåííîé âàëþòû
return $result;
}
$get = getCurs();
if (Config::get('curency_name') === 'RUB') {
$curency = Config::get('curency_value');
} else {
$curency = round($get[Config::get('curency_name')] * Config::get('curency_value'), 4);
}
if ($get['USD'] > 0) {
$price = [
'AUD',
'AZN',
'AMD',
'BYR',
'BGN',
'BRL',
'HUF',
'KRW',
'DKK',
'USD',
'EUR',
'INR',
'KZT',
'CAD',
'KGS',
'CNY',
'LVL',
'LTL',
'MDL',
'RON',
'TMT',
'NOK',
'PLN',
'XDR',
'SGD',
'TJS',
'TRY',
'UZS',
'UAH',
'GBP',
'CZK',
'SEK',
'CHF',
'ZAR',
'JPY',
];
$r = '';
foreach ($price as $value) {
if (!$get[$value]) {
continue;
}
$r .= ',`' . $value . '`="' . $get[$value] . '"';
}
Db::sql('insert into bank_table set time = unix_timestamp(), cur = ?, data = ?', [$curency, date('d.m.Y') . $r]);
}