deabf035 by Barry

Fix for threading in database calls.

1 parent ee933491
1 import operator 1 import operator
2 import sqlite3 2 import sqlite3
3 import datetime 3 import datetime
4 import logging
4 5
5 conn = sqlite3.connect('db.sqlite3')
6 6
7 def log(message): 7 def log(message):
8 try: 8 try:
...@@ -14,6 +14,7 @@ def log(message): ...@@ -14,6 +14,7 @@ def log(message):
14 ## Database Calls 14 ## Database Calls
15 ################## 15 ##################
16 16
17
17 # Converts a row into a dictionary 18 # Converts a row into a dictionary
18 def dict_factory(cursor, row): 19 def dict_factory(cursor, row):
19 if row == None: 20 if row == None:
...@@ -24,27 +25,37 @@ def dict_factory(cursor, row): ...@@ -24,27 +25,37 @@ def dict_factory(cursor, row):
24 return d 25 return d
25 26
26 def db_get_credit(member_id): 27 def db_get_credit(member_id):
28 conn = sqlite3.connect('db.sqlite3')
27 c = conn.cursor() 29 c = conn.cursor()
28 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone() 30 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
29 if credits: 31 if credits:
30 return credits[0] 32 return credits[0]
31 33
32 def db_buy_ticket(member_id, amount): 34 def db_buy_ticket(member_id, amount):
33 c = conn.cursor() 35 log("buying ticket - DB")
34 credits = c.execute("SELECT credits, tickets FROM members WHERE member_id = ?;", (member_id,)).fetchone() 36 try:
35 if not credits: 37 conn = sqlite3.connect('db.sqlite3')
36 return False, "Unable to find your account" 38 c = conn.cursor()
37 39 credits = c.execute("SELECT credits, tickets FROM members WHERE member_id = ?;", (member_id,)).fetchone()
38 if int(credits[0]) - int(100*amount) < 0: 40 log(credits)
39 return False, "You do not have enough credits to purchase a ticket. Credits: {} Tickets: {}".format(credits[0], credits[1]) 41 if not credits:
40 c = conn.cursor() 42 log('Bad account lookup, missing credits')
41 cost = int(100 * amount) 43 return False, "Unable to find your account"
42 c.execute("""UPDATE members SET credits = credits - ?, tickets = tickets + ? 44
43 WHERE member_id = ?;""", (cost, amount, member_id)) 45 if int(credits[0]) - int(100*amount) < 0:
44 conn.commit() 46 log('Not enough credits')
45 return True, int(credits[1]) + int(amount) 47 return False, "You do not have enough credits to purchase a ticket. Credits: {} Tickets: {}".format(credits[0], credits[1])
46 48 conn = sqlite3.connect('db.sqlite3')
49 c = conn.cursor()
50 cost = int(100 * amount)
51 c.execute("""UPDATE members SET credits = credits - ?, tickets = tickets + ?
52 WHERE member_id = ?;""", (cost, amount, member_id))
53 conn.commit()
54 return True, int(credits[1]) + int(amount)
55 except Exception as e:
56 log(e)
47 def db_update_credit(member_id, amount): 57 def db_update_credit(member_id, amount):
58 conn = sqlite3.connect('db.sqlite3')
48 c = conn.cursor() 59 c = conn.cursor()
49 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone() 60 credits = c.execute("SELECT credits FROM members WHERE member_id = ?;", (member_id,)).fetchone()
50 if not credits: 61 if not credits:
...@@ -63,6 +74,7 @@ def db_update_credit(member_id, amount): ...@@ -63,6 +74,7 @@ def db_update_credit(member_id, amount):
63 return True, "" 74 return True, ""
64 75
65 def db_add_minigame(member_id, minigame_name, state): 76 def db_add_minigame(member_id, minigame_name, state):
77 conn = sqlite3.connect('db.sqlite3')
66 c = conn.cursor() 78 c = conn.cursor()
67 db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone() 79 db_state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
68 if not db_state: 80 if not db_state:
...@@ -75,28 +87,33 @@ def db_add_minigame(member_id, minigame_name, state): ...@@ -75,28 +87,33 @@ def db_add_minigame(member_id, minigame_name, state):
75 conn.commit() 87 conn.commit()
76 88
77 def db_get_minigame_state(member_id, minigame_name): 89 def db_get_minigame_state(member_id, minigame_name):
90 conn = sqlite3.connect('db.sqlite3')
78 c = conn.cursor() 91 c = conn.cursor()
79 state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone() 92 state = c.execute("SELECT state FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)).fetchone()
80 if state: 93 if state:
81 return state[0] 94 return state[0]
82 95
83 def db_delete_minigame_state(member_id, minigame_name): 96 def db_delete_minigame_state(member_id, minigame_name):
97 conn = sqlite3.connect('db.sqlite3')
84 c = conn.cursor() 98 c = conn.cursor()
85 c.execute("DELETE FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name)) 99 c.execute("DELETE FROM minigames WHERE member_id = ? AND minigame_name = ?;", (member_id, minigame_name))
86 conn.commit() 100 conn.commit()
87 101
88 def db_add_message(message, delivery_time, channel, message_from, message_to, user_id): 102 def db_add_message(message, delivery_time, channel, message_from, message_to, user_id):
103 conn = sqlite3.connect('db.sqlite3')
89 c = conn.cursor() 104 c = conn.cursor()
90 c.execute("""INSERT INTO messages(message, delivery_time, channel, message_from, message_to, user_id) 105 c.execute("""INSERT INTO messages(message, delivery_time, channel, message_from, message_to, user_id)
91 VALUES(?, ?, ?, ?, ?, ?);""", (message, delivery_time, channel, message_from, message_to, user_id)) 106 VALUES(?, ?, ?, ?, ?, ?);""", (message, delivery_time, channel, message_from, message_to, user_id))
92 conn.commit() 107 conn.commit()
93 108
94 def db_delete_sent_message(message_id): 109 def db_delete_sent_message(message_id):
110 conn = sqlite3.connect('db.sqlite3')
95 c = conn.cursor() 111 c = conn.cursor()
96 c.execute("DELETE FROM messages WHERE message_id = ?;", (message_id,)) 112 c.execute("DELETE FROM messages WHERE message_id = ?;", (message_id,))
97 conn.commit() 113 conn.commit()
98 114
99 def db_get_aliases(member_id): 115 def db_get_aliases(member_id):
116 conn = sqlite3.connect('db.sqlite3')
100 c = conn.cursor() 117 c = conn.cursor()
101 aliases = c.execute("SELECT alias_name FROM aliases WHERE member_id = ?;", (member_id,)).fetchall() 118 aliases = c.execute("SELECT alias_name FROM aliases WHERE member_id = ?;", (member_id,)).fetchall()
102 if aliases: 119 if aliases:
...@@ -108,6 +125,7 @@ def db_get_aliases(member_id): ...@@ -108,6 +125,7 @@ def db_get_aliases(member_id):
108 return None 125 return None
109 126
110 def db_add_aliases(member_id, alias_name): 127 def db_add_aliases(member_id, alias_name):
128 conn = sqlite3.connect('db.sqlite3')
111 c = conn.cursor() 129 c = conn.cursor()
112 c.execute("INSERT INTO aliases(alias_name, member_id) VALUES (?, ?);", (alias_name, member_id,)) 130 c.execute("INSERT INTO aliases(alias_name, member_id) VALUES (?, ?);", (alias_name, member_id,))
113 conn.commit() 131 conn.commit()
...@@ -127,6 +145,7 @@ def db_get_messages(): ...@@ -127,6 +145,7 @@ def db_get_messages():
127 return db_messages 145 return db_messages
128 146
129 def db_get_whoplayed(game_name): 147 def db_get_whoplayed(game_name):
148 conn = sqlite3.connect('db.sqlite3')
130 c = conn.cursor() 149 c = conn.cursor()
131 members = c.execute("""SELECT m.member_name, xmg.launch_count 150 members = c.execute("""SELECT m.member_name, xmg.launch_count
132 FROM members m 151 FROM members m
...@@ -144,6 +163,7 @@ def db_get_whoplayed(game_name): ...@@ -144,6 +163,7 @@ def db_get_whoplayed(game_name):
144 #return sorted(member_list, reverse=True, key=lambda tup: tup[1]) 163 #return sorted(member_list, reverse=True, key=lambda tup: tup[1])
145 164
146 def db_get_games(username): 165 def db_get_games(username):
166 conn = sqlite3.connect('db.sqlite3')
147 c = conn.cursor() 167 c = conn.cursor()
148 games = c.execute("""SELECT g.game_name, xmg.launch_count FROM games g 168 games = c.execute("""SELECT g.game_name, xmg.launch_count FROM games g
149 INNER JOIN xmember_games xmg ON 169 INNER JOIN xmember_games xmg ON
...@@ -157,6 +177,7 @@ def db_get_games(username): ...@@ -157,6 +177,7 @@ def db_get_games(username):
157 return games_list 177 return games_list
158 178
159 def db_get_games_list(limit): 179 def db_get_games_list(limit):
180 conn = sqlite3.connect('db.sqlite3')
160 c = conn.cursor() 181 c = conn.cursor()
161 games_list = c.execute("""SELECT g.game_name, count(DISTINCT xmg.member_id) 182 games_list = c.execute("""SELECT g.game_name, count(DISTINCT xmg.member_id)
162 FROM games g 183 FROM games g
...@@ -169,6 +190,7 @@ def db_get_games_list(limit): ...@@ -169,6 +190,7 @@ def db_get_games_list(limit):
169 190
170 def db_add_game(member_id, game_name): 191 def db_add_game(member_id, game_name):
171 # 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 192 # 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
193 conn = sqlite3.connect('db.sqlite3')
172 c = conn.cursor() 194 c = conn.cursor()
173 games = c.execute("SELECT game_id FROM games WHERE game_name = ?;", (game_name,)).fetchone() 195 games = c.execute("SELECT game_id FROM games WHERE game_name = ?;", (game_name,)).fetchone()
174 db_game_id = 0 196 db_game_id = 0
...@@ -210,13 +232,14 @@ def db_get_member(discord_id=None, username=None): ...@@ -210,13 +232,14 @@ def db_get_member(discord_id=None, username=None):
210 result = None 232 result = None
211 if discord_id: 233 if discord_id:
212 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() 234 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()
213 if username: 235 elif username:
214 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() 236 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()
215 member_conn.close() 237 member_conn.close()
216 return dict_factory(c, result) 238 return dict_factory(c, result)
217 239
218 def db_create_member(member): 240 def db_create_member(member):
219 # 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 241 # 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
242 conn = sqlite3.connect('db.sqlite3')
220 c = conn.cursor() 243 c = conn.cursor()
221 c.execute("""INSERT INTO members (member_name, discord_id, discord_mention, 244 c.execute("""INSERT INTO members (member_name, discord_id, discord_mention,
222 is_afk, afk_at, status, prev_status, 245 is_afk, afk_at, status, prev_status,
...@@ -238,6 +261,7 @@ def db_update_member(member, db_member): ...@@ -238,6 +261,7 @@ def db_update_member(member, db_member):
238 status = db_member['status'] 261 status = db_member['status']
239 prev_status = db_member['prev_status'] 262 prev_status = db_member['prev_status']
240 263
264 conn = sqlite3.connect('db.sqlite3')
241 c = conn.cursor() 265 c = conn.cursor()
242 if member.name.lower() != db_membername: 266 if member.name.lower() != db_membername:
243 log("Member Name changed! {} to {}".format(byteify(db_membername), byteify(member.name.lower()))) 267 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 ...@@ -521,11 +521,17 @@ You can get extra credits by playing !slots <amount> and !bet <amount> on BlackJ
521 521
522 522
523 def do_buyticket(client, message_parts, message): 523 def do_buyticket(client, message_parts, message):
524 log("Buying Ticket")
524 member = data.db_get_member(message.author.id) 525 member = data.db_get_member(message.author.id)
526 log("Member: {}".format(member,))
525 if not member: 527 if not member:
528 log("there")
526 client.send_message(message.author, "There was a problem looking up your information.") 529 client.send_message(message.author, "There was a problem looking up your information.")
527 else: 530 else:
531 log("here")
532 log("Buying Ticket for: {}".format(byteify(member['member_name'])))
528 result, response = data.db_buy_ticket(member['member_id'], 1) 533 result, response = data.db_buy_ticket(member['member_id'], 1)
534 log("Buy Result: {} - {}".format(result, response))
529 if not result: 535 if not result:
530 client.send_message(message.author, response) 536 client.send_message(message.author, response)
531 return 537 return
...@@ -968,6 +974,7 @@ def on_message(message): ...@@ -968,6 +974,7 @@ def on_message(message):
968 for command, method in registered_commands.iteritems(): 974 for command, method in registered_commands.iteritems():
969 if message_parts[0] == command: 975 if message_parts[0] == command:
970 try: 976 try:
977 log("Calling {}".format(method,))
971 thread.start_new_thread(globals()[method], (client, message_parts[1:], message)) 978 thread.start_new_thread(globals()[method], (client, message_parts[1:], message))
972 except Exception as e: 979 except Exception as e:
973 log("{} - {}".format(format_exception(e), e.message)) 980 log("{} - {}".format(format_exception(e), e.message))
......