Skip to content

Commit

Permalink
fix calculation for future tournaments
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Gehorsam committed Sep 19, 2023
1 parent b5c9f1f commit 7833dcf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr

## [Unreleased]

### Fixed
- Fixes calculation of leaderboard and tournament times for rare types of CRON expressions that don't execute at a fixed interval.
- Improved how start and end times are calculated for tournaments occuring in the future.

### [3.17.1] - 2023-08-23
### Added
- Add Satori `recompute` optional input parameter to relevant operations.
Expand Down
27 changes: 15 additions & 12 deletions server/core_tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,23 +731,26 @@ func TournamentRecordsHaystack(ctx context.Context, logger *zap.Logger, db *sql.
func calculateTournamentDeadlines(startTime, endTime, duration int64, resetSchedule *cronexpr.Expression, t time.Time) (int64, int64, int64) {
tUnix := t.UTC().Unix()
if resetSchedule != nil {
if tUnix < startTime {
// if startTime is in the future, always use startTime
t = time.Unix(startTime, 0).UTC()
tUnix = t.UTC().Unix()
}

var startActiveUnix int64
// check if we are landing squarely on the reset schedule
if resetSchedule.Next(t.Add(-1*time.Second)) == t {
startActiveUnix = tUnix

if tUnix < startTime {
// the supplied time is behind the start time
startActiveUnix = resetSchedule.Next(time.Unix(startTime, 0).UTC()).UTC().Unix()
} else {
// otherwise assume the supplied time is ahead of the reset schedule.
startActiveUnix = resetSchedule.Last(t).UTC().Unix()
// check if we are landing squarely on the reset schedule
landsOnSched := resetSchedule.Next(t.Add(-1*time.Second)) == t
if landsOnSched {
startActiveUnix = tUnix
} else {
startActiveUnix = resetSchedule.Last(t).UTC().Unix()
}
}

// endActiveUnix is when the current iteration ends.
endActiveUnix := startActiveUnix + duration
expiryUnix := resetSchedule.Next(t).UTC().Unix()
// expiryUnix represent the start of the next schedule, i.e., when the next iteration begins. It's when the current records "expire".
expiryUnix := resetSchedule.Next(time.Unix(startActiveUnix, 0).UTC()).UTC().Unix()

if endActiveUnix > expiryUnix {
// Cap the end active to the same time as the expiry.
endActiveUnix = expiryUnix
Expand Down
15 changes: 8 additions & 7 deletions server/core_tournament_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,16 @@ func TestTournamentNowIsBeforeStart(t *testing.T) {
t.Fatal("Invalid cron schedule", err)
return
}

var now int64 = 1692003600 // 14 August 2023, 9:00:00
var startTime int64 = 1693558800 // 1 September 2023, 9:00:00
var duration int64 = 604800 * 1 // 1 week
var duration int64 = 604800 * 4 // 4 Weeks

nowUnix := time.Unix(now, 0).UTC()
startActiveUnix, endActiveUnix, expiryTime := calculateTournamentDeadlines(startTime, 0, duration, sched, nowUnix)
// September 14, 2023, 9:00:00
startActiveUnix, endActiveUnix, _ := calculateTournamentDeadlines(startTime, 0, duration, sched, nowUnix)

// 14 September 2023, 9:00:00
require.Equal(t, int64(1694682000), startActiveUnix, "Start active times should be equal.")
// September 21, 2023, 9:00:00
require.Equal(t, int64(1695286800), endActiveUnix, "End active times should be equal.")
// October 14, 2023 9:00:00
require.Equal(t, int64(1697274000), expiryTime, "Next reset times should be equal.")
// 12 October 2023, 9:00:00
require.Equal(t, int64(1697101200), endActiveUnix, "End active times should be equal.")
}

0 comments on commit 7833dcf

Please sign in to comment.