4e471aec by Barry

Bug fixes and a credit utilities.

1 parent 1dbdcd22
No preview for this file type
# -*- coding: utf-8 -*-
import requests
import discord
import random
......@@ -120,6 +121,21 @@ def db_get_credit(member_id):
if credits:
return credits[0]
def db_buy_ticket(member_id, amount):
c = conn.cursor()
credits = c.execute("SELECT credits, tickets FROM members WHERE member_id = ?;", (member_id,)).fetchone()
if not credits:
return False, "Unable to find your account"
if int(credits[0]) - int(100*amount) < 0:
return False, "You do not have enough credits to purchase a ticket. Credits: {} Tickets: {}".format(credits[0], credits[1])
c = conn.cursor()
cost = int(100 * amount)
c.execute("""UPDATE members SET credits = credits - ?, tickets = tickets + ?
WHERE member_id = ?;""", (cost, amount, member_id))
conn.commit()
return True, int(credits[1]) + int(amount)
def db_update_credit(member_id, amount):
c = conn.cursor()
credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
......@@ -128,7 +144,11 @@ def db_update_credit(member_id, amount):
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()
if int(credits[0]) < 0:
c.execute("""UPDATE members SET credits = 0
WHERE member_id = ?;""", (amount, member_id))
conn.commit()
c.execute("""UPDATE members SET credits = credits + ?
WHERE member_id = ?;""", (amount, member_id))
conn.commit()
......@@ -262,6 +282,18 @@ def db_add_game(member_id, game_name):
c.execute("UPDATE xmember_games SET launch_count = launch_count + 1 WHERE game_id = ? AND member_id = ?;", (db_game_id, member_id))
conn.commit()
def db_get_all_members():
# Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name
member_conn = sqlite3.connect('db.sqlite3')
c = member_conn.cursor()
results = c.execute("SELECT member_id, member_name, discord_id, discord_mention, is_afk, afk_at, status, prev_status, status_change_at, current_game FROM members;").fetchall()
member_conn.close()
members_list = []
for member in results:
members_list.append(dict_factory(c, member))
return members_list
def db_get_member(discord_id=None, username=None):
# Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name
member_conn = sqlite3.connect('db.sqlite3')
......@@ -449,10 +481,14 @@ 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:
!gimmecredits - Gives you some extra credits in case you run out.
!credits - Lists your current credits.
!bet <amount> - Start a game of BlackJack.
!hit - Draw a card
!stand - Show the cards
!buyticket - Purchases a raffle ticket for 100 credits
!raffle - Shows information about the current raffle
Spam:
!youtube <search term> - Returns the first video from the search results for the search term.
!gif <search term> - Returns the first gif from the search results.
......@@ -606,6 +642,62 @@ Stuff:
out_string = out_string[1900:]
return
if message.content.startswith('!gimmecredits'):
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'])
if credits < 5:
amount = random.randint(5, 50)
db_update_credit(member['member_id'], amount)
client.send_message(message.author, "You have been given {} credits.".format(amount,))
else:
client.send_message(message.author, "You already have credits. Stop begging.")
return
if message.content.startswith('!grantcredits'):
if message.author.id != '78767557628133376':
client.send_message(message.channel, "You are not Hellsbreath. Use !gimmecredits to get a few extra if you run out.")
return
members = db_get_all_members()
if len(members) < 0:
client.send_message(message.channel, "There was a problem looking up your information.")
else:
for member in members:
credits = db_get_credit(member['member_id'])
if credits < 100:
db_update_credit(member['member_id'], 100)
client.send_message(message.channel, "{} has been given {} credits.".format(member['member_name'], 100))
return
if message.content.startswith('!raffle'):
client.send_message(message.channel, """Current Raffle Item:
Game: The Witness
Description:
Inspired by Myst, The Witness has the player explore an open world island filled with a number of natural and man-made structures. The player progresses by solving puzzles which are based on interactions with mazes presented on panels around the island.
Raffle Date: 1/29/2016
You will be contacted if you win. To win you must purchase tickets with the !buyticket command for 100 credits.
You can get extra credits by playing !slots and !bet <amount> on BlackJack.
""")
return
if message.content.startswith('!buyticket'):
member = db_get_member(message.author.id)
if not member:
client.send_message(message.author, "There was a problem looking up your information.")
else:
result, response = db_buy_ticket(member['member_id'], 1)
if not result:
client.send_message(message.author, response)
return
credits = db_get_credit(member['member_id'])
client.send_message(message.author, "Raffle ticket purchased. Tickets: {} Credits: {}".format(response, credits))
return
if message.content.startswith('!credits'):
member = db_get_member(message.author.id)
if not member:
......@@ -615,6 +707,83 @@ Stuff:
client.send_message(message.author, "Credits: {}".format(credits))
return
if message.content.startswith('!slotsrules'):
client.send_message(message.channel, """Paying Combinations:
:moneybag:\t:moneybag:\t:moneybag:\t\t\t pays\t250
:bell:\t:bell:\t:bell:/:moneybag:\tpays\t20
:diamonds:\t:diamonds:\t:diamonds:/:moneybag:\tpays\t14
:spades:\t:spades:\t:spades:/:moneybag:\tpays\t10
:cherries:\t:cherries:\t:cherries:\t\t\t pays\t7
:cherries:\t:cherries:\t -\t\t\t\t pays\t5
:cherries:\t -\t\t -\t\t\t\t pays\t2
:black_square_button:\tblank space
All payouts are in credits. Each pull costs 1 credit. To Play: !slots""")
return
if message.content.startswith('!slots'):
member = db_get_member(message.author.id)
if not member:
client.send_message(message.author, "There was a problem looking up your information.")
return
elif type(message.channel) is not discord.channel.PrivateChannel:
client.send_message(message.author, "You must make all bets / gaming via private message.")
return
result, error_message = db_update_credit(member['member_id'], -1)
if not result:
client.send_message(message.author, error_message)
return
reel1 = [':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':cherries:', ':cherries:', ':cherries:', ':spades:', ':spades:', ':spades:', ':hearts:', ':hearts:', ':diamonds:', ':bell:', ':moneybag:']
reel2 = [':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':cherries:', ':cherries:', ':cherries:', ':spades:', ':spades:', ':spades:', ':hearts:', ':hearts:', ':diamonds:', ':bell:', ':moneybag:']
reel3 = [':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':black_square_button:', ':cherries:', ':cherries:', ':cherries:', ':spades:', ':spades:', ':spades:', ':hearts:', ':hearts:', ':diamonds:', ':bell:', ':moneybag:']
#:moneybag: :bell: :diamonds: :spades: :hearts:
val1 = random.choice(reel1)
val2 = random.choice(reel2)
val3 = random.choice(reel3)
winnings = 0
reels = [val1, val2, val3]
cherries = reels.count(":cherries:")
spades = reels.count(":spades:")
diamonds = reels.count(":diamonds:")
bells = reels.count(":bells:")
moneybags = reels.count(":moneybag:")
if moneybags == 3:
winnings = 250
elif bells == 3 or (bells == 2 and moneybags == 1):
winnings = 20
elif diamonds == 3 or (diamonds == 2 and moneybags == 1):
winnings = 14
elif spades == 3 or (spades == 2 and moneybags == 1):
winnings = 10
elif cherries == 3:
winnings = 7
elif cherries == 2:
winnings = 5
elif cherries == 1:
winnings = 2
out_string = """| {} | {} | {} |\n\n""".format(val1, val2, val3)
if winnings == 250:
out_string += "You Won the JACKPOT! Total Winnings: {}".format(winnings,)
elif winnings > 0:
out_string += "You Won! Total Winnings: {}".format(winnings,)
else:
out_string += "You lose. Total Winnings: {}".format(winnings,)
if winnings > 0:
result, error_message = db_update_credit(member['member_id'], winnings)
if not result:
client.send_message(message.author, error_message)
return
credits = db_get_credit(member['member_id'])
out_string += "\nCredits: {}".format(credits)
log(out_string)
client.send_message(message.author, out_string)
return
if message.content.startswith('!hit') or message.content.startswith('!draw'):
member = db_get_member(message.author.id)
if not member:
......