chefle-bot/gamelogic/satle.py

62 lines
1.4 KiB
Python

import sys
import time
import discord
import re
import sqlite3
def satle(message : discord.message, reverse):
if reverse == True:
reverse = -1
else:
reverse = 1
conn = sqlite3.connect('stats.db')
cursor = conn.cursor()
PATTERN = r"🛰Satle #[0-9]+ (\d)/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 SATLE 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
lines = message.content.splitlines()
match = re.match(PATTERN, lines[0].strip())
guesses = match.group(1)
total_guesses += int(guesses) * reverse
if lines[1].__contains__("🟩"):
wins += 1 * reverse
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 SATLE VALUES(?, ?, ?, ?, ?, ?, ?)''',
(message.author.id,total_games,wins,win_rate,total_guesses,average_guesses,new_sync_time))
conn.commit()
conn.close()