登录 立即注册

首页 > 绿虎论坛 > 软件开发 > 编程语言 > PHP (发帖)

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

作者: @Ta

时间: 01-21 09:14

点击: 7780
被下沉


$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|显示机器人聊天)』

1.

666,彩色昵称这个可以有 @老虎会游泳
https://www.chengyao.xyz

(/@Ta/2024-01-21 12:42//)

2.
层主 @咯叽 于 2024-03-02 09:13 删除了该楼层。
(/@Ta/2024-01-21 15:14//
被锁定
)

3. 虎绿林没有积分、没有等级、没有回复可见、没有未登录不能下载……用户可以自行删帖、沉贴、结贴、移版,用户可以随时更改用户名——和虎绿林的程序一样,虎绿林的用户也是自由的。
——老虎会游泳,2013年7月12日
(/@Ta/2024-01-21 15:18//)

回复需要登录

11月25日 12:10 星期一

本站由hu60wap6华为CPU驱动

备案号: 京ICP备18041936号-1