From fea5e46cda8ea9f7eca4724b669d9058f9707233 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Fri, 10 Dec 2021 16:36:55 -0600 Subject: [PATCH] Fix countdown back bug --- judge/jinja2/timedelta.py | 6 ++++-- resources/common.js | 5 ++--- templates/base.html | 2 +- templates/blog/list.html | 4 ++-- templates/contest/contest.html | 12 ++++++------ templates/contest/list.html | 6 +++--- templates/time-remaining-fragment.html | 4 ++-- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/judge/jinja2/timedelta.py b/judge/jinja2/timedelta.py index 03fd9db..2069610 100644 --- a/judge/jinja2/timedelta.py +++ b/judge/jinja2/timedelta.py @@ -24,5 +24,7 @@ def seconds(timedelta): @registry.filter @registry.render_with('time-remaining-fragment.html') -def as_countdown(timedelta): - return {'countdown': timedelta} +def as_countdown(time): + time_now = datetime.datetime.now(datetime.timezone.utc) + initial = abs(time - time_now) + return {'countdown': time, 'initial': initial} diff --git a/resources/common.js b/resources/common.js index 2455f75..9aad7de 100644 --- a/resources/common.js +++ b/resources/common.js @@ -183,8 +183,7 @@ if (!Date.now) { } function count_down(label) { - var initial = parseInt(label.attr('data-secs')); - var start = Date.now(); + var end_time = new Date(label.attr('data-secs')); function format(num) { var s = "0" + num; @@ -192,7 +191,7 @@ function count_down(label) { } var timer = setInterval(function () { - var time = Math.round(initial - (Date.now() - start) / 1000); + var time = Math.round((end_time - Date.now()) / 1000); if (time <= 0) { clearInterval(timer); setTimeout(function() { diff --git a/templates/base.html b/templates/base.html index 8160848..54c6eba 100644 --- a/templates/base.html +++ b/templates/base.html @@ -289,7 +289,7 @@ {% if request.participation.spectate %} {{ _('spectating') }} {% elif request.participation.end_time %} -
+
{{ request.participation.time_remaining|timedelta("localized") }}
{% else %} diff --git a/templates/blog/list.html b/templates/blog/list.html index 38ead62..386c84d 100644 --- a/templates/blog/list.html +++ b/templates/blog/list.html @@ -153,7 +153,7 @@ {{ contest.name }}
- {{ _('Ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) }} + {{ _('Ends in %(countdown)s.', countdown=contest.end_time|as_countdown) }}
{% endfor %} @@ -171,7 +171,7 @@ {{ contest.name }}
- {{ _('Starting in %(countdown)s.', countdown=contest.time_before_start|as_countdown) }} + {{ _('Starting in %(countdown)s.', countdown=contest.start_time|as_countdown) }}
{% endfor %} diff --git a/templates/contest/contest.html b/templates/contest/contest.html index a65d881..6ff6c95 100644 --- a/templates/contest/contest.html +++ b/templates/contest/contest.html @@ -30,26 +30,26 @@ {{- contest.start_time|utc|date('Y-m-d\TH:i:s') }}" class="date"> {%- if contest.is_in_contest(request.user) and not request.participation.live -%} {% if request.participation.spectate %} - {{- _('Spectating, contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) -}} + {{- _('Spectating, contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) -}} {% elif request.participation.end_time %} - {{- _('Participating virtually, %(countdown)s remaining.', countdown=request.participation.time_remaining|as_countdown) -}} + {{- _('Participating virtually, %(countdown)s remaining.', countdown=request.participation.end_time|as_countdown) -}} {% else %} {{- _('Participating virtually.') -}} {% endif %} {%- else -%} {% if contest.start_time > now %} - {{- _('Starting in %(countdown)s', countdown=contest.time_before_start|as_countdown) -}} + {{- _('Starting in %(countdown)s', countdown=contest.start_time|as_countdown) -}} {% elif contest.end_time < now %} {{- _('Contest is over.') -}} {% else %} {%- if has_joined -%} {% if live_participation.ended %} - {{- _('Your time is up! Contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) -}} + {{- _('Your time is up! Contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) -}} {% else %} - {{- _('You have %(countdown)s remaining.', countdown=live_participation.time_remaining|as_countdown) -}} + {{- _('You have %(countdown)s remaining.', countdown=live_participation.end_time|as_countdown) -}} {% endif %} {%- else -%} - {{ _('Contest ends in %(countdown)s.', countdown=contest.time_before_end|as_countdown) }} + {{ _('Contest ends in %(countdown)s.', countdown=contest.end_time|as_countdown) }} {%- endif -%} {% endif %} {%- endif -%} diff --git a/templates/contest/list.html b/templates/contest/list.html index 2b7abb5..402a086 100644 --- a/templates/contest/list.html +++ b/templates/contest/list.html @@ -252,7 +252,7 @@ {% if contest.time_limit %} {{ _('Window ends in %(countdown)s', countdown=participation.time_remaining|as_countdown)}} {% elif contest.time_before_end %} - {{ _('Ends in %(countdown)s', countdown=contest.time_before_end|as_countdown) }} + {{ _('Ends in %(countdown)s', countdown=contest.end_time|as_countdown) }} {% endif %} {{ time_left(contest) }} {% endif %} @@ -301,7 +301,7 @@ {% if contest.start_time %}
{% if contest.time_before_end %} - {{ _('Ends in %(countdown)s', countdown=contest.time_before_end|as_countdown) }} + {{ _('Ends in %(countdown)s', countdown=contest.end_time|as_countdown) }} {% endif %} {{ time_left(contest) }} {% endif %} @@ -336,7 +336,7 @@ {% if contest.start_time %}
{% if contest.time_before_start %} - {{ _('Starting in %(countdown)s.', countdown=contest.time_before_start|as_countdown) }} + {{ _('Starting in %(countdown)s.', countdown=contest.start_time|as_countdown) }} {% endif %} {{ time_left(contest) }} {% endif %} diff --git a/templates/time-remaining-fragment.html b/templates/time-remaining-fragment.html index eb73d10..19cc7f5 100644 --- a/templates/time-remaining-fragment.html +++ b/templates/time-remaining-fragment.html @@ -1,3 +1,3 @@ - - {{ countdown|timedelta("localized") }} + + {{ initial|timedelta("localized") }} \ No newline at end of file