This repository was archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
177 lines (139 loc) · 6.58 KB
/
Copy pathdatabase.py
File metadata and controls
177 lines (139 loc) · 6.58 KB
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
import mariadb
import sys
from objects.player import Player
from objects.cafe import Cafe
from objects.avatar import Avatar
class Database:
def __init__(self):
try:
conn = mariadb.connect(
user="root",
password="",
host="localhost",
port=3306,
database="gg_cafe",
autocommit=True
)
except mariadb.Error as e:
sys.stderr.write(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
self.conn = conn
self.cursor = conn.cursor()
def _create_player_by_params(self, player_args: tuple[int, str]) -> Player:
player = Player()
player.id = player_args[0]
player.cash = player_args[1]
player.gold = player_args[2]
player.xp = player_args[3]
player.instant_cookings = player_args[4]
player.open_jobs = player_args[5]
player.played_wheel = player_args[6]
player.allow_friend_requests = player_args[7]
player.allow_emails = player_args[8]
player.email_verified = player_args[9]
player.new_gifts = player_args[10]
player.avatar = Avatar(player_args[11], player_args[12], player_args[13], player_args[14], player_args[15],
player_args[16])
player.cafe = self.get_cafe(player_args[0])
player.parse_mastery(player_args[17])
player.parse_friends(player_args[18])
# COOPS
player.coop_id = player_args[19]
return player
def get_cafe(self, player_id: int) -> Cafe:
self.cursor.execute(
"SELECT owner_name, rating, luxury, expansion_id, tiles, objects, fridge_inv, furniture_inv, waiters FROM cafe WHERE player_id=?",
(player_id,))
cafe_args = self.cursor.fetchone()
cafe = Cafe()
cafe.user_id = player_id
cafe.player_id = player_id
cafe.owner_name = cafe_args[0]
cafe.rating = cafe_args[1]
cafe.luxury = cafe_args[2]
cafe.expansion_id = cafe_args[3]
cafe.size = cafe.expansion_id + 8
cafe.parse_tiles(cafe_args[4])
cafe.set_objects_from_json(cafe_args[5])
cafe.parse_fridge_inventory(cafe_args[6])
cafe.parse_furniture_inventory(cafe_args[7])
cafe.set_waiters_from_json(cafe_args[8])
return cafe
def try_login(self, name_or_email: str, password: str) -> tuple[int, str]:
self.cursor.execute("SELECT password, is_banned, username FROM player WHERE username=? OR email=?",
(name_or_email, name_or_email))
values = self.cursor.fetchone()
if self.cursor.rowcount == 0:
return 14, ""
elif password != values[0]:
return 14, ""
elif values[1] == 1:
return 19, ""
else:
return 0, values[2]
def get_player(self, username: str) -> Player:
self.cursor.execute(
"SELECT id, cash, gold, xp ,instant_cookings, open_jobs, played_wheel, allow_friend_requests, allow_emails, email_verified, new_gifts, username, gender, skin_color, top_color, hair_color, legs_color, mastery, friends, coop_id FROM player WHERE username=?",
(username,))
player_args = self.cursor.fetchone()
return self._create_player_by_params(player_args)
def get_player_by_id(self, player_id: int) -> Player:
self.cursor.execute(
"SELECT id, cash, gold, xp ,instant_cookings, open_jobs, played_wheel, allow_friend_requests, allow_emails, email_verified, new_gifts, username, gender, skin_color, top_color, hair_color, legs_color, mastery, friends, coop_id FROM player WHERE id=?",
(player_id,))
player_args = self.cursor.fetchone()
return self._create_player_by_params(player_args)
def get_players_by_coop(self, coop_id):
self.cursor.execute(
"SELECT id, xp, username, gender, skin_color, top_color, hair_color, legs_color FROM player WHERE coop_id=?",
(coop_id,))
player_args = self.cursor.fetchall()
return player_args
def new_player(self, username: str, password: str, email: str) -> None:
self.cursor.execute("INSERT INTO player (username, password, email) VALUES (?, ?, ?)",
(username, password, email))
def new_cafe(self, player_id: int) -> None:
pass
def update_player(self, player_id: int, **kwargs):
columns = ", ".join([f"{key} = ?" for key in kwargs.keys()])
query = f"UPDATE player SET {columns} WHERE id = ?"
values = [*kwargs.values(), player_id]
self.cursor.execute(query, values)
def update_cafe(self, player_id: int, **kwargs):
columns = ", ".join([f"{key} = ?" for key in kwargs.keys()])
query = f"UPDATE cafe SET {columns} WHERE player_id = ?"
values = [*kwargs.values(), player_id]
self.cursor.execute(query, values)
def get_friends(self, player_id: int):
self.cursor.execute(
"SELECT friends FROM player WHERE id=?",
(player_id,))
friends = self.cursor.fetchone()
return friends
def get_coop_by_id(self, coop_id: int):
self.cursor.execute(
"SELECT id, active_coop, runtime, extend_count, finish_level, dishes, host, "
"started_at, finishes_at, ended FROM coops WHERE id=?",
(coop_id,))
coop_details = self.cursor.fetchone()
return coop_details
def get_coop_by_host(self, player_id: int):
self.cursor.execute(
"SELECT id, active_coop, runtime, extend_count, finish_level, dishes, host, "
"started_at, finishes_at, ended FROM coops WHERE host=? and ended=0",
(player_id,))
coop_details = self.cursor.fetchone()
return coop_details
def new_coop(self, active_coop: int, dishes: str, player_id: int, started_at: str, finishes_at: str) -> None:
self.cursor.execute("INSERT INTO coops (active_coop, dishes, host, started_at, finishes_at) "
"VALUES (?, ?, ?, ?, ?)",
(active_coop, dishes, player_id, started_at, finishes_at))
def update_coop(self, coop_id: int, **kwargs):
columns = ", ".join([f"{key} = ?" for key in kwargs.keys()])
query = f"UPDATE coops SET {columns} WHERE id = ?"
values = [*kwargs.values(), coop_id]
self.cursor.execute(query, values)
#def get_shop_availability(self):
# self.cursor.execute("SELECT availability FROM shop")
# availability = self.cursor.fetchone()
# return availability