Skip to content

Commit f17dcc1

Browse files
committed
fix comp time left drifting
1 parent c0c9e32 commit f17dcc1

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

client/src/Challenges/challenges.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import { Transition, animated } from 'react-spring';
1313
const { Meta } = Card;
1414
const { Option } = Select;
1515

16-
var countDownTimes = {}
17-
1816
class Challenges extends React.Component {
1917
constructor(props) {
2018
super(props);
@@ -80,27 +78,36 @@ class Challenges extends React.Component {
8078
// Competition hasn't started
8179
if (currentTime < startTime) {
8280
const timeLeft = Math.ceil((startTime - currentTime) / 1000)
83-
countDownTimes[data[x]._id] = { time: timeLeft, tillStart: true } // time left in seconds till start
8481
countDownTimerStrings[data[x]._id] = this.getTimerString(timeLeft, true)
8582
}
8683
else {
8784
const timeLeft = Math.ceil((endTime - currentTime) / 1000)
88-
countDownTimes[data[x]._id] = { time: timeLeft, tillStart: false } // time left in seconds till end
8985
countDownTimerStrings[data[x]._id] = this.getTimerString(timeLeft, false)
9086
}
9187
}
9288
}
9389
this.setState({ countDownTimerStrings: countDownTimerStrings })
94-
setInterval(this.countDownTicker.bind(this), 1000 * 10)
90+
setInterval(this.countDownTicker.bind(this), 1000 * 15, data)
9591
return [challengeMetaInfo, data];
9692
}
9793

98-
countDownTicker() {
99-
let countDownTimerStrings = this.state.countDownTimerStrings
100-
for (const key in countDownTimes) {
101-
const current = countDownTimes[key]
102-
current.time -= 10
103-
countDownTimerStrings[key] = this.getTimerString(current.time, current.tillStart)
94+
countDownTicker(data) {
95+
let countDownTimerStrings = {}
96+
for (let x = 0; x < data.length; x++) {
97+
if ("time" in data[x].meta) {
98+
const startTime = new Date(data[x].meta.time[0])
99+
const endTime = new Date(data[x].meta.time[1])
100+
const currentTime = new Date()
101+
// Competition hasn't started
102+
if (currentTime < startTime) {
103+
const timeLeft = Math.ceil((startTime - currentTime) / 1000)
104+
countDownTimerStrings[data[x]._id] = this.getTimerString(timeLeft, true)
105+
}
106+
else {
107+
const timeLeft = Math.ceil((endTime - currentTime) / 1000)
108+
countDownTimerStrings[data[x]._id] = this.getTimerString(timeLeft, false)
109+
}
110+
}
104111
}
105112
this.setState({ countDownTimerStrings: countDownTimerStrings })
106113
}
@@ -176,7 +183,7 @@ class Challenges extends React.Component {
176183
for (let i = 0; i < originalData.length; i++) {
177184
originalDataDictionary[originalData[i]._id] = originalData[i].challenges
178185
}
179-
this.setState({disableNonCatFB: data.disableNonCatFB, userCategories: data.userCategories, categories: categoryMetaInfo, originalData: originalDataDictionary, loadingChall: false })
186+
this.setState({ disableNonCatFB: data.disableNonCatFB, userCategories: data.userCategories, categories: categoryMetaInfo, originalData: originalDataDictionary, loadingChall: false })
180187
let categoryChall = this.props.match.params.categoryChall;
181188
const mongoID = /^[a-f\d]{24}$/i
182189
if (typeof categoryChall !== "undefined") {
@@ -196,7 +203,7 @@ class Challenges extends React.Component {
196203
}
197204
}
198205
if (foundChallenge) {
199-
this.setState({ currentCategory: foundChallenge.category, currentCategoryChallenges: [originalDataDictionary[foundChallenge.category]], foundChallenge: foundChallenge })
206+
this.setState({ currentCategory: foundChallenge.category, currentCategoryChallenges: [originalDataDictionary[foundChallenge.category]], foundChallenge: foundChallenge })
200207
}
201208
else message.error("Challenge with ID '" + challenge + "' not found.")
202209
}

client/src/Scoreboard/Scoreboard.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Scoreboard extends React.Component {
6565

6666
lastSolveTiming() {
6767
if (!updating) {
68+
// this uses absolute timing, so the drift from setInterval does not matter
6869
let scoreArray = this.state.scores
6970
for (let x = 0; x < scoreArray.length; x++) {
7071

0 commit comments

Comments
 (0)