Skip to content

Commit

Permalink
Removed insert on duplicate update on animals to prevent db locks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Aug 5, 2024
1 parent 2dce5a3 commit 840da6a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
27 changes: 19 additions & 8 deletions src/commands/commandList/zoo/animalUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const mysql = require('../../../botHandlers/mysqlHandler.js');
const global = require('../../../utils/global.js');
const animals = require('../../../utils/animalInfoUtil.js');
const eventUtil = require('../../../utils/eventUtil.js');
const cacheUtil = require('../../../utils/cacheUtil.js');

let enableDistortedTier = true;
setTimeout(() => {
Expand Down Expand Up @@ -151,7 +152,7 @@ function getEventRarity(animal, event) {
return rate;
}

exports.getMultipleAnimals = function (count, user, opt) {
exports.getMultipleAnimals = async function (count, user, opt) {
const total = {};
let xp = 0;
for (let i = 0; i < count; i++) {
Expand All @@ -175,7 +176,7 @@ exports.getMultipleAnimals = function (count, user, opt) {
}

const ordered = sortAnimals(total);
const { sql, typeCount } = createSql(ordered, user);
const { sql, typeCount } = await createSql(ordered, user);

return {
animals: total,
Expand Down Expand Up @@ -252,12 +253,16 @@ function sortAnimals(animals) {
return animalList;
}

function createSql(orderedAnimal, user) {
async function createSql(orderedAnimal, user) {
let animalSql = [];
let animalCountSql = {};

let animalCase = '( CASE\n';
let animals = [];
orderedAnimal.forEach((animal) => {
animalSql.push(`(${user.id}, '${animal.value}', ${animal.count}, ${animal.count})`);
animals.push(`'${animal.value}'`);
animalCase += `WHEN name = '${animal.value}' THEN ${animal.count}\n`

Check failure on line 265 in src/commands/commandList/zoo/animalUtil.js

View workflow job for this annotation

GitHub Actions / ESLint

src/commands/commandList/zoo/animalUtil.js#L265

Missing semicolon (semi)
if (!animalCountSql[animal.rank])
animalCountSql[animal.rank] = {
rank: animal.rank,
Expand All @@ -266,12 +271,11 @@ function createSql(orderedAnimal, user) {
animalCountSql[animal.rank].count += animal.count;
});
animalCountSql = Object.values(animalCountSql);
animalCase += 'ELSE 0 END)'

Check failure on line 274 in src/commands/commandList/zoo/animalUtil.js

View workflow job for this annotation

GitHub Actions / ESLint

src/commands/commandList/zoo/animalUtil.js#L274

Missing semicolon (semi)

let sql = `INSERT INTO animal (id, name, count, totalcount)
VALUES ${animalSql.join(',')}
ON DUPLICATE KEY UPDATE
count = count + VALUES(count),
totalcount = totalcount + VALUES(totalcount);`;
let sql = `UPDATE animal SET
count = count + ${animalCase}, totalcount = totalcount + ${animalCase}
WHERE id = ${user.id} AND name in (${animals.join(',')});`

Check failure on line 278 in src/commands/commandList/zoo/animalUtil.js

View workflow job for this annotation

GitHub Actions / ESLint

src/commands/commandList/zoo/animalUtil.js#L278

Missing semicolon (semi)
sql += `INSERT INTO animal_count (id, ${animalCountSql
.map((animalCount) => animalCount.rank)
.join(',')})
Expand All @@ -283,8 +287,15 @@ function createSql(orderedAnimal, user) {
})
.join(',')};
`;
await checkDbInsert(user.id, orderedAnimal);
return {
sql,
typeCount: animalCountSql,
};
}

async function checkDbInsert(id, animals) {
for (let i in animals) {
await cacheUtil.insertAnimal(id, animals[i].value);
}
}
2 changes: 1 addition & 1 deletion src/commands/commandList/zoo/autohunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function claim(p, msg, con, query, bot) {

//Get all animal
let radar = autohuntutil.getLvl(query.radar, 0, 'radar');
let { animals, animalSql } = animalUtil.getMultipleAnimals(query.huntcount, p.msg.author, {
let { animals, animalSql } = await animalUtil.getMultipleAnimals(query.huntcount, p.msg.author, {
patreon: patreon,
huntbot: radar.stat,
});
Expand Down
2 changes: 1 addition & 1 deletion src/commands/commandList/zoo/catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async function getAnimals(p, result, gems, uid) {
if (gems['Patreon']) count += 1;
}

let { ordered, animalSql, typeCount, xp } = animalUtil.getMultipleAnimals(
let { ordered, animalSql, typeCount, xp } = await animalUtil.getMultipleAnimals(
count,
p.msg.author,
opt
Expand Down
30 changes: 30 additions & 0 deletions src/utils/cacheUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const mysql = require('./../botHandlers/mysqlHandler.js');
class CacheUtil {
constructor() {
this.getUid = this.getUid.bind(this);
this.getAnimalNames = this.getAnimalNames.bind(this);
this.get = this.get.bind(this);
this.set = this.set.bind(this);
this.cache = {};
Expand Down Expand Up @@ -102,6 +103,35 @@ class CacheUtil {
return quest.qname === questName && (showLocked || quest.locked === 0);
});
}

async getAnimalNames(id) {
id = BigInt(id);
let result = this.get('animalNames', id);
if (result) {
return result;
}

const sql = `SELECT name FROM animal WHERE id = ${id};`;
result = await mysql.query(sql);
const animals = {};
result.forEach((row) => {
animals[row.name] = true;
});
this.set('animalNames', id, animals);
return animals;
}

async insertAnimal(id, animalName) {
const animals = await this.getAnimalNames(id);
if (animals[animalName]) {
return;
}

const sql = `INSERT INTO animal (id, name, count, totalcount) VALUES (${id}, '${animalName}', 0, 0);`;
await mysql.query(sql);

animals[animalName] = true;
}
}

module.exports = new CacheUtil();

0 comments on commit 840da6a

Please sign in to comment.