Skip to content

Commit

Permalink
Fixed Database
Browse files Browse the repository at this point in the history
Also fixed population giving the wrong result. Added more country data. Cleaned up and improved countryle.py code.
  • Loading branch information
coreyhsGames committed Mar 2, 2023
1 parent b394c18 commit f38e446
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 51 deletions.
110 changes: 62 additions & 48 deletions cogs/countryle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@
"Angola": "Southern, Africa, 32000000, 21.55",
"Antigua and Barbuda": "Northern, North America, 98000, 25.85",
"Argentina": "Southern, South America, 45100000, 14.80",
"Armenia": "Northern, Asia, 2900000, 7.15",
"Australia": "Southern, Oceania, 25500000, 21.65"
"Armenia": "Northern, Asia, 2900000, 7.15",
"Australia": "Southern, Oceania, 25500000, 21.65",
"Austria": "Northern, Europe, 9000000, 6.35",
"Azerbaijan": "Northern, Asia, 10100000, 11.95",
"Bahrain": "Northern, Asia, 1700000, 26.80",
"Bangladesh": "Northern, Asia, 16000000, 25.00",
"Barbados": "Northern, North America, 280000, 26.00",
"Belarus": "Northern, Europe, 9500000, 6.15",
"Belgium": "Northern, Europe, 11000000, 9.55",
"Belize": "Northern, North America, 400000, 25.35",
"Benin": "Northern, Africa, 12000000, 27.55"
}

class countryle(commands.Cog):
def __init__(self, client):
self.client = client

with open("./config.json") as f:
with open("./config.json") as f:
config = json.load(f)

cluster = MongoClient(config['c01password'])
cluster = MongoClient(config['c01password'])
db_countryle = cluster['pyguesser']['countryle']

global db_countryle
db_countryle = cluster['pyguesser']['countryle']
class countryle(commands.Cog):
def __init__(self, client):
self.client = client

global is_correct
is_correct = False
Expand All @@ -36,12 +43,15 @@ def __init__(self, client):
async def play_countryle(self, ctx):
user_stats = db_countryle.find_one({"id": ctx.author.id})
if not ctx.author.bot:
if not user_stats:
new_user = {"id": ctx.author.id, "wins": 0, "times_played": 0}
db_countryle.insert_one(new_user)
if user_stats is None:
insert = {"id": ctx.author.id, "wins": 0, "games_played": 0}
db_countryle.insert_one(insert)

games_played = user_stats['games_played'] + 1
db_countryle.update_one({"id": ctx.author.id}, {"$set":{"games_played": games_played}})
else:
times_played = user_stats["times_played"] + 1
user_stats.update_one({"id": ctx.author.id}, {"$set": {"times_played": times_played}})
games_played = user_stats['games_played'] + 1
db_countryle.update_one({"id": ctx.author.id}, {"$set":{"games_played": games_played}})

puzzle_id = random_puzzle_id()
embed = generate_puzzle_embed(puzzle_id)
Expand All @@ -68,7 +78,7 @@ async def on_message(self, message: discord.Message):
await message.delete(delay=5)
return

embed = update_embed(embed, message .content)
embed = update_embed(embed, message.content, message.author)
await parent.edit(embed=embed)

try:
Expand All @@ -78,7 +88,7 @@ async def on_message(self, message: discord.Message):


def generate_puzzle_embed(puzzle_id: int) -> discord.Embed:
embed = discord.Embed(title="Countryle: IN-PROGRESS")
embed = discord.Embed(title="Countryle: IN-PROGRESS", colour=0xBA55D3)
embed.description = f"**🌐 Hemisphere | <:earth_oceania:1080012117035450408> Continent | 👥 Population | 🌡 Avg. Temp**"

embed.set_footer(text=f"Game ID: {puzzle_id} | To play, use the command pycountryle!\nTo guess, reply to this message with a valid country.")
Expand All @@ -99,11 +109,22 @@ def is_guessed_country_correct(guess, answer) -> bool:
def random_puzzle_id() -> int:
return random.randint(0, len(country_list) - 1)

def is_higher_or_lower(guess, answer) -> str:
if guess == answer:
return f"{guess} ✅"
elif guess > answer:
return f"{guess} ⬇"
elif guess < answer:
return f"{guess} ⬆"
else:
return f"{guess} ❌"

def generate_guessed_country(guess, answer, puzzle_id):
country_data_keys = list(country_data.keys())
correct_country = country_data_keys[puzzle_id]
correct_country_values = country_data[correct_country]
correct_country_values_split = correct_country_values.split(", ")
print(correct_country_values_split)

global correct_hemisphere, correct_continent, correct_population, correct_avg_temp

Expand All @@ -112,11 +133,12 @@ def generate_guessed_country(guess, answer, puzzle_id):
correct_population = correct_country_values_split[2]
correct_avg_temp = correct_country_values_split[3]

correct_population = format(int(correct_population),",")
correct_population = int(correct_population)
correct_avg_temp = float(correct_avg_temp)

guessed_country_values = country_data[f"{guess}"]
guessed_country_values_split = guessed_country_values.split(", ")
print(guessed_country_values_split)

global guessed_hemisphere, guessed_continent, guessed_population, guessed_avg_temp

Expand All @@ -125,54 +147,46 @@ def generate_guessed_country(guess, answer, puzzle_id):
guessed_population = guessed_country_values_split[2]
guessed_avg_temp = guessed_country_values_split[3]

guessed_population = format(int(guessed_population),",")
guessed_avg_temp = float(guessed_avg_temp)

global hemisphere_str, continent_str, population_str, avg_temp_str

if guessed_hemisphere == correct_hemisphere:
hemisphere_str = f"{guessed_hemisphere} ✅"
else:
hemisphere_str = f"{guessed_hemisphere} ❌"

if guessed_continent == correct_continent:
continent_str = f"{guessed_continent} ✅"
else:
continent_str = f"{guessed_continent} ❌"
guessed_population = int(guessed_population)
guessed_population_str = format(guessed_population, ",")
guessed_avg_temp = float(guessed_avg_temp)

hemisphere_str = is_higher_or_lower(guessed_hemisphere, correct_hemisphere)
continent_str = is_higher_or_lower(guessed_continent, correct_continent)
# population_str = is_higher_or_lower(guessed_population, correct_population)
avg_temp_str = is_higher_or_lower(guessed_avg_temp, correct_avg_temp)

# Displays population with commas
if guessed_population == correct_population:
population_str = f"{guessed_population} ✅"
elif guessed_population < correct_population:
population_str = f"{guessed_population} ⬆"
population_str = f"{guessed_population_str} ✅"
elif guessed_population > correct_population:
population_str = f"{guessed_population} ⬇"

if guessed_avg_temp == correct_avg_temp:
avg_temp_str = f"{guessed_avg_temp}°C ✅"
elif guessed_avg_temp < correct_avg_temp:
avg_temp_str = f"{guessed_avg_temp}°C ⬆"
elif guessed_avg_temp > correct_avg_temp:
avg_temp_str = f"{guessed_avg_temp}°C ⬇"
population_str = f"{guessed_population_str} ⬇"
elif guessed_population < correct_population:
population_str = f"{guessed_population_str} ⬆"
else:
population_str = f"{guessed_population_str} ❌"

return f"{hemisphere_str} | {continent_str} | {population_str} | {avg_temp_str}"


def update_embed(embed: discord.Embed, guess: str) -> discord.Embed:
def update_embed(embed: discord.Embed, guess: str, user: discord.Member) -> discord.Embed:
puzzle_id = int(embed.footer.text.split()[2])
answer = country_list[puzzle_id]
guessed_result = generate_guessed_country(guess, answer, puzzle_id)

is_correct = is_guessed_country_correct(guess, answer)

if is_correct == True:
num_of_guesses = len(embed.fields)
num_of_guesses = len(embed.fields) + 1

embed.add_field(name=f"{guess}:", value=f"{guessed_result}\n**Correct! ✅**\nGuesses: {num_of_guesses}", inline=False)
embed.title = f"{embed.title}: COMPLETE"
embed.add_field(name=f"{guess}:", value=f"{guessed_result}\n\n**Correct! ✅**\n\nStats:\nGuesses: {num_of_guesses}", inline=False)
embed.title = f"Countryle: COMPLETE"

user_stats = db_countryle.find_one({"id": embed.author.id})
wins = user_stats["wins"] + 1
user_stats.update_one({"id": embed.author.id}, {"$set": {"wins": wins}})
user_stats = db_countryle.find_one({'id': user.id})
wins = user_stats['wins'] + 1
db_countryle.update_one({"id": user.id}, {"$set":{"wins": wins}})
else:
embed.add_field(name=f"{guess}:", value=f"{guessed_result}", inline=False)
return embed
Expand Down
4 changes: 2 additions & 2 deletions cogs/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ async def leaderboard(self, ctx, num_of_players: int = 5):

rankings = db_countryle.find().sort("wins", -1)
i = 1
embed = discord.Embed(title="PyGuessr Leaderboard", description=f"Displays the best players (top {num_of_players}) in all of Discord, by most wins.", color=0xf54242)
embed = discord.Embed(title="PyGuessr Leaderboard", description=f"Displays the best players (top {num_of_players}) in all of Discord in PyGuessr's Countryle, sorted by most wins.", colour=0xBA55D3)
for x in rankings:
try:
temp = ctx.guild.get_member(x["id"])
temp_wins = x["wins"]
temp_games_played = x["games_played"]

field_stats = f'👑 **Wins:** {temp_wins} | 🗓️ **Games Played:** {temp_games_played}"'
field_stats = f'👑 **Wins:** {temp_wins} | 🗓️ **Games Played:** {temp_games_played}'
if i == 1:
i = "🥇"
embed.add_field(name=f"{i}: {temp.name}", value=field_stats, inline=False)
Expand Down
2 changes: 1 addition & 1 deletion country-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Armenia
Australia
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Expand Down Expand Up @@ -170,6 +169,7 @@ Syria
Tajikistan
Tanzania
Thailand
The Bahamas
The Gambia
Togo
Tonga
Expand Down

0 comments on commit f38e446

Please sign in to comment.