fix stats in getUserInfo request

This commit is contained in:
2024-11-18 14:49:41 +01:00
parent 9cbb11c329
commit b38f18d765

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/09/20 00:16:57 by edbernar #+# #+# # # Created: 2024/09/20 00:16:57 by edbernar #+# #+# #
# Updated: 2024/11/18 14:32:21 by tomoron ### ########.fr # # Updated: 2024/11/18 14:49:07 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -37,12 +37,13 @@ def getHistory(user, games):
}) })
return(res) return(res)
def getStats(user): def getStats(user, socket):
try:
games = GameResults.objects.filter(Q(player1=user) | Q(player2=user)) games = GameResults.objects.filter(Q(player1=user) | Q(player2=user))
nbGames = games.count() nbGames = games.count()
nbWin = games.filter(winner=user).count() nbWin = games.filter(winner=user).count()
history = getHistory(user, games.order_by("-end_date")[:10]) history = getHistory(user, games.order_by("-end_date")[:10])
nbForfeitOpponent = games.filter(Q(winner=user) & Q(forfeit=True)) nbForfeitOpponent = games.filter(Q(winner=user) & Q(forfeit=True)).count()
if(nbGames): if(nbGames):
forfeitRate = (100 / nbGames) * nbForfeitOpponent forfeitRate = (100 / nbGames) * nbForfeitOpponent
else: else:
@ -63,13 +64,15 @@ def getStats(user):
res = {} res = {}
res["nbLoss"] = nbGames - nbWin res["nbLoss"] = int(nbGames) - int(nbWin)
res["nbWin"] = nbWin res["nbWin"] = int(nbWin)
res["forfeitRate"] = forfeitRate res["forfeitRate"] = int(forfeitRate)
res["avgGoals"] = avgGoals res["avgGoals"] = int(avgGoals)
res["nbGames30Days"] = nbGames30Days res["nbGames30Days"] = int(nbGames30Days)
res["history"] = history res["history"] = history
return(res) return(res)
except Exception as e:
socket.sendError("invalid request", 9005, e)
@sync_to_async @sync_to_async
@ -85,7 +88,8 @@ def getUserInfo(socket, content):
socket.sync_send({"type":"user_info", "content": None}) socket.sync_send({"type":"user_info", "content": None})
return return
user = user[0] user = user[0]
stats = getStats(user) stats = getStats(user, socket)
print(stats)
online = True if user.id in socket.onlinePlayers else False online = True if user.id in socket.onlinePlayers else False
socket.sync_send({"type":"user_info", "content":{ socket.sync_send({"type":"user_info", "content":{
'username': user.username, 'username': user.username,