-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_database_connector.py
74 lines (51 loc) · 2.04 KB
/
room_database_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import psycopg2
from database import config
from secrets import token_urlsafe
from database_connector import DatabaseConnector
class RoomDatabaseConnector(DatabaseConnector):
def __init__(self):
super().__init__()
self.connection = self.connect()
def process_transaction(self, account_id, item):
cursor = self.connection.cursor()
cost = item.get_cost()
cursor.execute("""UPDATE account SET wealth = wealth - %s WHERE id = %s""", \
(item.get_cost(), account_id,))
self.connection.commit()
cursor.close()
def get_inventory(self, account_id):
cursor = self.connection.cursor()
cursor.execute("""SELECT inventory FROM account WHERE id = %s""", \
(account_id,))
fetched_data = cursor.fetchone()
if fetched_data is not None:
if len(fetched_data) > 0:
inventory = fetched_data[0]
cursor.close()
return inventory
cursor.close()
return None
def save_inventory(self, account_id, save_json):
cursor = self.connection.cursor()
cursor.execute("""UPDATE account SET inventory = %s WHERE id = %s""", \
(save_json, account_id,))
self.connection.commit()
cursor.close()
def save_persist_objects(self, room_id, save_json):
cursor = self.connection.cursor()
cursor.execute("""UPDATE room SET persist_objects = %s WHERE id = %s""", \
(save_json, room_id,))
self.connection.commit()
cursor.close()
def get_persist_objects(self, room_id):
cursor = self.connection.cursor()
cursor.execute("""SELECT persist_objects FROM room WHERE id = %s""", \
(room_id,))
fetched_data = cursor.fetchone()
if fetched_data is not None:
if len(fetched_data) > 0:
persist_objects = fetched_data[0]
cursor.close()
return persist_objects
cursor.close()
return None