-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
246 lines (174 loc) · 6.42 KB
/
queries.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import psycopg2
from psycopg2.extras import RealDictCursor
from datetime import datetime
from math import ceil
import time
def get_registered(db, new_username, new_password):
conn = psycopg2.connect(db)
cursor = conn.cursor()
# Set query
query = "INSERT INTO users (username, hash) "
query = query + "VALUES (%s, %s); "
# Run and commitquery
try:
cursor.execute(query, (new_username, new_password))
conn.commit()
results = 1
except psycopg2.errors.UniqueViolation as e:
results = e
cursor.close()
conn.close()
return results
def get_user_info(db, user_id):
# Create connection and cursor
conn = psycopg2.connect(db)
cursor = conn.cursor(cursor_factory=RealDictCursor)
# Create query
query = "SELECT * "
query = query + "FROM users "
query = query + "WHERE username = %s; "
cursor.execute(query, (user_id,))
user_info = cursor.fetchone()
# Close cursor and connection
cursor.close()
conn.close()
return(user_info)
# Can be deleted
def get_locs_info(db, id):
# Create connection and cursor
conn = psycopg2.connect(db)
cursor = conn.cursor(cursor_factory=RealDictCursor)
# Insert values from get_playable_location to games table
query = "SELECT * FROM locs WHERE id = %s; "
cursor.execute(query, (id,))
results = cursor.fetchone()
# Close cursor and connection
cursor.close()
conn.close()
return(results)
# Used by get_disconnected()
def get_current_game_deleted(db, current_game_id):
# TODO try-catch db connection
# Create connection and cursor
conn = psycopg2.connect(db)
cursor = conn.cursor()
# Delete game record on games table
query = "DELETE FROM games_fifty WHERE fifty_game_id = %s; "
cursor.execute(query, (current_game_id,))
# Commit update
conn.commit()
# Close cursor and connection
cursor.close()
conn.close()
return 1
def get_disconnected(db, current_game_id, current_game_start):
# Set current timestamp
current_game_end = datetime.now()
# Delay function by 6 seconds to allow other functions to update db
time.sleep(6)
conn = psycopg2.connect(db)
cursor = conn.cursor()
# Set query
query = """
SELECT fifty_game_duration
FROM games_fifty
WHERE fifty_game_id = %s;
"""
# Run query
cursor.execute(query, (current_game_id,))
# Get results
results = cursor.fetchone()
# Check if game record is deleted or still there
if results == None:
# Game record has been deleted
results = f"Game record has been deleted."
else:
# Game record is still there & game_duration is still None
if results[0] == None:
# Calculate duration
duration_sec, duration_min = game_answer_duration(current_game_start, current_game_end)
if duration_sec >= 10:
# Update fifty_game_duration
query = "UPDATE games_fifty SET fifty_game_duration = %s WHERE fifty_game_id = %s; "
cursor.execute(query, (duration_sec, current_game_id))
conn.commit()
results = f"Game record updated."
else:
# Delete games_fifty record
query = "DELETE FROM games_fifty WHERE fifty_game_id = %s; "
cursor.execute(query, (current_game_id,))
conn.commit()
results = f"Game record deleted."
# Game record is still there & game_duration is still None
else:
results = f"Game record has game_duration."
cursor.close()
conn.close()
return results
def get_disconnected_geo(db, current_game_id, current_game_start):
# Set current timestamp
current_game_end = datetime.now()
# Delay function by 6 seconds to allow other functions to update db
time.sleep(6)
conn = psycopg2.connect(db)
cursor = conn.cursor()
# Set query
query = """
SELECT geo_game_duration
FROM games_geofinder
WHERE geo_game_id = %s;
"""
# Run query
cursor.execute(query, (current_game_id,))
# Get results
results = cursor.fetchone()
# Check if game record is deleted or still there
if results == None:
# Game record has been deleted
results = f"Game record has been deleted."
else:
# Game record is still there & game_duration is still None
if results[0] == None:
# Calculate duration
duration_sec, duration_min = game_answer_duration(current_game_start, current_game_end)
if duration_sec >= 10:
# Update geo_game_duration
query = "UPDATE games_geofinder SET geo_game_duration = %s WHERE geo_game_id = %s; "
cursor.execute(query, (duration_sec, current_game_id))
conn.commit()
results = f"Game record updated."
else:
# Delete games_geofinder record
query = "DELETE FROM games_geofinder WHERE geo_game_id = %s; "
cursor.execute(query, (current_game_id,))
conn.commit()
results = f"Game record deleted."
# Game record is still there & game_duration is still None
else:
results = f"Game record has game_duration."
cursor.close()
conn.close()
return results
# Used by get_disconnected()
def game_answer_duration(game_start, game_end):
# Calculate time difference in seconds
game_duration = game_end - game_start
duration_sec = game_duration.seconds
duration_min = ceil(game_duration.seconds / 60)
return duration_sec, duration_min
# Used by /fifty/game
def get_loc_duration_total(db, current_game_id, user_id, loc_id, current_game_duration):
conn = psycopg2.connect(db)
cursor = conn.cursor(cursor_factory=RealDictCursor)
query = "SELECT COALESCE(SUM(game_duration), 0) AS total "
query = query + "FROM games "
query = query + "WHERE id != %s "
query = query + "AND user_id = %s "
query = query + "AND loc_id = %s; "
cursor.execute(query, (current_game_id, user_id, loc_id))
result = cursor.fetchone()
total = int(result["total"]) + int(current_game_duration)
# Close cursor and connection
cursor.close()
conn.close()
return total