Skip to content

Commit

Permalink
/stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
probablyjassin committed Nov 18, 2024
1 parent c1ac69b commit e31c559
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def help(self, ctx: MogiApplicationContext):
embed = create_embed(
"Help",
"Here you can find a brief summary of common commands.",
"https://raw.githubusercontent.com/mk8dx-yuzu/mk8dx-yuzu.github.io/main/public/favicon/ms-icon-310x310.png",
"https://raw.githubusercontent.com/mk8dx-yuzu/mk8dx-yuzu.github.io/main/public/images/kawaii_icon_by_kevnkkm.png",
help_fields,
{
"text": "Yuzu Online",
Expand Down
2 changes: 1 addition & 1 deletion cogs/profiles/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(self):

embed.set_author(
name="Yuzu-Lounge",
icon_url="https://raw.githubusercontent.com/mk8dx-yuzu/mk8dx-yuzu.github.io/main/public/favicon/android-icon-192x192.png",
icon_url="https://raw.githubusercontent.com/mk8dx-yuzu/mk8dx-yuzu.github.io/main/public/images/kawaii_icon_by_kevnkkm.png",
)

embed.set_thumbnail(
Expand Down
97 changes: 97 additions & 0 deletions cogs/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from discord import slash_command, Color
from discord.ext import commands

from utils.data.database import db_mogis
from models.MogiModel import MogiHistoryData
from models.CustomMogiContext import MogiApplicationContext
from utils.command_helpers.info_embed_factory import create_embed
from datetime import datetime
from collections import Counter


class stats(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot

@slash_command(name="stats", description="Show Lounge stats for the current season")
async def stats(self, ctx: MogiApplicationContext):
all_mogis = [MogiHistoryData.from_dict(mogi) for mogi in list(db_mogis.find())]

durations = [
datetime.fromtimestamp(mogi.finished_at)
- datetime.fromtimestamp(mogi.started_at)
for mogi in all_mogis
]
average_duration_minutes = (
sum([duration.total_seconds() for duration in durations])
/ len(durations)
/ 60
)

average_disconnections = sum([mogi.disconnections for mogi in all_mogis]) / len(
all_mogis
)
average_subs = sum([mogi.subs for mogi in all_mogis]) / len(all_mogis)

all_results_ever = [result for mogi in all_mogis for result in mogi.results]

player_id_counts = Counter(
player_id for mogi in all_mogis for player_id in mogi.player_ids
)
top_3_players = player_id_counts.most_common(3)

most_played_str = "\n".join(
[f"<@{entry[0]}>: {entry[1]}" for entry in top_3_players]
)

key_to_format = {
1: "FFA",
2: "2v2",
3: "3v3",
4: "4v4",
6: "6v6",
}

formats_dict = {}
for mogi in all_mogis:
if key_to_format[mogi.format] not in formats_dict:
formats_dict[key_to_format[mogi.format]] = 1
else:
formats_dict[key_to_format[mogi.format]] += 1

fields = {
"Total Mogis played": str(len(all_mogis)),
"Average Duration": f"{average_duration_minutes:.1f} minutes",
"Most Mogis played": most_played_str,
"Average DCs per Mogi": f"{average_disconnections:.1f}",
"Most DCs in a Mogi": f"{max([mogi.disconnections for mogi in all_mogis])}",
"Average Subs needed per Mogi": f"{average_subs:.1f}",
"Largest Gain": f"{max(all_results_ever)}",
"Largest Loss": f"{min(all_results_ever)}",
"How often do we play which format?": "\n".join(
[f"{key}: {formats_dict[key]}" for key in formats_dict.keys()]
),
}

embed = create_embed(
"Season 3 Mogi Stats",
"Some interesting stats",
"https://raw.githubusercontent.com/mk8dx-yuzu/mk8dx-yuzu.github.io/main/public/images/kawaii_icon_by_kevnkkm.png",
fields,
{
"text": "Yuzu Online",
"icon_url": "https://images-ext-1.discordapp.net/external/ymL8nMKRGEJwQZNCLRuCAbeHxt3n3HYA0XTD-JUW4m4/https/cdn.discordapp.com/icons/1084911987626094654/a_f51d88cf4421676675437f9cf4fbbff6.gif",
},
color=Color.dark_magenta(),
)

# TODO: Make the footer image and text take the guild's icon and name instead of hard coded values ↑
await ctx.respond(embed=embed)

@slash_command(name="mogis", description="Show all mogis for the current season")
async def mogis(self, ctx: MogiApplicationContext):
await ctx.respond(list((db_mogis.find())))


def setup(bot: commands.Bot):
bot.add_cog(stats(bot))
26 changes: 26 additions & 0 deletions models/MogiModel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from dataclasses import dataclass, field
from bson import ObjectId

from models.PlayerModel import PlayerProfile

Expand Down Expand Up @@ -263,3 +264,28 @@ def from_json(cls, data: dict) -> "Mogi":
finished_at=data.get("finished_at"),
disconnections=data.get("disconnections", 0),
)


@dataclass
class MogiHistoryData:
started_at: int
finished_at: int
player_ids: list[int]
format: int
subs: int
results: list[int]
disconnections: int
_id: ObjectId = field(default_factory=lambda: ObjectId())

@classmethod
def from_dict(cls, data: dict) -> "MogiHistoryData":
return cls(
_id=ObjectId(data["_id"]),
started_at=data["started_at"],
finished_at=data["finished_at"],
player_ids=data["player_ids"],
format=data["format"],
subs=data["subs"],
results=data["results"],
disconnections=data["disconnections"],
)
1 change: 1 addition & 0 deletions utils/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Exports:
db_players: Collection object
db_archived: Collection object
db_mogis: Collection object
"""

import atexit
Expand Down

0 comments on commit e31c559

Please sign in to comment.