1dbdcd22 by Barry

Added credit tracking and handling for blackjack.

1 parent f5a1eebd
......@@ -111,17 +111,17 @@ class Blackjack:
# TODO: Check for blackjack Ace + 10 pt
if self.player_points > 21:
return (-self.bet, 'You Bust: {}')
return (0, 'You Bust. Total Winnings: {}')
elif self.dealer_points > 21:
return (int(self.bet * 2), 'Dealer Busts: {}')
return (int(self.bet * 2), 'Dealer Busts. Total Winnings {}')
elif self.dealer_hand.is_blackjack():
return (-self.bet, 'You lose: {}')
return (0, 'You lose. Total Winnings: {}')
elif self.player_points > self.dealer_points:
return (int(self.bet * 2), 'You win: {}')
return (int(self.bet * 2), 'You win! Total Winnings {}')
elif self.player_points == self.dealer_points:
return (self.bet, 'push your bet is returned: {}')
else:
return (-self.bet, 'You lose: {}')
return (0, 'You lose: {}')
def print_hand(self, show_dealer=False):
out_string = ""
......
No preview for this file type
......@@ -114,20 +114,37 @@ def dict_factory(cursor, row):
d[col[0]] = row[idx]
return d
def db_get_credit(member_id):
c = conn.cursor()
credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
if credits:
return credits[0]
def db_update_credit(member_id, amount):
c = conn.cursor()
credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
if not credits:
return False, "Unable to find your account"
if int(credits[0]) + int(amount) < 0:
return False, "You do not have enough credits to cover amount requested. Credit: {} Amount Requested: {}".format(credits[0], amount)
c = conn.cursor()
c.execute("""UPDATE members SET credits = credits + ?
WHERE member_id = ?;""", (amount, member_id))
conn.commit()
return True, ""
def db_add_minigame(member_id, minigame_name, state):
c = conn.cursor()
db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id,minigame_name)).fetchone()
db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
if not db_state:
c = conn.cursor()
c.execute("""INSERT INTO minigames(member_id, minigame_name, state)
VALUES(?, ?, ?);""", (member_id, minigame_name, state))
conn.commit()
else:
c = conn.cursor()
c.execute("""UPDATE minigames SET state = ?
WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name))
conn.commit()
pass
def db_get_minigame_state(member_id, minigame_name):
c = conn.cursor()
......@@ -432,6 +449,7 @@ Games:
!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.
!whoplayed <gamename> - Returns a list of players who have played the game.
Minigames:
!credits - Lists your current credits.
!bet <amount> - Start a game of BlackJack.
!hit - Draw a card
!stand - Show the cards
......@@ -588,6 +606,15 @@ Stuff:
out_string = out_string[1900:]
return
if message.content.startswith('!credits'):
member = db_get_member(message.author.id)
if not member:
client.send_message(message.author, "There was a problem looking up your information.")
else:
credits = db_get_credit(member['member_id'])
client.send_message(message.author, "Credits: {}".format(credits))
return
if message.content.startswith('!hit') or message.content.startswith('!draw'):
member = db_get_member(message.author.id)
if not member:
......@@ -610,6 +637,14 @@ Stuff:
out_string += "\n\n" + bj.print_hand(show_dealer=True)
out_string += "\n\n" + response.format(win,)
db_delete_minigame_state(member['member_id'], 'blackjack')
result, error_message = db_update_credit(member['member_id'], int(win))
if not result:
client.send_message(message.author, error_message)
return
credits = db_get_credit(member['member_id'])
out_string += "\nCredits: {}".format(credits)
client.send_message(message.author, out_string)
else:
......@@ -632,7 +667,12 @@ Stuff:
out_string += "\n\n" + bj.print_hand(show_dealer=True)
out_string += "\n\n" + response.format(win,)
db_delete_minigame_state(member['member_id'], 'blackjack')
result, error_message = db_update_credit(member['member_id'], int(win))
if not result:
client.send_message(message.author, error_message)
return
credits = db_get_credit(member['member_id'])
out_string += "\nCredits: {}".format(credits)
client.send_message(message.author, out_string)
return
......@@ -659,18 +699,31 @@ Stuff:
out_string += "\n\n" + bj.print_hand(show_dealer=True)
out_string += "\n\n" + response.format(win,)
db_delete_minigame_state(member['member_id'], 'blackjack')
result, error_message = db_update_credit(member['member_id'], int(win))
if not result:
client.send_message(message.author, error_message)
return
credits = db_get_credit(member['member_id'])
out_string += "\nCredits: {}".format(credits)
client.send_message(message.author, out_string)
return
out_string = ""
bet_amount = message.content[5:]
log("Member: {} Bet: {}".format(member['member_name'], bet_amount))
if not bet_amount.isdigit():
client.send_message(message.author, "Please provide a bet amount. !bet 10")
return
result, error_message = db_update_credit(member['member_id'], -int(bet_amount))
if not result:
client.send_message(message.author, error_message)
return
out_string += "Welcome to BlackJack! :flower_playing_cards: You have placed a bet of: {}\n".format(bet_amount)
bj = Blackjack(bet_amount)
out_string += bj.print_hand()
actions = bj.get_actions()
if len(actions) > 0:
......@@ -681,6 +734,12 @@ Stuff:
out_string += "\n\n" + bj.print_hand(show_dealer=True)
out_string += "\n\n" + response.format(win,)
db_delete_minigame_state(member['member_id'], 'blackjack')
result, error_message = db_update_credit(member['member_id'], int(win))
if not result:
client.send_message(message.author, error_message)
return
credits = db_get_credit(member['member_id'])
out_string += "\nCredits: {}".format(credits)
client.send_message(message.author, out_string)
......