can invite to play a game in chat

This commit is contained in:
Xamora 2024-05-14 14:36:04 +02:00
parent 96a5094fd2
commit 1135014d0f
7 changed files with 69 additions and 52 deletions

View File

@ -6,6 +6,6 @@ from .views import ask
urlpatterns = [ urlpatterns = [
path("", chat.ChannelView.as_view(), name="chats_page"), path("", chat.ChannelView.as_view(), name="chats_page"),
path("ask/", ask.AskView.as_view(), name="chats_ask"), path("ask/", ask.AskView.as_view(), name="chats_ask"),
path("ask/accept/", ask.AskAcceptView.as_view(), name="chats_ask_accept"),
path("ask/<int:pk>", ask.AskView.as_view(), name="chats_ask_get"), path("ask/<int:pk>", ask.AskView.as_view(), name="chats_ask_get"),
path("ask/accept", ask.AskAcceptView.as_view(), name="chats_ask_accept"),
] ]

View File

@ -4,6 +4,7 @@ from rest_framework import permissions, status
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
from chat.models import AskModel from chat.models import AskModel
from games.models import GameModel
from notice.consumers import notice_manager from notice.consumers import notice_manager
@ -21,15 +22,15 @@ class AskView(APIView):
asker_id = request.user.pk asker_id = request.user.pk
asked_id = data["asked"] asked_id = data["asked"]
print("1")
if (asked_id is None): if (asked_id is None):
return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)
print("2")
if AskModel().is_asked(asker_id, asked_id): if AskModel().is_asked(asker_id, asked_id):
return Response(status=status.HTTP_208_ALREADY_REPORTED) 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() AskModel(asker_id=asker_id, asked_id=asked_id).save()
return Response(status=status.HTTP_201_CREATED) return Response(status=status.HTTP_201_CREATED)
@ -41,23 +42,17 @@ class AskView(APIView):
asked_id = request.user.pk asked_id = request.user.pk
if (asker_id is None): 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): if not AskModel().is_asked(asker_id, asked_id):
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
# Don't need more verification, just above is enough # Don't need more verification, just above is enough
asker = User.objects.get(pk=asker_id) 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) return Response(status=status.HTTP_200_OK)
@ -70,7 +65,7 @@ class AskView(APIView):
if (asked_id is None): if (asked_id is None):
return Response(status=status.HTTP_400_BAD_REQUEST) 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_204_NO_CONTENT)
return Response(status=status.HTTP_200_OK) return Response(status=status.HTTP_200_OK)
@ -82,6 +77,7 @@ class AskAcceptView(APIView):
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
def post(self, request): def post(self, request):
data: dict = request.data data: dict = request.data
asker_id = data["asker"] asker_id = data["asker"]
@ -91,12 +87,14 @@ class AskAcceptView(APIView):
return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)
if not AskModel().is_asked(asker_id, asked_id): 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) 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() notice_manager.accept_game(asker, request.user.username, id_game)
return Response(status=status.HTTP_200_OK)
AskModel.objects.get(asker_id=asker_id, asked_id=asked_id).delete()
return Response({"id_game":id_game}, status=status.HTTP_200_OK)

View File

@ -1,6 +1,6 @@
import {Client} from './Client.js'; import {Client} from './Client.js';
import {createNotification} from '../utils/noticeUtils.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 ProfilePageView from '../views/ProfilePageView.js';
import Search from '../views/Search.js'; import Search from '../views/Search.js';
@ -17,13 +17,12 @@ export default class Notice {
this.url = location.origin.replace('http', 'ws') + '/ws/notice'; this.url = location.origin.replace('http', 'ws') + '/ws/notice';
} }
start() { async start() {
this._socket = new WebSocket(this.url); this._socket = new WebSocket(this.url);
this._socket.onclose = _ => this._socket = undefined; this._socket.onclose = _ => this._socket = undefined;
this._socket.onmessage = message => { this._socket.onmessage = async message => {
const data = JSON.parse(message.data); const data = JSON.parse(message.data);
console.log(data)
if (data.type === 'friend_request') { if (data.type === 'friend_request') {
this.friend_request(data.author); this.friend_request(data.author);
@ -38,13 +37,11 @@ export default class Notice {
} else if (data.type === 'offline') { } else if (data.type === 'offline') {
this.offline(data.user) this.offline(data.user)
} else if (data.type == 'game_asked') { } else if (data.type == 'game_asked') {
this.game_asked(data.asker);
} else if (data.type == 'game_canceled') {
} else if (data.type == 'game_accepted') { } else if (data.type == 'game_accepted') {
this.game_accepted(data.asked, data.id_game);
} else if (data.type == 'game_refused') { } else if (data.type == 'game_refused') {
this.game_refused(data.asked);
} }
}; };
} }
@ -106,4 +103,24 @@ export default class Notice {
lastView.loadFriendshipStatus(); lastView.loadFriendshipStatus();
} }
} }
game_asked(asker) {
createNotification('Game Invite', `<b>${asker}</b> ask to play a 1vs1 pong`);
if (lastView instanceof Search)
lastView.display_invite();
}
game_refused(asked) {
createNotification('Game Refused', `<b>${asked}</b> refuse your proposition to play`);
if (lastView instanceof Search)
lastView.display_invite();
}
async game_accepted(asked, id_game) {
createNotification('Game Accepted', `<b>${asked}</b> accept your proposition to play`);
if (lastView instanceof Search)
lastView.display_invite();
await navigateTo(`/games/pong/${id_game}`);
}
} }

View File

@ -1,3 +1,5 @@
import { lastView, navigateTo } from '../../index.js';
import Search from '../../views/Search.js';
export default class Ask { export default class Ask {
constructor(client) { constructor(client) {
@ -10,16 +12,19 @@ export default class Ask {
}); });
} }
async ask_game_canceled() {
}
async ask_game_accepted(asker) { 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, 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) { async ask_game_refused(asker) {
@ -27,14 +32,17 @@ export default class Ask {
asker:asker, 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) { async is_asked(asked) {
let response = await this.client._get(`/api/chat/ask/${asked}`); let response = await this.client._get(`/api/chat/ask/${asked}`);
const statu = response.status; const statu = response.status;
console.log(statu);
if (statu == 404 || statu == 204) if (statu == 404 || statu == 204)
return false; return false;
return true; return true;

View File

@ -69,7 +69,7 @@ export default class extends AbstractView {
// Permet de savoir si c'est le même channel // Permet de savoir si c'est le même channel
// Check to know if it's the same 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) if (member_id == user.id)
this.channelManager.channel = undefined; this.channelManager.channel = undefined;
}); });

View File

@ -65,17 +65,14 @@ class NoticeManager:
def notify_friend_removed(self, user: User, friend: ProfileModel): def notify_friend_removed(self, user: User, friend: ProfileModel):
self.notify_user(user, {'type': 'friend_removed', 'friend': ProfileSerializer(friend).data}) self.notify_user(user, {'type': 'friend_removed', 'friend': ProfileSerializer(friend).data})
def ask_game(self, asker:User, asked: User): def ask_game(self, asked:User, asker: str):
self.notify_user(asker, {'type': 'game_asked', 'asker': ProfileSerializer(asked).data}) self.notify_user(asked, {'type': 'game_asked', 'asker': asker})
def ask_game_canceled(self, asker:User, asked: User): def refuse_game(self, asker: User, asked: str):
self.notify_user(asker, {'type': 'game_canceled', 'asker': ProfileSerializer(asked).data}) self.notify_user(asker, {'type': 'game_refused', 'asked': asked})
def refuse_game(self, asked: User, asker: User): def accept_game(self, asker: User, asked: str, id_game):
self.notify_user(asked, {'type': 'game_refused', 'asker': ProfileSerializer(asker).data}) self.notify_user(asker, {'type': 'game_accepted', 'asked': asked, 'id_game': id_game})
def accept_game(self, asked: User, asker: User):
self.notify_user(asked, {'type': 'game_accepted', 'asker': ProfileSerializer(asker).data})
notice_manager = NoticeManager() notice_manager = NoticeManager()

View File

@ -108,11 +108,8 @@ WSGI_APPLICATION = 'transcendence.wsgi.application'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql', 'ENGINE': 'django.db.backends.sqlite3',
'HOST': 'django-db', 'NAME': BASE_DIR / 'db.sqlite3',
'NAME': os.environ['POSTGRES_DB'],
'USER': os.environ['POSTGRES_USER'],
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
} }
} }