-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
211 lines (177 loc) · 7.73 KB
/
database.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import mysql.connector
import time
from threading import Thread
from datetime import date
ID = "id"
TELEGRAM_NAME = "telegram_name"
USER_NAME = "user_name"
PRIMARY_NAME = "primary_name"
BALANCE = "balance"
REGISTRATION_DATE = "registration_date"
CURRENT_ONLINE = "current_online"
MAX_ONLINE = "max_online"
START_ONLINE = "start_online"
DATA = "data"
CASH_SUM = "cash_sum"
class NewConnectionToAccountsDatabase:
def __init__(self):
self.__database = mysql.connector.connect( # connecting to database
host="sql7.freemysqlhosting.net",
user="sql7330856",
passwd="N2i39Qde6k",
database="sql7330856"
)
self.__sql_data_executor = self.__database.cursor()
self.__changes = []
def CommitToDatabase(self):
for change in self.__changes:
self.__sql_data_executor.execute(change)
self.__database.commit()
def GetFullAccountDataByID(self, id):
try:
data = {}
self.__sql_data_executor.execute("SELECT * FROM Accounts WHERE id = %s" % id)
result = self.__sql_data_executor.fetchall()[0]
data["id"] = result[0]
data["telegram_name"] = result[1]
data["user_name"] = result[2]
data["primary_name"] = result[3]
data["balance"] = result[4]
data["registration_date"] = result[5]
return data
except:
raise ValueError("There are no users with '%s' id" % id)
def GetFullAccountDataByTelegramName(self, telegram_name):
try:
data = {}
self.__sql_data_executor.execute("SELECT * FROM Accounts WHERE telegram_name= '%s'" % telegram_name)
result = self.__sql_data_executor.fetchall()[0]
data["id"] = result[0]
data["telegram_name"] = result[1]
data["user_name"] = result[2]
data["primary_name"] = result[3]
data["balance"] = result[4]
data["registration_date"] = result[5]
return data
except:
raise ValueError("There are no users with '%s' telegram_name" % telegram_name)
def GetFullAccountDataByUserName(self, user_name):
try:
data = {}
self.__sql_data_executor.execute("SELECT * FROM Accounts WHERE user_name= '%s'" % user_name)
result = self.__sql_data_executor.fetchall()[0]
data["id"] = result[0]
data["telegram_name"] = result[1]
data["user_name"] = result[2]
data["primary_name"] = result[3]
data["balance"] = result[4]
data["registration_date"] = result[5]
return data
except:
raise ValueError("There are no users with '%s' user_name" % user_name)
def CreateNewAccount(self, id):
try:
self.__changes.append("INSERT INTO Accounts(id, telegram_name, user_name, primary_name, balance, registration_date) VALUES(%s, '@UnknownTelegramUser', 'UnknownUser', 'telegram_name', 0, '%s')" % (
id, str(date.today())
))
except:
raise ValueError("Invalid argument")
def UpdateTelegramName(self, id, updated_telegram_name):
try:
self.__changes.append("UPDATE Accounts SET telegram_name = '%s' WHERE id = %s" % (updated_telegram_name, id))
except:
raise ValueError("Invalid Arguments")
def UpdateUserName(self, id, updated_user_name):
try:
self.__changes.append("UPDATE Accounts SET user_name = '%s' WHERE id = %s" % (updated_user_name, id))
except:
raise ValueError("Invalid Arguments")
def UpdatePrimaryName(self, id, updated_primary_name):
try:
self.__changes.append("UPDATE Accounts SET primary_name = '%s' WHERE id = %s" % (updated_primary_name, id))
except:
raise ValueError("Invalid Arguments")
def UpdateBalance(self, id, updated_balance):
try:
self.__changes.append("UPDATE Accounts SET balance = %s WHERE id = %s" % (updated_balance, id))
except:
raise ValueError("Invalid Arguments")
def CloseConnection(self):
self.__sql_data_executor.close()
self.__database.close()
class NewConnectionToFixedRoomsDatabase:
def __init__(self):
self.__database = mysql.connector.connect( # connecting to database
host="sql7.freemysqlhosting.net",
user="sql7330856",
passwd="N2i39Qde6k",
database="sql7330856"
)
self.__sql_data_executor = self.__database.cursor()
self.__changes = []
def GetAllFixedRoomsOnline(self):
self.__sql_data_executor.execute("SELECT current_online FROM RoomsFixed")
result = self.__sql_data_executor.fetchall()
data = {}
a = 0
for x in result:
a += 1
data[str(a)] = x[0]
return data
def GetAllFixedRoomData(self, id):
self.__sql_data_executor.execute("SELECT * FROM RoomsFixed WHERE id = %s" % id)
result = self.__sql_data_executor.fetchall()[0]
data = {}
data["id"] = result[0]
data["current_online"] = result[1]
data["max_online"] = result[2]
data["start_online"] = result[3]
data["data"] = result[4]
data["cash_sum"] = result[5]
return data
def UpdateRoomCurrentOnline(self, id, new_online):
self.__changes.append("UPDATE RoomsFixed SET current_online = %s WHERE id = %s" % (new_online, id))
def IncrementRoomCurrentOnline(self, id):
online = self.GetAllFixedRoomsOnline()[str(id)]
self.UpdateRoomCurrentOnline(id, online+1)
def DecrementRoomCurrentOnline(self, id):
online = self.GetAllFixedRoomsOnline()[str(id)]
self.UpdateRoomCurrentOnline(id, online - 1)
def UpdateRoomMaxOnline(self, id, new_online):
self.__changes.append("UPDATE RoomsFixed SET max_online = %s WHERE id = %s" % (new_online, id))
def UpdateRoomStartOnline(self, id, new_online):
self.__changes.append("UPDATE RoomsFixed SET start_online = %s WHERE id = %s" % (new_online, id))
def UpdateRoomCashSum(self, id, new_cash_sum):
self.__changes.append("UPDATE RoomsFixed SET cash_sum = %s WHERE id = %s" % (new_cash_sum, id))
def CommitToDatabase(self):
for change in self.__changes:
self.__sql_data_executor.execute(change)
self.__database.commit()
def AddNewUserToRoom(self, rid, uid):
data = self.GetAllFixedRoomData(rid)[DATA]
data += "%s " % (uid)
self.__changes.append("UPDATE RoomsFixed SET data = '%s' WHERE id = %s" % (data, rid))
def RemoveUserFromRoom(self, rid, uid):
data = self.GetAllFixedRoomData(rid)[DATA]
data = data.replace("%s " % uid, "")
self.__changes.append("UPDATE RoomsFixed SET data = '%s' WHERE id = %s" % (data, rid))
def ClearUsersFromRoom(self, id):
self.__changes.append("UPDATE RoomsFixed SET data = '' WHERE id = %s" % id)
def GetAllUsersFromRoom(self, id):
self.__sql_data_executor.execute("SELECT data FROM RoomsFixed WHERE id = %s" % id)
data = []
for user in self.__sql_data_executor.fetchall()[0][0].split():
data.append(int(user))
return data
def CloseConnection(self):
self.__sql_data_executor.close()
self.__database.close()
def CreateFixedRoom(self, id, current_online, max_online, start_online, status, data, cash_sum):
self.__changes.append(f"INSERT INTO RoomsFixed(id, current_online, max_online, start_online, status, data, cash_sum) VALUES(%s, %s, %s, %s, '%s', '%s', %s)" % (
id,
current_online,
max_online,start_online,
status,
data,
cash_sum
))