From 1135014d0f63d25a07d0703e13d4eebaf78bd4f6 Mon Sep 17 00:00:00 2001 From: Xamora Date: Tue, 14 May 2024 14:36:04 +0200 Subject: [PATCH] can invite to play a game in chat --- django/chat/urls.py | 2 +- django/chat/views/ask.py | 36 +++++++++++------------ django/frontend/static/js/api/Notice.js | 35 ++++++++++++++++------ django/frontend/static/js/api/chat/Ask.js | 24 ++++++++++----- django/frontend/static/js/views/Search.js | 2 +- django/notice/consumers.py | 15 ++++------ django/transcendence/settings.py | 7 ++--- 7 files changed, 69 insertions(+), 52 deletions(-) diff --git a/django/chat/urls.py b/django/chat/urls.py index 13da1af..ded41bd 100644 --- a/django/chat/urls.py +++ b/django/chat/urls.py @@ -6,6 +6,6 @@ from .views import ask urlpatterns = [ path("", chat.ChannelView.as_view(), name="chats_page"), path("ask/", ask.AskView.as_view(), name="chats_ask"), + path("ask/accept/", ask.AskAcceptView.as_view(), name="chats_ask_accept"), path("ask/", ask.AskView.as_view(), name="chats_ask_get"), - path("ask/accept", ask.AskAcceptView.as_view(), name="chats_ask_accept"), ] diff --git a/django/chat/views/ask.py b/django/chat/views/ask.py index 2e34020..1442cdc 100644 --- a/django/chat/views/ask.py +++ b/django/chat/views/ask.py @@ -4,6 +4,7 @@ from rest_framework import permissions, status from rest_framework.authentication import SessionAuthentication from chat.models import AskModel +from games.models import GameModel from notice.consumers import notice_manager @@ -21,15 +22,15 @@ class AskView(APIView): asker_id = request.user.pk asked_id = data["asked"] - print("1") - if (asked_id is None): return Response(status=status.HTTP_400_BAD_REQUEST) - print("2") if AskModel().is_asked(asker_id, asked_id): return Response(status=status.HTTP_208_ALREADY_REPORTED) - print("3") + + asked = User.objects.get(pk=asked_id) + + notice_manager.ask_game(asked, request.user.username) AskModel(asker_id=asker_id, asked_id=asked_id).save() return Response(status=status.HTTP_201_CREATED) @@ -41,23 +42,17 @@ class AskView(APIView): asked_id = request.user.pk if (asker_id is None): - asker_id = request.user.id - asked_id = data["asked"] - if (asked_id is None): - return Response(status=status.HTTP_400_BAD_REQUEST) + return Response(status=status.HTTP_400_BAD_REQUEST) - print(asker_id, " ", asked_id) - print(AskModel().is_asked(2, 1)) - print(AskModel().is_asked(1, 2)) if not AskModel().is_asked(asker_id, asked_id): return Response(status=status.HTTP_204_NO_CONTENT) # Don't need more verification, just above is enough asker = User.objects.get(pk=asker_id) - notice_manager.refuse_game(request.user, asker) + notice_manager.refuse_game(asker, request.user.username) - AskModel(asker_id=asker_id, asked_id=asked_id).delete() + AskModel.objects.get(asker_id=asker_id, asked_id=asked_id).delete() return Response(status=status.HTTP_200_OK) @@ -70,7 +65,7 @@ class AskView(APIView): if (asked_id is None): return Response(status=status.HTTP_400_BAD_REQUEST) - if not AskModel().is_asked(asker_id, asked_id): + if not AskModel().is_asked(asked_id, asker_id): return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_200_OK) @@ -82,6 +77,7 @@ class AskAcceptView(APIView): authentication_classes = (SessionAuthentication,) def post(self, request): + data: dict = request.data asker_id = data["asker"] @@ -91,12 +87,14 @@ class AskAcceptView(APIView): return Response(status=status.HTTP_400_BAD_REQUEST) if not AskModel().is_asked(asker_id, asked_id): - return Response(status=status.HTTP_404_NOT_FOUND) + return Response(status=status.HTTP_400_BAD_REQUEST) asker = User.objects.get(pk=asker_id) - asked = User.objects.get(pk=asked_id) + asked = request.user - notice_manager.accept_game(asker=asker, asked=asked) + id_game = GameModel().create({asker, asked}).pk - AskModel(asker_id=asker_id, asked_id=asked_id).delete() - return Response(status=status.HTTP_200_OK) + notice_manager.accept_game(asker, request.user.username, id_game) + + AskModel.objects.get(asker_id=asker_id, asked_id=asked_id).delete() + return Response({"id_game":id_game}, status=status.HTTP_200_OK) diff --git a/django/frontend/static/js/api/Notice.js b/django/frontend/static/js/api/Notice.js index 3cdbb8e..43e1771 100644 --- a/django/frontend/static/js/api/Notice.js +++ b/django/frontend/static/js/api/Notice.js @@ -1,6 +1,6 @@ import {Client} from './Client.js'; import {createNotification} from '../utils/noticeUtils.js' -import { lastView } from '../index.js'; +import { lastView, navigateTo } from '../index.js'; import ProfilePageView from '../views/ProfilePageView.js'; import Search from '../views/Search.js'; @@ -17,13 +17,12 @@ export default class Notice { this.url = location.origin.replace('http', 'ws') + '/ws/notice'; } - start() { + async start() { this._socket = new WebSocket(this.url); this._socket.onclose = _ => this._socket = undefined; - this._socket.onmessage = message => { + this._socket.onmessage = async message => { const data = JSON.parse(message.data); - console.log(data) if (data.type === 'friend_request') { this.friend_request(data.author); @@ -38,13 +37,11 @@ export default class Notice { } else if (data.type === 'offline') { this.offline(data.user) } else if (data.type == 'game_asked') { - - } else if (data.type == 'game_canceled') { - + this.game_asked(data.asker); } else if (data.type == 'game_accepted') { - + this.game_accepted(data.asked, data.id_game); } else if (data.type == 'game_refused') { - + this.game_refused(data.asked); } }; } @@ -106,4 +103,24 @@ export default class Notice { lastView.loadFriendshipStatus(); } } + + game_asked(asker) { + createNotification('Game Invite', `${asker} ask to play a 1vs1 pong`); + if (lastView instanceof Search) + lastView.display_invite(); + } + + game_refused(asked) { + createNotification('Game Refused', `${asked} refuse your proposition to play`); + if (lastView instanceof Search) + lastView.display_invite(); + } + + async game_accepted(asked, id_game) { + createNotification('Game Accepted', `${asked} accept your proposition to play`); + if (lastView instanceof Search) + lastView.display_invite(); + + await navigateTo(`/games/pong/${id_game}`); + } } diff --git a/django/frontend/static/js/api/chat/Ask.js b/django/frontend/static/js/api/chat/Ask.js index 7fd1f57..9c5bccc 100644 --- a/django/frontend/static/js/api/chat/Ask.js +++ b/django/frontend/static/js/api/chat/Ask.js @@ -1,3 +1,5 @@ +import { lastView, navigateTo } from '../../index.js'; +import Search from '../../views/Search.js'; export default class Ask { constructor(client) { @@ -10,16 +12,19 @@ export default class Ask { }); } - async ask_game_canceled() { - - } - async ask_game_accepted(asker) { - let response = await this.client._post(`/api/chat/ask/accept`, { + let response = await this.client._post(`/api/chat/ask/accept/`, { asker:asker, }); - console.log(response.status); + const statu = response.status; + if (statu == 404 || statu == 204) + return + if (lastView instanceof Search) + lastView.display_invite(); + + const data = await response.json(); + await navigateTo(`/games/pong/${data.id_game}`); } async ask_game_refused(asker) { @@ -27,14 +32,17 @@ export default class Ask { asker:asker, }); - console.log(response.status); + const statu = response.status; + if (statu == 404 || statu == 204) + return + if (lastView instanceof Search) + lastView.display_invite(); } async is_asked(asked) { let response = await this.client._get(`/api/chat/ask/${asked}`); const statu = response.status; - console.log(statu); if (statu == 404 || statu == 204) return false; return true; diff --git a/django/frontend/static/js/views/Search.js b/django/frontend/static/js/views/Search.js index a145465..180a0d7 100644 --- a/django/frontend/static/js/views/Search.js +++ b/django/frontend/static/js/views/Search.js @@ -69,7 +69,7 @@ export default class extends AbstractView { // Permet de savoir si c'est le même channel // Check to know if it's the same channel - this.channelManager.channel.members_id.forEach((member_id) => { + this.channelManager.channel.members.forEach((member_id) => { if (member_id == user.id) this.channelManager.channel = undefined; }); diff --git a/django/notice/consumers.py b/django/notice/consumers.py index b6a79bc..9ab38da 100644 --- a/django/notice/consumers.py +++ b/django/notice/consumers.py @@ -65,17 +65,14 @@ class NoticeManager: def notify_friend_removed(self, user: User, friend: ProfileModel): self.notify_user(user, {'type': 'friend_removed', 'friend': ProfileSerializer(friend).data}) - def ask_game(self, asker:User, asked: User): - self.notify_user(asker, {'type': 'game_asked', 'asker': ProfileSerializer(asked).data}) + def ask_game(self, asked:User, asker: str): + self.notify_user(asked, {'type': 'game_asked', 'asker': asker}) - def ask_game_canceled(self, asker:User, asked: User): - self.notify_user(asker, {'type': 'game_canceled', 'asker': ProfileSerializer(asked).data}) + def refuse_game(self, asker: User, asked: str): + self.notify_user(asker, {'type': 'game_refused', 'asked': asked}) - def refuse_game(self, asked: User, asker: User): - self.notify_user(asked, {'type': 'game_refused', 'asker': ProfileSerializer(asker).data}) - - def accept_game(self, asked: User, asker: User): - self.notify_user(asked, {'type': 'game_accepted', 'asker': ProfileSerializer(asker).data}) + def accept_game(self, asker: User, asked: str, id_game): + self.notify_user(asker, {'type': 'game_accepted', 'asked': asked, 'id_game': id_game}) notice_manager = NoticeManager() diff --git a/django/transcendence/settings.py b/django/transcendence/settings.py index b747a7e..d157a6a 100644 --- a/django/transcendence/settings.py +++ b/django/transcendence/settings.py @@ -108,11 +108,8 @@ WSGI_APPLICATION = 'transcendence.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'HOST': 'django-db', - 'NAME': os.environ['POSTGRES_DB'], - 'USER': os.environ['POSTGRES_USER'], - 'PASSWORD': os.environ['POSTGRES_PASSWORD'], + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', } }