From f8d03bdbc332e7aef51b65d9c927e4e17b37615c Mon Sep 17 00:00:00 2001 From: Dan Hess Date: Mon, 12 Apr 2021 15:44:50 -0800 Subject: [PATCH] Randomize output, insert failed cards back, show percentage correct at end --- cogs/japanese.py | 4 +++- utils/flash_card.py | 32 +++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cogs/japanese.py b/cogs/japanese.py index af269ca..2988746 100644 --- a/cogs/japanese.py +++ b/cogs/japanese.py @@ -1,4 +1,6 @@ import json +import random + from discord.ext import commands from utils import ( conjugator, @@ -164,7 +166,7 @@ query ($name: String) { return await ctx.send(f"The JLPT {level} has {len(packs)} packs available") pack = packs[pack - 1] - await FlashCardDisplay(pack).start(ctx, wait=True) + await FlashCardDisplay(random.shuffle(pack.copy())).start(ctx, wait=True) def setup(bot): diff --git a/utils/flash_card.py b/utils/flash_card.py index 51c11a5..7a95758 100644 --- a/utils/flash_card.py +++ b/utils/flash_card.py @@ -1,3 +1,4 @@ +import random import discord from discord.ext import menus @@ -40,10 +41,10 @@ class FlashCardDisplay(menus.Menu): def __init__(self, pack): super().__init__(clear_reactions_after=True) self.pack = pack - self.count = 0 - self.current_card = pack[self.count] - self.amount_of_cards = len(pack) + self.current_card = pack.pop() self.showing_answer = False + self.incorrect = 0 + self.correct = 0 async def send_initial_message(self, ctx, channel): await ctx.send( @@ -54,17 +55,26 @@ class FlashCardDisplay(menus.Menu): embed = self.modify_embed(self.current_card.show_question()) return await self.ctx.send(embed=embed) - def next_card(self): + def next_card(self, reinsert=False): try: - self.count += 1 - self.current_card = self.pack[self.count] + if reinsert: + spot = random.randint(0, round(len(self.pack) * 3 / 4 - 1)) + self.pack.insert(spot, self.current_card) + self.current_card = self.pack.pop() return self.modify_embed(self.current_card.show_question()) except IndexError: - # Probably show results self.stop() + embed = discord.Embed( + description=f"Got {self.correct}/{self.incorrect + self.correct} right. " + f"{self.correct/(self.incorrect + self.correct) * 100:.2f}%" + ) + embed = embed.set_author( + name=self.ctx.author, icon_url=self.ctx.author.avatar_url + ) + return embed def modify_embed(self, embed): - embed = embed.set_footer(text=f"{self.count + 1}/{self.amount_of_cards}") + embed = embed.set_footer(text=f"{len(self.pack) + 1} cards left") embed = embed.set_author( name=self.ctx.author, icon_url=self.ctx.author.avatar_url ) @@ -85,16 +95,16 @@ class FlashCardDisplay(menus.Menu): async def do_failure(self, payload): if not self.showing_answer: return - # Save that they got it wrong + self.incorrect += 1 self.showing_answer = False - embed = self.next_card() + embed = self.next_card(reinsert=True) return await self.message.edit(embed=embed) @menus.button("🟢") async def do_correct(self, payload): if not self.showing_answer: return - # Save that they got it right + self.correct += 1 self.showing_answer = False embed = self.next_card() return await self.message.edit(embed=embed)