Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting to choose all decks from a language #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions database/db_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ def get_deck_chat(self, chat_id):
return self.db["groups"].find_one({"id": chat_id})["deck"]

def get_deck(self, deck_language, deck_name):
return self.cards[deck_language][deck_name]
if deck_name == "all":
# put all decks for a language in one object for special key
return_deck = {}
for deck in get_decks(deck_language):
return_deck.update(deck)
return return_deck
else:
return self.cards[deck_language][deck_name]

def get_deck_languages(self):
return self.cards.keys()
Expand Down Expand Up @@ -209,16 +216,22 @@ def init_shutdown(self):
# reload part

def reload_decks(self):
deck_num = 0
for filename in os.listdir('./decks'):
if filename.endswith(".json"):
temp = json.load(open('./decks/' + filename))
language = temp["language"]
name = temp["name"]
[temp.pop(key) for key in ["name", "language"]]
temp_deck = {}
# pack cards in an additional id key
for card in temp:
temp_deck[deck_num] = temp
deck_num += 1
if language in self.cards:
self.cards[language][name] = temp
self.cards[language][name] = temp_deck
else:
self.cards[language] = {name: temp}
self.cards[language] = {name: temp_deck}


database = Database()
5 changes: 4 additions & 1 deletion objects/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def word_list(words):
class Deck:
def __init__(self, deck_language, deck_name):
deck = database.database.get_deck(deck_language, deck_name)
self.topic = random.choice(list(deck))
choosen_deck = random.choice(list(deck))
# get the content of the dict since it is packed in an additional id key
# it only contains one key value pair
self.topic = choosen_deck[choosen_deck.keys()[0]]
self.words = deck[self.topic]
self.word_list = word_list(self.words)
self.secret = random.choice(deck[self.topic])
2 changes: 2 additions & 0 deletions utils/specific_helpers/group_settings_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def deck_languages_buttons(languages, chat_id):

def deck_buttons(deck, chat_id):
buttons = []
# add an "all" button. no pack should be called "all" now
buttons.append(InlineKeyboardButton("all", callback_data=f"1deck_{str(chat_id)}_all"))
for deck_name in deck:
buttons.append(InlineKeyboardButton(deck_name, callback_data=f"1deck_{str(chat_id)}_{deck_name}"))
return build_menu(buttons, 3)