async def start_round(ctx): table = Channel(ctx) table.set({ "waitingForPlayers": False, "pot": 0, "currentBet": 0, "phase": 0, "cards": [], "deck": joker.new_deck() }) position = 0 for player in table.get("players"): table.remove("players", player) player_id = player["userId"] hand = [] card1, deck = joker.draw(table.get_deck()) card2, deck = joker.draw(deck) table.set_deck(deck) hand.append(card1) hand.append(card2) table.add_player(player_id, position, hand) position = position + 1 for player_id in table.get("waitingLine"): hand = [] card1, deck = joker.draw(table.get_deck()) card2, deck = joker.draw(deck) table.set_deck(deck) hand.append(card1) hand.append(card2) table.add_player(player_id, position, hand) position = position + 1 table.remove("waitingLine", player_id) table.set({ "positionTurn": -1, }) await table.update(messages.game.newhand) await next_turn(ctx)
async def end_round(ctx): table = Channel(ctx) highest_score = 0 winning_position = [-1] for player in table.get("players"): in_pot = player["inPot"] member = Member(ctx, player["userId"]) # Adds 1 to total hands played member.set("handsPlayed", member.get("handsPlayed") + 1) # Adds amount in pot to total losses member.set("totalLosses", member.get("totalLosses") + in_pot) # Detracts amount bet from users hand member.set("balance", member.get("balance") - in_pot) # Finds the position[s] with the winning hand hand = player["hand"] position = player["position"] score = joker.Score(hand, table.get("cards")).get() if score > highest_score: highest_score = score winning_position = [position] elif score == highest_score: winning_position.append(position) # Splits up pot among winners winning_amount = table.get("pot") / len(winning_position) for player in table.get("players"): if player["position"] in winning_position: member = Member(ctx, player["userId"]) # Adds winnings to balance member.set("balance", member.get("balance") + winning_amount) # Adds winnings to total winnings member.set("totalWinnings", member.get("totalWinnings") + winning_amount) # Adds 1 to hands won member.set("handsWon", member.get("handsWon") + 1) # Removes players in leaving queue from table for player_id in table.get("leavingLine"): if table.get_database().is_there("players", {"userId": player_id}): table.remove("players", {"userId": player_id}) table.remove("leavingLine", player_id) await table.update( f"{ctx.message.guild.get_member(player_id).mention} has left the table." ) # Set waiting for players to true if less than 2 players if len(table.get("players")) < 2: table.set({"waitingForPlayers": True}) # Deletes table if waiting to delete if table.get("waitingToClose"): await table.update(messages.closetable.closed) table.get_database().delete_entry("channelId", ctx.message.channel.id)
class Player: def __init__(self, ctx): """ Initializes a class for player-related functions :param ctx: The context object of the message """ self.ctx = ctx self.server_id = self.ctx.message.guild.id self.player_id = self.ctx.message.author.id self.channel_id = self.ctx.message.channel.id self.table = Channel(self.ctx) self.database = None def get_balance(self, player_id=None): """ Returns the balance of a given player :param player_id: The owner of the balance to return :return: The balance of the given player """ if self.database is None: self.database = Database("Members", self.server_id) if player_id is None: player_id = self.player_id return self.database.get_value("userId", player_id, "balance") def set_balance(self, balance, player_id=None): """ Sets the balance of a given player :param balance: The number to set the player's balance to :param player_id: The target player for the balance change """ if self.database is None: self.database = Database("Members", self.server_id) if player_id is None: player_id = self.player_id self.database.set_value("userId", player_id, {"balance": balance}) async def dm(self, message, player_id=None): """ Directly message the given player :param message: The message to send the player :param player_id: The player to send the message to """ if player_id is None: player_id = self.player_id player = self.ctx.guild.get_member(player_id) if player.dm_channel is not None: await player.dm_channel.send(message) else: await player.create_dm() await player.dm_channel.send(message) def is_playing(self, player_id=None): """ Returns whether a given player is active in a game :rtype: bool :param player_id: The target player :return: Returns the boolean of whether the target player is active in a game """ if player_id is None: player_id = self.player_id for player in self.table.get("players"): if player["userId"] == player_id: return True return False def is_in_waiting_line(self, player_id=None): """ Returns whether a given player is in a waiting line :rtype: bool :param player_id: The target player :return: Returns the boolean of whether the target player is in a waiting line """ if player_id is None: player_id = self.player_id for pid in self.table.get("waitingLine"): if pid == player_id: return True return False def is_in_leaving_line(self, player_id=None): """ Returns whether a given player is in a leaving line :rtype: bool :param player_id: The target player :return: Returns the boolean of whether the target player is in a leaving line """ if player_id is None: player_id = self.player_id for pid in self.table.get("leavingLine"): if pid == player_id: return True return False async def join(self, player_id=None): """ Attempts to add a player to a waiting line or remove a player from a leaving line :param player_id: The target player """ if player_id is None: player_id = self.player_id # adds player to waiting line if player is not already in waiting line or currently playing if not self.is_in_waiting_line( player_id=player_id) and not self.is_playing( player_id=player_id) and not self.is_in_leaving_line( player_id=player_id): Member(self.ctx, player_id).new() await self.table.update(messages.player.join(self.ctx)) await self.table.add_to_waiting_line(player_id=player_id) # removes player from leaving line if they are in leaving line elif self.is_in_leaving_line(player_id=player_id) and self.is_playing( player_id=player_id): await self.table.update(messages.player.cancelLeave(self.ctx)) await self.table.remove_from_leaving_line(player_id=player_id) async def leave(self, player_id=None): """ Attempts to remove a player from a waiting line or add a player to a leaving line :param player_id: The target player """ if player_id is None: player_id = self.player_id # removes player from waiting line if they are in it if self.is_in_waiting_line(player_id=player_id): await self.table.update(messages.player.leave2(self.ctx)) await self.table.remove_from_waiting_line(player_id=player_id) # adds player to leaving line if they aren't in leaving line and are in game elif not self.is_in_leaving_line( player_id=player_id) and self.is_playing(player_id=player_id): await self.table.update(messages.player.leave(self.ctx)) await self.table.add_to_leaving_line(player_id=player_id)
async def next_turn(ctx): table = Channel(ctx) # Grabs list of players players = table.get("players") # Checks if round needs to be advanced '''advanceRound = True for player in players: if not player["takenTurn"]: advanceRound = False if not player["inPot"] == (table.functions.get_value(ctx, "pot")/players.len): advanceRound = False if advanceRound: table.functions.set_value(ctx, {"phase": table.functions.get_value(ctx, "phase")+1}) ''' # Grabs which player's turn positionTurn = table.functions.get(ctx, "positionTurn") + 1 if positionTurn > len(players): positionTurn = 0 # Grabs community cards cards = "" for card in table.functions.get(ctx, "cards"): cards = cards + card + " " cards = cards.rstrip() if cards == "": cards = "None" # Searches for player whose turn it is for player in players: if positionTurn == player["position"]: # Grabs user id userId = player["userId"] # Sends message in table regarding whose turn it is await table.update(ctx, messages.game.turn(ctx, userId)) # Sets player as taking turn player["takingTurn"] = True # Grabs table values pot = table.functions.get(ctx, "pot") currentBet = table.functions.get(ctx, "pot") inPot = player["inPot"] amountToRaise = currentBet - inPot balance = member.functions.get(ctx, userId, "balance") # Formats player's hand hand = "%s %s" % (player["hand"][0], player["hand"][1]) # Formats message to send to player interface = f"Pot: **${pot}**\nCurrent Bet: **${currentBet}**\nYou've bet: **${inPot}**\nYou need to bet: **${amountToRaise}**\nYour balance: **${balance}**\nHand: **{hand}**\nCommunity Cards: **{cards}**\n" if table.functions.get(ctx, "currentBet") == 0: interface = interface + "Type `!check` to check the bet.\n" else: interface = interface + f"Type `!call` to call the bet and add {amountToRaise} to the pot.\n" interface = interface + "Or, type `!raise [amount] to raise the bet by a specific amount." # Sends player the DM. await pl.dm(ctx, userId, interface) # Updates values in database table.functions.set(ctx, {"players": players}) table.functions.set(ctx, {"positionTurn": positionTurn})