diff --git a/CTFd/api/v1/challenges.py b/CTFd/api/v1/challenges.py index 2ccd9598..9f108747 100644 --- a/CTFd/api/v1/challenges.py +++ b/CTFd/api/v1/challenges.py @@ -6,7 +6,17 @@ from sqlalchemy.sql import and_ from CTFd.cache import clear_standings from CTFd.models import ChallengeFiles as ChallengeFilesModel -from CTFd.models import Challenges, Fails, Flags, Hints, HintUnlocks, Solves, Tags, db +from CTFd.models import ( + Challenges, + Fails, + Flags, + Hints, + HintUnlocks, + Solves, + Submissions, + Tags, + db, +) from CTFd.plugins.challenges import CHALLENGE_CLASSES, get_chal_class from CTFd.schemas.flags import FlagSchema from CTFd.schemas.hints import HintSchema @@ -273,6 +283,15 @@ class Challenge(Resource): else: response["solves"] = None + if authed(): + # Get current attempts for the user + attempts = Submissions.query.filter_by( + account_id=user.account_id, challenge_id=challenge_id + ).count() + else: + attempts = 0 + + response["attempts"] = attempts response["files"] = files response["tags"] = tags response["hints"] = hints @@ -283,6 +302,8 @@ class Challenge(Resource): files=files, tags=tags, hints=[Hints(**h) for h in hints], + max_attempts=chal.max_attempts, + attempts=attempts, challenge=chal, ) diff --git a/CTFd/themes/core/templates/challenge.html b/CTFd/themes/core/templates/challenge.html index 817b1dfb..f5059eea 100644 --- a/CTFd/themes/core/templates/challenge.html +++ b/CTFd/themes/core/templates/challenge.html @@ -83,6 +83,16 @@ {% endfor %} + {% if max_attempts > 1 %} +
+ {{ attempts }}/{{ max_attempts }} attempt{{ attempts|pluralize(attempts) }} +
+