在 SQLite xp 系统上为奖励积分添加冷却时间

编程入门 行业动态 更新时间:2024-10-26 00:23:21
本文介绍了在 SQLite xp 系统上为奖励积分添加冷却时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望通过只允许每 60 秒获得一次 xp 来改进我的积分系统.我尝试了一些东西,但没有一个真正接近.当前积分奖励代码为

I'm looking to improve my points system by only allowing xp to be gained once every 60 seconds. I've tried a few things but none have really come close. The current point awarding code is

client.on('ready', () => { // Check if the table "points" exists. const table = sql .prepare( "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';" ) .get(); if (!table['count(*)']) { // create and setup the database correctly. sql .prepare( 'CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);' ) .run(); // "id" row is always unique and indexed. sql.prepare('CREATE UNIQUE INDEX idx_scores_id ON scores (id);').run(); sql.pragma('synchronous = 1'); sql.pragma('journal_mode = wal'); } // get and set the score data. client.getScore = sql.prepare( 'SELECT * FROM scores WHERE user = ? AND guild = ?' ); client.setScore = sql.prepare( 'INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);' ); }); client.on('message', (message) => { if (message.author.bot) return; let score; if (message.guild) { score = client.getScore.get(message.author.id, message.guild.id); if (!score) { score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1, }; } score.points++; const curLevel = Math.floor(0.2 * Math.sqrt(score.points)); if (score.level < curLevel) { score.level++; client.channels.cache .get('738662532700700719') .send(`${message.author} has leveled up to level **${curLevel}**!`); } client.setScore.run(score); } if (message.content.indexOf(config.prefix) !== 0) return; const args = message.content .slice(config.prefix.length) .trim() .split(/ +/g); const command = args.shift().toLowerCase(); });

推荐答案

让我想到的是存储您上次给用户点数的时间戳.然后,每次您想为用户的新消息奖励更多积分时,请检查当前时间是否比您上次分配用户积分的时间晚 60 秒以上.

What springs to mind for me would be to store the timestamp you last gave the user points. Then for each time you want to award the user more points for a new message, check if the current time is more than or equal to 60 seconds later from the last time you assigned the user points.

看看下面的示例代码并尝试一下.它可能需要调整,因为我对 SQLite 并没有真正的经验,但我会链接我在下面使用的资源.

Take a look at the sample code below and give it a try. It might need tweaking as I'm not really experienced with SQLite but I'll link the resources I used below.

client.on('ready', () => { // Check if the table "points" exists. const table = sql .prepare( "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';" ) .get(); if (!table['count(*)']) { // create and setup the database correctly. // Includes the new column 'lastAwardedDate'. sql .prepare( 'CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER, lastAwardedDate TEXT);' ) .run(); // "id" row is always unique and indexed. sql.prepare('CREATE UNIQUE INDEX idx_scores_id ON scores (id);').run(); sql.pragma('synchronous = 1'); sql.pragma('journal_mode = wal'); } // get and set the score data. client.getScore = sql.prepare( 'SELECT * FROM scores WHERE user = ? AND guild = ?' ); client.setScore = sql.prepare( 'INSERT OR REPLACE INTO scores (id, user, guild, points, level, lastAwardedDate) VALUES (@id, @user, @guild, @points, @level, @lastAwardedDate);' ); }); // Define a constant value for the delay (in ms). const pointDelay = 60 * 1000; client.on('message', (message) => { if (message.author.bot) return; let score; if (message.guild) { score = client.getScore.get(message.author.id, message.guild.id); if (!score) { score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1, }; } else { // Check if the current time minus the last awarded time is less than the delay. if (new Date() - Date.parse(score.lastAwardedDate) < pointDelay) { return; } } score.points++; score.lastAwardedDate = new Date().toString(); const curLevel = Math.floor(0.2 * Math.sqrt(score.points)); if (score.level < curLevel) { score.level++; client.channels.cache .get('738662532700700719') .send(`${message.author} has leveled up to level **${curLevel}**!`); } client.setScore.run(score); } if (message.content.indexOf(config.prefix) !== 0) return; const args = message.content .slice(config.prefix.length) .trim() .split(/ +/g); const command = args.shift().toLowerCase(); });

我使用的来源:

  • SQLite 日期 &时间
  • Date.toISOString

更多推荐

在 SQLite xp 系统上为奖励积分添加冷却时间

本文发布于:2023-11-30 16:44:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上为   积分   时间   系统   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!