Untitled
unknown
python
a year ago
3.5 kB
10
Indexable
current_riddle_index = 0
attempts = 0
passes = 0
passed_riddles = []
def get_remaining_info():
remaining_riddles = (len(riddles) - current_riddle_index - 1) + len(passed_riddles)
return remaining_riddles, passes
async def start_game():
pyrandom.shuffle(riddles)
await send_riddle()
await handle_user_response()
async def send_riddle(prefix="🧩 First riddle:"):
global current_riddle_index, riddles, passed_riddles
if current_riddle_index >= len(riddles):
if passed_riddles:
riddles, passed_riddles = passed_riddles, []
current_riddle_index = 0
else:
await end_game(END_CONGRATS_MSG)
return False
question = riddles[current_riddle_index]['question']
remaining_riddles, passes_utilises = get_remaining_info()
embed = Message(
description=(
f"{prefix}\n\n"
f"❔ **{question}**\n\n"
f"Remaining riddles: **`{remaining_riddles}`** | Passes used: **`{passes_utilises}`** out of **`{MAX_PASS}`**"
),
color=0xff0080
)
send_message(channel.id, message=embed)
return True
async def handle_user_response():
if current_riddle_index >= len(riddles) and not passed_riddles:
return
response = await get_response()
if response:
await process_response(response.content.lower())
else:
await end_game(END_TIMEOUT_MSG)
return
async def get_response():
def predicate(event):
return event.author_id == user.id and event.channel_id == channel.id
return await wait_for("message", timeout=TIMEOUT, predicate=predicate)
async def end_game(message):
update_server_var("riddle_game", "False")
send_message(channel.id, message)
async def process_response(response):
global current_riddle_index, attempts, passes, passed_riddles
current_riddle = riddles[current_riddle_index]
answers = current_riddle["answers"]
similarity = 0
def checker(correct_ans, index):
nonlocal similarity
result = fuzz.ratio(response, correct_ans.lower())
similarity = max(similarity, result)
loop(answers, checker)
is_good_match = similarity >= THRESHOLD
if response in ["skip", "s"]:
await handle_pass()
elif is_good_match:
await handle_correct_response()
else:
await handle_incorrect_response()
async def handle_pass():
global current_riddle_index, attempts, passes, passed_riddles
remaining_riddles, _ = get_remaining_info()
if passes >= MAX_PASS or remaining_riddles == 0:
send_message(channel.id, PASS_WARN_MSG)
else:
passes += 1
passed_riddles.append(riddles[current_riddle_index])
attempts = 0
current_riddle_index += 1
await send_riddle(prefix=PASS_MSG)
await handle_user_response()
async def handle_correct_response():
global current_riddle_index, attempts
current_riddle_index += 1
attempts = 0
if await send_riddle(prefix=CORRECT_MSG):
await handle_user_response()
async def handle_incorrect_response():
global attempts
attempts += 1
if attempts >= MAX_ATT:
await end_game(END_MAX_ATT_MSG)
return
else:
attempts_restants = MAX_ATT - attempts
send_message(channel.id, INCORRECT_MSG.format(attempts_restants))
await handle_user_response()
await start_game()Editor is loading...
Leave a Comment