Skip to content

Commit

Permalink
Merge pull request #32 from SAT-Duel/18-fix-joining-room
Browse files Browse the repository at this point in the history
added status for tournament participation and added finish tournament…
  • Loading branch information
FarmerJohnsBessie authored Aug 1, 2024
2 parents 1a3ef7d + ecb8eb3 commit ab5a348
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
19 changes: 19 additions & 0 deletions api/migrations/0033_tournamentparticipation_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.4 on 2024-08-01 23:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0032_tournamentparticipation_last_correct_submission'),
]

operations = [
migrations.AddField(
model_name='tournamentparticipation',
name='status',
field=models.CharField(choices=[('Active', 'Active'), ('Completed', 'Completed')], default='Active', max_length=10),
preserve_default=False,
),
]
1 change: 1 addition & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class TournamentParticipation(models.Model):
end_time = models.DateTimeField(null=True, blank=True)
score = models.IntegerField(default=0)
last_correct_submission = models.DurationField(null=True, blank=True)
status = models.CharField(max_length=10, choices=[('Active', 'Active'), ('Completed', 'Completed')])

def __str__(self):
return f"{self.user.username} in {self.tournament.name}"
Expand Down
13 changes: 13 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ class TournamentQuestionSerializer(serializers.ModelSerializer):
class Meta:
model = TournamentQuestion
fields = ['id', 'participation', 'question', 'status', 'time_taken']


class TPSubmitAnswerSerializer(serializers.ModelSerializer):
tournament_questions = serializers.SerializerMethodField()
user = UserSerializer()
class Meta:
model = TournamentParticipation
fields = ['id', 'user', 'score', 'last_correct_submission', 'tournament_questions']

def get_tournament_questions(self, obj):
# Retrieve and order the questions by ID
questions = obj.tournamentquestion_set.all().order_by('id')
return TournamentQuestionSerializer(questions, many=True).data
18 changes: 11 additions & 7 deletions api/tournaments_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from api.models import Tournament, TournamentParticipation, Question, TournamentQuestion, Profile
from api.serializers import TournamentSerializer, TournamentParticipationSerializer, TournamentQuestionSerializer, \
ProfileSerializer
ProfileSerializer, TPSubmitAnswerSerializer


@api_view(['GET', 'POST'])
Expand Down Expand Up @@ -56,7 +56,8 @@ def join_tournament(request, pk):
user=user,
tournament=tournament,
start_time=timezone.now(),
end_time=timezone.now() + duration
end_time=timezone.now() + duration,
status='Active'
)

questions = tournament.questions.all()
Expand Down Expand Up @@ -97,8 +98,9 @@ def get_tournament_questions(request, pk):
@permission_classes([IsAuthenticated])
def tournament_leaderboard(request, pk):
tournament = get_object_or_404(Tournament, pk=pk)
participations = TournamentParticipation.objects.filter(tournament=tournament).order_by('-score')
serializer = TournamentParticipationSerializer(participations, many=True)
participations = TournamentParticipation.objects.filter(tournament=tournament).order_by('-score',
'last_correct_submission')
serializer = TPSubmitAnswerSerializer(participations, many=True)
return Response(serializer.data)


Expand Down Expand Up @@ -129,7 +131,7 @@ def submit_answer(request, pk):
# Update the score of the user
if is_correct:
participation.score += 1
participation.last_correct_submission = timezone.now()-participation.start_time
participation.last_correct_submission = timezone.now() - participation.start_time
participation.save()

serializer = TournamentQuestionSerializer(tournament_question)
Expand All @@ -139,8 +141,10 @@ def submit_answer(request, pk):
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def finish_participation(request, pk):
participation = get_object_or_404(TournamentParticipation, pk=pk)
participation.end_time = timezone.now()
tournament = get_object_or_404(Tournament, pk=pk)
user = request.user
participation = get_object_or_404(TournamentParticipation, user=user, tournament=tournament)
participation.status = 'Completed'
participation.save()
serializer = TournamentParticipationSerializer(participation)
return Response(serializer.data)
Expand Down
2 changes: 1 addition & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
path('tournaments/<int:pk>/questions/', tournaments_views.get_tournament_questions, name='get-tournament-questions'),
path('tournaments/<int:pk>/leaderboard/', tournaments_views.tournament_leaderboard, name='tournament-leaderboard'),
path('tournaments/<int:pk>/submit-answer/', tournaments_views.submit_answer, name='submit-answer'),
path('participations/<int:pk>/finish/', tournaments_views.finish_participation, name='finish-participation'),
path('tournaments/<int:pk>/finish/', tournaments_views.finish_participation, name='finish-participation'),

path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
Expand Down

0 comments on commit ab5a348

Please sign in to comment.