-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
195 lines (144 loc) · 5.19 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
import sqlite3
import random
from datetime import date, datetime
import json
import string
conn = sqlite3.connect("database.db", check_same_thread=False)
c = conn.cursor()
def generate_id(table_name, id_field):
while True:
id = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
c.execute(f'SELECT * FROM {table_name} WHERE {id_field}=?', (id, ))
if c.fetchone() is None:
break
return id
# function should return all users from db
def get_users():
c.execute("SELECT username FROM users;")
users = c.fetchall()
return users
def validate_user(username, password):
c.execute("SELECT * FROM users WHERE username=? AND password=?",
(username, password))
return c.fetchone() is not None
def add_user(username, email, password):
# function should all user to db
c.execute("INSERT INTO users VALUES(?, ?, ?)", (username, email, password))
conn.commit()
return "user added to db"
def user_exists(username, email):
# function should check if username or email are already used in database
c.execute("SELECT * FROM users WHERE username = ? OR email=?",
(username, email))
if len(c.fetchall()) == 0:
return False
return True
# function should return all rooms in db
def get_rooms():
c.execute("SELECT * FROM rooms;")
rooms = c.fetchall()
return rooms
# function should retrurn all rooms with selected username
def get_rooms_with_username(username):
c.execute("SELECT * FROM rooms WHERE room_owner = ?;", (username, ))
return c.fetchall()
def create_room(user, name):
id = random.randint(1, 10000)
c.execute("INSERT INTO rooms VALUES (?, ?, ?, ?);",
(id, name, user, date.today()))
conn.commit() # dodati moramo še commit ker zapisujemo v bazo
return id
def join_room(room, user):
c.execute("INSERT INTO user_in_room VALUES (?, ?)", (room, user))
conn.commit()
def get_rooms_with_username2(username):
c.execute("SELECT * FROM user_in_room WHERE username = ?;", (username, ))
return c.fetchall()
def get_room_name_from_id(room):
#name = "ime ki ga dobimo iz baze"
# select stavek kjer preko ID-ja dobimo ime
c.execute("SELECT name FROM rooms WHERE id=?;", (room, ))
return c.fetchone()[0]
def user_not_in_room(room, user):
# preverimo ali je user že v tej sobi
c.execute("SELECT * FROM user_in_room WHERE room_id = ? AND username = ?",
(room, user))
if len(c.fetchall()) == 0:
return True
return False
def room_exists(room): # podamo ID
# funkcija vrne True ali False - True če soba ostaja, False če ne
c.execute("SELECT * FROM rooms WHERE id = ?", (room, ))
if len(c.fetchall()) == 0:
return False
return True
def get_users_in_room(room): # podamo ID rooma
# funkcija naj vrne seznam userjev
c.execute("SELECT username FROM user_in_room WHERE room_id = ?", (room, ))
return c.fetchall()
# return ["miha", "test", "mark"]
def get_messages(room):
# # funkcija naj vrne vsa sporočila od vseh userjev za to sobo
# TODO: funkacija na vrne samo sporočila za to sobo - room
c.execute(
"SELECT * FROM messages WHERE room_id = ? ORDER BY date DESC LIMIT 5;",
(room, ))
messages = []
raw = c.fetchall()
for r in raw:
temp = {}
temp["username"] = r[0]
temp["text"] = r[1]
temp["date"] = r[3]
messages.append(temp)
return messages
# TODO: dokončaj funkcijo add_messages
def add_message_to_room(username, message, room):
date = datetime.now()
c.execute("INSERT INTO messages VALUES(?, ?, ?, ?)",
(username, message, room, date))
conn.commit()
def is_room_owner(user, room):
c.execute("SELECT * FROM rooms WHERE id=? AND room_owner=?", (room, user))
return len(c.fetchall()) != 0
def remove_user_from_room(user, room):
c.execute("DELETE FROM user_in_room WHERE username = ? AND room_id = ?",
(user, room))
conn.commit()
def get_tictactoe(game_id):
c.execute("SELECT * FROM tictactoe WHERE game_id=?", (game_id, ))
game = c.fetchone()
if game is None:
return None
return {
"game_id": game[0],
"room_id": game[1],
"player_x": game[2],
"player_o": game[3],
"state": json.loads(game[4])["state"],
"next_player": game[5],
"winner": game[6],
"ended": game[7]
}
def update_tictactoe_state(game_id, new_state, next_player):
state_str = json.dumps(new_state)
c.execute("""
UPDATE tictactoe
SET state=?, next_player=?
WHERE game_id=?
""", (state_str, next_player, game_id))
conn.commit()
def update_tictactoe_winner(game_id, winner_id):
c.execute("""
UPDATE tictactoe
SET winner=?, ended=?
WHERE game_id=?
""", (winner_id, True, game_id))
conn.commit()
def create_tictactoe(room_id, player_x, player_o):
game_id = generate_id('tictactoe', 'game_id')
c.execute("""
INSERT INTO tictactoe
VALUES(?, ?, ?, ?, '{"state": [["#", "#", "#"], ["#", "#", "#"], ["#", "#", "#"]]}', ?, NULL, false)
""", (game_id, room_id, player_x, player_o, player_x))
conn.commit()