数据库表设计:

  1. 用户表(users)

    • id: 用户ID
    • username: 用户名
    • password: 密码
    • balance: 余额
    • title: 头衔
  2. 成就表(achievements)

    • id: 成就ID
    • name: 成就名称
    • description: 成就描述
    • reward: 成就奖励(余额、头衔等)
  3. 成就进度表(achievement_progress)

    • id: 进度ID
    • user_id: 用户ID(外键关联用户表)
    • achievement_id: 成就ID(外键关联成就表)
    • progress: 进度
  4. 系统通知表(system_notifications)

    • id: 通知ID
    • user_id: 用户ID(外键关联用户表)
    • message: 通知内容
    • is_read: 是否已读

Express代码示例:

  1. 创建数据库连接
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/achievements');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('Connected to the database');
});
  1. 创建用户模型和成就模型
const userSchema = new mongoose.Schema({
  username: String,
  password: String,
  balance: Number,
  title: String
});

const achievementSchema = new mongoose.Schema({
  name: String,
  description: String,
  reward: String
});

const User = mongoose.model('User', userSchema);
const Achievement = mongoose.model('Achievement', achievementSchema);
  1. 创建成就进度模型和系统通知模型
const achievementProgressSchema = new mongoose.Schema({
  user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  achievement_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Achievement' },
  progress: Number
});

const systemNotificationSchema = new mongoose.Schema({
  user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  message: String,
  is_read: Boolean
});

const AchievementProgress = mongoose.model('AchievementProgress', achievementProgressSchema);
const SystemNotification = mongoose.model('SystemNotification', systemNotificationSchema);
  1. 实现获取成就和进度的路由
app.get('/achievements', (req, res) => {
  Achievement.find({}, (err, achievements) => {
    if (err) {
      console.error(err);
      res.status(500).send('Error retrieving achievements');
    } else {
      res.json(achievements);
    }
  });
});

app.get('/achievement_progress/:user_id', (req, res) => {
  AchievementProgress.find({ user_id: req.params.user_id })
    .populate('achievement_id')
    .exec((err, progress) => {
      if (err) {
        console.error(err);
        res.status(500).send('Error retrieving achievement progress');
      } else {
        res.json(progress);
      }
    });
});
  1. 实现完成成就和发送系统通知的路由
app.post('/achievement_progress/:user_id/:achievement_id', (req, res) => {
  AchievementProgress.findOneAndUpdate(
    { user_id: req.params.user_id, achievement_id: req.params.achievement_id },
    { $inc: { progress: 1 } },
    { upsert: true, new: true },
    (err, progress) => {
      if (err) {
        console.error(err);
        res.status(500).send('Error updating achievement progress');
      } else {
        if (progress.progress >= 100) {
          // 达成成就,发送系统通知
          const notification = new SystemNotification({
            user_id: req.params.user_id,
            message: `Congratulations! You have achieved the ${progress.achievement_id.name} achievement.`,
            is_read: false
          });
          notification.save(err => {
            if (err) {
              console.error(err);
              res.status(500).send('Error saving system notification');
            } else {
              res.json(progress);
            }
          });
        } else {
          res.json(progress);
        }
      }
    }
  );
});

以上代码只是一个示例,实际应用中可能需要根据具体需求进行修改和完善。同时,需要在合适的地方添加身份验证和权限控制的逻辑,以确保只有授权用户能够进行相关操作。

成就体系 需要有一个页面可以让用户看到有哪些成就可以获得然后成就的进度如何 成就达到之后有奖励奖励可以是余额或者是头衔或者是一些其他什么东西 系统需要记录用户在网站上的行为统计到相应的进度中 成就达成之后需要发送系统通知给用户进行查看进行数据库表设计和express代码

原文地址: http://www.cveoy.top/t/topic/jcWm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录