added playername cache
Made period optional added player cache should fix user user_id on lb instead of usernames
This commit was merged in pull request #2.
This commit is contained in:
+78
-78
@@ -26,17 +26,19 @@ class Leaderboard(commands.Cog):
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
leaderboard_type: str,
|
||||
period: str
|
||||
period: str = "alltime"
|
||||
):
|
||||
try:
|
||||
if leaderboard_type == "accuracy":
|
||||
msg = get_accuracy_leaderboard(
|
||||
msg = Leaderboard.get_accuracy_leaderboard(
|
||||
self,
|
||||
period,
|
||||
interaction.guild
|
||||
)
|
||||
|
||||
elif leaderboard_type == "perfect":
|
||||
msg = get_perfect_score_leaderboard(
|
||||
msg = Leaderboard.get_perfect_score_leaderboard(
|
||||
self,
|
||||
period,
|
||||
interaction.guild
|
||||
)
|
||||
@@ -49,71 +51,31 @@ class Leaderboard(commands.Cog):
|
||||
"Error loading leaderboard."
|
||||
)
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(Leaderboard(bot))
|
||||
|
||||
def get_accuracy_leaderboard(period, guild):
|
||||
conn = sqlite3.connect("Goonerdle.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
User_ID,
|
||||
COUNT(*) AS games_played,
|
||||
ROUND(
|
||||
(SUM(Result) * 100.0) /
|
||||
(COUNT(*) * 10),
|
||||
2
|
||||
) AS accuracy
|
||||
FROM Result
|
||||
WHERE
|
||||
? = 'alltime'
|
||||
OR (
|
||||
? = 'monthly'
|
||||
AND DateScore >= date('now', 'start of month')
|
||||
)
|
||||
OR (
|
||||
? = 'yearly'
|
||||
AND DateScore >= date('now', 'start of year')
|
||||
)
|
||||
GROUP BY User_ID
|
||||
HAVING COUNT(*) >= 5
|
||||
ORDER BY accuracy DESC
|
||||
LIMIT 10
|
||||
""", (period, period, period))
|
||||
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
lines = []
|
||||
|
||||
for position, (user_id, games_played, accuracy) in enumerate(rows, start=1):
|
||||
|
||||
member = guild.get_member(user_id)
|
||||
def get_playername(self, user_id : int):
|
||||
member = self.bot.player_cache.get(user_id)
|
||||
|
||||
if member:
|
||||
name = member.display_name
|
||||
else:
|
||||
name = f"User {user_id}"
|
||||
|
||||
return name
|
||||
|
||||
lines.append(
|
||||
f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)"
|
||||
)
|
||||
def get_accuracy_leaderboard(self, period, guild):
|
||||
conn = sqlite3.connect("Goonerdle.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
return "🏆 **Accuracy Leaderboard** 🏆\n\n" + "\n".join(lines)
|
||||
|
||||
def get_perfect_score_leaderboard(period, guild):
|
||||
conn = sqlite3.connect("Goonerdle.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
User_ID,
|
||||
COUNT(*) AS perfect_games
|
||||
FROM Result
|
||||
WHERE
|
||||
Result = 10
|
||||
AND (
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
User_ID,
|
||||
COUNT(*) AS games_played,
|
||||
ROUND(
|
||||
(SUM(Result) * 100.0) /
|
||||
(COUNT(*) * 10),
|
||||
2
|
||||
) AS accuracy
|
||||
FROM Result
|
||||
WHERE
|
||||
? = 'alltime'
|
||||
OR (
|
||||
? = 'monthly'
|
||||
@@ -123,28 +85,66 @@ def get_perfect_score_leaderboard(period, guild):
|
||||
? = 'yearly'
|
||||
AND DateScore >= date('now', 'start of year')
|
||||
)
|
||||
GROUP BY User_ID
|
||||
HAVING COUNT(*) >= 5
|
||||
ORDER BY accuracy DESC
|
||||
LIMIT 10
|
||||
""", (period, period, period))
|
||||
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
lines = []
|
||||
|
||||
for position, (user_id, games_played, accuracy) in enumerate(rows, start=1):
|
||||
name = Leaderboard.get_playername(self, user_id)
|
||||
|
||||
lines.append(
|
||||
f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)"
|
||||
)
|
||||
GROUP BY User_ID
|
||||
ORDER BY perfect_games DESC
|
||||
LIMIT 10
|
||||
""", (period, period, period))
|
||||
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
return f"🏆 **Accuracy Leaderboard {period}** 🏆\n\n" + "\n".join(lines)
|
||||
|
||||
lines = []
|
||||
def get_perfect_score_leaderboard(self, period, guild):
|
||||
conn = sqlite3.connect("Goonerdle.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
for position, (user_id, perfect_games) in enumerate(rows, start=1):
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
User_ID,
|
||||
COUNT(*) AS perfect_games
|
||||
FROM Result
|
||||
WHERE
|
||||
Result = 10
|
||||
AND (
|
||||
? = 'alltime'
|
||||
OR (
|
||||
? = 'monthly'
|
||||
AND DateScore >= date('now', 'start of month')
|
||||
)
|
||||
OR (
|
||||
? = 'yearly'
|
||||
AND DateScore >= date('now', 'start of year')
|
||||
)
|
||||
)
|
||||
GROUP BY User_ID
|
||||
ORDER BY perfect_games DESC
|
||||
LIMIT 10
|
||||
""", (period, period, period))
|
||||
|
||||
member = guild.get_member(user_id)
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
if member:
|
||||
name = member.display_name
|
||||
else:
|
||||
name = f"User {user_id}"
|
||||
lines = []
|
||||
|
||||
lines.append(
|
||||
f"{position}. {name} — **{perfect_games}** perfect games"
|
||||
)
|
||||
for position, (user_id, perfect_games) in enumerate(rows, start=1):
|
||||
name = Leaderboard.get_playername(self, user_id)
|
||||
|
||||
return "🏆 **Perfect Game Leaderboard** 🏆\n\n" + "\n".join(lines)
|
||||
lines.append(
|
||||
f"{position}. {name} — **{perfect_games}** perfect games"
|
||||
)
|
||||
|
||||
return f"🏆 **Perfect Game Leaderboard {period}** 🏆\n\n" + "\n".join(lines)
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(Leaderboard(bot))
|
||||
Reference in New Issue
Block a user