Untitled
unknown
python
a year ago
9.2 kB
12
Indexable
# Check if a game is already in progress on this server
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 the server variable to indicate that a game is starting
update_server_var("devinette", "True")
# List of riddles for the game
riddles = [
{
"question": "I am the most famous Italian plumber in video game history, often seen with a red hat. Who am I?",
"answers": ["mario"] # Accepted answer for this riddle
},
{
"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"] # Accepted answers for this riddle
},
{
"question": "I am a video game franchise developed by Mojang, where players can build and explore blocky worlds. What is my name?",
"answers": ["minecraft"] # Accepted answers for this riddle
},
{
"question": "I am a kart racing game where players can choose from iconic Nintendo characters. What is my name?",
"answers": ["mario kart"] # Accepted answers for this riddle
},
]
# Constants for managing the game
MAX_PASSES = 3 # Maximum number of passes allowed
MAX_ATTEMPTS = 3 # Maximum number of attempts per riddle
RESPONSE_TIMEOUT = 65 # Time to wait for a response in seconds
# Prefixes and messages for managing responses
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_PASSES_MSG = f"❌ You have reached the maximum number of passes (`{MAX_PASSES}`). 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 to store user data during the game
class UserData:
def __init__(self, riddles):
self.riddles = riddles.copy() # Copy the riddles to avoid accidental modification
self.passed = [] # List of skipped riddles
self.current_index = 0 # Index of the current riddle
self.attempts = 0 # Number of attempts for the current riddle
self.passes = 0 # Number of passes used
# Function to get remaining information about riddles and passes
def get_remaining_info(user_data):
remaining_riddles = (len(user_data.riddles) - user_data.current_index - 1) + len(user_data.passed)
passes_remaining = MAX_PASSES - user_data.passes
return remaining_riddles, passes_remaining
# Function to start the game
async def start_game(interaction, user, channel):
pyrandom.shuffle(riddles) # Shuffle the riddles
user_data = UserData(riddles) # Create an instance of UserData with shuffled riddles
respond_interaction(interaction, embed=load_embed("clyzurbw903hpcza7h4354w0p")) # Respond to the interaction with a welcome message
await send_riddle(channel, user_data) # Send the first riddle
await handle_user_response(user, channel, user_data) # Handle the user's response
# Function to send a riddle
async def send_riddle(channel, user_data, prefix=""):
async def send_riddle_message():
question = user_data.riddles[user_data.current_index]['question'] # Get the question of the current riddle
remaining_riddles, passes_remaining = get_remaining_info(user_data) # Get remaining information
message = f"{prefix}\n> 👉 **{question}**\nRemaining riddles: `{remaining_riddles}` | Passes remaining: `{passes_remaining}`"
send_message(channel.id, message) # Send the riddle message
if user_data.current_index < len(user_data.riddles): # If there are still riddles to ask
await send_riddle_message() # Send the current riddle
elif user_data.passed: # If all riddles have been asked and some riddles have been skipped
user_data.riddles = user_data.passed # Reset riddles with the skipped riddles
user_data.passed = [] # Reset the list of skipped riddles
user_data.current_index = 0 # Reset the current index
pyrandom.shuffle(user_data.riddles) # Shuffle riddles again
await send_riddle_message() # Send the riddle for the new series
else:
await end_game(channel, CONGRATULATIONS_MSG) # If no riddles are available, send a congratulations message
return True
return False
# Function to handle the user's response
async def handle_user_response(user, channel, user_data):
if user_data.current_index >= len(user_data.riddles) and not user_data.passed:
return # If all riddles have been asked and no more riddles are available, do nothing
response = await get_answer(user, channel) # Wait for the user's response
if not response:
await end_game(channel, END_GAME_TIMEOUT_MSG.format(user=user.mention)) # If no response is received within the timeout, send a game over message
else:
await process_answer(user, channel, response.content.lower(), user_data) # Process the user's response
# Function to get a response from the user
async def get_answer(user, channel):
def predicate(event):
return event.author_id == user.id and event.channel_id == channel.id # Filter messages from the user in the correct channel
return await wait_for("message", timeout=RESPONSE_TIMEOUT, predicate=predicate) # Wait for a message matching the criteria
# Function to end the game
async def end_game(channel, message):
update_server_var("devinette", "False") # Reset the server variable
send_message(channel.id, message) # Send a game over message
# Function to process the user's answer
async def process_answer(user, channel, answer, user_data):
current_riddle = user_data.riddles[user_data.current_index] # Get the current riddle
correct_answers = current_riddle["answers"] # Get the correct answers for this riddle
if answer in ["pass", "p"]: # If the user wants to skip
await handle_pass(user, channel, user_data) # Handle the pass
elif answer in correct_answers: # If the answer is correct
await handle_correct_answer(user, channel, user_data) # Handle the correct answer
else: # If the answer is incorrect
await handle_incorrect_answer(user, channel, user_data) # Handle the incorrect answer
# Function to handle skipping a riddle
async def handle_pass(user, channel, user_data):
if user_data.passes >= MAX_PASSES: # If the maximum number of passes has been reached
await end_game(channel, END_GAME_MAX_PASSES_MSG) # Send a game over message
return
remaining_riddles, _ = get_remaining_info(user_data) # Get remaining information
if remaining_riddles == 0: # If no riddles are left to ask
send_message(channel.id, LAST_RIDDLE_PASS_WARNING) # Send a warning message
else:
user_data.passes += 1 # Increment the number of passes used
user_data.passed.append(user_data.riddles[user_data.current_index]) # Add the current riddle to the list of skipped riddles
user_data.attempts = 0 # Reset the number of attempts
user_data.current_index += 1 # Move to the next riddle
await send_riddle(channel, user_data, prefix=PASS_ANSWER_PREFIX) # Send the next riddle with the skip prefix
await handle_user_response(user, channel, user_data) # Handle the next user response
# Function to handle a correct answer
async def handle_correct_answer(user, channel, user_data):
user_data.current_index += 1 # Move to the next riddle
user_data.attempts = 0 # Reset the number of attempts
if await send_riddle(channel, user_data, prefix=CORRECT_ANSWER_PREFIX): # Send the next riddle with the correct answer prefix
return
await handle_user_response(user, channel, user_data) # Handle the next user response
# Function to handle an incorrect answer
async def handle_incorrect_answer(user, channel, user_data):
user_data.attempts += 1 # Increment the number of attempts
if user_data.attempts >= MAX_ATTEMPTS: # If the maximum number of attempts has been reached
await end_game(channel, END_GAME_MAX_ATTEMPTS_MSG) # Send a game over message
else:
attempts_remaining = MAX_ATTEMPTS - user_data.attempts # Calculate remaining attempts
send_message(channel.id, INCORRECT_ANSWER_MSG.format(attempts_remaining)) # Send a message with the number of attempts remaining
await handle_user_response(user, channel, user_data) # Handle the next user response
# Start the game with the given interaction, user, and channel
await start_game(interaction, user, channel)
Editor is loading...
Leave a Comment