chefle-bot/gamelogic/globle.py

61 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import time
import discord
import re
import sqlite3
def globle(message : discord.message, reverse, bad = False):
if reverse == True:
reverse = -1
else:
reverse = 1
conn = sqlite3.connect('stats.db')
cursor = conn.cursor()
if bad:
PATTERN = r".* = ([0-9]+)"
else:
PATTERN = r"I guessed todays Globle in ([0-9]+) tries:"
match = re.search(PATTERN, message.content.strip())
if not match:
print("AAAAA")
print(message.content)
return
total_guesses = 0
total_games = 0
sync_time = 0
new_sync_time = (int)(time.mktime(message.created_at.timetuple()))
cursor.execute('''SELECT * FROM GLOBLE WHERE NAME = ?''', (message.author.id,))
data = cursor.fetchone()
if data:
total_games = data[1]
total_guesses = data[2]
sync_time = data[4]
if sync_time > new_sync_time and reverse == 1:
return
guesses = match.group(1)
total_guesses += int(guesses) * reverse
total_games += 1 * reverse
average_guesses = total_guesses / total_games
cursor.execute('''INSERT OR REPLACE INTO GLOBLE VALUES(?, ?, ?, ?, ?)''',
(message.author.id,total_games,total_guesses,average_guesses,new_sync_time))
conn.commit()
conn.close()
def bad_globle(message : discord.message, reverse):
globle(message, reverse, True)