Added costcodle and Dockerfile

This commit is contained in:
termite 2025-01-22 11:08:57 -08:00
parent 6b5cc61c3f
commit 48f116c041
8 changed files with 114 additions and 4 deletions

21
Dockerfile Normal file
View file

@ -0,0 +1,21 @@
# Use an official Python runtime as the base image
FROM python:3.13-slim
# Set the working directory in the container
WORKDIR /bot
# Copy the current directory contents into the container at /app
COPY . /bot
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
EXPOSE 443
# Define environment variable
ENV NAME DISCORD_TOKEN
# Run app.py when the container launches
CMD ["python", "bot.py"]

1
README.md Normal file
View file

@ -0,0 +1 @@
To use the docker container, just use it, ports 443 and 80 for discord api, that simple

20
bot.py
View file

@ -49,7 +49,8 @@ possible_games = [
"Flagle",
"Genshindle",
"Planespottle",
"Travle"
"Travle",
"Costcodle"
]
chat_limit = 1000
@ -266,6 +267,20 @@ async def stats(
f"Win Rate: {win_rate:.2f}%\n"
f"Perfect Games: {perfects}\n",
ephemeral=False # Send message only to the user who called the command
)
case "costcodle":
total_games = data[1]
wins = data[2]
win_rate = data[3]
total_guesses = data[4]
average_guesses = data[5]
await interaction.followup.send(
f"Costcodle stats for {user.mention}:\n"
f"Total Games Played: {total_games}\n"
f"Average Guesses per Winning Game: {average_guesses:.2f}\n"
f"Win Rate: {win_rate:.2f}%\n",
ephemeral=False # Send message only to the user who called the command
)
case _:
@ -401,7 +416,8 @@ def process_message(message : discord.message, reverse : bool):
gamlog.flagle : r"#Flagle #[0-9]+ \(.*\) ./6",
gamlog.genshindle : r"I (found|couldn't find) today's #Genshindle",
gamlog.planespottle : r"Planespottle #[0-9]+ (failed to guess|in \d/5 guesses)!",
gamlog.travle : r"#travle.* #[0-9]+ (\+([0-9]+)|\(([0-9]+) away\))"
gamlog.travle : r"#travle.* #[0-9]+ (\+([0-9]+)|\(([0-9]+) away\))",
gamlog.costcodle : r"Costcodle #[0-9]+ ./6",
}

9
db.py
View file

@ -95,6 +95,15 @@ def create_table():
GUESS_AVG REAL NOT NULL,
LAST_SYNC INTEGER NOT NULL
); ''',
''' CREATE TABLE IF NOT EXISTS COSTCODLE(
NAME INTEGER PRIMARY KEY NOT NULL,
GAMES INTEGER NOT NULL,
WINS INTEGER NOT NULL,
WIN_RATE REAL NOT NULL,
GUESSES INTEGER NOT NULL,
GUESS_AVG REAL NOT NULL,
LAST_SYNC INTEGER NOT NULL
); ''',
''' CREATE TABLE IF NOT EXISTS GENSHINDLE(
NAME INTEGER PRIMARY KEY NOT NULL,
GAMES INTEGER NOT NULL,

View file

@ -11,4 +11,4 @@ from .planespottle import planespottle
from .wheretaken import wheretaken
from .wheretaken import whentaken
from .travle import travle
from .costcodle import costcodle

63
gamelogic/costcodle.py Normal file
View file

@ -0,0 +1,63 @@
import sys
import time
import discord
import re
import sqlite3
def costcodle(message : discord.message, reverse):
if reverse == True:
reverse = -1
else:
reverse = 1
conn = sqlite3.connect('stats.db')
cursor = conn.cursor()
PATTERN = r"Costcodle #[0-9]+ (.)/6"
match = re.match(PATTERN, message.content.strip())
total_guesses = 0
total_games = 0
wins = 0
sync_time = 0
new_sync_time = (int)(time.mktime(message.created_at.timetuple()))
cursor.execute('''SELECT * FROM COSTCODLE WHERE NAME = ?''', (message.author.id,))
data = cursor.fetchone()
if data:
total_games = data[1]
wins = data[2]
total_guesses = data[4]
sync_time = data[6]
if sync_time > new_sync_time and reverse == 1:
return
guesses = match.group(1)
if guesses != 'X':
total_guesses += int(guesses) * reverse
wins += 1 * reverse
else:
total_guesses += 6
total_games += 1 * reverse
if wins != 0:
average_guesses = total_guesses / wins
else:
average_guesses = 0
win_rate = (wins / total_games) * 100
cursor.execute('''INSERT OR REPLACE INTO COSTCODLE VALUES(?, ?, ?, ?, ?, ?, ?)''',
(message.author.id,total_games,wins,win_rate,total_guesses,average_guesses,new_sync_time))
conn.commit()
conn.close()

View file

@ -50,7 +50,7 @@ def travle(message : discord.message, reverse):
else:
colored_match = re.search(COLORED_PATTERN, message.content.strip())
guesses = int(match.group(3))
guesses += str(colored_match).count('🟥')
guesses += len(str(colored_match)) - str(colored_match).count("🟩")
total_guesses += guesses