Skip to content

Commit

Permalink
Merge pull request #7 from SAT-Duel/6-improve-matching-page
Browse files Browse the repository at this point in the history
Added some functions regarding matching. User cannot try to match twice
  • Loading branch information
FarmerJohnsBessie authored Jul 6, 2024
2 parents 89844ff + 874fda5 commit c88b967
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Room(models.Model):
user2 = models.ForeignKey(User, related_name='room_user2', on_delete=models.CASCADE, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
questions = models.ManyToManyField(Question, blank=True)
status = models.CharField(max_length=10,
choices=[('Searching','Searching'), ('Battling', 'Battling'), ('Ended', 'Ended')])

def is_full(self):
return self.user2 is not None
Expand Down
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
path('match/update/', views.update_match_question, name='update_match_question'),
path('match/status/', views.get_room_status, name='get_room_status'),
path('match/get_opponent_progress/', views.get_opponent_progres, name='get_opponent_progress'),
path('match/rejoin/', views.rejoin_match, name='rejoin_match'),

path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
Expand Down
42 changes: 35 additions & 7 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from django.db.models import Q

from api.serializers import QuestionSerializer, ProfileSerializer, RoomSerializer, TrackedQuestionSerializer, \
ProfileBiographySerializer, UserSerializer, FriendRequestSerializer
Expand Down Expand Up @@ -39,6 +40,7 @@ def check_answer(request):
correct = (question.answer_text == selected_choice)
return Response({'result': 'correct' if correct else 'incorrect'})


@api_view(['POST'])
def get_answer(request):
data = request.data
Expand All @@ -47,7 +49,8 @@ def get_answer(request):
question = Question.objects.get(id=question_id)
except Question.DoesNotExist:
return Response({'error': 'Question does not exist'}, status=404)
return Response({'answer': question.answer_text, 'explanation': question.explanation, 'answer_choice': question.answer})
return Response(
{'answer': question.answer_text, 'explanation': question.explanation, 'answer_choice': question.answer})


@api_view(['GET', 'POST'])
Expand All @@ -70,14 +73,19 @@ def profile_view(request):
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def match(request):
room = Room.objects.filter(user2__isnull=True).first()
if room and room.user1 == request.user:
return Response({'error': 'You are already in the room'}, status=400)
user_in_room = Room.objects.filter(user1=request.user, status__in=['Searching', 'Battling']).first()

if user_in_room:
return Response({'error': 'You are already matching or in a room'}, status=400)

room = Room.objects.filter(user2__isnull=True, status='Searching').first()

if room:
room.user2 = request.user
room.status = 'Battling'
room.save()
else:
room = Room.objects.create(user1=request.user)
room = Room.objects.create(user1=request.user, status='Searching')

serializer = RoomSerializer(room)
return Response(serializer.data, status=200)
Expand All @@ -101,6 +109,17 @@ def get_match_questions(request):
serializer = TrackedQuestionSerializer(tracked_questions, many=True)
return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def rejoin_match(request):
room = Room.objects.filter(
Q(user1=request.user, status='Battling') | Q(user2=request.user, status='Battling'),
).first()
print(room)
if room:
return Response({'room_id': room.id})
return Response({'error': 'No room found'}, status=404)

@api_view(['POST'])
def get_question(request):
Expand Down Expand Up @@ -177,6 +196,7 @@ def update_biography(request):
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -186,6 +206,7 @@ def search_users(request):
serializer = UserSerializer(users, many=True)
return Response(serializer.data)


@api_view(['POST'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -197,14 +218,16 @@ def send_friend_request(request):
if FriendRequest.objects.filter(from_user=from_user, to_user=to_user, status='pending').exists():
return Response({'detail': 'Friend request already sent.'}, status=status.HTTP_400_BAD_REQUEST)
if from_user == to_user:
return Response({'detail': 'You cannot send friend request to yourself.'}, status=status.HTTP_400_BAD_REQUEST)
return Response({'detail': 'You cannot send friend request to yourself.'},
status=status.HTTP_400_BAD_REQUEST)
if from_user.profile.friends.filter(id=to_user_id).exists():
return Response({'detail': 'You are already friends.'}, status=status.HTTP_400_BAD_REQUEST)
FriendRequest.objects.create(from_user=from_user, to_user=to_user)
return Response({'detail': 'Friend request sent.'}, status=status.HTTP_201_CREATED)
except User.DoesNotExist:
return Response({'detail': 'User not found.'}, status=status.HTTP_404_NOT_FOUND)


@api_view(['POST'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -222,6 +245,7 @@ def respond_friend_request(request, request_id):
except FriendRequest.DoesNotExist:
return Response({'detail': 'Friend request not found.'}, status=status.HTTP_404_NOT_FOUND)


@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -230,6 +254,7 @@ def list_friend_requests(request):
serializer = FriendRequestSerializer(friend_requests, many=True)
return Response(serializer.data)


@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -240,6 +265,7 @@ def list_friends(request):
serializer = ProfileSerializer(profiles, many=True)
return Response(serializer.data)


@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -249,4 +275,6 @@ def view_profile(request, user_id):
except User.DoesNotExist:
return Response({'error': 'User not found'}, status=404)
serializer = ProfileSerializer(user.profile)
return Response(serializer.data)
return Response(serializer.data)


Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit c88b967

Please sign in to comment.