Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37b4d2d62e | ||
|
|
9c762fb346 | ||
|
|
2c9e9e246a | ||
|
|
ab721c1625 | ||
|
|
92511db446 | ||
|
|
44c2720f80 | ||
|
|
35cebdebce | ||
|
|
e4437cc654 | ||
|
|
fe2c400a2a | ||
|
|
39875585ef | ||
|
|
7796ead644 | ||
|
|
531d2f3a0e | ||
|
|
8595a12f7f | ||
|
|
f76171ac84 | ||
|
|
1605c8ad50 | ||
|
|
63e77958fb | ||
|
|
0bae0ea5ff | ||
|
|
c67e9ae617 | ||
|
|
7e23ac14ff | ||
|
|
a0a7e37921 | ||
|
|
c01c888503 | ||
|
|
3cf4fc6b33 | ||
|
|
3627419e12 | ||
|
|
3446ce9ca2 | ||
|
|
2db744d170 | ||
|
|
fa33be1b4a |
+21
@@ -0,0 +1,21 @@
|
||||
FROM python:3.8.10-slim
|
||||
|
||||
# install dependencies for mysqlclient
|
||||
# RUN apt-get update && apt-get -y upgrade && apt-get -y install python3-dev default-libmysqlclient-dev build-essential
|
||||
|
||||
RUN pip install --upgrade pip
|
||||
|
||||
WORKDIR /home
|
||||
|
||||
COPY classes classes/
|
||||
COPY cogs cogs/
|
||||
COPY utilities utilities/
|
||||
COPY highscores.py highscores.py
|
||||
COPY requirements.txt requirements.txt
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
# remove build deps that aren't needed to run
|
||||
# RUN apt-get -y remove python3-dev build-essential && apt-get -y autoremove
|
||||
|
||||
CMD ["python3", "./highscores.py"]
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
# this file is intended to build the docker image and push to the correct repository. Intended to be run by my buildbot instance.
|
||||
|
||||
# build, tag, and push develop
|
||||
docker build -t viker.cheezers.net:5000/agon:develop .
|
||||
docker push viker.cheezers.net:5000/agon:develop
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
# this file is intended to build the docker image and push to the correct repository. Intended to be run by my buildbot instance.
|
||||
|
||||
# check if there is a git tag. if so, tag with that tag and release
|
||||
# expecting tag to be 6 characters long. otherwise isn't not the right tag
|
||||
GITTAG=$(git describe --tags)
|
||||
length=${#GITTAG}
|
||||
|
||||
if [ $length -eq 6 ]; then
|
||||
docker build -t viker.cheezers.net:5000/agon:release -t viker.cheezers.net:5000/agon:$GITTAG .
|
||||
docker push viker.cheezers.net:5000/agon:release iker.cheezers.net:5000/agon:$GITTAG
|
||||
else
|
||||
docker build -t viker.cheezers.net:5000/agon:release .
|
||||
docker push viker.cheezers.net:5000/agon:release
|
||||
fi
|
||||
@@ -6,9 +6,11 @@ from nextcord.ext import commands
|
||||
|
||||
from .create import CreateCog
|
||||
from .delete import DeleteCog
|
||||
from .help import HelpCog
|
||||
from .refresh import RefreshCog
|
||||
from .set import SetCog
|
||||
from .update import UpdateCog
|
||||
from .view import ViewCog
|
||||
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
@@ -19,6 +21,8 @@ def setup(bot: commands.Bot):
|
||||
"""
|
||||
bot.add_cog(CreateCog(bot))
|
||||
bot.add_cog(DeleteCog())
|
||||
bot.add_cog(HelpCog())
|
||||
bot.add_cog(RefreshCog(bot))
|
||||
bot.add_cog(SetCog())
|
||||
bot.add_cog(UpdateCog(bot))
|
||||
bot.add_cog(ViewCog())
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Commands for helping a user.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
from nextcord.ext import commands
|
||||
|
||||
|
||||
class HelpCog(commands.Cog, name="Help commands."):
|
||||
"""
|
||||
Class holds commands for getting help.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for HelpCog class. Does nothing.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command()
|
||||
async def help(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for help. Never called.
|
||||
"""
|
||||
|
||||
@help.subcommand(description="Describes how to get started with Agon.")
|
||||
async def setup(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Sends a message describing how to get started with Agon.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
"""
|
||||
|
||||
# I hate this but don't want indents in the string.
|
||||
# pylint: disable=line-too-long
|
||||
help_str: str = """Welcome to Agon! Three simple steps are needed to get started.
|
||||
1.) Set a channel to recieve submission verifications with "/set verify_channel."
|
||||
This channel is used to allow moderators to check if a submission is real/valid.
|
||||
|
||||
2.) Set a channel for submissions to be posted in with "/set submission_channel."
|
||||
This channel will be where all user submission messages are sent.
|
||||
|
||||
3.) Set up your highscore tables! Use "/create group" to first create a group message in the channel you would like and then use "/create category" to create a category in that group message!"""
|
||||
# pylint: enable=line-too-long
|
||||
print(help_str)
|
||||
|
||||
await interaction.response.send_message(content=help_str)
|
||||
@@ -48,7 +48,7 @@ class SetCog(commands.Cog, name="Set command."):
|
||||
Command function to register a CategoryGroup's output channel.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to register channel to. Is SlashOption.
|
||||
channel (nextcord.abc.GuildChannel, optional): Channel to register
|
||||
Is a SlashOption. Limited to text channels, Defaults to None.
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
View command heirarchy.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
import pandas as pd
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_data_classes import Category, CategoryGroup, Score, engine
|
||||
from utilities.utils import MAX_LINE_LENGTH, autofill_categories, autofill_groups, format_score_string
|
||||
|
||||
|
||||
class ViewCog(commands.Cog, name="View Command"):
|
||||
"""
|
||||
Class holds commands for viewing data.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Default constructor for ViewCog class. Does nothing.
|
||||
"""
|
||||
|
||||
@nextcord.slash_command()
|
||||
async def view(self, interaction: nextcord.Interaction):
|
||||
"""
|
||||
Parent slash command for view. Never called.
|
||||
"""
|
||||
|
||||
@view.subcommand(description="Lists all scores for a given category.")
|
||||
async def leaderboard(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
group_id: str = nextcord.SlashOption(
|
||||
name="group", description="Group to show leaderboard from.", required=True
|
||||
),
|
||||
category_id: str = nextcord.SlashOption(
|
||||
name="category", description="Category to show leaderboard.", required=True
|
||||
),
|
||||
):
|
||||
"""
|
||||
Gathers all users that have a submission in the group/category combo and returns a message with them
|
||||
in ranked order.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String representation of Group id to search from. Is SlashOption.
|
||||
category_id (str): String representation of Category id to show leaderboard. Is SlashOption.
|
||||
"""
|
||||
str_list: "list[str]" = []
|
||||
|
||||
# get group, category, and all scores sorted correctly.
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
|
||||
stmt = select(Category).where(Category.CategoryId == int(category_id))
|
||||
category = session.scalar(stmt)
|
||||
|
||||
d_f = pd.read_sql_query(
|
||||
sql=session.query(Score).where(Score.CategoryId == category_id).statement, con=engine
|
||||
)
|
||||
|
||||
scores = d_f.sort_values("Score", ascending=category.IsAscending).reset_index()
|
||||
|
||||
# get the longest string
|
||||
longest_str: int = scores.UserName.map(len).max()
|
||||
|
||||
for i, row in scores.iterrows():
|
||||
str_list.append(
|
||||
format_score_string(
|
||||
row["UserName"], row["Score"], longest_str, category.IsTimeRecord, i + 1, MAX_LINE_LENGTH
|
||||
)
|
||||
)
|
||||
|
||||
message_str: str = f"**{group.GroupName} {category.CategoryName} Leaderboard!**\n```"
|
||||
if len(str_list) == 0:
|
||||
message_str += "No Submissions!"
|
||||
else:
|
||||
for line in str_list:
|
||||
message_str += f"{line}\n"
|
||||
|
||||
message_str += "```"
|
||||
|
||||
await interaction.response.send_message(content=message_str)
|
||||
|
||||
@leaderboard.on_autocomplete("group_id")
|
||||
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
|
||||
"""
|
||||
Autocomplete function for table names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
group_id (str): String to be autocompleted with Group name.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))
|
||||
|
||||
@leaderboard.on_autocomplete("category_id")
|
||||
async def category_autocomplete(self, interaction: nextcord.Interaction, category_id: str, group_id: str):
|
||||
"""
|
||||
Autocomplete function for category names.
|
||||
|
||||
Args:
|
||||
interaction (nextcord.Interaction): Interaction that invoked the command.
|
||||
category_id (str): String to be autocompleted with Category name.
|
||||
group_id (str): Group to find Category from.
|
||||
"""
|
||||
await interaction.response.send_autocomplete(
|
||||
autofill_categories(interaction.guild_id, int(group_id), category_id)
|
||||
)
|
||||
@@ -19,7 +19,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_guild_join(self, guild: nextcord.Guild):
|
||||
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
|
||||
"""Listener for Guild Join. Adds guild to database."""
|
||||
|
||||
new_guild = Guild(GuildId=guild.id)
|
||||
|
||||
|
||||
+14
-1
@@ -2,7 +2,7 @@ version: "3"
|
||||
services:
|
||||
sqldb:
|
||||
restart: unless-stopped
|
||||
image: "highscores_db"
|
||||
image: mysql:latest
|
||||
container_name: "Highscores-DataBase"
|
||||
volumes:
|
||||
- highscores_data:/var/lib/mysql
|
||||
@@ -12,5 +12,18 @@ services:
|
||||
- MYSQL_ROOT_PASSWORD=${SQL_PASS}
|
||||
- MYSQL_ROOT_USERNAME=${SQL_USER}
|
||||
|
||||
bot:
|
||||
restart: unless-stopped
|
||||
build: .
|
||||
container_name: "Highscores-Bot"
|
||||
depends_on:
|
||||
- sqldb
|
||||
environment:
|
||||
- DISCORD_TOKEN=${DISCORD_TOKEN}
|
||||
- SQL_DB=${SQL_DB}
|
||||
- SQL_HOST=${SQL_HOST}
|
||||
- SQL_PASS=${SQL_PASS}
|
||||
- SQL_USER=${SQL_USER}
|
||||
|
||||
volumes:
|
||||
highscores_data:
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
FROM mysql:latest
|
||||
|
||||
COPY highscores_database.sql /docker-entrypoint-initdb.d/
|
||||
@@ -17,7 +17,7 @@ CREATE TABLE CategoryGroups(
|
||||
PadLength smallint not null DEFAULT 26,
|
||||
UsernameLength smallint DEFAULT 12,
|
||||
CONSTRAINT UniqueGuildCategoryGroup UNIQUE (GuildId, GroupName),
|
||||
FOREIGN KEY (GuildId) REFERENCES Guilds(GuildId),
|
||||
FOREIGN KEY (GuildId) REFERENCES Guilds(GuildId) ON DELETE CASCADE,
|
||||
PRIMARY KEY (GroupId)
|
||||
);
|
||||
|
||||
@@ -30,7 +30,7 @@ CREATE TABLE Categories(
|
||||
IsTimeRecord boolean not null DEFAULT false,
|
||||
IsValidated boolean not null DEFAULT true,
|
||||
CONSTRAINT UniqueCategoryInGroup UNIQUE (GroupId, CategoryName),
|
||||
FOREIGN KEY (GroupId) REFERENCES CategoryGroups(GroupId),
|
||||
FOREIGN KEY (GroupId) REFERENCES CategoryGroups(GroupId) ON DELETE CASCADE,
|
||||
PRIMARY Key (CategoryId)
|
||||
);
|
||||
|
||||
@@ -38,6 +38,6 @@ CREATE TABLE Scores(
|
||||
UserName varchar(30) not null,
|
||||
Score float not null, -- Maybe should have time and int scores?? timestamp type?
|
||||
CategoryId int unsigned not null,
|
||||
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId),
|
||||
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId) ON DELETE CASCADE,
|
||||
PRIMARY KEY (CategoryId, UserName)
|
||||
);
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
nextcord>=2.3.2
|
||||
python-dotenv>=0.20.0
|
||||
pandas>=1.5.2
|
||||
sqlalchemy>=1.4.44
|
||||
mysqlclient>-2.1.1
|
||||
sqlalchemy<2.0
|
||||
pymysql>=1.0.2
|
||||
cryptography>=2.8
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy.ext.automap import automap_base
|
||||
from utilities import config
|
||||
|
||||
# connect with sql alchemy and autoload schema
|
||||
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
url = f"mysql+pymysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||
engine = create_engine(url)
|
||||
Base = automap_base()
|
||||
Base.prepare(autoload_with=engine)
|
||||
|
||||
+37
-14
@@ -231,20 +231,12 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
|
||||
# add each score to list
|
||||
for ind in range(max_cat_size): # need to subtract to deal with category name string
|
||||
if ind in processed_group.index:
|
||||
user_length = group.UsernameLength
|
||||
username = processed_group["UserName"][ind][:user_length].ljust(user_length, " ")
|
||||
username = processed_group["UserName"][ind]
|
||||
score = processed_group["Score"][ind]
|
||||
|
||||
# get score, pad to correct length
|
||||
if category.IsTimeRecord:
|
||||
# MM:SS.ms is 7 characters long
|
||||
# if there are single digit minutes, add a space for formatting.
|
||||
score = get_time_str(processed_group["Score"][ind])
|
||||
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
||||
score = " " + score
|
||||
else:
|
||||
score = str(int(processed_group["Score"][ind])).rjust(7, " ")
|
||||
|
||||
str_list[ind + 1] += f"{ind + 1}: {username} - {score}".ljust(pad_length, " ")
|
||||
str_list[ind + 1] += format_score_string(
|
||||
username, score, group.UsernameLength, category.IsTimeRecord, ind + 1, pad_length
|
||||
)
|
||||
else:
|
||||
# if not enough entries, pad out
|
||||
if ind + 1 <= category.Size:
|
||||
@@ -262,7 +254,6 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
|
||||
# need to use index to allow for modifying the list
|
||||
for ind, line in enumerate(str_list):
|
||||
if len(line) > MAX_LINE_LENGTH:
|
||||
|
||||
split_loc = int(MAX_LINE_LENGTH / pad_length) * pad_length
|
||||
new_line = line[split_loc:]
|
||||
str_list[ind] = line[:split_loc]
|
||||
@@ -283,6 +274,38 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
|
||||
return ret_string + "```"
|
||||
|
||||
|
||||
def format_score_string(
|
||||
username: str, score: float, username_length: int, is_time_record: bool, rank: int, pad_length: int
|
||||
) -> str:
|
||||
"""
|
||||
Builds the string to show a user's score.
|
||||
|
||||
Args:
|
||||
username (str): username.
|
||||
score (int): score in int form.
|
||||
username_length (float): limit to how long username is allowed to be.
|
||||
is_time_record (bool): if the score represents an number or a time record.
|
||||
rank (int): the user's rank.
|
||||
pad_length (int): limit to how long the string can be.
|
||||
|
||||
Returns:
|
||||
str: string to show user's score.
|
||||
"""
|
||||
username = username[:username_length].ljust(username_length, " ")
|
||||
|
||||
# get score, pad to correct length
|
||||
if is_time_record:
|
||||
# MM:SS.ms is 7 characters long
|
||||
# if there are single digit minutes, add a space for formatting.
|
||||
score = get_time_str(score)
|
||||
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
||||
score = " " + score
|
||||
else:
|
||||
score = str(int(score)).rjust(7, " ")
|
||||
|
||||
return f"{rank}: {username} - {score}".ljust(pad_length, " ")
|
||||
|
||||
|
||||
def get_time_str(time_in: float) -> str:
|
||||
"""
|
||||
Formats a float value into the desired string format for a timestamp.
|
||||
|
||||
Reference in New Issue
Block a user