Spaces:
Running
Running
File size: 3,880 Bytes
a4cc435 294884c a4cc435 048f218 bbd997e a4cc435 048f218 a4cc435 048f218 a4cc435 048f218 a4cc435 bbd997e a4cc435 bbd997e a4cc435 bbd997e d1d8602 a4cc435 d1d8602 a4cc435 bbd997e d1d8602 a4cc435 |
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 |
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
from gamification.imports import *
from gamification.levelLogic import get_all_levels_func
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=5)
# utils
def get_particular_level(totalPoints,dreamJob)->UserLevel:
# query db and get the results of all the level probably re use a function
levels = get_all_levels_func()
particularLevel= [level for level in levels if (level.maxPoints >= totalPoints) and (level.minPoints<=totalPoints)and (level.careerPath==dreamJob)]
defaulLevel= [level for level in levels if level.levelName=="default" ]
if len(particularLevel)>0:
return particularLevel
return defaulLevel
def get_dream_job(userId):
db_uri=MONGO_URI
db_name = "crayonics"
collection_name = "Questionaire"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
questionaire_doc = collection.find_one({"userId": userId})
if questionaire_doc:
return questionaire_doc['dreamRole']
else: return False
def create_points_func(document:UserPoints)->bool:
from app import handle_change3
db_uri = MONGO_URI
db_name = "crayonics"
collection_name="Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
if document!=None:
doc = document.model_dump()
doc['earnedAt']=datetime.now()
result = collection.insert_one(doc)
executor.submit (handle_change3 , document.userId)
return True
else:
client.close()
return False
def get_all_points_func(userId) -> IndividualUserLevel:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name = "Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
dreamJob = get_dream_job(userId=userId)
point_cursor = collection.find({"userId": userId})
# Convert the cursor to a list so we can reuse it
points_list = list(point_cursor)
# Calculate the total points
totalPoints = sum([point['numOfPoints'] for point in points_list])
particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
# Create the individual points list
individualPoints = [indpoint for indpoint in points_list]
print([pointss for pointss in individualPoints if all(val is not None for val in pointss.values())])
# Create the IndividualUserLevel object with totalPoints and individualPoints
points = IndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,minPoints=particularLevelInfo[0].minPoints,maxPoints=particularLevelInfo[0].maxPoints,levelNumber=particularLevelInfo[0].levelNumber, individualPoints=individualPoints)
return points
def get_all_simple_points_func(userId) -> SimpleIndividualUserLevel:
db_uri = MONGO_URI
db_name = "crayonics"
collection_name = "Points"
client = MongoClient(db_uri)
db = client[db_name]
collection = db[collection_name]
dreamJob = get_dream_job(userId=userId)
point_cursor = collection.find({"userId": userId})
try:
points_list = list(point_cursor)
totalPoints = sum([point['numOfPoints'] for point in points_list])
particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
points = SimpleIndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,maxPoints=particularLevelInfo[0].maxPoints,minPoints=particularLevelInfo[0].minPoints,levelNumber=particularLevelInfo[0].levelNumber)
except:
totalPoints = 0
points = SimpleIndividualUserLevel(totalpoints=totalPoints)
return points
|