71937a50 by Barry

Added some interfaces to the pankration and db

1 parent 9f06bad8
...@@ -2,6 +2,7 @@ import operator ...@@ -2,6 +2,7 @@ import operator
2 import sqlite3 2 import sqlite3
3 import datetime 3 import datetime
4 import logging 4 import logging
5 import pickle
5 6
6 7
7 def byteify(input): 8 def byteify(input):
...@@ -236,6 +237,36 @@ def db_get_all_members(): ...@@ -236,6 +237,36 @@ def db_get_all_members():
236 members_list.append(dict_factory(c, member)) 237 members_list.append(dict_factory(c, member))
237 return members_list 238 return members_list
238 239
240 def db_get_pankration_record(member_id):
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 pan_record = sqlite3.connect('db.sqlite3')
243
244 c = pan_record.cursor()
245 result = None
246 result = c.execute("SELECT * FROM pankration WHERE member_id = ?;", (member_id,)).fetchone()
247 pan_record.close()
248 return dict_factory(c, result)
249
250 def db_add_monster(member_id, monster):
251 pan_record = db_get_pankration_record(member_id)
252 log("{}".format(member_id))
253 conn = sqlite3.connect('db.sqlite3')
254 c = conn.cursor()
255 db_state = c.execute("SELECT monster_inventory FROM pankration WHERE member_id = ?;", (member_id,)).fetchone()
256 if not db_state:
257 c.execute("""INSERT INTO pankration(member_id, wins, losses, monster_inventory)
258 VALUES(?, ?, ?, ?);""", (member_id, 0, 0, pickle.dumps([monster])))
259 conn.commit()
260 else:
261 log(db_state)
262 inv = str(db_state[0])
263 log(inv)
264 monster_inventory = pickle.loads(inv)
265 monster_inventory.append(monster)
266 c.execute("""UPDATE pankration SET monster_inventory = ?
267 WHERE member_id = ?;""", (pickle.dumps(monster_inventory), member_id))
268 conn.commit()
269
239 def db_get_member(discord_id=None, username=None): 270 def db_get_member(discord_id=None, username=None):
240 # 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 271 # 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 member_conn = sqlite3.connect('db.sqlite3') 272 member_conn = sqlite3.connect('db.sqlite3')
......
No preview for this file type
...@@ -85,6 +85,7 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help', ...@@ -85,6 +85,7 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help',
85 '!rigged': 'do_rigged', 85 '!rigged': 'do_rigged',
86 '!listzones': 'do_list_zones', 86 '!listzones': 'do_list_zones',
87 '!huntmonster': 'do_hunt_monster', 87 '!huntmonster': 'do_hunt_monster',
88 '!listmonster': 'do_list_monsters',
88 } 89 }
89 90
90 91
...@@ -1014,12 +1015,24 @@ def do_rigged(client, message_parts, message): ...@@ -1014,12 +1015,24 @@ def do_rigged(client, message_parts, message):
1014 send_message(client, message.channel, ":musical_note: {} :musical_note:".format(random.choice(lines))) 1015 send_message(client, message.channel, ":musical_note: {} :musical_note:".format(random.choice(lines)))
1015 return 1016 return
1016 1017
1018 def do_list_monsters(client, message_parts, message):
1019 member = data.db_get_member(message.author.id)
1020 log(member)
1021 monster_data = data.db_get_pankration_record(member['member_id'])
1022 if monster_data:
1023 monsters = pickle.loads(str(monster_data['monster_inventory']))
1024 send_message(client, message.channel, "\n\n".join(str(monster) for monster in monsters))
1025 else:
1026 send_message(client, message.channel, 'You have no monsters.')
1027
1017 def do_hunt_monster(client, message_parts, message): 1028 def do_hunt_monster(client, message_parts, message):
1018 p = Pankration() 1029 p = Pankration()
1019 hunt_response = p.hunt_monster(' '.join(message_parts)) 1030 hunt_response = p.hunt_monster(' '.join(message_parts))
1020 str_out = hunt_response.message + "\n\n" 1031 str_out = hunt_response.message + "\n\n"
1021 if hunt_response.result == HuntResponse.SUCCESS: 1032 if hunt_response.result == HuntResponse.SUCCESS:
1022 monster = hunt_response.monster 1033 monster = hunt_response.monster
1034 member = data.db_get_member(message.author.id)
1035 data.db_add_monster(member['member_id'], monster)
1023 str_out += str(monster) 1036 str_out += str(monster)
1024 send_message(client, message.channel, str_out) 1037 send_message(client, message.channel, str_out)
1025 1038
......