can invite to play a game in chat
This commit is contained in:
parent
96a5094fd2
commit
1135014d0f
@ -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/<int:pk>", ask.AskView.as_view(), name="chats_ask_get"),
|
||||
path("ask/accept", ask.AskAcceptView.as_view(), name="chats_ask_accept"),
|
||||
]
|
||||
|
@ -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)
|
||||
|
@ -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', `<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}`);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
});
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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',
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user