game/_incl_data/crons/auction.php
2023-08-08 17:42:05 +03:00

54 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Core\Db;
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'autoload.php';
/**
* CRON Комиссионного магазина
* Если предмет висит уже 2 и более недель, продавать предмет за 50% от его стоимости с учетом износа.
*
* Запуск раз в минуту.
*
* @author Insallah
*/
const TIME_LAST = 86400; // 1 day
const AUC = '<span style="color: #009966;">Филиал Аукциона</span>';
const AUC_WIN = 'Вы выиграли торги. Предмет "%s" за <b>%s кр.</b> был добавлен к вам в инвентарь.';
const AUC_SELL = 'Предмет "%s" был продан на торгах, <b>%s</b> кр. за товар отправлены вам по почте.';
const AUC_NOT_SELL = 'Предмет "%s" не был продан, он возвращен вам в инвентарь.';
function sendToChat(string $message, string $to = '', string $from = '')
{
$sql = 'insert into chat (new, login, `to`, text, time, type) values (1, ?, ?, ?, unix_timestamp(), 6)';
Db::sql($sql, [$from, $to, $message]);
}
function changeOwner(int $userId, int $itemId)
{
$sql = 'update items_users set uid = ?, lastUPD = unix_timestamp() where (id = ? or inGroup = ?) and uid = 0';
Db::sql($sql, [$userId, $itemId, $itemId]);
}
$items = Db::getRow('select * from items_auc where time_end = 0 and time < unix_timestamp() - ? order by user_buy', [TIME_LAST]);
foreach ($items as $item) {
if ($item['x'] > 0) {
$item['name'] .= ' (x' . $item['x'] . ')';
}
$userLogin = Db::getValue('select login from users where id = ?', [$item['uid']]);
if ($item['user_buy'] && Db::getValue('select count(*) from users where id = ?', [$item['user_buy']])) {
changeOwner($item['user_buy'], $item['item_id']);
sendToChat(sprintf(AUC_WIN, $item['name'], $item['price']), $item['user_buy'], AUC);
if ($userLogin) {
sendToChat(sprintf(AUC_SELL, $item['name'], $item['price']), $userLogin, AUC);
Db::sql('insert into items_users (`delete`, item_id, 1price, uid, lastUPD, `data`) values (0, 1220, ?, ?, unix_timestamp(), ?)', [$item['price'], '-51' . $item['uid'], '']);
}
} elseif ($userLogin) {
changeOwner($item['uid'], $item['item_id']);
sendToChat(sprintf(AUC_NOT_SELL, $item['name']), $userLogin, AUC);
}
Db::sql('update items_auc set time_end = unix_timestamp() where id = ?', [$item['id']]);
}