Skip to content

Commit

Permalink
command to refresh animals
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Jul 21, 2024
1 parent 22fd601 commit 1a5d649
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/commands/commandList/admin/refreshAnimals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* OwO Bot for Discord
* Copyright (C) 2024 Christopher Thai
* This software is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
* For more information, see README.md and LICENSE
*/

const CommandInterface = require('../../CommandInterface.js');

module.exports = new CommandInterface({
alias: ['refreshanimals', 'ra'],

owner: true,

execute: function (p) {
let animalName;
if (p.args[0]) {
animalName = p.args[0];
}
p.send('Refreshing all animals');
p.pubsub.publish('refreshAnimals', { animalName });
},
});
2 changes: 1 addition & 1 deletion src/commands/commandList/zoo/owodex.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = new CommandInterface({
if (result[0].saccount > 0)
sac = animal.essence + ' Essence | ' + global.toFancyNum(result[0].saccount) + ' killed';
let alias = 'None';
if (animal.alt.length > 0) alias = animal.alt.splice(1).join(', ');
if (animal.alt.length > 0) alias = animal.alt.slice(1).join(', ');
let phys = `<:hp:531620120410456064> \`${animal.hp}\` <:att:531616155450998794> \`${animal.att}\` <:pr:531616156222488606> \`${animal.pr}\` `;
let mag = `<:wp:531620120976687114> \`${animal.wp}\` <:mag:531616156231139338> \`${animal.mag}\` <:mr:531616156226945024> \`${animal.mr}\` `;
let rarity = global.toFancyNum(totalAnimals) + ' total caught';
Expand Down
47 changes: 43 additions & 4 deletions src/utils/animalInfoUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ const mysql = require('./../botHandlers/mysqlHandler.js');
class AnimalJson {
constructor() {
this.initialize = this.initialize.bind(this);
this.reinitialize = this.reinitialize.bind(this);
this.parseAnimal = this.parseAnimal.bind(this);
this.parseRank = this.parseRank.bind(this);
this.getAnimal = this.getAnimal.bind(this);
this.getRank = this.getRank.bind(this);
this.getRanks = this.getRanks.bind(this);
this.getOrder = this.getOrder.bind(this);

this.initialize();
}

async initialize() {
Expand All @@ -40,6 +39,30 @@ class AnimalJson {
});
}

async reinitialize(animalName) {
if (animalName) {
animalName = this.animalNameToKey[animalName] || animalName;
return this.reinitializeAnimal(animalName);
}
const newAnimalJson = new AnimalJson();
await newAnimalJson.initialize();
this.copy(newAnimalJson);
}

async reinitializeAnimal(animalName) {
const result = await mysql.query(`SELECT * FROM animals WHERE name = ?`, animalName);
this.parseAnimal(result[0]);
this.updateAnimalsAndRanks();
}

copy(newAnimalJson) {
this.animalNameToKey = newAnimalJson.animalNameToKey;
this.animals = newAnimalJson.animals;
this.order = newAnimalJson.order;
this.ranks = newAnimalJson.ranks;
this.rankNameToKey = newAnimalJson.rankNameToKey;
}

/**
* Parse animal for db
*/
Expand All @@ -62,8 +85,11 @@ class AnimalJson {
for (let key in this.animals) {
const animal = this.animals[key];
const rank = this.getRank(animal.rank);
rank.addAnimal(animal);
rank.addAnimalToTemp(animal);
}
Object.values(this.ranks).forEach((rank) => {
rank.useTemp();
});
}

getRank(rankName) {
Expand Down Expand Up @@ -154,6 +180,7 @@ class AnimalRank {
this.points = animalJson.ranks[rank].points;
this.essence = animalJson.ranks[rank].essence;
this.animals = [];
this.tempAnimals = [];
this.placeholder = animalJson.ranks[rank].placeholder;
this.conditional = animalJson.ranks[rank].conditional;
this.rarity = animalJson.ranks[rank].rarity;
Expand All @@ -165,6 +192,18 @@ class AnimalRank {
this.animals.push(animal.value);
animal.setRank(this);
}

addAnimalToTemp(animal) {
this.tempAnimals.push(animal.value);
animal.setRank(this);
}

useTemp() {
this.animals = this.tempAnimals;
this.tempAnimals = [];
}
}

module.exports = new AnimalJson();
const obj = new AnimalJson();
obj.initialize();
module.exports = obj;
16 changes: 16 additions & 0 deletions src/utils/pubsubHandlers/refreshAnimals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* OwO Bot for Discord
* Copyright (C) 2024 Christopher Thai
* This software is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
* For more information, see README.md and LICENSE
*/

exports.handle = async function (main, message) {
const { animalName } = JSON.parse(message);
await main.animalUtil.reinitialize(animalName);
if (animalName) {
console.log(`Succesfully reinitialized animal: ${animalName}`);
} else {
console.log('Succesfully reinitialized animals');
}
};

0 comments on commit 1a5d649

Please sign in to comment.