warframe-worldstate-parser
Version:
An Open parser for Warframe's Worldstate in Javascript
100 lines (85 loc) • 2.58 kB
JavaScript
import { timeDeltaToString, steelPath } from 'warframe-worldstate-data/utilities';
const monday = 1;
/**
* Gets the first day of the week
* @returns {Date} first day of the week
*/
function getFirstDayOfWeek() {
const resultDate = new Date();
/* istanbul ignore next */
const offset = resultDate.getUTCDay() === 0 ? 6 : resultDate.getUTCDay() - monday;
resultDate.setUTCDate(resultDate.getUTCDate() - offset);
resultDate.setUTCHours(0);
resultDate.setUTCMinutes(0);
resultDate.setUTCSeconds(0);
resultDate.setUTCMilliseconds(0);
return resultDate;
}
/**
* Get the last day of the week
* @returns {Date} last day of the week
*/
function getLastDayOfWeek() {
const last = new Date(getFirstDayOfWeek());
last.setUTCDate(last.getUTCDate() + 6);
last.setUTCHours(23);
last.setUTCMinutes(59);
last.setUTCSeconds(59);
last.setUTCMilliseconds(0);
return last;
}
/**
* When was the start of the day
* @returns {Date} start of the day
*/
function getStartOfDay() {
const today = new Date();
today.setUTCHours(0);
today.setUTCMinutes(0);
today.setUTCSeconds(0);
today.setUTCMilliseconds(0);
return today;
}
/**
* When was the end of the day
* @returns {Date} end of the day
*/
function getEndOfDay() {
const last = new Date();
last.setUTCHours(23);
last.setUTCMinutes(59);
last.setUTCSeconds(59);
last.setUTCMilliseconds(0);
return last;
}
/**
* Start of Steel Path cycle calculations
* @type {Date}
*/
const start = new Date('2020-11-16T00:00:00.000Z');
export default class SteelPathOffering {
constructor({ locale }) {
const sSinceStart = (Date.now() - start.getTime()) / 1000;
const eightWeeks = 4838400;
const sevenDays = 604800;
const ind = Math.floor((sSinceStart % eightWeeks) / sevenDays);
this.currentReward = steelPath(locale).rotation[ind];
this.activation = getFirstDayOfWeek();
this.expiry = getLastDayOfWeek();
this.remaining = timeDeltaToString(this.expiry.getTime() - Date.now());
this.rotation = steelPath(locale).rotation;
this.evergreens = steelPath(locale).evergreen;
/**
* General data pertaining to incursions
* @type {object}
* @property {string} id Identifier for steel path incursion based on start of day.
* @property {Date} activation when the current incursions became active
* @property {Date} expiry when the current incursions become inactive
*/
this.incursions = {
id: `spi:${getStartOfDay().getTime()}`,
activation: getStartOfDay(),
expiry: getEndOfDay(),
};
}
}