summary refs log tree commit diff stats
path: root/components
diff options
context:
space:
mode:
authorsuhas <hi@suhas.one>2023-11-16 23:00:29 -0600
committersuhas <hi@suhas.one>2023-11-16 23:00:29 -0600
commit2e38b5aefb08abdd34d5244d0998274e5f35ff3e (patch)
treea221637a7eaa9195c8ae5322d8c73c1ad70e90a2 /components
parente0ede3dddabf27cde5a1db35e499885ece51f542 (diff)
downloadqbb-2e38b5aefb08abdd34d5244d0998274e5f35ff3e.tar.gz
add stats tracking
Diffstat (limited to 'components')
-rw-r--r--components/AnswerModal.py56
-rw-r--r--components/TossupButtons.py14
-rw-r--r--components/__init__.py0
-rw-r--r--components/__pycache__/AnswerModal.cpython-311.pycbin7397 -> 10282 bytes
-rw-r--r--components/__pycache__/TossupButtons.cpython-311.pycbin8765 -> 9411 bytes
-rw-r--r--components/__pycache__/__init__.cpython-311.pycbin0 -> 153 bytes
6 files changed, 62 insertions, 8 deletions
diff --git a/components/AnswerModal.py b/components/AnswerModal.py
index 9bd5f17..9d7b1cb 100644
--- a/components/AnswerModal.py
+++ b/components/AnswerModal.py
@@ -1,18 +1,28 @@
-from discord.ui import Modal, TextInput, View
+from __future__ import annotations
+from discord.ui import Modal, TextInput
 from httpx import AsyncClient
 from discord import Interaction, Color
+from prisma import Prisma
+from prisma.models import User 
+from typing import TYPE_CHECKING, Literal
+if TYPE_CHECKING:
+    from .TossupButtons import TossupButtons, SoloTossupButtons
+from discord import Button
+from common.types import category_field_translations
 
 
 class Answer(Modal, title="Submit Answer"):
-    def __init__(self, correct_answer: str, view: View) -> None:
+    def __init__(self, correct_answer: str, view: TossupButtons) -> None:
         self.correct_answer = correct_answer
         self.view = view
+        self.stop_working = False
         super().__init__()
 
     answer = TextInput(label="Answer", placeholder="Your answer here")
 
     async def on_submit(self, interaction: Interaction) -> None:
         c = AsyncClient()
+        db = Prisma(auto_register=True)
         answer_check_resp = await c.get(
             "https://qbreader.org/api/check-answer",
             params={
@@ -21,8 +31,13 @@ class Answer(Modal, title="Submit Answer"):
             },
         )
         answer_check_data = answer_check_resp.json()
+        await db.connect()
 
         if answer_check_data["directive"] == "accept":
+            if self.stop_working:
+                return await interaction.response.send_message('Someone has already answered this correctly!', ephemeral=True)
+            if interaction.message is None:
+                return 
             e = interaction.message.embeds[0]
             e.color = Color.green()
             e.title = '[CORRECT!] Random Tossup'
@@ -32,17 +47,40 @@ class Answer(Modal, title="Submit Answer"):
             e.add_field(name='Answered by', value=interaction.user.mention)
             items = self.view.children
             for item in items:
-                item.disabled = True
+                if isinstance(item, Button):
+                    item.disabled = True
+            self.stop_working = True
             await interaction.response.edit_message(embed=e, view=self.view)
+            await User.prisma().upsert(where={'id': interaction.user.id}, data={
+                'create': {'questions_correct': 1, 'id': interaction.user.id, 'questions_incorrect': 0},
+                'update': {'questions_correct': {'increment': 1}}
+            })
+            category = category_field_translations[self.view.tossup['category']]
+            await db.execute_raw(f"""
+                INSERT INTO CategoryBreakdown(userId, {category}_correct) VALUES (?, 0)
+                    ON CONFLICT(userId) DO UPDATE SET {category}_correct={category}_correct+1
+            """, interaction.user.id)
         elif answer_check_data['directive'] == 'prompt':
             await interaction.response.send_message("Prompt! Try answering the question again", ephemeral=True)
         else:
             await interaction.response.send_message(f"Incorrect! You've been locked out from the question. The correct answer was {self.view.tossup['answer']}", ephemeral=True)
             self.view.already_answered.append(interaction.user.id)
+            await User.prisma().upsert(
+                where={'id': interaction.user.id},
+                data={
+                    'create': {'questions_correct': 0, 'id': interaction.user.id, 'questions_incorrect': 1},
+                    'update': {'questions_incorrect': {'increment': 1}}
+                }
+            )
+            category = category_field_translations[self.view.tossup['category']]
+            await db.execute_raw(f"""
+                INSERT INTO CategoryBreakdown(userId, {category}_incorrect) VALUES (?, 0)
+                    ON CONFLICT(userId) DO UPDATE SET {category}_incorrect={category}_incorrect+1
+            """, interaction.user.id)
         await c.aclose()
 
 class SoloAnswer(Modal, title="Submit Answer"):
-    def __init__(self, correct_answer: str, view: View) -> None:
+    def __init__(self, correct_answer: str, view: SoloTossupButtons) -> None:
         self.correct_answer = correct_answer
         self.view = view
         super().__init__()
@@ -61,6 +99,8 @@ class SoloAnswer(Modal, title="Submit Answer"):
         answer_check_data = answer_check_resp.json()
 
         if answer_check_data["directive"] == "accept":
+            if interaction.message is None:
+                return
             e = interaction.message.embeds[0]
             e.color = Color.green()
             e.title = '[CORRECT!] Random Tossup'
@@ -69,9 +109,12 @@ class SoloAnswer(Modal, title="Submit Answer"):
             e.add_field(name='Official answer', value=self.view.tossup['answer'])
             items = self.view.children
             for item in items:
-                item.disabled = True
+                if isinstance(item, Button):
+                    item.disabled = True
             await interaction.response.edit_message(embed=e, view=self.view)
         else:
+            if interaction.message is None:
+                return
             e = interaction.message.embeds[0]
             e.color = Color.red()
             e.title = '[INCORRECT] Random Tossup'
@@ -80,6 +123,7 @@ class SoloAnswer(Modal, title="Submit Answer"):
             e.add_field(name='Your answer', value=self.answer.value)
             items = self.view.children
             for item in items:
-                item.disabled = True
+                if isinstance(item, Button):
+                    item.disabled = True
             await interaction.response.edit_message(embed=e, view=self.view)
         await c.aclose()
diff --git a/components/TossupButtons.py b/components/TossupButtons.py
index 7238cac..73edd21 100644
--- a/components/TossupButtons.py
+++ b/components/TossupButtons.py
@@ -29,6 +29,8 @@ class TossupButtons(View):
             return await interaction.response.send_message(
                 "you've already answered!", ephemeral=True
             )
+        if interaction.message is None:
+            return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
         e = interaction.message.embeds[0]
         self.i += 1
         if self.i == len(self.tossup["sentences"]):
@@ -46,13 +48,16 @@ class TossupButtons(View):
         self.final_answer_votes += 1
         button.label = f"vote to reveal answer ({self.final_answer_votes}/3)"
         if self.final_answer_votes >= 3:
+            if interaction.message is None:
+                return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
             e = interaction.message.embeds[0]
             e.title = '[SKIPPED] Random Tossup'
             e.color = Color.orange()
             e.description = self.tossup["question"]
             e.add_field(name="Answer", value=self.tossup["answer"])
             for item in self.children:
-                item.disabled = True
+                if isinstance(item, Button):
+                    item.disabled = True
             return await interaction.response.edit_message(embed=e, view=self)
         await interaction.response.edit_message(view=self)
         await interaction.followup.send("you've voted!", ephemeral=True)
@@ -76,6 +81,8 @@ class SoloTossupButtons(View):
             return await interaction.response.send_message(
                 "not your tossup!", ephemeral=True
             )
+        if interaction.message is None:
+            return interaction.response.send_message("Couldn't find the message!", ephemeral=True)
         e = interaction.message.embeds[0]
         self.i += 1
         if self.i == len(self.tossup["sentences"]) - 1:
@@ -89,11 +96,14 @@ class SoloTossupButtons(View):
             return await interaction.response.send_message(
                 "not your tossup!", ephemeral=True
             )
+        if interaction.message is None:
+            return await interaction.response.send_message('Error!')
         e = interaction.message.embeds[0]
         e.title = f'[SKIPPED] Random Tossup'
         e.color = Color.orange()
         e.description = self.tossup["question"]
         e.add_field(name="Answer", value=self.tossup["answer"])
         for item in self.children:
-            item.disabled = True
+            if isinstance(item, Button):
+                item.disabled = True
         return await interaction.response.edit_message(embed=e, view=self)
diff --git a/components/__init__.py b/components/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/components/__init__.py
diff --git a/components/__pycache__/AnswerModal.cpython-311.pyc b/components/__pycache__/AnswerModal.cpython-311.pyc
index 0ca3cb1..8b217c5 100644
--- a/components/__pycache__/AnswerModal.cpython-311.pyc
+++ b/components/__pycache__/AnswerModal.cpython-311.pyc
Binary files differdiff --git a/components/__pycache__/TossupButtons.cpython-311.pyc b/components/__pycache__/TossupButtons.cpython-311.pyc
index fc443d0..9281a92 100644
--- a/components/__pycache__/TossupButtons.cpython-311.pyc
+++ b/components/__pycache__/TossupButtons.cpython-311.pyc
Binary files differdiff --git a/components/__pycache__/__init__.cpython-311.pyc b/components/__pycache__/__init__.cpython-311.pyc
new file mode 100644
index 0000000..b9cce83
--- /dev/null
+++ b/components/__pycache__/__init__.cpython-311.pyc
Binary files differ