async def exec(self, member:discord.Member) -> None: self._permission_check(self.ctx, member) author = User(self.ctx.author.id) other = User(member.id) self._has_cards_check(other.all_cards) self._has_other_card_check(author.all_cards) target_card = random.choice([x[0] for x in other.all_cards if x[0] != 1008 and x[0] != 0]) await self._attack_defense_check(self.ctx, other, target_card) author.remove_card(self.id) removed_card_other = other.remove_card(target_card) removed_card_author = author.remove_card(random.choice([x[0] for x in author.all_cards if x[0] != 0])) other.add_card(removed_card_author[0], removed_card_author[1]["fake"]) author.add_card(removed_card_other[0], removed_card_other[1]["fake"]) await self.ctx.send(f'Successfully swapped cards! Gave {member} the card `{removed_card_author[0]}` and took card number `{removed_card_other[0]}` from them!')
def _has_met_check(self, prefix:str, author:User, other:discord.Member) -> None: if not author.has_met(other.id): raise CheckFailure(f"You haven\'t met this user yet! Use `{prefix}meet <@someone>` if they send a message in a channel to be able to use this card on them")
def _has_effect_check(self, user:User, effect:str) -> None: if user.has_effect(effect)[0]: raise CheckFailure("You already have this effect in place!")
def _has_any_card(self, card_id:int, user:User) -> None: if not user.has_any_card(card_id): raise CheckFailure("The specified user doesn't have this card")
def __init__(self, ctx, difficulty: str): self.ctx = ctx self.user = User(ctx.author.id) self.difficulty = difficulty self.level = 1
class CountGame: """A game where you have to remember numbers and type them in the right order""" def __init__(self, ctx, difficulty: str): self.ctx = ctx self.user = User(ctx.author.id) self.difficulty = difficulty self.level = 1 def _handle_reward(self) -> int: """Creates a jenny reward based on the level and difficulty""" return ((2 if self.user.is_entitled_to_double_jenny else 1) * int( random.randint(20, 30) * self.level * (0.5 if self.difficulty == "easy" else 1)) ) if self.level > 1 else 0 def _assign_until_unique(self, already_assigned: List[int]) -> int: """Picks one random free spot to put the next number in""" r = random.randint(1, 25) if r in already_assigned: return self._assign_until_unique(already_assigned) else: return r def _create_solutions(self, keep_used: bool) -> None: """Creates the solution dictionary""" res: dict = (self.solutions if hasattr(self, "solutions") else {}) if keep_used else {} for i in range(1 if keep_used else self.level): res[len(res) + 1 if keep_used else i + 1] = self._assign_until_unique(list(res.values())) self.solutions = res async def _send_solutions(self, msg: discord.Message = None) -> discord.Message: """Sends the solutions before hiding them""" view = View(self.ctx.author.id) view.stage = 1 for i in range(25): view.add_item( discord.ui.Button(label=str( [k for k, v in self.solutions.items() if v - 1 == i][0]) if i + 1 in list(self.solutions.values()) else " ", disabled=True, style=discord.ButtonStyle.grey)) if not msg: msg = await self.ctx.bot.send_message( self.ctx, content= "Press the buttons in the order displayed as soon as the time starts. Good luck!", view=view) else: await msg.edit("One more button to remember. Get ready!", view=view) await asyncio.sleep(3 if self.level == 1 else ( self.level * 2 * (0.5 if self.difficulty == "easy" else 1))) return msg async def _handle_game(self, msg: discord.Message) -> discord.Message: """The core of the game, creates the buttons and waits until the buttons return a result and handles it""" view = View(self.ctx.author.id, timeout=self.level * 10 * (0.5 if self.difficulty == "easy" else 1)) view.stage = 1 for i in range(25): view.add_item(CountButtons(self.solutions, i + 1, label=" ")) await msg.edit(content="Can you remember?", view=view) await view.wait() if not hasattr( view, "correct" ) or not view.correct: # This happens when the user has lost the game or it timed out reward = self._handle_reward() resp = "Too slow!" if not hasattr(view, "correct") else "Wrong choice!" for child in view.children: child.disabled = True await msg.edit(view=view) self.user.add_jenny(reward) return await self.ctx.send( resp + " But well done, you made it to level " + str(self.level) + " which brings you a reward of " + str(reward) + " Jenny!") self.level += 1 if self.level == 26: reward = self._handle_reward() return await self.ctx.send( "Well done, you completed the game! Your reward is " + str(reward) + " Jenny. Keep up the great work!") await asyncio.sleep(5) self._create_solutions(self.difficulty == "easy") new_msg = await self._send_solutions(msg) await self._handle_game(new_msg) async def start(self): """The function to call to start the game""" self._create_solutions(self.difficulty == "easy") msg = await self._send_solutions() await self._handle_game(msg)
async def _eval_outcome(self, winlose: int, choice1, choice2, player1: discord.Member, player2: discord.Member) -> discord.Message: """Evaluates the outcome, informs the players and handles the points """ p1 = User(player1.id) p2 = User(player2.id) if winlose == -1: if self.points: p1.add_jenny(self.points) if player2 != self.ctx.me: p2.remove_jenny(self.points) return await self.ctx.send( f'{self.emotes[choice1]} > {self.emotes[choice2]}: {player1.mention} won against {player2.mention} winning {self.points} Jenny which adds to a total of {p1.jenny}' ) else: return await self.ctx.send( f'{self.emotes[choice1]} > {self.emotes[choice2]}: {player1.mention} won against {player2.mention}' ) elif winlose == 0: return await self.ctx.send( f'{self.emotes[choice1]} = {self.emotes[choice2]}: {player1.mention} tied against {player2.mention}' ) elif winlose == 1: if self.points: p1.remove_jenny(self.points) if player2 != self.ctx.me: p2.add_jenny(self.points) return await self.ctx.send( f'{self.emotes[choice1]} < {self.emotes[choice2]}: {player1.mention} lost against {player2.mention} losing {self.points} Jenny which leaves them a total of {p1.jenny}' ) else: return await self.ctx.send( f'{self.emotes[choice1]} < {self.emotes[choice2]}: {player1.mention} lost against {player2.mention}' )
async def save_user(self, data) -> None: """This functions purpose is not that much getting user data but saving a user in the database""" User(data.user)