From 8688f60223ac7ef23b501d3cd7788cb9cdd2291a Mon Sep 17 00:00:00 2001 From: queicherius Date: Tue, 26 Mar 2019 20:43:20 +0000 Subject: [PATCH] Correct raids endpoint to use the weekly reset instead of the daily reset --- src/endpoints/account.js | 6 +-- src/helpers/getLastDailyReset.js | 9 ---- src/helpers/resetTime.js | 61 +++++++++++++++++++++ tests/endpoints/account.spec.js | 2 +- tests/helpers/getLastDailyReset.spec.js | 20 ------- tests/helpers/resetTime.spec.js | 72 +++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 33 deletions(-) delete mode 100644 src/helpers/getLastDailyReset.js create mode 100644 src/helpers/resetTime.js delete mode 100644 tests/helpers/getLastDailyReset.spec.js create mode 100644 tests/helpers/resetTime.spec.js diff --git a/src/endpoints/account.js b/src/endpoints/account.js index 8dbfafe..37d51ad 100644 --- a/src/endpoints/account.js +++ b/src/endpoints/account.js @@ -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) { @@ -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 [] } @@ -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 [] } diff --git a/src/helpers/getLastDailyReset.js b/src/helpers/getLastDailyReset.js deleted file mode 100644 index ae9a2b0..0000000 --- a/src/helpers/getLastDailyReset.js +++ /dev/null @@ -1,9 +0,0 @@ -function getLastDailyReset () { - // Get a string of the date/time of today, in UTC, at midnight - const date = new Date().toISOString().replace(/T.*Z/, `T00:00:00.000Z`) - - // Parse the string back into a Date object - return new Date(date) -} - -module.exports = getLastDailyReset diff --git a/src/helpers/resetTime.js b/src/helpers/resetTime.js new file mode 100644 index 0000000..59d50ee --- /dev/null +++ b/src/helpers/resetTime.js @@ -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 +} diff --git a/tests/endpoints/account.spec.js b/tests/endpoints/account.spec.js index f5a1fea..d0a58b5 100644 --- a/tests/endpoints/account.spec.js +++ b/tests/endpoints/account.spec.js @@ -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([]) diff --git a/tests/helpers/getLastDailyReset.spec.js b/tests/helpers/getLastDailyReset.spec.js deleted file mode 100644 index 4fb804d..0000000 --- a/tests/helpers/getLastDailyReset.spec.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env jest */ -const mockdate = require('mockdate') -const getLastDailyReset = require('../../src/helpers/getLastDailyReset') - -describe('helpers > getLastDailyReset', () => { - const _lastDailyReset = (date) => { - mockdate.set(date) - return getLastDailyReset().toISOString() - } - - it('gets the last reset correctly', () => { - 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') - }) -}) diff --git a/tests/helpers/resetTime.spec.js b/tests/helpers/resetTime.spec.js new file mode 100644 index 0000000..69c2465 --- /dev/null +++ b/tests/helpers/resetTime.spec.js @@ -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') + }) +})