1dbdcd22 by Barry

Added credit tracking and handling for blackjack.

1 parent f5a1eebd
...@@ -111,17 +111,17 @@ class Blackjack: ...@@ -111,17 +111,17 @@ class Blackjack:
111 111
112 # TODO: Check for blackjack Ace + 10 pt 112 # TODO: Check for blackjack Ace + 10 pt
113 if self.player_points > 21: 113 if self.player_points > 21:
114 return (-self.bet, 'You Bust: {}') 114 return (0, 'You Bust. Total Winnings: {}')
115 elif self.dealer_points > 21: 115 elif self.dealer_points > 21:
116 return (int(self.bet * 2), 'Dealer Busts: {}') 116 return (int(self.bet * 2), 'Dealer Busts. Total Winnings {}')
117 elif self.dealer_hand.is_blackjack(): 117 elif self.dealer_hand.is_blackjack():
118 return (-self.bet, 'You lose: {}') 118 return (0, 'You lose. Total Winnings: {}')
119 elif self.player_points > self.dealer_points: 119 elif self.player_points > self.dealer_points:
120 return (int(self.bet * 2), 'You win: {}') 120 return (int(self.bet * 2), 'You win! Total Winnings {}')
121 elif self.player_points == self.dealer_points: 121 elif self.player_points == self.dealer_points:
122 return (self.bet, 'push your bet is returned: {}') 122 return (self.bet, 'push your bet is returned: {}')
123 else: 123 else:
124 return (-self.bet, 'You lose: {}') 124 return (0, 'You lose: {}')
125 125
126 def print_hand(self, show_dealer=False): 126 def print_hand(self, show_dealer=False):
127 out_string = "" 127 out_string = ""
......
No preview for this file type
...@@ -114,20 +114,37 @@ def dict_factory(cursor, row): ...@@ -114,20 +114,37 @@ def dict_factory(cursor, row):
114 d[col[0]] = row[idx] 114 d[col[0]] = row[idx]
115 return d 115 return d
116 116
117 def db_get_credit(member_id):
118 c = conn.cursor()
119 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
120 if credits:
121 return credits[0]
122
123 def db_update_credit(member_id, amount):
124 c = conn.cursor()
125 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
126 if not credits:
127 return False, "Unable to find your account"
128
129 if int(credits[0]) + int(amount) < 0:
130 return False, "You do not have enough credits to cover amount requested. Credit: {} Amount Requested: {}".format(credits[0], amount)
131 c = conn.cursor()
132 c.execute("""UPDATE members SET credits = credits + ?
133 WHERE member_id = ?;""", (amount, member_id))
134 conn.commit()
135 return True, ""
136
117 def db_add_minigame(member_id, minigame_name, state): 137 def db_add_minigame(member_id, minigame_name, state):
118 c = conn.cursor() 138 c = conn.cursor()
119 db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id,minigame_name)).fetchone() 139 db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
120 if not db_state: 140 if not db_state:
121 c = conn.cursor()
122 c.execute("""INSERT INTO minigames(member_id, minigame_name, state) 141 c.execute("""INSERT INTO minigames(member_id, minigame_name, state)
123 VALUES(?, ?, ?);""", (member_id, minigame_name, state)) 142 VALUES(?, ?, ?);""", (member_id, minigame_name, state))
124 conn.commit() 143 conn.commit()
125 else: 144 else:
126 c = conn.cursor()
127 c.execute("""UPDATE minigames SET state = ? 145 c.execute("""UPDATE minigames SET state = ?
128 WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name)) 146 WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name))
129 conn.commit() 147 conn.commit()
130 pass
131 148
132 def db_get_minigame_state(member_id, minigame_name): 149 def db_get_minigame_state(member_id, minigame_name):
133 c = conn.cursor() 150 c = conn.cursor()
...@@ -432,6 +449,7 @@ Games: ...@@ -432,6 +449,7 @@ Games:
432 !gameslist <count> - Returns a list of the top 20 games and the number of people who have played that game. if you pass a limit it will show that many games instead. 449 !gameslist <count> - Returns a list of the top 20 games and the number of people who have played that game. if you pass a limit it will show that many games instead.
433 !whoplayed <gamename> - Returns a list of players who have played the game. 450 !whoplayed <gamename> - Returns a list of players who have played the game.
434 Minigames: 451 Minigames:
452 !credits - Lists your current credits.
435 !bet <amount> - Start a game of BlackJack. 453 !bet <amount> - Start a game of BlackJack.
436 !hit - Draw a card 454 !hit - Draw a card
437 !stand - Show the cards 455 !stand - Show the cards
...@@ -588,6 +606,15 @@ Stuff: ...@@ -588,6 +606,15 @@ Stuff:
588 out_string = out_string[1900:] 606 out_string = out_string[1900:]
589 return 607 return
590 608
609 if message.content.startswith('!credits'):
610 member = db_get_member(message.author.id)
611 if not member:
612 client.send_message(message.author, "There was a problem looking up your information.")
613 else:
614 credits = db_get_credit(member['member_id'])
615 client.send_message(message.author, "Credits: {}".format(credits))
616 return
617
591 if message.content.startswith('!hit') or message.content.startswith('!draw'): 618 if message.content.startswith('!hit') or message.content.startswith('!draw'):
592 member = db_get_member(message.author.id) 619 member = db_get_member(message.author.id)
593 if not member: 620 if not member:
...@@ -610,6 +637,14 @@ Stuff: ...@@ -610,6 +637,14 @@ Stuff:
610 out_string += "\n\n" + bj.print_hand(show_dealer=True) 637 out_string += "\n\n" + bj.print_hand(show_dealer=True)
611 out_string += "\n\n" + response.format(win,) 638 out_string += "\n\n" + response.format(win,)
612 db_delete_minigame_state(member['member_id'], 'blackjack') 639 db_delete_minigame_state(member['member_id'], 'blackjack')
640 result, error_message = db_update_credit(member['member_id'], int(win))
641 if not result:
642 client.send_message(message.author, error_message)
643 return
644
645 credits = db_get_credit(member['member_id'])
646 out_string += "\nCredits: {}".format(credits)
647
613 client.send_message(message.author, out_string) 648 client.send_message(message.author, out_string)
614 649
615 else: 650 else:
...@@ -632,7 +667,12 @@ Stuff: ...@@ -632,7 +667,12 @@ Stuff:
632 out_string += "\n\n" + bj.print_hand(show_dealer=True) 667 out_string += "\n\n" + bj.print_hand(show_dealer=True)
633 out_string += "\n\n" + response.format(win,) 668 out_string += "\n\n" + response.format(win,)
634 db_delete_minigame_state(member['member_id'], 'blackjack') 669 db_delete_minigame_state(member['member_id'], 'blackjack')
635 670 result, error_message = db_update_credit(member['member_id'], int(win))
671 if not result:
672 client.send_message(message.author, error_message)
673 return
674 credits = db_get_credit(member['member_id'])
675 out_string += "\nCredits: {}".format(credits)
636 client.send_message(message.author, out_string) 676 client.send_message(message.author, out_string)
637 return 677 return
638 678
...@@ -659,18 +699,31 @@ Stuff: ...@@ -659,18 +699,31 @@ Stuff:
659 out_string += "\n\n" + bj.print_hand(show_dealer=True) 699 out_string += "\n\n" + bj.print_hand(show_dealer=True)
660 out_string += "\n\n" + response.format(win,) 700 out_string += "\n\n" + response.format(win,)
661 db_delete_minigame_state(member['member_id'], 'blackjack') 701 db_delete_minigame_state(member['member_id'], 'blackjack')
702 result, error_message = db_update_credit(member['member_id'], int(win))
703 if not result:
704 client.send_message(message.author, error_message)
705 return
706 credits = db_get_credit(member['member_id'])
707 out_string += "\nCredits: {}".format(credits)
708
662 client.send_message(message.author, out_string) 709 client.send_message(message.author, out_string)
663 710
664 return 711 return
665 712
666 out_string = "" 713 out_string = ""
667 bet_amount = message.content[5:] 714 bet_amount = message.content[5:]
715 log("Member: {} Bet: {}".format(member['member_name'], bet_amount))
716
668 if not bet_amount.isdigit(): 717 if not bet_amount.isdigit():
669 client.send_message(message.author, "Please provide a bet amount. !bet 10") 718 client.send_message(message.author, "Please provide a bet amount. !bet 10")
670 return 719 return
720
721 result, error_message = db_update_credit(member['member_id'], -int(bet_amount))
722 if not result:
723 client.send_message(message.author, error_message)
724 return
671 out_string += "Welcome to BlackJack! :flower_playing_cards: You have placed a bet of: {}\n".format(bet_amount) 725 out_string += "Welcome to BlackJack! :flower_playing_cards: You have placed a bet of: {}\n".format(bet_amount)
672 bj = Blackjack(bet_amount) 726 bj = Blackjack(bet_amount)
673
674 out_string += bj.print_hand() 727 out_string += bj.print_hand()
675 actions = bj.get_actions() 728 actions = bj.get_actions()
676 if len(actions) > 0: 729 if len(actions) > 0:
...@@ -681,6 +734,12 @@ Stuff: ...@@ -681,6 +734,12 @@ Stuff:
681 out_string += "\n\n" + bj.print_hand(show_dealer=True) 734 out_string += "\n\n" + bj.print_hand(show_dealer=True)
682 out_string += "\n\n" + response.format(win,) 735 out_string += "\n\n" + response.format(win,)
683 db_delete_minigame_state(member['member_id'], 'blackjack') 736 db_delete_minigame_state(member['member_id'], 'blackjack')
737 result, error_message = db_update_credit(member['member_id'], int(win))
738 if not result:
739 client.send_message(message.author, error_message)
740 return
741 credits = db_get_credit(member['member_id'])
742 out_string += "\nCredits: {}".format(credits)
684 743
685 client.send_message(message.author, out_string) 744 client.send_message(message.author, out_string)
686 745
......