Auto commit: 2026-02-18T23:48:34.425Z
This commit is contained in:
parent
3e41d10cfc
commit
c85cab9e98
14
api/chat.php
14
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");
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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'];
|
||||
|
||||
@ -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
|
||||
|
||||
55
includes/points_helper.php
Normal file
55
includes/points_helper.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?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']);
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user