成就系统:解锁成就,领取奖励,成就你的精彩!
成就系统:解锁成就,领取奖励,成就你的精彩!
成就系统 是一个激励用户积极参与网站并提升游戏体验的重要机制。用户可以通过完成各种任务来解锁成就,并获得相应的奖励。
系统特点:
- 成就展示页面: 用户可以查看所有可获得的成就,以及每个成就的当前进度。
- 奖励机制: 成就达成后,用户将获得奖励,例如余额、头衔或其他物品。
- 行为记录: 系统记录用户的行为,并统计到相应的成就进度中。
- 系统通知: 当用户达成成就时,系统将发送通知提醒用户。
数据库表设计:
-
用户表 (users)
- id: 用户ID
- username: 用户名
- password: 密码
- balance: 余额
- title: 头衔
-
成就表 (achievements)
- id: 成就ID
- name: 成就名称
- description: 成就描述
- reward: 成就奖励(余额、头衔等)
-
成就进度表 (achievement_progress)
- id: 进度ID
- user_id: 用户ID(外键关联用户表)
- achievement_id: 成就ID(外键关联成就表)
- progress: 进度
-
系统通知表 (system_notifications)
- id: 通知ID
- user_id: 用户ID(外键关联用户表)
- message: 通知内容
- is_read: 是否已读
Express 代码示例:
- 创建数据库连接
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');
});
- 创建用户模型和成就模型
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);
- 创建成就进度模型和系统通知模型
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);
- 实现获取成就和进度的路由
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);
}
});
});
- 实现完成成就和发送系统通知的路由
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);
}
}
}
);
});
注意:
- 以上代码示例仅供参考,实际应用中可能需要根据具体需求进行修改和完善。
- 请在合适的地方添加身份验证和权限控制的逻辑,以确保只有授权用户能够进行相关操作。
- 为了优化 SEO,请确保网站内容高质量且原创,并使用恰当的关键词和描述。
- 定期更新网站内容,并进行网站速度和移动设备友好性的优化。
相信通过合理的设计和开发,您的成就系统将能够有效地激励用户,提升用户参与度,并为您的网站带来更多价值!
原文地址: http://www.cveoy.top/t/topic/g1yV 著作权归作者所有。请勿转载和采集!