Skip to content

Commit

Permalink
feat: add api for player performance in fsm
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Nov 16, 2024
1 parent 0f9e5d6 commit 54d4542
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
7 changes: 7 additions & 0 deletions apps/fsm/models/form.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Dict
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import QuerySet
Expand Down Expand Up @@ -192,6 +193,12 @@ class AnswerSheetType(models.TextChoices):
blank=True,
)

def assess(self) -> Dict[int, dict]:
result: Dict[int, dict] = {}
for answer in self.answers.filter(is_final_answer=True):
result[answer.id] = answer.assess()
return result

def delete(self):
self.answers.clear()
return super(AnswerSheet, self).delete()
Expand Down
24 changes: 15 additions & 9 deletions apps/fsm/models/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Literal
from typing import Dict, Union
from django.db import models
from polymorphic.models import PolymorphicModel

Expand All @@ -24,11 +24,11 @@ class AnswerTypes(models.TextChoices):
is_correct = models.BooleanField(default=False)

def get_allocated_rewards(self):
assess_result = self.assess()
assess_result_score = self.assess()['score']
net_rewards = get_net_rewards(self.problem)
return get_allocated_rewards(net_rewards=net_rewards, allocation_percentage=assess_result)
return get_allocated_rewards(net_rewards=net_rewards, allocation_percentage=assess_result_score)

def assess(self) -> Literal[0, 100]:
def assess(self) -> Dict[str, Union[int, str]]:
raise NotImplementedError("Subclasses should implement this method.")

def __str__(self):
Expand All @@ -48,12 +48,15 @@ class SmallAnswer(Answer):
blank=True, on_delete=models.PROTECT, related_name='answers')
text = models.TextField()

def assess(self):
def assess(self) -> Dict[str, Union[int, str]]:
result = 100
if self.problem.correct_answer:
if self.text.strip() != self.problem.correct_answer.text.strip():
return 0
return result
return {
'score': result,
'feedback': 'empty',
}

@property
def string_answer(self):
Expand Down Expand Up @@ -82,12 +85,15 @@ class MultiChoiceAnswer(Answer):
def string_answer(self):
return json.dumps([choice.id for choice in self.choices.all()])

def assess(self):
result = 100
def assess(self) -> Dict[str, Union[int, str]]:
result: int = 100
for choice in self.choices.all():
if not choice.is_correct:
result = 0
return result
return {
'score': result,
'feedback': 'empty',
}


class UploadFileAnswer(Answer):
Expand Down
8 changes: 8 additions & 0 deletions apps/fsm/views/player_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,11 @@ def finish_fsm(self, request, pk=None):
player.finished_at = timezone.now()
player.save()
return Response(status=status.HTTP_204_NO_CONTENT)

@action(detail=True, methods=['get'], url_path='performance')
def get_player_performance(self, request, pk=None):
player: Player = self.get_object()
return Response(
data=player.answer_sheet.assess(),
status=status.HTTP_200_OK,
)

0 comments on commit 54d4542

Please sign in to comment.