我用hu60实现了简单的积分功能

@Ta 01-21 09:14 7609点击
被下沉

$modifyUserId = $this->user->uid; // 获取当前登录用户的UID
$sql = "SELECT uid, points, experience FROM hu60_user WHERE uid = :modifyUserId";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':modifyUserId', $modifyUserId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result) {
    $current_points = $result['points'];
    $current_experience = $result['experience'];
} else {
    die('找不到用户。');
}

// 定义每次发帖增加的积分值和经验值
$points_to_add = 100;
$experience_to_add = 50;

// 检查用户当天已增加的积分和经验值
$sql = "SELECT SUM(points_added) AS total_points_added, SUM(experience_added) AS total_experience_added FROM hu60_points_record WHERE uid = :modifyUserId AND DATE(timestamp) = CURDATE()";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':modifyUserId', $modifyUserId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result) {
    $total_points_added = $result['total_points_added'];
    $total_experience_added = $result['total_experience_added'];
} else {
    $total_points_added = 0;
    $total_experience_added = 0;
}

// 计算剩余可增加的积分和经验值
$remaining_points = 500 - $total_points_added;
$remaining_experience = 300 - $total_experience_added;

// 检查用户是否已达到每日积分和经验值上限
if ($remaining_points > 0 && $remaining_experience > 0) {
    // 计算实际可增加的积分和经验值
    $actual_points_to_add = min($points_to_add, $remaining_points);
    $actual_experience_to_add = min($experience_to_add, $remaining_experience);

    // 更新用户的积分和经验值
    $sql = "UPDATE hu60_user SET points = points + :pointsToAdd, experience = experience + :experienceToAdd WHERE uid = :modifyUserId";
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':pointsToAdd', $actual_points_to_add, PDO::PARAM_INT);
    $stmt->bindParam(':experienceToAdd', $actual_experience_to_add, PDO::PARAM_INT);
    $stmt->bindParam(':modifyUserId', $modifyUserId, PDO::PARAM_INT);
    $ok = $stmt->execute();
    if ($ok) {
        // 记录积分和经验值增加记录
           $recordSql = "INSERT INTO hu60_points_record (uid, points_added, experience_added) VALUES (:modifyUserId, :pointsToAdd, :experienceToAdd)";
           $recordStmt = $this->db->prepare($recordSql);
           $recordStmt->bindParam(':modifyUserId', $modifyUserId, PDO::PARAM_INT);
           $recordStmt->bindParam(':pointsToAdd', $actual_points_to_add, PDO::PARAM_INT);
           $recordStmt->bindParam(':experienceToAdd', $actual_experience_to_add, PDO::PARAM_INT);
           $recordStmt->execute();
       }
   }

*
 * 设置用户积分
 * 参数:
 * $points 积分值
 * 返回值:成功返回TRUE,失败返回FALSE
 */
public function setPoints($points)
{
    $db = self::conn();
    $rs = $db->prepare('UPDATE hu60_user SET points=? WHERE uid=?');

    if (!$rs || !$rs->execute([$points, $this->uid])) {
        throw new UserException('数据库异常,无法设置用户积分!', 10500);
    }

    // 更新缓存中的用户数据
    self::$data[$this->uid]['points'] = $points;

    return true;
}

/**
 * 设置用户经验
 * 参数:
 * $experience 经验值
 * 返回值:成功返回TRUE,失败返回FALSE
 */
public function setexperience($experience)
{
    $db = self::conn();
    $rs = $db->prepare('UPDATE hu60_user SET experience=? WHERE uid=?');

    if (!$rs || !$rs->execute([$experience, $this->uid])) {
        throw new UserException('数据库异常,无法设置用户积分!', 10500);
    }

    // 更新缓存中的用户数据
    self::$data[$this->uid]['experience'] = $experience;

    return true;
}

    /**
     * 获取用户积分
     */
    public function points()
    {
        $db = self::conn(true);
        $rs = $db->prepare('SELECT `points` FROM `' . DB_A . 'user` WHERE `uid`=?');

        if (!$rs || !$rs->execute(array($this->uid))) {
            throw new UserException('数据库异常,无法读取用户积分!', 10500);
        }

        $data = $rs->fetch(db::num);

        return $data[0];
    }
    
        /**
     * 获取用户经验值
     */
    public function experience()
    {
        $db = self::conn(true);
        $rs = $db->prepare('SELECT `experience` FROM `' . DB_A . 'user` WHERE `uid`=?');

        if (!$rs || !$rs->execute(array($this->uid))) {
            throw new UserException('数据库异常,无法读取用户积分!', 10500);
        }

        $data = $rs->fetch(db::num);

        return $data[0];
    }

全部由gpt完成,现在只用积分功能实现了购买彩色昵称的功能,其他功能还没有思路从哪里开始改
回复列表(3|隐藏机器人聊天)
添加新回复
回复需要登录