Skip to content

Commit

Permalink
Implemented leaderboard function, set to currently identify the top 2…
Browse files Browse the repository at this point in the history
…0 countries in each respective COVID-19 stat (deaths, cases, recoveries)
  • Loading branch information
kevin-pierce committed Jul 26, 2020
1 parent 660a540 commit d4b601b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions commands/deaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
return deaths;
}
let totalDeaths = await getTotalDeaths();
console.log(totalDeaths);
return message.channel.send(`Globally, ${totalDeaths["deaths"]} people have died due to COVID-19.`);
}
// Data for TODAY
Expand Down
75 changes: 75 additions & 0 deletions commands/leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const axios = require("axios");
const Discord = require("discord.js");

module.exports = {
name: "leaderboard",
description: "Provides a top-10 ranking for each Country in each respective category",
async execute(message, args){

let leadingDeaths = [];
let leadingRecoveries = [];
let leadingCases = [];

if (!args.length){
let getCovData = async () => {
let response = await axios.get("https://corona.lmao.ninja/v2/countries?yesterday&sort");
let data = response.data;
return data;
}
let countryData = await getCovData();

// Only take the top ~20 countries data for each comparison
for (country in countryData){
if (countryData[country]["deaths"] > 5000) leadingDeaths.push(countryData[country]);
if (countryData[country]["recovered"] > 80000) leadingRecoveries.push(countryData[country]);
if (countryData[country]["cases"] > 150000) leadingCases.push(countryData[country]);
}
// Sort the arrays
leadingDeaths.sort(compareDeaths);
leadingRecoveries.sort(compareRecoveries);
leadingCases.sort(compareCases);
}
}
}

let compareDeaths = (a, b) => {
// Use toUpperCase() to ignore character casing
const countryA = a["deaths"]
const countryB = b["deaths"]

let comparison = 0;
if (countryA > countryB) {
comparison = -1;
} else if (countryA < countryB) {
comparison = 1;
}
return comparison;
}

let compareRecoveries = (a, b) => {
// Use toUpperCase() to ignore character casing
const countryA = a["recovered"]
const countryB = b["recovered"]

let comparison = 0;
if (countryA > countryB) {
comparison = -1;
} else if (countryA < countryB) {
comparison = 1;
}
return comparison;
}

let compareCases = (a, b) => {
// Use toUpperCase() to ignore character casing
const countryA = a["cases"]
const countryB = b["cases"]

let comparison = 0;
if (countryA > countryB) {
comparison = -1;
} else if (countryA < countryB) {
comparison = 1;
}
return comparison;
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ client.on("message", async message => {
if (command === "deaths") {
client.commands.get("deaths").execute(message, args);
}
// Show stats on the number of recovered
else if (command === "recovered") {
client.commands.get("recovered").execute(message, args);
}
else if (command === "leaderboard") {
client.commands.get("leaderboard").execute(message, args);
}
});

// Login to Discord using the app's token
Expand Down

0 comments on commit d4b601b

Please sign in to comment.