Skip to content

Commit

Permalink
feat(event): remove points to advanced users on ttt loss
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Nov 30, 2024
1 parent a3d955d commit 920b8c1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lang/bot_events/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
},
"soon": "Událost se blíží! Sledujte informace, protože začne %{date}",
"tictactoe": {
"reward-title": "Tic-tac-toe vítezství!",
"reward-desc": "Tím, že jsi vyhrál tuto hru, jsi získal ** body%{points} události**!"
"won": {
"title": "Tic-tac-toe vítezství!",
"desc": "Tím, že jsi vyhrál tuto hru, jsi získal ** body%{points} události**!"
}
},
"tip-title": "Náhodný tip",
"unclassed": "nezařazené",
Expand Down
10 changes: 8 additions & 2 deletions lang/bot_events/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@
},
"soon": "An event is coming soon! Watch for information as it will begin on %{date}",
"tictactoe": {
"reward-title": "Tic-tac-toe victory!",
"reward-desc": "By winning this game, you earned **%{points} event points**!"
"lost": {
"title": "Tic-tac-toe defeat",
"desc": "By losing this game, you lost **%{points} event points**..."
},
"won": {
"title": "Tic-tac-toe victory!",
"desc": "By winning this game, you earned **%{points} event points**!"
}
},
"tip-title": "Random tip",
"unclassed": "unclassed",
Expand Down
10 changes: 8 additions & 2 deletions lang/bot_events/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@
},
"soon": "Un événement arrive bientôt ! Surveillez les informations, car il débutera le %{date}",
"tictactoe": {
"reward-title": "Victoire au morpion !",
"reward-desc": "En remportant ce match, tu as gagné **%{points} points d'événement** !"
"lost": {
"title": "Défaite au morpion",
"desc": "En perdant ce match, tu as perdu **%{points} points d'événement**..."
},
"won": {
"title": "Victoire au morpion !",
"desc": "En remportant ce match, tu as gagné **%{points} points d'événement** !"
}
},
"tip-title": "Astuce aléatoire",
"unclassed": "Non classé",
Expand Down
6 changes: 4 additions & 2 deletions lang/bot_events/lolcat.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
},
"soon": "Hey hey hey! ur too soon, try checking on %{date}! :hype: ",
"tictactoe": {
"reward-title": "WOW, u won against the most supremely smartest tic-tac-toe AI ever!",
"reward-desc": "So uh, as a reward, I've gave u **%{points} event points**, check 'em with `/event profile`!"
"won": {
"title": "WOW, u won against the most supremely smartest tic-tac-toe AI ever!",
"desc": "So uh, as a reward, I've gave u **%{points} event points**, check 'em with `/event profile`!"
}
},
"tip-title": "Cool tip",
"unclassed": "unclassd",
Expand Down
45 changes: 43 additions & 2 deletions modules/bot_events/bot_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ async def on_tictactoe_win(self, interaction: discord.Interaction):
user = interaction.user
# send win reward embed
emb = discord.Embed(
title=await self.bot._(interaction, "bot_events.tictactoe.reward-title"),
description=await self.bot._(interaction, "bot_events.tictactoe.reward-desc", points=points),
title=await self.bot._(interaction, "bot_events.tictactoe.won.title"),
description=await self.bot._(interaction, "bot_events.tictactoe.won.desc", points=points),
color=self.current_event_data["color"],
)
emb.set_author(name=user.global_name, icon_url=user.display_avatar)
Expand All @@ -158,6 +158,37 @@ async def on_tictactoe_win(self, interaction: discord.Interaction):
# give points
await self.db_add_user_points(user.id, points)

@commands.Cog.listener()
async def on_tictactoe_lose(self, interaction: discord.Interaction):
"Grant points to the user if they lost a game of tictactoe"
if not self.current_event:
return
user = interaction.user
# check if user points is high enough to add some difficulty (ie. remove points on loss)
if not self.current_event_data["objectives"]:
return
user_points = await self.db_get_user_points(user.id)
first_cap = self.current_event_data["objectives"][0]["points"]
last_cap = self.current_event_data["objectives"][-1]["points"]
if user_points < first_cap * 1.1:
return
if first_cap == last_cap or user_points < last_cap * 1.1:
# remove 3 points if user has more than 110% of the first objective
points = -3
else:
# remove 5 points if user has more than 110% of the max objective
points = -5
# send loss reward embed
emb = discord.Embed(
title=await self.bot._(interaction, "bot_events.tictactoe.lost.title"),
description=await self.bot._(interaction, "bot_events.tictactoe.lost.desc", points=-points),
color=self.current_event_data["color"],
)
emb.set_author(name=user.global_name, icon_url=user.display_avatar)
await interaction.followup.send(embed=emb)
# remove points
await self.db_add_user_points(user.id, points)

events_main = app_commands.Group(
name="event",
description="Participate in bot special events!",
Expand Down Expand Up @@ -344,6 +375,16 @@ def _check_reward_date(self, reward_date: str | None):
parsed_date = datetime.datetime.strptime(reward_date, "%Y-%m-%d").replace(tzinfo=datetime.UTC)
return self.bot.utcnow() < parsed_date

async def db_get_user_points(self, user_id: int) -> int | None:
"Get the user's event points"
if not self.bot.database_online or self.bot.current_event is None:
return None
query = "SELECT `points` FROM `event_points` WHERE `user_id` = %s AND `beta` = %s;"
async with self.bot.db_main.read(query, (user_id, self.bot.beta), fetchone=True) as query_result:
if query_result:
return query_result["points"]
return None

async def db_add_user_points(self, user_id: int, points: int):
"Add some 'other' events points to a user"
try:
Expand Down
10 changes: 8 additions & 2 deletions modules/tictactoe/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,16 @@ async def check_game_end(self):
return False
self.stop()
await self.update_grid(result)
if not is_draw and self.is_user_turn:
await self.bot.dispatch("tictactoe_win", self.interaction)
if not is_draw:
if self.is_user_turn:
self.bot.dispatch("tictactoe_win", self.interaction)
else:
self.bot.dispatch("tictactoe_lose", self.interaction)
return True

async def on_error(self, interaction, error, item, /):
await self.bot.dispatch("error", error, interaction)


async def setup(bot):
await bot.add_cog(TicTacToe(bot))

0 comments on commit 920b8c1

Please sign in to comment.