Add leaderboard
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Dryusdan 2024-01-05 11:57:24 +01:00
parent f0bf12e7f4
commit 19123e0881
Signed by: Dryusdan
GPG key ID: EC1438DDE24E27D7
9 changed files with 105 additions and 21 deletions

28
poetry.lock generated
View file

@ -814,6 +814,32 @@ files = [
{file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"},
]
[[package]]
name = "ruff"
version = "0.1.11"
description = "An extremely fast Python linter and code formatter, written in Rust."
optional = false
python-versions = ">=3.7"
files = [
{file = "ruff-0.1.11-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:a7f772696b4cdc0a3b2e527fc3c7ccc41cdcb98f5c80fdd4f2b8c50eb1458196"},
{file = "ruff-0.1.11-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:934832f6ed9b34a7d5feea58972635c2039c7a3b434fe5ba2ce015064cb6e955"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ea0d3e950e394c4b332bcdd112aa566010a9f9c95814844a7468325290aabfd9"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9bd4025b9c5b429a48280785a2b71d479798a69f5c2919e7d274c5f4b32c3607"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e1ad00662305dcb1e987f5ec214d31f7d6a062cae3e74c1cbccef15afd96611d"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:4b077ce83f47dd6bea1991af08b140e8b8339f0ba8cb9b7a484c30ebab18a23f"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c4a88efecec23c37b11076fe676e15c6cdb1271a38f2b415e381e87fe4517f18"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:5b25093dad3b055667730a9b491129c42d45e11cdb7043b702e97125bcec48a1"},
{file = "ruff-0.1.11-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:231d8fb11b2cc7c0366a326a66dafc6ad449d7fcdbc268497ee47e1334f66f77"},
{file = "ruff-0.1.11-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:09c415716884950080921dd6237767e52e227e397e2008e2bed410117679975b"},
{file = "ruff-0.1.11-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:0f58948c6d212a6b8d41cd59e349751018797ce1727f961c2fa755ad6208ba45"},
{file = "ruff-0.1.11-py3-none-musllinux_1_2_i686.whl", hash = "sha256:190a566c8f766c37074d99640cd9ca3da11d8deae2deae7c9505e68a4a30f740"},
{file = "ruff-0.1.11-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:6464289bd67b2344d2a5d9158d5eb81025258f169e69a46b741b396ffb0cda95"},
{file = "ruff-0.1.11-py3-none-win32.whl", hash = "sha256:9b8f397902f92bc2e70fb6bebfa2139008dc72ae5177e66c383fa5426cb0bf2c"},
{file = "ruff-0.1.11-py3-none-win_amd64.whl", hash = "sha256:eb85ee287b11f901037a6683b2374bb0ec82928c5cbc984f575d0437979c521a"},
{file = "ruff-0.1.11-py3-none-win_arm64.whl", hash = "sha256:97ce4d752f964ba559c7023a86e5f8e97f026d511e48013987623915431c7ea9"},
{file = "ruff-0.1.11.tar.gz", hash = "sha256:f9d4d88cb6eeb4dfe20f9f0519bd2eaba8119bde87c3d5065c541dbae2b5a2cb"},
]
[[package]]
name = "six"
version = "1.16.0"
@ -1040,4 +1066,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "adc3df7f52289930591b6b4b856ac5a82eb61b2c236f8e0812846fd51ac2ecb0"
content-hash = "8748f0cc48406b5bb13e9dd9169cae31f96d5ba22e24eab6f50cdcee730c95e2"

View file

@ -19,6 +19,9 @@ pendulum = "^3.0.0"
pyyaml = "^6.0.1"
[tool.poetry.group.dev.dependencies]
ruff = "^0.1.11"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

View file

@ -9,7 +9,7 @@ from discord import app_commands
from discord.ext import commands
from discord.utils import get
from src.sql.database import engine_rw as engine
from src.sql.controllers.pschitt_user import PschittUser
from src.sql.controllers.pschitt_user import PschittUser, PschittUsers
from src.sql.models.pschitt import PschittUserBase, PschittUserModel, PschittBase
from src.logs import Log
from src.settings import Settings
@ -24,6 +24,7 @@ intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
class DiscordClient(discord.Client):
def __init__(self):
super().__init__(
@ -34,12 +35,12 @@ class DiscordClient(discord.Client):
log.warning("Creating table")
log.info("Load tables")
from src.sql.schemas.pschitt import Pschitt, PschittUser
log.info("Create PschittUser")
PschittUser.__table__.create(bind=engine, checkfirst=True)
log.info("Create Pschitt")
Pschitt.__table__.create(bind=engine, checkfirst=True)
async def on_ready(self):
await self.wait_until_ready()
if not self.synced:
@ -68,9 +69,36 @@ async def pschitt(interaction: discord.Interaction):
pschitt_user.add_point()
total_pschitt = len(pschitt_user.pschitt)
await interaction.response.send_message(
f"<@{member_id}> à crevé-e ! <:pschitt:1175769812778098799> "
f"<@{member_id}> à crevé-e ! <:pschitt:1175769812778098799> "
)
await interaction.followup.send(f"Ça fait un total de {total_pschitt} crevaison(s)")
await interaction.followup.send(
f"Ça fait un total de {total_pschitt} crevaison{'s' if total_pschitt > 1 else ''}"
)
@tree.command(
name="pschitt_leaderboard",
description="Les points de l'année en cours",
guild=discord.Object(id=settings["discord"]["guild_id"]),
)
async def biketag_list(interaction: discord.Interaction):
# for user in board:
description = ""
count = 1
for user in PschittUsers.get_all():
pschitt_point = user[1]
pschitt_user = get(client.get_all_members(), id=user[0].user_id)
description += f"**{count}.** {emoji.emojize(':crown:') if count == 1 else ''} {pschitt_user.mention} | Point{'s' if pschitt_point > 1 else ''} : {pschitt_point} \n"
count += 1
description += emoji.emojize("\nÀ vos bicyclettes <:pschitt:1175769812778098799> !")
embedMessage = discord.Embed(
color=0x00FF00,
title=f"Les points de l'année en cours !",
description=description,
)
await interaction.response.send_message(embed=embedMessage)
client.run(token=settings["discord"]["token"])

View file

@ -13,6 +13,15 @@ log = Log("controllers_pschitt")
config = Settings().get_config()
class PschittUsers(object):
def __init__(self):
return
@staticmethod
def get_all():
return PschittUserCrud.get_all_users()
class PschittUser(object):
def __init__(self, user_id, guild_id):
self.id = None

View file

@ -17,4 +17,3 @@ class PschittCrud:
session.add(pschitt_schema)
session.commit()
return True

View file

@ -3,7 +3,7 @@ from src.settings import Settings
from src.sql.models.pschitt import PschittUserBase, PschittUserModel, PschittBase
from src.sql.schemas.pschitt import Pschitt, PschittUser
from src.sql.database import SessionLocalRo, SessionLocalRw
from sqlalchemy import desc, text
from sqlalchemy import desc, func, text
log = Log("crud_PschittUser")
config = Settings().get_config()
@ -25,7 +25,15 @@ class PschittUserCrud:
def get_all_users():
"""get_all_users"""
with SessionLocalRo() as session:
return session.query(PschittUser).order_by(desc("point")).all()
# req = session.query(func.count(PschittUser.pschitt).group_by(PschittUser.id))
req = (
session.query(PschittUser, func.count(Pschitt.datetime).label("total"))
.join(Pschitt)
.group_by(PschittUser.id)
.order_by(text("total DESC"))
.all()
)
return req
@staticmethod
def get_user(user: PschittUserBase):
@ -35,7 +43,11 @@ class PschittUserCrud:
:type user: PschittUserBase
"""
with SessionLocalRo() as session:
return session.query(PschittUser).filter(PschittUser.user_id == user.user_id).first()
return (
session.query(PschittUser)
.filter(PschittUser.user_id == user.user_id)
.first()
)
return None
@staticmethod
@ -54,7 +66,9 @@ class PschittUserCrud:
def update_user(user: PschittUserModel):
with SessionLocalRw() as session:
biketag_user = (
session.query(PschittUser).filter(PschittUser.user_id == user.id).first()
session.query(PschittUser)
.filter(PschittUser.user_id == user.id)
.first()
)
biketag_user.point = user.point
session.commit()
@ -65,4 +79,3 @@ class PschittUserCrud:
@staticmethod
def delete_user(user: PschittUserBase):
return user

View file

@ -17,13 +17,16 @@ class PschittUserModel(PschittUserBase):
pschitt: list
pass
class PschittBase(BaseModel):
pschitt_user_id: int
class Config:
"""Config."""
orm_mode = True
class PschittModel(PschittBase):
id: int
datetime: datetime

View file

@ -6,21 +6,21 @@ from typing import List, Optional
from src.sql.database import Base
#pschitt_association_table = Table(
# pschitt_association_table = Table(
# "pschitt",
# Base.metadata,
# Column("pschitt_user_id", ForeignKey("pschitt_user.id")),
# Column("pschitt_datetime_id", ForeignKey("pschitt_datetime.id")),
#)
# )
#association_table = Table(
# association_table = Table(
# "association_table",
# Base.metadata,
# Column("pschitt_user_id", ForeignKey("pschitt_user.id"), primary_key=True),
# Column("pschitt_datetime_id", ForeignKey("pschitt_datetime.id"), primary_key=True),
#)
# )
#class PschittAssociation(Base):
# class PschittAssociation(Base):
# """PschittAssociation"""
#
# __tablename__ = "pschitt"
@ -29,15 +29,19 @@ from src.sql.database import Base
# pschitt_user_id: Column(BigInteger, ForeignKey("pschitt_user.id"), primary_key=True)
# pschitt_datetime_id: Column(BigInteger, ForeignKey("pschitt_datetime.id"), primary_key=True)
class Pschitt(Base):
"""Pschitt"""
__tablename__ = "pschitt_datetime"
id: Mapped[int] = mapped_column(primary_key=True)
pschitt_user_id: Mapped[int] = mapped_column(ForeignKey("pschitt_user.id"))
datetime = Column('date', DateTime(timezone=True), server_default=func.now(), nullable=False)
datetime = Column(
"date", DateTime(timezone=True), server_default=func.now(), nullable=False
)
pschitt_user: Mapped[List["PschittUser"]] = relationship(back_populates="pschitt")
class PschittUser(Base):
"""Pschitt"""
@ -45,5 +49,6 @@ class PschittUser(Base):
id: Mapped[int] = mapped_column(primary_key=True)
user_id = Column(BigInteger, nullable=False, unique=True)
guild_id = Column(BigInteger, nullable=False)
pschitt: Mapped[List["Pschitt"]] = relationship(back_populates="pschitt_user", lazy="immediate")
pschitt: Mapped[List["Pschitt"]] = relationship(
back_populates="pschitt_user", lazy="immediate"
)

View file

@ -3,5 +3,3 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import List
from src.sql.database import Base