-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
129 lines (106 loc) · 2.84 KB
/
db.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
import os
from bson.objectid import ObjectId
import pymongo
from pymongo import MongoClient
mongoPassword = os.getenv('mongoPassword')
mongoUri = "mongodb+srv://HexHax:" + mongoPassword + "@hackathons-nfrzv.mongodb.net/Emission?retryWrites=true&w=majority"
client = MongoClient(mongoUri)
db=client.Emission
users = db.users
# For hashing passwords
import bcrypt
def get_part(userId, part):
query = {}
query[part] = 1
p = users.find_one({"_id": ObjectId(userId)}, query)
return p
def getPartWith(by, value, part):
query = {}
query[part] = 1
p = users.find_one({by: value}, query)
return p[part]
def newUser():
user = {
"username": "",
"password": "",
"score": 0,
"settings": {
},
"activities": {
}
}
return user
def checkUser(checkBy, value):
if checkBy == '_id': value = ObjectId(value)
query = {}
query["_id"] = 1
p = users.find_one({checkBy: value}, query)
if p:
return True
return False
def checkPassword(username, password):
# hashed the password
p = users.find_one({"username": username})
if p:
try:
return bcrypt.checkpw(password, p["password"])
except:
# in case the password is not salted correctly, invalidate verification
return False
return False
def addUser(username, password):
user = newUser()
user['username'] = username
user['password'] = bcrypt.hashpw(password, bcrypt.gensalt())
users.insert_one(user)
def updateActivities(userid, activities):
newValues = { "$set": activities }
users.update_one({'_id': ObjectId(userid)}, newValues)
def get_data(userid, parts):
query = {'_id': 0}
for i in parts:
query[i] = 1
p = users.find_one({'_id': ObjectId(userid)}, query)
return p
def get_leaderboard(id):
parts = get_data(id, ['friends', 'username'])
friends = [] if 'friends' not in parts.keys() else parts['friends']
username = parts['username']
participants = users.find({}, {'username': 1, 'score': 1, '_id': 0})
participants.sort([("score", pymongo.ASCENDING)])
globalRanks = []
aroundRanks = []
friendRanks = []
everyone = []
index = 0
countdown = 0
for x in participants:
if x['score'] == 0:
continue
else:
x['place'] = index
if x['username'] == username:
x['you'] = True
begin = index-4 if len(parts)-index > 4 else 0
countdown = 8-(index-begin)
aroundRanks = everyone[begin:index]
if len(globalRanks) < 8: globalRanks.append(x)
if x['username'] in friends: friendRanks.append(x)
if (countdown > 0):
countdown -= 1
aroundRanks.append(x)
everyone.append(x)
index += 1
return {
"global": globalRanks,
"around": aroundRanks,
"friends": friendRanks
}
def befriend(id, friendName):
users.update_one({'_id': ObjectId(id)}, {'$push': {"friends": friendName}})
def search_names(name):
people = users.find({
"username": {"$regex": "^" + name.lower(), "$options" :'i'}
}
, {'username': 1}).limit(20);
return [x['username'] for x in people]