Skip to content

Commit

Permalink
Correct raids endpoint to use the weekly reset instead of the daily r…
Browse files Browse the repository at this point in the history
…eset
  • Loading branch information
queicherius committed Mar 26, 2019
1 parent 022b545 commit 8688f60
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/endpoints/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const CharactersEndpoint = require('./characters')
const PvpEndpoint = require('./pvp')
const CommerceEndpoint = require('./commerce')
const accountBlob = require('./account-blob.js')
const getLastDailyReset = require('../helpers/getLastDailyReset')
const resetTime = require('../helpers/resetTime')

class AccountEndpoint extends AbstractEndpoint {
constructor (client) {
Expand Down Expand Up @@ -172,7 +172,7 @@ class DungeonsEndpoint extends AbstractEndpoint {
async get () {
// Discard stale data if the last account update was before the last daily reset
const account = await new AccountEndpoint(this).schema('2019-03-26').get()
if (new Date(account.last_modified) < getLastDailyReset()) {
if (new Date(account.last_modified) < resetTime.getLastDailyReset()) {
return []
}

Expand Down Expand Up @@ -317,7 +317,7 @@ class RaidsEndpoint extends AbstractEndpoint {
async get () {
// Discard stale data if the last account update was before the last daily reset
const account = await new AccountEndpoint(this).schema('2019-03-26').get()
if (new Date(account.last_modified) < getLastDailyReset()) {
if (new Date(account.last_modified) < resetTime.getLastWeeklyReset()) {
return []
}

Expand Down
9 changes: 0 additions & 9 deletions src/helpers/getLastDailyReset.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/helpers/resetTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const DAY_MS = 24 * 60 * 60 * 1000

function getDateAtTime (date, time) {
return new Date(date.toISOString().replace(/T.*Z/, `T${time}.000Z`))
}

function getDailyReset (date) {
date = date ? new Date(date) : new Date()

date = new Date(date.getTime() + DAY_MS)
return getDateAtTime(date, '00:00:00')
}

function getLastDailyReset (date) {
return new Date(getDailyReset(date).getTime() - DAY_MS)
}

function getWeeklyReset (date) {
date = date ? new Date(date) : new Date()

const weekday = date.getUTCDay()
const hours = date.getUTCHours()
const minutes = date.getUTCMinutes()
let dayDiff = 0

switch (weekday) {
case 0:
// 0 -> 1 sunday
dayDiff = 1
break
case 1:
// 1 -> 0 monday (if before reset)
// 1 -> 7 monday (if after reset)
const pastReset = hours > 7 || (hours === 7 && minutes >= 30)
dayDiff = pastReset ? 7 : 0
break
default:
// 2 -> 6 tuesday
// 3 -> 5 wednesday
// 4 -> 4 thursday
// 5 -> 3 friday
// 6 -> 2 saturday
dayDiff = 8 - weekday
break
}

date = new Date(date.getTime() + dayDiff * DAY_MS)
return getDateAtTime(date, '07:30:00')
}

function getLastWeeklyReset (date) {
return new Date(getWeeklyReset(date).getTime() - 7 * DAY_MS)
}

module.exports = {
getDateAtTime,
getDailyReset,
getLastDailyReset,
getWeeklyReset,
getLastWeeklyReset
}
2 changes: 1 addition & 1 deletion tests/endpoints/account.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ describe('endpoints > account', () => {
expect(endpoint.cacheTime).not.toEqual(undefined)
expect(endpoint.url).toEqual('/v2/account/raids')

fetchMock.addResponse({ name: 'AAA.1234', last_modified: '2019-04-01T23:53:00Z' })
fetchMock.addResponse({ name: 'AAA.1234', last_modified: '2019-04-01T06:53:00Z' })
fetchMock.addResponse(['spirit_woods', 'keep_construct'])
let content = await endpoint.get()
expect(content).toEqual([])
Expand Down
20 changes: 0 additions & 20 deletions tests/helpers/getLastDailyReset.spec.js

This file was deleted.

72 changes: 72 additions & 0 deletions tests/helpers/resetTime.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-env jest */
const mockdate = require('mockdate')
const resetTime = require('../../src/helpers/resetTime')

describe('helpers > resetTime', () => {
it('gets a day at a specific time', () => {
expect(resetTime.getDateAtTime(new Date('2017-08-13T02:04:52.278Z'), '00:01:00'))
.toEqual(new Date('2017-08-13T00:01:00.000Z'))
})

it('gets the next daily reset', () => {
const dailyReset = (date) => {
return resetTime.getDailyReset(date).toISOString()
}

expect(dailyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-14T00:00:00.000Z')
expect(dailyReset('2017-08-11T02:04:52.278Z')).toEqual('2017-08-12T00:00:00.000Z')
expect(dailyReset('2017-07-31T12:04:52.278Z')).toEqual('2017-08-01T00:00:00.000Z')

expect(dailyReset('2017-08-11T23:59:59.000Z')).toEqual('2017-08-12T00:00:00.000Z')
expect(dailyReset('2017-08-12T00:00:00.000Z')).toEqual('2017-08-13T00:00:00.000Z')
expect(dailyReset('2017-08-12T00:00:01.000Z')).toEqual('2017-08-13T00:00:00.000Z')
})

it('gets the last daily reset', () => {
const lastDailyReset = (date) => {
mockdate.set(date)
return resetTime.getLastDailyReset().toISOString()
}

expect(lastDailyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-13T00:00:00.000Z')
expect(lastDailyReset('2017-08-11T02:04:52.278Z')).toEqual('2017-08-11T00:00:00.000Z')
expect(lastDailyReset('2017-07-31T12:04:52.278Z')).toEqual('2017-07-31T00:00:00.000Z')

expect(lastDailyReset('2017-08-11T23:59:59.000Z')).toEqual('2017-08-11T00:00:00.000Z')
expect(lastDailyReset('2017-08-12T00:00:00.000Z')).toEqual('2017-08-12T00:00:00.000Z')
expect(lastDailyReset('2017-08-12T00:00:01.000Z')).toEqual('2017-08-12T00:00:00.000Z')
})

it('gets the next weekly reset', () => {
const weeklyReset = (date) => {
return resetTime.getWeeklyReset(date).toISOString()
}

expect(weeklyReset('2017-08-10T02:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(weeklyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(weeklyReset('2017-08-14T06:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(weeklyReset('2017-07-31T09:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z')

expect(weeklyReset('2017-08-14T07:29:59.000Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(weeklyReset('2017-08-14T07:30:00.000Z')).toEqual('2017-08-21T07:30:00.000Z')
expect(weeklyReset('2017-08-14T07:30:01.000Z')).toEqual('2017-08-21T07:30:00.000Z')
expect(weeklyReset('2017-08-14T07:35:00.000Z')).toEqual('2017-08-21T07:30:00.000Z')
})

it('gets the last weekly reset', () => {
const lastWeeklyReset = (date) => {
mockdate.set(date)
return resetTime.getLastWeeklyReset().toISOString()
}

expect(lastWeeklyReset('2017-08-10T02:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z')
expect(lastWeeklyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z')
expect(lastWeeklyReset('2017-08-14T06:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z')
expect(lastWeeklyReset('2017-07-31T09:04:52.278Z')).toEqual('2017-07-31T07:30:00.000Z')

expect(lastWeeklyReset('2017-08-14T07:29:59.000Z')).toEqual('2017-08-07T07:30:00.000Z')
expect(lastWeeklyReset('2017-08-14T07:30:00.000Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(lastWeeklyReset('2017-08-14T07:30:01.000Z')).toEqual('2017-08-14T07:30:00.000Z')
expect(lastWeeklyReset('2017-08-14T07:35:00.000Z')).toEqual('2017-08-14T07:30:00.000Z')
})
})

0 comments on commit 8688f60

Please sign in to comment.