Rework bc, rob & poke leaderboards

pull/104/head
Stijn De Clercq 2022-02-05 22:55:09 +01:00
parent 7ad2bf351e
commit 062d54722b
4 changed files with 65 additions and 58 deletions

View File

@ -85,7 +85,6 @@ class Events(commands.Cog):
Logs commands in your terminal. Logs commands in your terminal.
:param ctx: Discord Context :param ctx: Discord Context
""" """
print("a")
print(stringFormatters.format_command_usage(ctx)) print(stringFormatters.format_command_usage(ctx))
command_stats.invoked(command_stats.InvocationType.TextCommand) command_stats.invoked(command_stats.InvocationType.TextCommand)

View File

@ -1,3 +1,5 @@
from typing import Callable, Optional
from data.menus import paginated_leaderboard from data.menus import paginated_leaderboard
from decorators import help from decorators import help
import discord import discord
@ -10,7 +12,7 @@ import math
import requests import requests
# TODO some sort of general leaderboard because all of them are the same # TODO some sort of general leaderboard generation because all of them are the same
class Leaderboards(commands.Cog): class Leaderboards(commands.Cog):
def __init__(self, client): def __init__(self, client):
@ -21,6 +23,26 @@ class Leaderboards(commands.Cog):
def cog_check(self, ctx): def cog_check(self, ctx):
return not self.client.locked return not self.client.locked
def _generate_embed_data(self, entries: list,
key_f: Callable = lambda x: x[0],
data_f: Callable = lambda x: x[1],
ignore_non_pos: bool = True) -> Optional[list[tuple]]:
data = []
for i, v in enumerate(sorted(entries, key=data_f, reverse=True)):
entry_data = data_f(v)
# Leaderboard is empty
if i == 0 and entry_data == 0 and ignore_non_pos:
return None
# Ignore entries with no data
if ignore_non_pos and entry_data <= 0:
continue
data.append((key_f(v), f"{entry_data:,}",))
return data
@commands.group(name="Leaderboard", aliases=["Lb", "Leaderboards"], case_insensitive=True, usage="[Categorie]*", @commands.group(name="Leaderboard", aliases=["Lb", "Leaderboards"], case_insensitive=True, usage="[Categorie]*",
invoke_without_command=True) invoke_without_command=True)
@commands.check(checks.allowedChannels) @commands.check(checks.allowedChannels)
@ -45,14 +67,11 @@ class Leaderboards(commands.Cog):
user[1] += platDinks[str(user[0])] * Numbers.q.value user[1] += platDinks[str(user[0])] * Numbers.q.value
entries[i] = user entries[i] = user
data = [] data = self._generate_embed_data(entries, key_f=lambda x: x[0], data_f=lambda x: (float(x[1]) + float(x[3])))
for i, user in enumerate(sorted(entries, key=lambda x: (float(x[1]) + float(x[3])), reverse=True)):
if i == 0 and float(user[1]) + float(user[3]) == 0.0: if data is None:
return await self.empty_leaderboard(ctx, "Dinks Leaderboard", return await self.empty_leaderboard(ctx, "Dinks Leaderboard",
"Er zijn nog geen personen met Didier Dinks.") "Er zijn nog geen personen met Didier Dinks.")
elif float(user[1]) + float(user[3]) > 0.0:
total_dinks = math.floor(float(user[1]) + float(user[3]))
data.append((user[0], total_dinks,))
lb = paginated_leaderboard.Leaderboard( lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Dinks Leaderboard", data=data, fetch_names=True ctx=ctx, title="Dinks Leaderboard", data=data, fetch_names=True
@ -79,61 +98,49 @@ class Leaderboards(commands.Cog):
@leaderboard.command(name="Bitcoin", aliases=["Bc"], hidden=True) @leaderboard.command(name="Bitcoin", aliases=["Bc"], hidden=True)
async def bitcoin(self, ctx): async def bitcoin(self, ctx):
users = currency.getAllRows() users = currency.getAllRows()
boardTop = [] data = self._generate_embed_data(users, data_f=lambda x: round(float(x[8]), 8))
for i, user in enumerate(sorted(users, key=lambda x: x[8], reverse=True)):
# Don't create an empty leaderboard
if i == 0 and float(user[8]) == 0.0:
return await self.empty_leaderboard(ctx, "Bitcoin Leaderboard",
"Er zijn nog geen personen met Bitcoins.")
elif float(user[8]) > 0.0:
# Only add people with more than 0
# Get the username in this guild
name = self.utilsCog.getDisplayName(ctx, user[0])
if int(user[0]) == int(ctx.author.id):
boardTop.append("**{} ({:,})**".format(name, round(user[8], 8)))
else:
boardTop.append("{} ({:,})".format(name, round(user[8], 8)))
await self.startPaginated(ctx, boardTop, "Bitcoin Leaderboard") if data is None:
return await self.empty_leaderboard(ctx, "Bitcoin Leaderboard",
"Er zijn nog geen personen met Bitcoins.")
lb = paginated_leaderboard.Leaderboard(
ctx=ctx, title="Bitcoin Leaderboard", data=data, fetch_names=True
)
await lb.send(ctx)
@leaderboard.command(name="Rob", hidden=True) @leaderboard.command(name="Rob", hidden=True)
async def rob(self, ctx): async def rob(self, ctx):
users = list(stats.getAllRows()) users = list(stats.getAllRows())
boardTop = [] data = self._generate_embed_data(users, data_f=lambda x: math.floor(float(x[4])))
for i, user in enumerate(sorted(users, key=lambda x: x[4], reverse=True)):
# Don't create an empty leaderboard if data is None:
if i == 0 and float(user[4]) == 0.0: return await self.empty_leaderboard(ctx, "Rob Leaderboard",
return await self.empty_leaderboard(ctx, "Rob Leaderboard", "Er heeft nog niemand Didier Dinks gestolen.")
"Er heeft nog niemand Didier Dinks gestolen.")
elif float(user[4]) > 0.0: lb = paginated_leaderboard.Leaderboard(
# Only add people with more than 0 ctx=ctx, title="Rob Leaderboard", data=data, fetch_names=True
# Get the username in this guild )
name = self.utilsCog.getDisplayName(ctx, user[0])
if int(user[0]) == int(ctx.author.id): await lb.send(ctx)
boardTop.append("**{} ({:,})**".format(name, math.floor(float(user[4]))))
else:
boardTop.append("{} ({:,})".format(name, math.floor(float(user[4]))))
await self.startPaginated(ctx, boardTop, "Rob Leaderboard")
@leaderboard.command(name="Poke", hidden=True) @leaderboard.command(name="Poke", hidden=True)
async def poke(self, ctx): async def poke(self, ctx):
s = stats.getAllRows() entries = stats.getAllRows()
blacklist = poke.getAllBlacklistedUsers() blacklist = poke.getAllBlacklistedUsers()
boardTop = [] # Remove blacklisted users
for i, user in enumerate(sorted(s, key=lambda x: x[1], reverse=True)): entries = list(filter(lambda x: x[0] not in blacklist, entries))
if i == 0 and int(user[1]) == 0:
return await self.empty_leaderboard(ctx, "Poke Leaderboard", "Er is nog niemand getikt.")
elif int(user[1]) == 0: data = self._generate_embed_data(entries, data_f=lambda x: round(int(x[1])))
break if data is None:
# Don't include blacklisted users return await self.empty_leaderboard(ctx, "Poke Leaderboard", "Er is nog niemand getikt.")
elif str(user[0]) not in blacklist:
name = self.utilsCog.getDisplayName(ctx, user[0]) lb = paginated_leaderboard.Leaderboard(
if int(user[0]) == int(ctx.author.id): ctx=ctx, title="Poke Leaderboard", data=data, fetch_names=True
boardTop.append("**{} ({:,})**".format(name, round(int(user[1])))) )
else:
boardTop.append("{} ({:,})".format(name, round(int(user[1])))) await lb.send(ctx)
await self.startPaginated(ctx, boardTop, "Poke Leaderboard")
@leaderboard.command(name="Xp", aliases=["Level"], hidden=True) @leaderboard.command(name="Xp", aliases=["Level"], hidden=True)
async def xp(self, ctx): async def xp(self, ctx):

View File

@ -2,6 +2,7 @@ import discord
from discord.ext import menus from discord.ext import menus
# TODO rework pagination
class CommandsList(menus.ListPageSource): class CommandsList(menus.ListPageSource):
def __init__(self, data, colour=discord.Colour.blue()): def __init__(self, data, colour=discord.Colour.blue()):
super().__init__(data, per_page=15) super().__init__(data, per_page=15)

View File

@ -23,7 +23,7 @@ class Leaderboard:
def __post_init__(self): def __post_init__(self):
if self.format_f is None: if self.format_f is None:
self.format_f = self._format self .format_f = lambda x: x
def _should_highlight(self, data) -> bool: def _should_highlight(self, data) -> bool:
"""Check if an entry should be highlighted""" """Check if an entry should be highlighted"""
@ -38,7 +38,7 @@ class Leaderboard:
if self.fetch_names: if self.fetch_names:
name = get_display_name(self.ctx, int(data[0])) name = get_display_name(self.ctx, int(data[0]))
s = f"{index + 1}: {name} ({data[1]})" s = f"{index + 1}: {name} ({self.format_f(data[1])})"
return s return s
@ -64,7 +64,7 @@ class Leaderboard:
description = "" description = ""
for i, v in enumerate(self.data): for i, v in enumerate(self.data):
s = self.format_f(i, v) s = self._format(i, v)
if self._should_highlight(v[0]): if self._should_highlight(v[0]):
s = f"**{s}**" s = f"**{s}**"