Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import discord
import asyncio
from botClient import client, colorCode
import random
from discord.ext import commands
"""This module is for games which are more complex and require more lines of code to write, as well as statistics."""
class Games:
guessAttemptsRight = 0
guessAttemptsTotal = 0
guessAttemptsTimeout = 0
def __init__(self, client):
self.client = client
@commands.command(name='statsguess', aliases=['guessstats', 'guesstats', 'gstats'])
async def guess_stats(self, ctx):
try:
await ctx.message.delete()
except discord.HTTPException:
pass
if guessAttemptsTotal == 0:
await ctx.send("You haven't attempted the guess game yet.", delete_after=5)
return
if guessAttemptsRight == 0:
guessRate = str(0) + '%'
else:
guessRate = str(round((guessAttemptsRight * 100) / guessAttemptsTotal, 1)) + '%.'
embed = discord.Embed(title="Stats", description="Letter guess game statistics", color=colorCode[0])
embed.add_field(name="Successful attempts: ", value=str(guessAttemptsRight), inline=True)
embed.add_field(name="Total attempts: ", value=str(guessAttemptsTotal), inline=True)
embed.add_field(name="Attempt success rate: ", value=str(guessRate), inline=True)
embed.add_field(name="Timeouts: ", value=str(guessAttemptsTimeout), inline=True)
embed.set_footer(text="Requested by " + ctx.message.author.name + '.', icon_url=ctx.message.author.avatar_url)
await ctx.send(embed=embed)
@commands.command(name='resetguess', aliases=['guessreset', 'resetstatsguess'])
async def guess_reset(self, ctx):
try:
await ctx.message.delete()
except discord.HTTPException:
pass
global guessAttemptsRight
global guessAttemptsTotal
global guessAttemptsTimeout
guessAttemptsRight = 0
guessAttemptsTotal = 0
guessAttemptsTimeout = 0
await ctx.send('Guess game results history has been cleared.', delete_after=10)
@commands.command(name='startguess', aliases=['playguess', 'guessplay', 'guessgameplay', 'letterguess', 'gstart'])
async def guess_game(self, ctx, maxAttempts = 3):
try:
await ctx.message.delete()
except discord.HTTPException:
pass
global guessAttemptsRight
global guessAttemptsTotal
global guessAttemptsTimeout
if maxAttempts <= 3:
await ctx.send('Guess a lowercase letter from the English alphabet.')
elif maxAttempts < 1:
await ctx.send('You need at least 1 attempt to start the game.')
return
else:
await ctx.send("Guess a lowercase letter from the English alphabet. \n"
f"Attempts: {maxAttempts} (>3). Game won't count.")
letterList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't',
'u', 'v', 'w', 'x', 'y', 'z']
letterPosition = random.randint(0, 25)
answer = letterList[letterPosition]
counter = 1
while counter <= maxAttempts:
def pred(m):
return m.author == ctx.message.author and m.channel == ctx.message.channel
try:
guess = await client.wait_for('message', check=pred, timeout=7)
except asyncio.TimeoutError:
await ctx.send(f'Sorry, you took too long. It was "{answer}".')
if maxAttempts <= 3:
guessAttemptsTimeout += 1
if counter != 1:
if maxAttempts <= 3:
guessAttemptsTotal += 1
await ctx.send(
'Added as a loss (quit while game was in progress)')
else:
await ctx.send(
"Not counted as a loss. (first guess not attempted)")
return
if guess.content.startswith('>'):
if counter != 1:
if maxAttempts <= 3:
guessAttemptsTotal += 1
await ctx.send("Game stopped, count as loss.")
else:
await ctx.send("Game stopped.")
return
try:
guessPosition = letterList.index(str(guess.content)) # saves the position of the guess letter
except ValueError:
await ctx.send(
'That is not a valid input (single lowercase English letters only).')
if counter != 1:
if maxAttempts <= 3:
guessAttemptsTotal += 1
await ctx.send('Game reset.')
return
if str(guess.content) == answer:
if counter == 1:
await ctx.send(f'LUCKY! The answer is indeed "{answer}".')
else:
await ctx.send(f'You are right! The answer is indeed "{answer}".')
await ctx.send('Guessed correctly after ' + str(counter) + ' attempt(s).')
if maxAttempts <= 3:
guessAttemptsRight += 1
guessAttemptsTotal += 1
return
else:
if maxAttempts - counter == 0:
await ctx.send(
f'Max attempts reached. Sorry. It was actually "{answer}".')
if maxAttempts <= 3:
guessAttemptsTotal += 1
return
else:
await ctx.send(f'Wrong. You have {maxAttempts - counter} attempt(s) left.')
#either I was tired of I'm hopeless, this part took me 2 hours to think through
if abs(guessPosition - letterPosition) > 12:
answerDistance = 26 - abs(guessPosition - letterPosition)
else:
answerDistance = abs(letterPosition - guessPosition)
await ctx.send\
(f'Hint: The letter you are looking for is {answerDistance} positions away.')
counter += 1
def setup(client):
client.add_cog(Games(client))