deabf035 by Barry

Fix for threading in database calls.

1 parent ee933491
import operator
import sqlite3
import datetime
import logging
conn = sqlite3.connect('db.sqlite3')
def log(message):
try:
......@@ -14,6 +14,7 @@ def log(message):
## Database Calls
##################
# Converts a row into a dictionary
def dict_factory(cursor, row):
if row == None:
......@@ -24,27 +25,37 @@ def dict_factory(cursor, row):
return d
def db_get_credit(member_id):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
if credits:
return credits[0]
def db_buy_ticket(member_id, amount):
log("buying ticket - DB")
try:
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
credits = c.execute("SELECT credits, tickets FROM members WHERE member_id = ?;", (member_id,)).fetchone()
log(credits)
if not credits:
log('Bad account lookup, missing credits')
return False, "Unable to find your account"
if int(credits[0]) - int(100*amount) < 0:
log('Not enough credits')
return False, "You do not have enough credits to purchase a ticket. Credits: {} Tickets: {}".format(credits[0], credits[1])
conn = sqlite3.connect('db.sqlite3')
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)
except Exception as e:
log(e)
def db_update_credit(member_id, amount):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
if not credits:
......@@ -63,6 +74,7 @@ def db_update_credit(member_id, amount):
return True, ""
def db_add_minigame(member_id, minigame_name, state):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
if not db_state:
......@@ -75,28 +87,33 @@ def db_add_minigame(member_id, minigame_name, state):
conn.commit()
def db_get_minigame_state(member_id, minigame_name):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
if state:
return state[0]
def db_delete_minigame_state(member_id, minigame_name):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("DELETE FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name))
conn.commit()
def db_add_message(message, delivery_time, channel, message_from, message_to, user_id):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("""INSERT INTO messages(message, delivery_time, channel, message_from, message_to, user_id)
VALUES(?, ?, ?, ?, ?, ?);""", (message, delivery_time, channel, message_from, message_to, user_id))
conn.commit()
def db_delete_sent_message(message_id):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("DELETE FROM messages WHERE message_id = ?;", (message_id,))
conn.commit()
def db_get_aliases(member_id):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
aliases = c.execute("SELECT alias_name FROM aliases WHERE member_id = ?;", (member_id,)).fetchall()
if aliases:
......@@ -108,6 +125,7 @@ def db_get_aliases(member_id):
return None
def db_add_aliases(member_id, alias_name):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("INSERT INTO aliases(alias_name, member_id) VALUES (?, ?);", (alias_name, member_id,))
conn.commit()
......@@ -127,6 +145,7 @@ def db_get_messages():
return db_messages
def db_get_whoplayed(game_name):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
members = c.execute("""SELECT m.member_name, xmg.launch_count
FROM members m
......@@ -144,6 +163,7 @@ def db_get_whoplayed(game_name):
#return sorted(member_list, reverse=True, key=lambda tup: tup[1])
def db_get_games(username):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
games = c.execute("""SELECT g.game_name, xmg.launch_count FROM games g
INNER JOIN xmember_games xmg ON
......@@ -157,6 +177,7 @@ def db_get_games(username):
return games_list
def db_get_games_list(limit):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
games_list = c.execute("""SELECT g.game_name, count(DISTINCT xmg.member_id)
FROM games g
......@@ -169,6 +190,7 @@ def db_get_games_list(limit):
def db_add_game(member_id, game_name):
# 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
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
games = c.execute("SELECT game_id FROM games WHERE game_name = ?;", (game_name,)).fetchone()
db_game_id = 0
......@@ -210,13 +232,14 @@ def db_get_member(discord_id=None, username=None):
result = None
if discord_id:
result = c.execute("SELECT member_id, member_name, discord_id, discord_mention, is_afk, afk_at, status, prev_status, status_change_at, current_game, credits, tickets FROM members WHERE discord_id = ?;", (discord_id,)).fetchone()
if username:
elif username:
result = c.execute("SELECT member_id, member_name, discord_id, discord_mention, is_afk, afk_at, status, prev_status, status_change_at, current_game, credits, tickets FROM members WHERE member_name = ?;", (username,)).fetchone()
member_conn.close()
return dict_factory(c, result)
def db_create_member(member):
# 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
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("""INSERT INTO members (member_name, discord_id, discord_mention,
is_afk, afk_at, status, prev_status,
......@@ -238,6 +261,7 @@ def db_update_member(member, db_member):
status = db_member['status']
prev_status = db_member['prev_status']
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
if member.name.lower() != db_membername:
log("Member Name changed! {} to {}".format(byteify(db_membername), byteify(member.name.lower())))
......
No preview for this file type
......@@ -521,11 +521,17 @@ You can get extra credits by playing !slots <amount> and !bet <amount> on BlackJ
def do_buyticket(client, message_parts, message):
log("Buying Ticket")
member = data.db_get_member(message.author.id)
log("Member: {}".format(member,))
if not member:
log("there")
client.send_message(message.author, "There was a problem looking up your information.")
else:
log("here")
log("Buying Ticket for: {}".format(byteify(member['member_name'])))
result, response = data.db_buy_ticket(member['member_id'], 1)
log("Buy Result: {} - {}".format(result, response))
if not result:
client.send_message(message.author, response)
return
......@@ -968,6 +974,7 @@ def on_message(message):
for command, method in registered_commands.iteritems():
if message_parts[0] == command:
try:
log("Calling {}".format(method,))
thread.start_new_thread(globals()[method], (client, message_parts[1:], message))
except Exception as e:
log("{} - {}".format(format_exception(e), e.message))
......