Riddle

mail@pastecode.io avatar
unknown
python
a month ago
3.7 kB
2
Indexable
Never
RIDDLES_TOTAL = 15
MAX_PASS = 3
MAX_ATT = 3
TIMEOUT = 10
THRESHOLD = 75
PASSED = 0

CORRECT_MSG = "✅ Bonne réponse ! Prochaine devinette :"
INCORRECT_MSG = "❌ Mauvaise réponse. Réessayez ! Tentatives restantes : `{remaining_attempts}`"

PASS_MSG = "🔄 Devinette passée. Suivante :"
PASS_WARN_MSG = "⚠️ Plus de passes disponibles ou c'est la dernière devinette."

END_TIMEOUT_MSG = f"⏰ Temps écoulé, {user.mention}. **GAME OVER.**"
END_MAX_ATT_MSG = f"❌ Limite de tentatives atteinte (`{MAX_ATT}`). **GAME OVER.**"
END_CONGRATS_MSG = "# 🎉 Bravo ! Vous avez terminé le jeu !"

final_response = END_CONGRATS_MSG
CURRENT_PREFIX = ""

db = load_database("clz8durud000kdvyvjsrhhuze")
riddles = db.find(where={"theme": theme})
riddles = pyrandom.sample(riddles, RIDDLES_TOTAL) if len(riddles) >= RIDDLES_TOTAL else riddles

RIDDLE_TEMPLATE = (
    "{prefix}\n\n"
    "❔ **{question}**\n"
    f"Theme: **{theme}** - "
    "Questions Remaining: **{remaining}** - "
    "Passes Utilized: **{passes_utilises}** "
    f"out of **`{MAX_PASS}`**"
)

async def is_correct(riddle, user_input):
    similarity = 0

    def checker(correct_ans, index):
        nonlocal similarity
        result = fuzz.ratio(user_input, correct_ans.lower())
        similarity = max(similarity, result)

    loop(riddle["answers"], checker)
    return similarity >= THRESHOLD

async def handle_response(i, index, riddle, riddle_index):
    def checker(event):
        return event.author_id == user.id
    
    event = await wait_for("message", timeout=10, check=checker)

    if not event:
        return break_loop("TIMED_OUT")
    
    delete_message(event.channel_id, event.message.id)

    if event.message.content in ["pass", "p"]:
        return break_loop("PASSED")
    
    if await is_correct(riddle, event.message.content):
        return break_loop("CORRECT")
    
    respond_interaction(
        interaction, 
        content=RIDDLE_TEMPLATE.format(
            prefix=INCORRECT_MSG.format(remaining_attempts=MAX_ATT - (index + 1)),
            question=riddle["question"],
            remaining=len(riddles) - (riddle_index + 1),
            passes_utilises=PASSED,
        )
    )


async def send_riddle(riddle, index, prefix = ""):
    global PASSED, CURRENT_PREFIX
    respond_interaction(
        interaction, 
        content=RIDDLE_TEMPLATE.format(
            prefix=CURRENT_PREFIX,
            question=riddle["question"],
            remaining=len(riddles) - (index + 1),
            passes_utilises=PASSED,
        )
    )

    data = loop(3, handle_response, riddle=riddle, riddle_index=index)
    if data == "PASSED":
        PASSED += 1
    
    if data in ["TIMED_OUT", "PASSED", "CORRECT"]:
        return data

    # gave wrong answer 3 times
    return "INCORRECT"

async def handle_game(riddle, index):
    global final_response, CURRENT_PREFIX

    result = await send_riddle(riddle, index, prefix=CURRENT_PREFIX)

    if result == "TIMED_OUT":
        final_response = END_TIMEOUT_MSG
        return break_loop()
    
    elif result == "CORRECT":
        CURRENT_PREFIX = CORRECT_MSG
        return
    
    elif result == "PASSED":
        CURRENT_PREFIX = PASS_MSG
    
        if PASSED == MAX_PASS - 1:
            CURRENT_PREFIX = PASS_WARN_MSG
            return
        elif PASSED == MAX_PASS:
            final_response = END_MAX_ATT_MSG
            return break_loop()

        return
    
    elif result == "INCORRECT":
        final_response = INCORRECT_MSG
        return break_loop()
        # you might wanted to format this message

loop(riddles, handle_game)
Leave a Comment