Skip to content

Commit 7833dcf

Browse files
author
Luke Gehorsam
committed
fix calculation for future tournaments
1 parent b5c9f1f commit 7833dcf

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
55

66
## [Unreleased]
77

8+
### Fixed
9+
- Fixes calculation of leaderboard and tournament times for rare types of CRON expressions that don't execute at a fixed interval.
10+
- Improved how start and end times are calculated for tournaments occuring in the future.
11+
812
### [3.17.1] - 2023-08-23
913
### Added
1014
- Add Satori `recompute` optional input parameter to relevant operations.

server/core_tournament.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -731,23 +731,26 @@ func TournamentRecordsHaystack(ctx context.Context, logger *zap.Logger, db *sql.
731731
func calculateTournamentDeadlines(startTime, endTime, duration int64, resetSchedule *cronexpr.Expression, t time.Time) (int64, int64, int64) {
732732
tUnix := t.UTC().Unix()
733733
if resetSchedule != nil {
734-
if tUnix < startTime {
735-
// if startTime is in the future, always use startTime
736-
t = time.Unix(startTime, 0).UTC()
737-
tUnix = t.UTC().Unix()
738-
}
739-
740734
var startActiveUnix int64
741-
// check if we are landing squarely on the reset schedule
742-
if resetSchedule.Next(t.Add(-1*time.Second)) == t {
743-
startActiveUnix = tUnix
735+
736+
if tUnix < startTime {
737+
// the supplied time is behind the start time
738+
startActiveUnix = resetSchedule.Next(time.Unix(startTime, 0).UTC()).UTC().Unix()
744739
} else {
745-
// otherwise assume the supplied time is ahead of the reset schedule.
746-
startActiveUnix = resetSchedule.Last(t).UTC().Unix()
740+
// check if we are landing squarely on the reset schedule
741+
landsOnSched := resetSchedule.Next(t.Add(-1*time.Second)) == t
742+
if landsOnSched {
743+
startActiveUnix = tUnix
744+
} else {
745+
startActiveUnix = resetSchedule.Last(t).UTC().Unix()
746+
}
747747
}
748748

749+
// endActiveUnix is when the current iteration ends.
749750
endActiveUnix := startActiveUnix + duration
750-
expiryUnix := resetSchedule.Next(t).UTC().Unix()
751+
// expiryUnix represent the start of the next schedule, i.e., when the next iteration begins. It's when the current records "expire".
752+
expiryUnix := resetSchedule.Next(time.Unix(startActiveUnix, 0).UTC()).UTC().Unix()
753+
751754
if endActiveUnix > expiryUnix {
752755
// Cap the end active to the same time as the expiry.
753756
endActiveUnix = expiryUnix

server/core_tournament_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ func TestTournamentNowIsBeforeStart(t *testing.T) {
9797
t.Fatal("Invalid cron schedule", err)
9898
return
9999
}
100+
100101
var now int64 = 1692003600 // 14 August 2023, 9:00:00
101102
var startTime int64 = 1693558800 // 1 September 2023, 9:00:00
102-
var duration int64 = 604800 * 1 // 1 week
103+
var duration int64 = 604800 * 4 // 4 Weeks
104+
103105
nowUnix := time.Unix(now, 0).UTC()
104-
startActiveUnix, endActiveUnix, expiryTime := calculateTournamentDeadlines(startTime, 0, duration, sched, nowUnix)
105-
// September 14, 2023, 9:00:00
106+
startActiveUnix, endActiveUnix, _ := calculateTournamentDeadlines(startTime, 0, duration, sched, nowUnix)
107+
108+
// 14 September 2023, 9:00:00
106109
require.Equal(t, int64(1694682000), startActiveUnix, "Start active times should be equal.")
107-
// September 21, 2023, 9:00:00
108-
require.Equal(t, int64(1695286800), endActiveUnix, "End active times should be equal.")
109-
// October 14, 2023 9:00:00
110-
require.Equal(t, int64(1697274000), expiryTime, "Next reset times should be equal.")
110+
// 12 October 2023, 9:00:00
111+
require.Equal(t, int64(1697101200), endActiveUnix, "End active times should be equal.")
111112
}

0 commit comments

Comments
 (0)