Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 9bbc30c

Browse files
committed
Make game start countdown timer count down
Rather than being a static number updated on page refresh, the countdown clock will not update itself. This makes for a more interesting display. Using time remaining rather than an absolute time to prevent jittery behaviour at the start, though at the expense of accuracy.
1 parent eb06ec4 commit 9bbc30c

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/controllers/IndexController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class="fb-cta cta--yellow">
124124
);
125125
$game = $config_game->getValue();
126126
$next_game = $config_next_game->getValue();
127+
$remaining = 0;
127128
if ($game === '1') {
128129
$next_game_text = tr('In Progress');
129130
$countdown = array('--', '--', '--', '--');
@@ -137,6 +138,7 @@ class="fb-cta cta--yellow">
137138
$now = new DateTime('now');
138139
$countdown_diff = $now->diff($game_start);
139140
$countdown = explode('-', $countdown_diff->format('%d-%h-%i-%s'));
141+
$remaining = $game_start->getTimestamp() - $now->getTimestamp();
140142
}
141143
return
142144
<div class="fb-row-container full-height fb-scroll">
@@ -149,21 +151,21 @@ class=
149151
<h1 class="fb-glitch" data-text={$next_game_text}>
150152
{$next_game_text}
151153
</h1>
152-
<ul class="upcoming-game-countdown">
154+
<ul class="upcoming-game-countdown" data-remaining={$remaining}>
153155
<li>
154-
<span class="count-number">{$countdown[0]}</span>
156+
<span class="count-number countdown-days">{$countdown[0]}</span>
155157
{tr('_days')}
156158
</li>
157159
<li>
158-
<span class="count-number">{$countdown[1]}</span>
160+
<span class="count-number countdown-hours">{$countdown[1]}</span>
159161
{tr('_hours')}
160162
</li>
161163
<li>
162-
<span class="count-number">{$countdown[2]}</span>
164+
<span class="count-number countdown-minutes">{$countdown[2]}</span>
163165
{tr('_minutes')}
164166
</li>
165167
<li>
166-
<span class="count-number">{$countdown[3]}</span>
168+
<span class="count-number countdown-seconds">{$countdown[3]}</span>
167169
{tr('_seconds')}
168170
</li>
169171
</ul>

src/static/js/countdown.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @flow
2+
3+
var $ = require('jquery');
4+
5+
function noCountdown(container) {
6+
$('.countdown-days', container).text('--');
7+
$('.countdown-hours', container).text('--');
8+
$('.countdown-minutes', container).text('--');
9+
$('.countdown-seconds', container).text('--');
10+
}
11+
12+
module.exports = {
13+
_container: null,
14+
_remaining: 0,
15+
_timeDiff: 0,
16+
_interval: null,
17+
18+
startCountdown: function() {
19+
this._container = $('.upcoming-game-countdown');
20+
if( !this._container.length ) {
21+
return;
22+
}
23+
24+
this._remaining = parseInt(this._container.data().remaining);
25+
26+
if(!this._container.length || !this._remaining) {
27+
return noCountdown(this._container);
28+
}
29+
30+
var self = this;
31+
this._interval = setInterval(
32+
function() {
33+
self.setTimeRemaining()
34+
},
35+
1000
36+
);
37+
},
38+
39+
setTimeRemaining: function setTimeRemaining() {
40+
this._remaining -= 1;
41+
var secs = this._remaining;
42+
43+
if( secs < 0 ) {
44+
noCountdown(this._container);
45+
if(this._interval) {
46+
clearInterval(this._interval);
47+
}
48+
return;
49+
}
50+
51+
var days = parseInt((secs/(60*60*24)) % 24);
52+
var hours = parseInt((secs/(60*60)) % 24);
53+
var minutes = parseInt((secs/60) % 60);
54+
var seconds = parseInt(secs % 60);
55+
56+
$('.countdown-days', this._container).text(days);
57+
$('.countdown-hours', this._container).text(hours);
58+
$('.countdown-minutes', this._container).text(minutes);
59+
$('.countdown-seconds', this._container).text(seconds);
60+
}
61+
};

src/static/js/fb-ctf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Utils = require('./utils');
77
var Modal = require('./modal');
88
var Slider = require('./slider');
99
var Clock = require('./clock');
10+
var Countdown = require('./countdown');
1011
var Graphics = require('./graphics');
1112

1213
var d3 = require('d3');
@@ -2862,6 +2863,9 @@ function setupInputListeners() {
28622863
$customEmblemCarouselNotice.removeClass('active');
28632864
});
28642865

2866+
// Start the countdown clock
2867+
Countdown.startCountdown();
2868+
28652869
}; // FB_CTF.init()
28662870
})(window.FB_CTF = {});
28672871

0 commit comments

Comments
 (0)