Exemple #1
0
class Lottery:
    def __init__(self, bot):
        self.bot = bot
        self.database = database.Database(self.bot)
        self.coins = Coin(bot)
        # self.lotteryTickets = {} // Different implementation
        self.ticketCost = 100
        self.winningTicket = 0
        self.ticketCounter = 0
        self.prizePool = 0
        self.generate_tickets(100)

    @commands.command(
        name="buyticket",
        pass_context=True,
        help="Buy tickets for 100 coins for a chance to win loyal guns")
    async def buy_ticket(self, ctx):
        user = ctx.message.author
        if not self.coins.check_balance(user, self.ticketCost):
            await self.bot.say((
                "{}, You don't have enough coins to buy a ticket \n - Current balance: "
                + str(self.database.get_coins(user.id))).format(user.mention))
        else:
            self.database.remove_coins(user.id, self.ticketCost, user.mention)
            if self.ticketCounter == self.winningTicket:
                self.generate_tickets(100)
                self.database.insert_coins(user.id, self.prizePool,
                                           user.mention)
                await self.bot.say((
                    "{}, Congratulations, you won the lottery with a prizepool of "
                    + str(self.prizePool) + "\n- Current balance: " +
                    str(self.database.get_coins(user.id))).format(user.mention)
                                   )
            else:
                self.ticketCounter += 1
                await self.bot.say(
                    ("{}, You lost, better luck next time!" +
                     "\n - Current balance: " +
                     str(self.database.get_coins(user.id))).format(
                         user.mention))

    # Creates a new winning number, while resetting the counter, were the nr_of_tickets represents the odds of winning
    def generate_tickets(self, nr_of_tickets):
        self.ticketCounter = 1
        self.prizePool = nr_of_tickets * int(self.ticketCost * 0.9) / 2
        self.winningTicket = random.randint(1, nr_of_tickets)
Exemple #2
0
class Holdem:
    def __init__(self, bot):
        self.bot = bot
        self.database = database.Database(self.bot)
        self.holdem_players = []
        self.game_status = 0
        self.deck = []
        self.dealersHand = []
        self.coins = Coin(bot)

        # TODO: Command: holdem new, join, start, Call, raise, fold
        # TODO: Gather players

        @commands.group(name="holdem", pass_context=True)
        async def holdem(self, ctx):
            if ctx.invoked_subcommand is None:
                await self.bot.say(
                    "This is not a valid command, please add a subcommand")

        @holdem.command(name="new", pass_context=True)
        async def new_game(self, ctx):
            if self.game_status != 0:
                self.bot.say("A game is already running, please wait to join")
                return None,
            self.deck = self.generateDeck()
            self.game_status = 1
            welcome_msg =   "_______                   _    _       _     _\n" \
                          " |__   __|                 | |  | |     | |   | |               \n" \
                          "    | | _____  ____ _ ___  | |__| | ___ | | __| | ___ _ __ ___  \n" \
                          "    | |/ _ \ \/ / _` / __| |  __  |/ _ \| |/ _` |/ _ | '_ ` _ \ \n" \
                          "    | |  __/>  | (_| \__ \ | |  | | (_) | | (_| |  __| | | | | |\n" \
                          "    |_|\___/_/\_\__,_|___/ |_|  |_|\___/|_|\__,_|\___|_| |_| |_|\n"
            await self.bot.say(
                welcome_msg, "A Texas Hold'em table has opened"
                "Please use !holdem join <Blind> to join the table")

        @holdem.command(name="join", pass_context=True)
        async def join_game(self, ctx, bet):
            user = ctx.message.author
            if self.game_status != 1:
                await self.bot.say(
                    "No tables are open, please open one to play")
                return None

            if self.players_at_table(ctx.message.author):
                await self.bot.say(user.mention +
                                   "You're seated and ready to play")
                return None

            if float(bet) <= 0 and not self.coins.check_balance(bet):
                await self.bot.say(user.mention +
                                   " please make sure your bet is higher"
                                   "than 0 and that you've got enough coins")
                return None

            self.database.remove_coins(userid=user.id,
                                       coins=float(bet),
                                       mention=user.mention)

            # TODO : Make drawcard Dynamic
            self.holdem_players.append({
                "user":
                user,
                "cards": [self.drawCard(), self.drawCard()],
                "bet":
                float(bet),
                "Status":
                0
            })

            await self.bot.say(
                user.mention +
                " You've joined the table, please wait for the game to start,")

        @holdem.command(name="start", pass_context=True)
        async def start_game(self, ctx):

            if len(self.holdem_players) < 2:
                await self.bot.say(
                    "There needs to be at least 2 players at the table")
                return None
            # TODO: Solve this when you use the join command
            elif len(self.holdem_players) > 9:
                await self.bot.say("There are too many people at the table")
                return None

            self.game_status = 3
            self.dealersHand = [self.drawCard(), self.drawCard()]

            start_msg = "Let's play Texas Hold'em!"
            await self.bot.say(start_msg)

            for player in self.holdem_players:
                self.bot.send_message(
                    player["user"], "This is your hand: " +
                    player['cards'][0].getStringSymbol() +
                    player['cards'][0].getStringValue())
                pass

            community_cards_msg = "Here are the community cards: " + \
                         self.dealerCards[0].getStringSymbol() + self.dealerCards[0].getStringValue()
            await self.bot.say(community_cards_msg)

        @holdem.command(name="Call", pass_context=True)
        async def holdem_call(self, ctx):
            player = self.players_at_table(ctx.message.author)
            if player is None:
                await self.bot.say("You're not at this table, " +
                                   ctx.message.author.mention)
                return None

            if player["status"] != 0:
                await self.bot.say(ctx.message.author.mention +
                                   " is calling the bet")
                return None
            pass

        @holdem.command(name="raise", pass_context=True)
        async def holdem_stand(self, ctx, bet_raise):
            player = self.players_at_table(ctx.message.author)
            if player is None:
                await self.bot.say("You're not at this table, " +
                                   ctx.message.author.mention)
                return None

            if bet_raise <= 0 or None:
                await self.bot.say(ctx.message.author.mention +
                                   "You need to specify a bet")
                return None

            for player in self.players_at_table:
                if player["name"] == player:
                    player["bet"] += bet_raise

            await self.bot.say(ctx.message.author.mention + " Raised the bet")
Exemple #3
0
class Gamble:
    def __init__(self, bot):
        self.bot = bot  # The bot object.
        self.database = database.Database(self.bot)  # database object -> used to update and get coin amount
        self.blackjack_players = []
        self.blackjack_game_status = 0
        self.deck = []
        self.coins = Coin(bot)
        self.dealerCards = []


    @commands.group(name="bj", pass_context=True)
    async def bj (self, ctx) :
        if ctx.invoked_subcommand is None:
            await self.bot.say('Invalid rungame command passed...')

    @bj.command(name="new", pass_context=True)
    async def new_blackjack_game(self, ctx):
        if not global_methods.is_admin(ctx.message.author):
            await self.bot.say("You're not a big boy")
            return None
        if self.blackjack_game_status != 0:
            self.bot.say("A game is already in place.. wait until it is finished.")
            return None,

        self.deck = self.generateCards()
        self.blackjack_game_status = 1
        await self.bot.say("A blackjack table have now opened.. "
                           "please do ``` !bj join <bet> ``` to join the table.")

    @bj.command(name="join", pass_context=True)
    async def join_blackjack_game(self, ctx, bet):
        user = ctx.message.author
        if self.blackjack_game_status != 1:
            await self.bot.say("No table is open for the moment..")
            return None

        if self.player_in_blackjack_table(ctx.message.author) :
            await self.bot.say(
                "{}, You're in here, buddy!.".format(user.mention))
            return None

        if float(bet) <= 0 and not self.coins.check_balance(bet):
            await self.bot.say(
                "{}, please make sure your bet is higher than 0 and you've enough coins.".format(user.mention))
            return None

        self.database.remove_coins(userid=user.id, coins=float(bet), mention=user.mention)

        self.blackjack_players.append({
            "user": user, "cards": [self.drawCard(), self.drawCard()], "bet": float(bet), "status": 0
        })

        await self.bot.say(
            "{}, you've joined the table.. please wait for a host to start the round".format(user.mention))

    @bj.command(name="start", pass_context=True)
    async def start_blackjack_table(self, ctx):
        if not global_methods.is_admin(ctx.message.author):
            await self.bot.say("You're not a big boy")
            return None
        self.blackjack_game_status = 3;
        self.dealerCards = [self.drawCard(), self.drawCard()]
        output = "Welcome to the blackjack room!!! \n"
        output += "Dealers card shown to you fellows is {}\n".format(
            self.dealerCards[0].getStringSymbol() + self.dealerCards[0].getStringValue())
        output += "====================================================\n"
        for player in self.blackjack_players:
            output += "{} has these cards: {}. That's a total score of {}\n" \
                .format(player['user'].mention,
                        player['cards'][0].getStringSymbol() + player['cards'][0].getStringValue()
                        + " " + player['cards'][1].getStringSymbol() + player['cards'][1].getStringValue(),
                        self.blackjack_calculate_card_values(player['cards']))
            pass

        await self.bot.say(output)
        pass

    @bj.command(name = "hit", pass_context=True)
    async def blackjack_hit(self, ctx):
        player = self.player_in_blackjack_table(ctx.message.author)
        if player == None:
            await self.bot.say("{}, you're currently not in a game..".format(ctx.message.author.mention))
            return None

        if player['status'] != 0 :
            await self.bot.say("{}, you're standing..".format(ctx.message.author.mention))
            return None

        player['cards'].append(self.drawCard())
        output = "{}, you're selected hit.. \n".format(ctx.message.author.mention)

        cards = ""
        for card in player['cards'] : cards += card.getStringSymbol() + card.getStringValue()
        output += "{}, you've these cards: {}. That's a total score of {}\n" \
            .format(player['user'].mention, cards,
                    self.blackjack_calculate_card_values(player['cards']))

        if self.blackjack_calculate_card_values(player['cards']) >= 21:
            player['status'] = 1
            if self.blackjack_is_every_one_standing():
                await self.blackjack_calculate_winner()

        await self.bot.say(output)

    @bj.command(name="stand", pass_context=True)
    async def blackjack_stand(self, ctx):
        player = self.player_in_blackjack_table(ctx.message.author)
        if player == None:
            await self.bot.say("{}, you're currently not in a game..".format(ctx.message.author.mention))
            return None

        if player['status'] != 0:
            await self.bot.say("{}, you're standing..".format(ctx.message.author.mention))
            return None
        pass

        player['status'] = 1
        await self.bot.say("{}, you're standing..".format(ctx.message.author.mention))

        if self.blackjack_is_every_one_standing():
            await self.blackjack_calculate_winner()

    @bj.command(name="forceStand", pass_context=True)
    async def blackjack_force_stand(self, ctx) :
        if not global_methods.is_admin(ctx.message.author):
            await self.bot.say("You're not a big boy")
            return None
        for player in self.blackjack_players :
            player['status'] = 1

        await self.blackjack_calculate_winner()

    async def blackjack_calculate_winner(self):

        while self.blackjack_calculate_card_values(self.dealerCards) <= 17:
            self.dealerCards.append(self.drawCard())

        dealer_score = self.blackjack_calculate_card_values(self.dealerCards)
        dealer_cards = ""
        for card in self.dealerCards: dealer_cards += card.getStringSymbol() + card.getStringValue()
        output = "====================================================\n"
        output += "                                        ~WINNERS AND LOSERS~ \n"
        output += "Dealer got these cards: {}, that's a total score of {}\n".format(dealer_cards, dealer_score)

        for player in self.blackjack_players :
            player_score = self.blackjack_calculate_card_values(player['cards'])
            player_cards = ""
            for card in player['cards']: player_cards += card.getStringSymbol() + card.getStringValue()

            if dealer_score > 21 and player_score <= 21 or \
                dealer_score < 21 and player_score > dealer_score and player_score <= 21:
                output += "{} won over dealer with a score of {} with cards {} \n".format(
                    player['user'].mention, player_score, player_cards)
                if len(player['cards']) == 2 and player_score == 21 :
                    self.database.insert_coins(player['user'].id, player['bet'] * 3, player['user'].mention)
                else :
                    self.database.insert_coins(player['user'].id, player['bet'] * 2, player['user'].mention)

            elif dealer_score == player_score and dealer_score <= 21 :
                output += "{} draw with dealer with a score of {} with cards {} \n".format(
                    player['user'].mention, player_score, player_cards)

                self.database.insert_coins(player['user'].id, player['bet'], player['user'].mention)

            else :
                output += "{} lost against dealer with a score of {} with cards {} \n".format(
                    player['user'].mention, player_score, player_cards)

        self.blackjack_players = []
        self.dealerCards = []
        self.blackjack_game_status = 0
        output += "====================================================\n"
        await self.bot.say(output)



    def blackjack_is_every_one_standing(self):
        for player in self.blackjack_players :
            if player['status'] == 0: return False
        return True

    def player_in_blackjack_table(self, user):
        for player in self.blackjack_players :
            if player['user'] == user: return player
        return None

    def blackjack_calculate_card_values(self, cards:list):
        score = 0
        for card in cards :
            temp_score = score + card.getCardValue()
            if temp_score > 21 :
                if card.getCardValue() == 11 :
                    temp_score -= 10;
                else :
                    for c_temp in cards :
                        if c_temp.getCardValue() == 21 :
                            temp_score -= 10
                            if temp_score <= 21 : break

            score = temp_score

        return score

    def drawCard (self) :
        if not len(self.deck) > 0 :
            self.deck = self.generateCards()
        return self.deck.pop()


    def generateCards(self):
        '''
        Generate cards and then shuffle them
        @return shuffled deck
        '''
        cards = []  # The veriable that holds all the cards
        deck = 5  # How many deck that is created

        # create deck(s)
        for y in range(0, deck):
            symbol = 0  # deligere symbol
            value = 0  # Deligere value

            # Generate a deck of cards
            for x in range(0, 4):
                for i in range(0, 13):
                    obj = Card(symbol, value)
                    cards.append(obj)
                    value += 1
                symbol += 1
                value = 0

        # Shuffle the decks and return it
        return random.sample(cards, len(cards))