Skip to content

Commit

Permalink
Merge pull request #22 from probablyjassin:enh/general
Browse files Browse the repository at this point in the history
Enh/general
  • Loading branch information
probablyjassin authored Nov 4, 2024
2 parents 500b1c0 + c870367 commit 2042608
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 57 deletions.
1 change: 0 additions & 1 deletion cogs/mogi/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
is_mogi_in_progress,
is_mogi_manager,
)
from utils.command_helpers.confirm import confirmation
from utils.command_helpers.apply_update_roles import update_roles
from utils.command_helpers.wait_for import get_awaited_message

Expand Down
1 change: 0 additions & 1 deletion cogs/mogi/leave_mogi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from discord import slash_command
from discord.utils import get
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
Expand Down
3 changes: 1 addition & 2 deletions cogs/mogi/start.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from discord import SlashCommandGroup, Option
from discord.utils import get
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.command_helpers.btn_factory import create_button_view
from utils.command_helpers.vote_factory import create_button_view
from utils.command_helpers.checks import (
is_mogi_not_in_progress,
is_mogi_manager,
Expand Down
3 changes: 0 additions & 3 deletions cogs/mogi/stop.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from discord import slash_command, Message
from discord.utils import get
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.command_helpers.checks import is_mogi_in_progress

from config import GUILD_IDS


class stop(commands.Cog):
def __init__(self, bot):
Expand Down
7 changes: 7 additions & 0 deletions cogs/mogi/sub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ async def sub(

ctx.mogi.subs.append(replacement_profile)

await (await ctx.guild.fetch_member(player_profile.discord_id)).remove_roles(
ctx.inmogi_role
)
await (await ctx.guild.fetch_member(replacement_profile.discord_id)).add_roles(
ctx.inmogi_role
)

await ctx.respond(
f"<@{player_profile.discord_id}> has been subbed out for <@{replacement_profile.discord_id}>"
)
Expand Down
4 changes: 2 additions & 2 deletions cogs/profiles/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from models.CustomMogiContext import MogiApplicationContext
from utils.data.database import db_players
from utils.maths.ranks import getRankByMMR
from models.RankModel import Rank


class leaderboard(commands.Cog):
Expand Down Expand Up @@ -63,7 +63,7 @@ async def leaderboard(
losses = len([delta for delta in player["history"] if delta < 0])

tabledata["Player"].append(player["name"])
tabledata["Rank"].append(getRankByMMR(player["mmr"]).name)
tabledata["Rank"].append(Rank.getRankByMMR(player["mmr"]).rankname)
tabledata["MMR"].append(player["mmr"])
tabledata["Wins"].append(wins)
tabledata["Losses"].append(losses)
Expand Down
6 changes: 2 additions & 4 deletions cogs/profiles/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from models.CustomMogiContext import MogiApplicationContext
from models.RankModel import Rank
from models.PlayerModel import PlayerProfile

from utils.command_helpers.find_player import search_player
from utils.maths.ranks import getRankByMMR

from datetime import datetime
from bson.int64 import Int64
Expand Down Expand Up @@ -63,8 +61,8 @@ def __init__(self):
value=f"{datetime.fromtimestamp(player.joined).strftime('%b %d %Y')}",
)

player_rank: Rank = getRankByMMR(player.mmr)
embed.add_field(name="Rank", value=f"{player_rank.name}")
player_rank: Rank = Rank.getRankByMMR(player.mmr)
embed.add_field(name="Rank", value=player_rank.rankname)

player_wins = len([delta for delta in player.history if delta >= 0])
player_losses = len([delta for delta in player.history if delta < 0])
Expand Down
6 changes: 5 additions & 1 deletion models/CustomMogiContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@


class MogiApplicationContext(discord.ApplicationContext):
"""## `discord.ApplicationContext` but with the `mogi` attribute:
"""## `discord.ApplicationContext` with custom Lounge attributes:
- `mogi`: `Mogi` object of the channel
- `main_guild`: `discord.Guild` object of the main guild
- `inmogi_role`: `discord.Role` object of the InMogi role
- `get_lounge_role(name: str)`: method to get a role by name
Represents a Discord application command interaction context.
Expand Down
44 changes: 27 additions & 17 deletions models/RankModel.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import math
from dataclasses import dataclass
from enum import Enum
from typing import Union, Tuple


@dataclass
class Rank:
class Rank(Enum):
"""
### A class to represent a rank a player has depending on their MMR.
#### Attributes:
name (str): The name of the rank. Must be one of "Wood", "Bronze", "Silver", "Gold", "Platinum", "Diamond", "Master".
range (tuple): A tuple representing the range of values for the rank.
"""

name: str
range: tuple[int | float, int | float]
WOOD = ("Wood", (-math.inf, 1))
BRONZE = ("Bronze", (2, 1499))
SILVER = ("Silver", (1400, 2999))
GOLD = ("Gold", (3000, 5099))
PLATINUM = ("Platinum", (5100, 6999))
DIAMOND = ("Diamond", (7000, 9499))
MASTER = ("Master", (9500, math.inf))

def __post_init__(self):
valid_names = {
"Wood",
"Bronze",
"Silver",
"Gold",
"Platinum",
"Diamond",
"Master",
}
if self.name not in valid_names:
raise ValueError(
f"Invalid rank name: {self.name}. Must be one of {valid_names}."
)
def __init__(
self, rankname: str, mmrrange: Tuple[Union[int, float], Union[int, float]]
):
self.rankname = rankname
self.mmrrange = mmrrange

def __str__(self):
return self.rankname

@classmethod
def getRankByMMR(cls, mmr: int) -> "Rank":
for rank in cls.__members__.values():
start, end = rank.mmrrange
if start <= mmr <= end:
return rank
return None
8 changes: 4 additions & 4 deletions utils/command_helpers/apply_update_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from models.CustomMogiContext import MogiApplicationContext
from models.MogiModel import Mogi
from utils.maths.ranks import getRankByMMR
from models.RankModel import Rank


async def update_roles(
Expand All @@ -26,14 +26,14 @@ async def update_roles(
await ctx.send(f"Excluded {discord_member.mention} because they subbed")
continue

current_rank = getRankByMMR(player.mmr)
new_rank = getRankByMMR(
current_rank = Rank.getRankByMMR(player.mmr)
new_rank = Rank.getRankByMMR(
player.mmr + mogi.mmr_results_by_group[mogi.players.index(player)]
)

if current_rank != new_rank:

await ctx.send(f"{discord_member.mention} is now in {new_rank.name}")
await ctx.send(f"{discord_member.mention} is now in {new_rank.rankname}")

await discord_member.remove_roles(
ctx.get_lounge_role(f"Lounge - {current_rank}")
Expand Down
File renamed without changes.
12 changes: 9 additions & 3 deletions utils/data/roombrowser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import requests
from typing import Literal
from enum import Enum

from models.RoomModel import Room
from config import YUZU_API_URL, YUZU_SERVER_IP, SERVER_MAIN_PORT, SERVER_LOUNGE_PORT


def get_room_info(server: str = Literal["main", "lounge"]) -> Room:
class ServerType(Enum):
MAIN = "main"
LOUNGE = "lounge"


def get_room_info(server: ServerType) -> Room:

try:
data: dict = (requests.get(f"http://{YUZU_API_URL}/lobby")).json()
servers: list[dict] = data.get("rooms", [])
Expand All @@ -17,7 +23,7 @@ def get_room_info(server: str = Literal["main", "lounge"]) -> Room:
if room["address"] == YUZU_SERVER_IP
and (
room["port"] == SERVER_MAIN_PORT
if server == "main"
if server == ServerType.MAIN
else room["port"] == SERVER_LOUNGE_PORT
)
][0]
Expand Down
19 changes: 0 additions & 19 deletions utils/maths/ranks.py

This file was deleted.

0 comments on commit 2042608

Please sign in to comment.