56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
function awardPoints($username, $pointsToAdd) {
|
|
if (empty($username) || $username === 'Anónimo') {
|
|
return false;
|
|
}
|
|
|
|
$db = db();
|
|
|
|
// Get current points
|
|
$stmt = $db->prepare("SELECT id, points FROM fans WHERE name = ?");
|
|
$stmt->execute([$username]);
|
|
$fan = $stmt->fetch();
|
|
|
|
$oldPoints = 0;
|
|
if ($fan) {
|
|
$oldPoints = $fan['points'];
|
|
$newPoints = $oldPoints + $pointsToAdd;
|
|
$db->prepare("UPDATE fans SET points = ? WHERE id = ?")->execute([$newPoints, $fan['id']]);
|
|
} else {
|
|
$newPoints = $pointsToAdd;
|
|
$db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, $newPoints]);
|
|
}
|
|
|
|
// Check level up
|
|
$milestones = [100, 500, 1000, 2500, 5000, 10000];
|
|
foreach ($milestones as $level => $threshold) {
|
|
if ($oldPoints < $threshold && $newPoints >= $threshold) {
|
|
$levelNumber = $level + 1;
|
|
announceLevelUp($username, $levelNumber, $threshold);
|
|
break; // Only announce the highest level reached if jumping multiple
|
|
}
|
|
}
|
|
|
|
return $newPoints;
|
|
}
|
|
|
|
function announceLevelUp($username, $level, $points) {
|
|
$db = db();
|
|
$messages = [
|
|
"🎉 ¡Increíble! **$username** ha alcanzado el **Nivel $level** con $points puntos. ¡Qué gran fan! 🙌✨",
|
|
"🚀 ¡Subida de nivel! **$username** ya es **Nivel $level** ($points puntos). ¡La comunidad sigue creciendo! 💎",
|
|
"👑 ¡Atención! **$username** ha llegado al **Nivel $level** ($points puntos). ¡Su pasión por la música no tiene límites! 🔥",
|
|
"🌟 ¡Brillante! **$username** acaba de subir al **Nivel $level** ($points puntos). ¡Gracias por ser parte de Lili Records! 🎈"
|
|
];
|
|
|
|
$message = $messages[array_rand($messages)];
|
|
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
|
|
$ip = explode(',', $ip)[0];
|
|
|
|
$db->prepare("INSERT INTO messages (username, ip_address, message, type) VALUES (?, ?, ?, ?)")
|
|
->execute(['Lili Bot 🤖', $ip, $message, 'text']);
|
|
}
|