Файл: new_top/core/classes/Points.php
Строк: 147
<?php
/**
* User points: earn from unique inbound traffic, spend on GOLD / ads
*/
declare(strict_types=1);
class Points
{
public static function enabled(): bool
{
return (int) self::cfg('enabled', '1') === 1;
}
public static function cfg(string $key, string $default = '0'): string
{
try {
$v = Database::fetchColumn('SELECT v FROM points_config WHERE k = ?', [$key]);
return $v !== false && $v !== null ? (string)$v : $default;
} catch (Throwable $e) {
return $default;
}
}
public static function setCfg(string $key, string $value): void
{
Database::query(
'INSERT INTO points_config (k, v) VALUES (?, ?) ON DUPLICATE KEY UPDATE v = VALUES(v)',
[$key, $value]
);
}
public static function balance(int $userId): int
{
try {
return (int) Database::fetchColumn('SELECT points FROM user WHERE id = ?', [$userId]);
} catch (Throwable $e) {
return 0;
}
}
public static function add(int $userId, int $delta, string $reason, int $refId = 0): bool
{
if ($userId < 1 || $delta === 0) {
return false;
}
try {
Database::pdo()->beginTransaction();
$row = Database::fetch('SELECT points FROM user WHERE id = ? FOR UPDATE', [$userId]);
if (!$row) {
Database::pdo()->rollBack();
return false;
}
$bal = (int)$row['points'] + $delta;
if ($bal < 0) {
Database::pdo()->rollBack();
return false;
}
Database::query('UPDATE user SET points = ? WHERE id = ?', [$bal, $userId]);
Database::query(
'INSERT INTO points_log (user_id, delta, balance, reason, ref_id, time) VALUES (?, ?, ?, ?, ?, ?)',
[$userId, $delta, $bal, mb_substr($reason, 0, 64), $refId, time()]
);
Database::pdo()->commit();
return true;
} catch (Throwable $e) {
try {
Database::pdo()->rollBack();
} catch (Throwable $e2) {
}
return false;
}
}
/** Reward platform owner for a unique counted inbound click */
public static function rewardInbound(int $platformId): void
{
if (!self::enabled()) {
return;
}
$reward = (int) self::cfg('in_reward', '1');
if ($reward < 1) {
return;
}
try {
$pf = Database::fetch('SELECT user_id FROM platforms WHERE id = ?', [$platformId]);
if (!$pf || (int)$pf['user_id'] < 1) {
return;
}
self::add((int)$pf['user_id'], $reward, 'in', $platformId);
} catch (Throwable $e) {
}
}
public static function buyGold(int $userId, int $platformId, int $days): array
{
$days = max(1, min(90, $days));
$costDay = max(1, (int) self::cfg('gold_day_cost', '50'));
$cost = $costDay * $days;
$pf = Database::fetch('SELECT * FROM platforms WHERE id = ? AND user_id = ?', [$platformId, $userId]);
if (!$pf) {
return ['ok' => false, 'error' => 'Площадка не найдена'];
}
if (!self::add($userId, -$cost, 'gold', $platformId)) {
return ['ok' => false, 'error' => 'Недостаточно баллов (нужно ' . $cost . ')'];
}
$now = time();
$base = max($now, (int)$pf['gold']);
$until = $base + $days * 86400;
Database::query('UPDATE platforms SET gold = ? WHERE id = ?', [$until, $platformId]);
return ['ok' => true, 'until' => $until, 'cost' => $cost];
}
/**
* Pay for ad order with points instead of pending money.
* rate: points per 1 currency unit of ad price
*/
public static function payAd(int $userId, int $adId): array
{
$ad = Database::fetch('SELECT * FROM ads WHERE id = ? AND user_id = ? AND status = 0', [$adId, $userId]);
if (!$ad) {
return ['ok' => false, 'error' => 'Заявка не найдена'];
}
$rate = max(1, (int) self::cfg('ad_point_per_currency', '1'));
$cost = (int) ceil((float)$ad['price'] * $rate);
if ($cost < 1) {
$cost = 1;
}
if (!self::add($userId, -$cost, 'ad', $adId)) {
return ['ok' => false, 'error' => 'Недостаточно баллов (нужно ' . $cost . ')'];
}
$days = max(1, (int)$ad['days']);
$end = time() + $days * 86400;
Database::query(
'UPDATE ads SET status = 1, time_pay = ?, time_end = ? WHERE id = ?',
[time(), $end, $adId]
);
return ['ok' => true, 'cost' => $cost, 'end' => $end];
}
public static function history(int $userId, int $limit = 30): array
{
try {
return Database::fetchAll(
'SELECT * FROM points_log WHERE user_id = ? ORDER BY id DESC LIMIT ' . (int)$limit,
[$userId]
);
} catch (Throwable $e) {
return [];
}
}
public static function reasonLabel(string $r): string
{
switch ($r) {
case 'in': return 'Переход на площадку';
case 'gold': return 'Покупка GOLD';
case 'ad': return 'Оплата рекламы';
case 'admin': return 'Админ';
default: return $r;
}
}
}