BirdQuizz/game.py

34 lines
1.3 KiB
Python
Raw Normal View History

2022-05-26 10:27:26 +02:00
import random
import os
from glob import glob
2022-05-26 16:14:55 +02:00
import json
2022-05-26 10:27:26 +02:00
2022-05-26 16:14:55 +02:00
with open("./data/level_species_cleaned.json", "r") as f:
LEVEL_SPECIES_LIST = json.load(f)
2022-05-26 10:27:26 +02:00
format_name = lambda folder_name : folder_name.replace('_', ' ')
2022-05-26 16:14:55 +02:00
to_folder_name = lambda species_name : species_name.replace(' ', '_').replace('\'', '').lower()
2022-05-26 10:27:26 +02:00
2022-05-26 16:14:55 +02:00
def get_proposals(question_species_name, available_species, n):
2022-05-26 10:27:26 +02:00
proposals = [question_species_name]
for i in range(n):
2022-05-26 16:14:55 +02:00
proposition = random.choice(available_species)
2022-05-26 10:27:26 +02:00
while proposition == question_species_name:
2022-05-26 16:14:55 +02:00
proposition = random.choice(available_species)
2022-05-26 10:27:26 +02:00
proposals.append(proposition)
random.shuffle(proposals)
return proposals
2022-05-26 16:14:55 +02:00
def new_question(level):
available_species = LEVEL_SPECIES_LIST[level]
question_species_name = random.choice(available_species)
question_species_folder = to_folder_name(question_species_name)
2022-05-26 10:27:26 +02:00
question = {}
audio_paths = list(map(os.path.basename, glob(f"static/data/src_audio/{question_species_folder}/*.mp3")))
audio_path = random.choice(audio_paths)
question["species"] = question_species_name
question["species_folder"] = question_species_folder
question["audio_path"] = audio_path
2022-05-26 16:14:55 +02:00
question["proposals"] = get_proposals(question_species_name, available_species, 5)
2022-05-26 10:27:26 +02:00
return question