Skip to content

Commit

Permalink
cache quests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Aug 4, 2024
1 parent 905f4fb commit 2dce5a3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 46 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"lint:fix": "npx eslint --fix .",
"prettier": "npx prettier --check .",
"prettier:fix": "npx prettier --write .",
"format": "npm run lint:fix && npm run prettier:fix"
"format": "npm run lint:fix && npm run prettier:fix",
"checkCircular": "./node_modules/dpdm/lib/bin/dpdm.js --exit-code circular:1 ./src/owo.js"
},
"dependencies": {
"@2toad/profanity": "^1.3.0",
Expand Down
27 changes: 12 additions & 15 deletions src/botHandlers/questHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const quests = require('../data/quests.json');
const mysql = require('./mysqlHandler.js');
const global = require('../utils/global.js');
const questBy = ['friendlyBattle', 'friendlyBattleBy', 'emoteBy', 'prayBy', 'curseBy', 'cookieBy'];
const cacheUtil = require('../utils/cacheUtil.js');

module.exports = class Quest {
/* Constructer to grab mysql connection */
Expand All @@ -32,11 +33,7 @@ module.exports = class Quest {
/* Special quest parameters */
if (questName == 'friendlyBattleBy') questName = 'friendlyBattle';

/* Check if user has this quest */
let result = await mysql.query(
'SELECT * FROM quest WHERE qname = ? AND locked = 0 AND uid = (SELECT uid FROM user WHERE id = ?);',
[questName, id]
);
let result = await cacheUtil.getQuestByName(questName, id);

if (!result[0]) return;

Expand Down Expand Up @@ -69,11 +66,11 @@ async function check(msg, id, username, questName, result, count, extra) {

/* Check if the quest is complete */
let text, rewardSql, sql, variables, rewardVar;
const uid = await cacheUtil.getUid(id);
if (current >= needed) {
sql =
'DELETE FROM quest WHERE qid = ? AND qname = ? AND uid = (SELECT uid FROM user WHERE id = ?);';
sql = 'DELETE FROM quest WHERE qid = ? AND qname = ? AND uid = ?;';

variables = [result.qid, questName, id];
variables = [result.qid, questName, uid];
text = '**📜 | ' + username + '**! You finished a quest and earned: ';
if (rewardType == 'lootbox') {
text += '<:box:427352600476647425>'.repeat(reward);
Expand All @@ -83,13 +80,13 @@ async function check(msg, id, username, questName, result, count, extra) {
} else if (rewardType == 'crate') {
text += '<:crate:523771259302182922>'.repeat(reward);
rewardSql =
"INSERT INTO crate (uid,boxcount,claim) VALUES ((SELECT uid FROM user WHERE id = ?),?,'2017-01-01 10:10:10') ON DUPLICATE KEY UPDATE boxcount = boxcount + ?;";
rewardVar = [id, reward, reward];
"INSERT INTO crate (uid,boxcount,claim) VALUES (?,?,'2017-01-01 10:10:10') ON DUPLICATE KEY UPDATE boxcount = boxcount + ?;";
rewardVar = [uid, reward, reward];
} else if (rewardType == 'shards') {
text += '<:weaponshard:655902978712272917>**x' + reward + '**';
rewardSql =
'INSERT INTO shards (uid,count) VALUES ((SELECT uid FROM user WHERE id = ?),?) ON DUPLICATE KEY UPDATE count = count + ?;';
rewardVar = [id, reward, reward];
'INSERT INTO shards (uid,count) VALUES (?,?) ON DUPLICATE KEY UPDATE count = count + ?;';
rewardVar = [uid, reward, reward];
} else {
text += global.toFancyNum(reward) + ' <:cowoncy:416043450337853441>';
rewardSql =
Expand All @@ -98,13 +95,13 @@ async function check(msg, id, username, questName, result, count, extra) {
}
text += '!';
} else {
sql =
'UPDATE IGNORE quest SET count = count + ? WHERE qid = ? AND qname = ? AND uid = (SELECT uid FROM user WHERE id = ?);';
variables = [count, result.qid, questName, id];
sql = 'UPDATE IGNORE quest SET count = count + ? WHERE qid = ? AND qname = ? AND uid = ?;';
variables = [count, result.qid, questName, uid];
}

/* Query sql */
result = await mysql.query(sql, variables);
cacheUtil.clearQuests(id);
if (result.affectedRows == 1 && rewardSql) {
await mysql.query(rewardSql, rewardVar);
await msg.channel.createMessage(text);
Expand Down
7 changes: 6 additions & 1 deletion src/commands/commandList/economy/quest.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ async function rrQuest(p) {
p.msg.author.id +
') ORDER BY qid asc;';
result = await p.query(sql);
p.cache.clearQuests(p.msg.author.id);

/* Display the result */
let quests = parseQuests(p.msg.author.id, result[3], afterMid);
Expand Down Expand Up @@ -150,6 +151,7 @@ async function lockUnlockQuest(p) {
sql += `SELECT questTime FROM timers WHERE uid = (SELECT uid FROM user WHERE id = ${p.msg.author.id});`;

result = await p.query(sql);
p.cache.clearQuests(p.msg.author.id);

/* Parse dates */
let afterMid = dateUtil.afterMidnight(result[2][0] ? result[2][0].questTime : undefined);
Expand Down Expand Up @@ -201,7 +203,10 @@ async function addQuest(p) {
if (quest) sql += quest.sql;
if (quests) sql += quests.sql;

if (sql != '') await p.query(sql);
if (sql != '') {
await p.query(sql);
p.cache.clearQuests(p.msg.author.id);
}

/*Create embed */
let embed = constructEmbed(p, afterMid, quests);
Expand Down
92 changes: 82 additions & 10 deletions src/utils/cacheUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,100 @@ const mysql = require('./../botHandlers/mysqlHandler.js');

class CacheUtil {
constructor() {
this.getAnimalCount = this.getAnimalCount.bind(this);
this.animalCount = {};
this.getUid = this.getUid.bind(this);
this.get = this.get.bind(this);
this.set = this.set.bind(this);
this.cache = {};
}

async getAnimalCount(animalName, ttl = 1 * 60 * 60 * 1000) {
if (this.animalCount[animalName] !== undefined) {
return this.animalCount[animalName];
get(type, key) {
if (this.cache[type]) {
return this.cache[type][key];
}
return undefined;
}

set(type, key, value, ttl) {
if (!this.cache[type]) {
this.cache[type] = {};
}
this.cache[type][key] = value;
if (ttl) {
setTimeout(() => {
delete this.cache[type][key];
}, ttl);
}
}

clear(type, key) {
if (!this.cache[type]) {
return;
}
delete this.cache[type][key];
}

async getAnimalCount(animalName) {
let result = this.get('animalcount', animalName);
if (result) {
return result;
}

const sql = `SELECT SUM(totalcount) as total FROM animal WHERE name = ?;`;
const result = await mysql.query(sql, animalName);
result = await mysql.query(sql, animalName);
const total = result[0].total;

// Only cache if we have over 1000 animals
if (total > 1000) {
this.animalCount[animalName] = total;
setTimeout(() => {
delete this.animalCount[animalName];
}, ttl);
this.set('animalcount', animalName, total, 1 * 60 * 60 * 1000);
}
return total;
}

async getUid(id) {
id = BigInt(id);
let result = this.get('uid', id);
if (result) {
return result;
}
let sql = 'SELECT uid FROM user where id = ?;';
result = await mysql.query(sql, id);

if (result[0]?.uid) {
this.set('uid', id, result[0].uid);
return result[0].uid;
}

sql = 'INSERT INTO user (id, count) VALUES (?, 0);';
result = await mysql.query(sql, id);

this.set('uid', id, result.insertId);
return result.insertId;
}

async getQuests(id) {
let result = this.get('quest', id);
if (result) {
return result;
}

/* Check if user has this quest */
const uid = await this.getUid(id);
result = await mysql.query('SELECT * FROM quest WHERE uid = ?;', [uid]);

this.set('quest', id, result, 1 * 60 * 60 * 1000);
return result;
}

clearQuests(id) {
this.clear('quest', id);
}

async getQuestByName(questName, id, showLocked = false) {
const quests = await this.getQuests(id);
return quests.filter((quest) => {
return quest.qname === questName && (showLocked || quest.locked === 0);
});
}
}

module.exports = new CacheUtil();
21 changes: 2 additions & 19 deletions src/utils/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ try {
filter2.removeWords(goodwords);
const namor = require('namor');
const animalInfo = require('./animalInfoUtil.js');
const mysql = require('./../botHandlers/mysqlHandler.js');
const cacheUtil = require('./cacheUtil.js');
let client, main;
let totalShards;
const uidDict = {};

/**
* Checks if its an integer
Expand Down Expand Up @@ -231,23 +230,7 @@ exports.parseTime = function (diff) {

/* gets uid from discord id */
exports.getUid = async function (id) {
id = BigInt(id);
if (uidDict[id]) {
return uidDict[id];
}
let sql = 'SELECT uid FROM user where id = ?;';
let result = await mysql.query(sql, id);

if (result[0]?.uid) {
uidDict[id] = result[0].uid;
return result[0].uid;
}

sql = 'INSERT INTO user (id, count) VALUES (?, 0);';
result = await mysql.query(sql, id);

uidDict[id] = result.insertId;
return result.insertId;
return cacheUtil.getUid(id);
};

exports.getUserUid = async function (user) {
Expand Down

0 comments on commit 2dce5a3

Please sign in to comment.