Skip to content

Commit

Permalink
fix: Better fix for current season
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Aug 24, 2021
1 parent 0cf6c49 commit c653b9a
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions src/scripts/destiny2/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function DestinyApiClient(d2ApiClient) {
this.checkManifestVersion = async function () {
return new Promise(async function (resolve, reject) {
let manifest = await self.getManifest();
if (manifest.Response.version !== self.lastVersion) {
let lastVersion = (await db.getItem("manifestVersion")) ?? "null";
if (manifest.Response.version !== lastVersion) {
/* Currently cached data is older than 60 minutes, so we clear it. */
await db.removeItem("lastManifestUpdate");
await db.removeItem("manifest");
Expand All @@ -72,12 +73,11 @@ function DestinyApiClient(d2ApiClient) {
await db.removeItem(`destinyContent-${dataType}`);
}

self.lastVersion = manifest.Response.version;
self.cachedManifest = manifest.Response;

db.setItem("manifestVersion", self.lastVersion);
db.setItem("manifest", JSON.stringify(self.cachedManifest));
db.setItem("lastManifestUpdate", Date.now());
await db.setItem("manifestVersion", self.lastVersion);
await db.setItem("manifest", JSON.stringify(self.cachedManifest));
await db.setItem("lastManifestUpdate", Date.now());

await self.loadDestinyContentData();

Expand Down Expand Up @@ -516,22 +516,6 @@ function DestinyApiClient(d2ApiClient) {
return lastPlayedCharacter;
};

this.getCurrentSeason = async function () {
await self.checkManifestVersion();

let utcNow = GetUTCDate();

let seasonArray = convertObjectToArray(
self.destinyDataDefinition.DestinySeasonDefinition
);

return seasonArray.find(
(i) =>
Date.parse(i.startDate) <= utcNow.getTime() &&
Date.parse(i.endDate) >= utcNow.getTime()
);
};

this.equipItems = async function (_lastPlayer) {
return new Promise(async (resolve, reject) => {
await pluginClient.POSTJson(
Expand Down Expand Up @@ -792,13 +776,13 @@ function DestinyApiClient(d2ApiClient) {
return null;
}

let currentSeason = await self.getCurrentSeason();

let seasonDefinition =
self.destinyDataDefinition.DestinySeasonDefinition[currentSeason.hash];
self.destinyDataDefinition.DestinySeasonDefinition[
namedObject.profile.currentSeasonHash
];
let seasonPassDefinition =
self.destinyDataDefinition.DestinySeasonPassDefinition[
currentSeason.seasonPassHash
seasonDefinition.seasonPassHash
];

let trackableDataItems = [];
Expand All @@ -825,12 +809,6 @@ function DestinyApiClient(d2ApiClient) {
}

function sortTrackableItems(a, b) {
if (typeof a.endDate !== "undefined") {
return typeof b.endDate === "undefined" || a.endDate < b.endDate
? -1
: 1;
}

if (
typeof a.nextLevelAt !== "undefined" &&
typeof b.nextLevelAt !== "undefined"
Expand All @@ -841,6 +819,12 @@ function DestinyApiClient(d2ApiClient) {
return aProgress < bProgress ? 1 : -1;
}

if (typeof a.endDate !== "undefined") {
return typeof b.endDate === "undefined" || a.endDate < b.endDate
? -1
: 1;
}

return a.order < b.order ? 1 : -1;
}

Expand Down

0 comments on commit c653b9a

Please sign in to comment.