Untitled
unknown
python
a year ago
6.0 kB
10
Indexable
if server_var("devinette") == "True":
set_error(f"❌ {user.mention}, A game is already in progress. Please wait until the current game is finished.")
else:
update_server_var("devinette", "True")
riddles = [
{
"question": "I am the most famous Italian plumber in video game history, often seen with a red hat. Who am I?",
"answers": ["mario"]
},
{
"question": "I am a life simulation game developed by Maxis, where players can create and control virtual characters. What is my name?",
"answers": ["the sims", "sims", "les sims"]
},
{
"question": "I am a video game franchise developed by Mojang, where players can build and explore blocky worlds. What is my name?",
"answers": ["minecraft"]
},
{
"question": "I am a kart racing game where players can choose from iconic Nintendo characters. What is my name?",
"answers": ["mario kart"]
},
]
MAX_PASS = 3
MAX_ATTEMPTS = 3
RESPONSE_TIMEOUT = 65
CORRECT_ANSWER_PREFIX = "✅ Correct answer! Here is the next one:"
INCORRECT_ANSWER_MSG = "❌ Incorrect answer. Try again! Attempts remaining: `{}`"
PASS_ANSWER_PREFIX = "🔄 You have skipped this riddle. Here is the next one:"
LAST_RIDDLE_PASS_WARNING = "⚠️ You can no longer skip; this is the last riddle. You must answer it."
END_GAME_TIMEOUT_MSG = "⏰ Time is up, {user}. **Game over.**"
END_GAME_MAX_PASS_MSG = f"❌ You have reached the maximum number of passes (`{MAX_PASS}`). Game over."
END_GAME_MAX_ATTEMPTS_MSG = f"❌ You have reached the maximum number of attempts (`{MAX_ATTEMPTS}`). **Game over.**"
CONGRATULATIONS_MSG = "## 🎉 Congratulations! You have answered all the riddles correctly!"
######################################### ADD THIS IN POST HOOK #######################################################
class UserData:
def __init__(self, riddles):
self.riddles = riddles.copy()
self.passed = []
self.current_index = 0
self.attempts = 0
self.passes = 0
def get_remaining_info(user_data):
remaining_riddles = (len(user_data.riddles) - user_data.current_index - 1) + len(user_data.passed)
passes_remaining = MAX_PASS - user_data.passes
return remaining_riddles, passes_remaining
async def start_game(interaction, user, channel):
pyrandom.shuffle(riddles)
user_data = UserData(riddles)
respond_interaction(interaction, embed=load_embed("clyzurbw903hpcza7h4354w0p"))
await send_riddle(channel, user_data)
await handle_user_response(user, channel, user_data)
async def send_riddle(channel, user_data, prefix=""):
async def send_riddle_message():
global remaining_riddles, passes_restantes, question
question = user_data.riddles[user_data.current_index]['question']
remaining_riddles, passes_restantes = get_remaining_info(user_data)
message = f"{prefix}\n> 👉 **{question}**\nRemaining riddles: `{remaining_riddles}` | Passes remaining: `{passes_restantes}`"
send_message(channel.id, message)
if user_data.current_index < len(user_data.riddles):
await send_riddle_message()
elif user_data.passed:
user_data.riddles = user_data.passed
user_data.passed = []
user_data.current_index = 0
pyrandom.shuffle(user_data.riddles)
await send_riddle_message()
else:
await end_game(channel, CONGRATULATIONS_MSG)
return True
return False
async def handle_user_response(user, channel, user_data):
if user_data.current_index >= len(user_data.riddles) and not user_data.passed:
return
response = await get_answer(user, channel)
if not response:
await end_game(channel, END_GAME_TIMEOUT_MSG.format(user=user.mention))
else:
await process_answer(user, channel, response.content.lower(), user_data)
async def get_answer(user, channel):
def predicate(event):
return event.author_id == user.id and event.channel_id == channel.id
return await wait_for("message", timeout=RESPONSE_TIMEOUT, predicate=predicate)
async def end_game(channel, message):
update_server_var("devinette", "False")
send_message(channel.id, message)
async def process_answer(user, channel, answer, user_data):
current_riddle = user_data.riddles[user_data.current_index]
correct_answers = current_riddle["answers"]
if answer in ["pass", "p"]:
await handle_pass(user, channel, user_data)
elif answer in correct_answers:
await handle_correct_answer(user, channel, user_data)
else:
await handle_incorrect_answer(user, channel, user_data)
async def handle_pass(user, channel, user_data):
if user_data.passes >= MAX_PASS:
await end_game(channel, END_GAME_MAX_PASS_MSG)
return
remaining_riddles, _ = get_remaining_info(user_data)
if remaining_riddles == 0:
send_message(channel.id, LAST_RIDDLE_PASS_WARNING)
else:
user_data.passes += 1
user_data.passed.append(user_data.riddles[user_data.current_index])
user_data.attempts = 0
user_data.current_index += 1
await send_riddle(channel, user_data, prefix=PASS_ANSWER_PREFIX)
await handle_user_response(user, channel, user_data)
async def handle_correct_answer(user, channel, user_data):
user_data.current_index += 1
user_data.attempts = 0
if await send_riddle(channel, user_data, prefix=CORRECT_ANSWER_PREFIX):
return
await handle_user_response(user, channel, user_data)
async def handle_incorrect_answer(user, channel, user_data):
user_data.attempts += 1
if user_data.attempts >= MAX_ATTEMPTS:
await end_game(channel, END_GAME_MAX_ATTEMPTS_MSG)
else:
attempts_remaining = MAX_ATTEMPTS - user_data.attempts
send_message(channel.id, INCORRECT_ANSWER_MSG.format(attempts_remaining))
await handle_user_response(user, channel, user_data)
await start_game(interaction, user, channel)
Editor is loading...
Leave a Comment