diff --git a/api/chat.php b/api/chat.php index 4bb34d0..624c1da 100644 --- a/api/chat.php +++ b/api/chat.php @@ -100,18 +100,8 @@ if ($method === 'GET') { $stmt->execute([$username, $ip, $message, $type]); // Award points to the fan based on chat activity - $fanStmt = db()->prepare("SELECT id, points FROM fans WHERE name = ?"); - $fanStmt->execute([$username]); - $fan = $fanStmt->fetch(); - - if ($fan) { - $newPoints = $fan['points'] + 10; - db()->prepare("UPDATE fans SET points = ? WHERE id = ?")->execute([$newPoints, $fan['id']]); - } else { - // Check if photo exists for this user in user_likes or elsewhere (optional enhancement) - db()->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, 10]); - $newPoints = 10; - } + require_once __DIR__ . '/../includes/points_helper.php'; + $newPoints = awardPoints($username, 5); // 5 points per message instead of 10 to balance // Auto-update Fan of the Month if this user has the highest points $maxPointsStmt = db()->query("SELECT MAX(points) as max_p FROM fans"); diff --git a/api/cron_weekly_report.php b/api/cron_weekly_report.php index 0fc5c09..993f444 100644 --- a/api/cron_weekly_report.php +++ b/api/cron_weekly_report.php @@ -87,20 +87,13 @@ $stmt_lock = $db->prepare("SELECT last_run FROM automation_logs WHERE task_name $stmt_lock->execute([$today]); if (!$stmt_lock->fetch()) { + require_once __DIR__ . '/../includes/points_helper.php'; foreach ($top_chatters as $i => $c) { if ($i >= 3) break; $username = $c['username']; $points_to_add = $rewards[$i]; - $stmt_fan = $db->prepare("SELECT id FROM fans WHERE name = ?"); - $stmt_fan->execute([$username]); - $fan = $stmt_fan->fetch(); - - if ($fan) { - $db->prepare("UPDATE fans SET points = points + ? WHERE id = ?")->execute([$points_to_add, $fan['id']]); - } else { - $db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, $points_to_add]); - } + awardPoints($username, $points_to_add); $rewarded_users[$username] = $points_to_add; } diff --git a/api/like_song.php b/api/like_song.php index ed45010..8bbdaa4 100644 --- a/api/like_song.php +++ b/api/like_song.php @@ -23,6 +23,10 @@ try { $stmtUser = $pdo->prepare("INSERT INTO user_likes (username, total_likes) VALUES (?, 1) ON DUPLICATE KEY UPDATE total_likes = total_likes + 1"); $stmtUser->execute([$username]); + // Award points for like + require_once __DIR__ . '/../includes/points_helper.php'; + awardPoints($username, 10); + // 3. Post to chat $message = "¡Le dio un ❤️ a la canción: $song_title!"; $ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR']; diff --git a/api/song_requests.php b/api/song_requests.php index 2ff6f25..ddcb876 100644 --- a/api/song_requests.php +++ b/api/song_requests.php @@ -49,16 +49,8 @@ if ($method === 'POST') { // Award points for song request if ($requester !== 'Anónimo') { - $fanStmt = $db->prepare("SELECT id, points FROM fans WHERE name = ?"); - $fanStmt->execute([$requester]); - $fan = $fanStmt->fetch(); - - if ($fan) { - $newPoints = $fan['points'] + 25; // More points for a request - $db->prepare("UPDATE fans SET points = ? WHERE id = ?")->execute([$newPoints, $fan['id']]); - } else { - $db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$requester, 25]); - } + require_once __DIR__ . '/../includes/points_helper.php'; + awardPoints($requester, 25); } // Check if this user is now the #1 listener of the week diff --git a/includes/points_helper.php b/includes/points_helper.php new file mode 100644 index 0000000..6af1e39 --- /dev/null +++ b/includes/points_helper.php @@ -0,0 +1,55 @@ +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']); +} diff --git a/weekly_report.php b/weekly_report.php index a849d51..e0418ae 100644 --- a/weekly_report.php +++ b/weekly_report.php @@ -228,6 +228,7 @@ if ($action === 'send') { $stmt->execute([$today]); if (!$stmt->fetch()) { + require_once __DIR__ . '/includes/points_helper.php'; $rewards = [50, 30, 20]; $rewarded_users = []; foreach ($top_chatters as $i => $c) { @@ -235,15 +236,7 @@ if ($action === 'send') { $username = $c['username']; $points_to_add = $rewards[$i]; - $stmt_fan = $db->prepare("SELECT id FROM fans WHERE name = ?"); - $stmt_fan->execute([$username]); - $fan = $stmt_fan->fetch(); - - if ($fan) { - $db->prepare("UPDATE fans SET points = points + ? WHERE id = ?")->execute([$points_to_add, $fan['id']]); - } else { - $db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, $points_to_add]); - } + awardPoints($username, $points_to_add); $rewarded_users[$username] = $points_to_add; }