add games history, number of games won and number of games lost to the getUserInfo response

This commit is contained in:
2024-09-28 04:47:11 +02:00
parent 322a96eaea
commit 20ead24a3a
2 changed files with 49 additions and 12 deletions

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
# Updated: 2024/09/28 03:48:09 by tomoron ### ########.fr #
# Updated: 2024/09/28 04:10:56 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -177,6 +177,8 @@ class Game:
return(1)
def endGame(self, winner):
if(self.end):
return
self.p1.sync_send({"action":"game","content":{"action":10,"won":winner==1}})
self.p2.sync_send({"action":"game","content":{"action":10,"won":winner==2}})
self.winner = winner
@ -186,10 +188,12 @@ class Game:
socket.game = None
if (socket == self.p1):
self.left = 1
self.p2.sync_send({"type":"game","content":{"action":4}})
if(self.p2 != None):
self.p2.sync_send({"type":"game","content":{"action":4}})
else:
self.left = 2
self.p1.sync_send({"type":"game","content":{"action":4}})
if(self.p1 != None):
self.p1.sync_send({"type":"game","content":{"action":4}})
while(Game.waitingForPlayerLock):
time.sleeep(0.05)
Game.waitingForPlayerLock = True
@ -448,6 +452,8 @@ class Game:
await asyncio.sleep(sleep_time)
print("game end")
await self.saveResults()
self.p1.game = None
self.p2.game = None
@sync_to_async
def saveResults(self):

View File

@ -6,28 +6,59 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/20 00:16:57 by edbernar #+# #+# #
# Updated: 2024/09/23 15:08:37 by edbernar ### ########.fr #
# Updated: 2024/09/28 04:43:44 by tomoron ### ########.fr #
# #
# **************************************************************************** #
from django.db.models import Q
from asgiref.sync import sync_to_async
from ..models import User
from ..models import User, GameResults
import json
def getHistory(user, games):
res = []
for x in games:
p1 = {"score":int(x.p1Score), "username":x.player1.username}
p2 = {"score":int(x.p2Score), "username":x.player2.username}
if(x.player2 == user):
p1, p2 = p2, p1
res.append({
"id":x.id,
"p1":p1,
"p2":p2,
"won":x.winner == user
})
return(res)
@sync_to_async
def getUserInfo(socket, content):
try:
if (content.get('id')):
user = User.objects.filter(id=content['id']).values().first()
user = User.objects.filter(id=content['id'])
elif (content.get('username')):
user = User.objects.filter(username=content['username']).values().first()
user = User.objects.filter(username=content['username'])
else:
user = None
if (not user):
if (not user or not user.exists()):
socket.sync_send({"type":"user_info", "content": None})
return
online = True if user['id'] in socket.onlinePlayers else False
print("User is online: ", online)
socket.sync_send({"type":"user_info", "content":{'username': user['username'], 'pfp': user['pfp'], 'banner': user['banner'], 'id': user['id'], 'online': online, 'github': user['github_link'], 'discord': user['discord_username']}})
user = user[0]
games = GameResults.objects.filter(Q(player1=user) | Q(player2=user))
nbGames = games.count()
nbWin = games.filter(winner=user).count()
history = getHistory(user, games.order_by("-end_date")[:10])
online = True if user.id in socket.onlinePlayers else False
socket.sync_send({"type":"user_info", "content":{
'username': user.username,
'pfp': user.pfp,
'banner': user.banner,
'id': user.id,
'online': online,
'github': user.github_link,
'discord': user.discord_username,
'nbWin':nbWin,
'nbLoss':nbGames - nbWin,
'history':history
}})
except Exception as e:
socket.sendError("invalid request", 9005, e)