Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
feat: adds new inventory double check (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
cujarrett authored Jun 2, 2021
1 parent 028bc9d commit f26f4d0
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 172 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@semantic-release/github": "~7.2.3",
"@semantic-release/npm": "~7.1.3",
"@semantic-release/release-notes-generator": "~9.0.2",
"aws-sdk": "~2.913.0",
"aws-sdk": "~2.920.0",
"commitizen": "~4.2.4",
"cz-conventional-changelog": "~3.3.0",
"eslint": "~7.27.0",
Expand Down
18 changes: 2 additions & 16 deletions src/integrations/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,10 @@ module.exports.getLastSoldAda1Mods = async () => {
})
}

let sortedResults = results.sort((first, second) => {
const sortedResults = results.sort((first, second) => {
return new Date(first.timestamp) - new Date(second.timestamp)
})

// When Bungie's API is down during reset the mods are sold "late" so the next day when the app
// searches for the last sold mods in the last 24 hours, more than the current four are sold, we
// need to remove the oldest ones to get to the newest mods sold
if (sortedResults.length > 4) {
sortedResults = sortedResults.slice(Math.max(sortedResults.length - 4, 0))
}

return sortedResults
}

Expand Down Expand Up @@ -182,17 +175,10 @@ module.exports.getLastSoldBanshee44Mods = async () => {
})
}

let sortedResults = results.sort((first, second) => {
const sortedResults = results.sort((first, second) => {
return new Date(first.timestamp) - new Date(second.timestamp)
})

// When Bungie's API is down during reset the mods are sold "late" so the next day when the app
// searches for the last sold mods in the last 24 hours, more than the current four are sold, we
// need to remove the oldest ones to get to the newest mods sold
if (sortedResults.length > 4) {
sortedResults = sortedResults.slice(Math.max(sortedResults.length - 4, 0))
}

return sortedResults
}

Expand Down
160 changes: 86 additions & 74 deletions src/util/get-ada-1-inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,104 +8,116 @@ const { getLastSoldMessge } = require("./get-last-sold-message.js")
const { getManifest } = require ("./get-manifest.js")
const { isBungieApiDownForMaintenance } = require("./is-bungie-api-down-for-maintenance.js")
const { isNewInventory } = require ("./is-new-inventory.js")
const { isSameInventory } = require ("./is-same-inventory.js")

module.exports.getAda1Inventory = async (auth) => {
console.log("getAda1Inventory called")
const { accessToken, apiKey } = auth
const options = {
"method": "get",
"headers": {
"Authorization": `Bearer ${accessToken}`,
"X-API-Key": apiKey
}
}

// eslint-disable-next-line max-len
const adaItemDefinitionsEndpoint = "https://www.bungie.net/Platform/Destiny2/3/Profile/4611686018467431261/Character/2305843009299499863/Vendors/350061650/?components=300,301,302,304,305,400,401,402"
let adaInventoryResponse = await fetch(adaItemDefinitionsEndpoint, options)

let isValidAuth = adaInventoryResponse.status === 200
const maxRetries = 5
let manifest
let manifestRetries
let itemDefinitions
let inventoryItemDefinitionEndpoint
let usedCachedData = true
const maxAuthRetries = 5
let authRetries = 0
if (!isValidAuth) {
const isBungieApiDownForMaintenanceFlag = await isBungieApiDownForMaintenance(auth)
if (isBungieApiDownForMaintenanceFlag) {
// eslint-disable-next-line max-len
throw new Error("The Bungie API is down for maintenance. Check https://twitter.com/BungieHelp for more info.")
}

while (authRetries < maxRetries && !isValidAuth) {
authRetries += 1
adaInventoryResponse = await fetch(adaItemDefinitionsEndpoint, options)
isValidAuth = adaInventoryResponse.status === 200
const getCurrentAda1Mods = async () => {
console.log("getCurrentAda1Mods called")
const { accessToken, apiKey } = auth
const options = {
"method": "get",
"headers": {
"Authorization": `Bearer ${accessToken}`,
"X-API-Key": apiKey
}
}

if (authRetries === maxRetries && !isValidAuth) {
throw new Error(`The Bungie auth failed to load ${maxRetries} times`)
}
}
// eslint-disable-next-line max-len
const adaItemDefinitionsEndpoint = "https://www.bungie.net/Platform/Destiny2/3/Profile/4611686018467431261/Character/2305843009299499863/Vendors/350061650/?components=300,301,302,304,305,400,401,402"
let adaInventoryResponse = await fetch(adaItemDefinitionsEndpoint, options)

const adaInventory = await adaInventoryResponse.json()
const categoryData = adaInventory.Response.categories.data.categories
const salesData = adaInventory.Response.sales.data
let isValidAuth = adaInventoryResponse.status === 200
if (!isValidAuth) {
const isBungieApiDownForMaintenanceFlag = await isBungieApiDownForMaintenance(auth)
if (isBungieApiDownForMaintenanceFlag) {
// eslint-disable-next-line max-len
throw new Error("The Bungie API is down for maintenance. Check https://twitter.com/BungieHelp for more info.")
}

let manifest
let manifestRetries
let itemDefinitions
let inventoryItemDefinitionEndpoint
while (authRetries < maxAuthRetries && !isValidAuth) {
authRetries += 1
adaInventoryResponse = await fetch(adaItemDefinitionsEndpoint, options)
isValidAuth = adaInventoryResponse.status === 200
}

const getItemDefinitions = async () => {
console.log("getItemDefinitions called")
if (itemDefinitions === undefined) {
try {
const manifestResponse = await getManifest()
manifest = manifestResponse.manifest
manifestRetries = manifestResponse.manifestRetries
} catch (error) {
const result = { metadata: { error } }
console.log(`Completing request:\n${JSON.stringify(result, null, " ")}`)
return JSON.stringify(result, null, " ")
if (authRetries === maxAuthRetries && !isValidAuth) {
throw new Error(`The Bungie auth failed to load ${maxAuthRetries} times`)
}
}

inventoryItemDefinitionEndpoint = getInventoryItemDefinitionEndpoint(manifest)
const itemDefinitionsResponse = await fetch(inventoryItemDefinitionEndpoint)
itemDefinitions = await itemDefinitionsResponse.json()
const adaInventory = await adaInventoryResponse.json()
const categoryData = adaInventory.Response.categories.data.categories
const salesData = adaInventory.Response.sales.data

const getItemDefinitions = async () => {
console.log("getItemDefinitions called")
if (itemDefinitions === undefined) {
try {
const manifestResponse = await getManifest()
manifest = manifestResponse.manifest
manifestRetries = manifestResponse.manifestRetries
} catch (error) {
const result = { metadata: { error } }
console.log(`Completing request:\n${JSON.stringify(result, null, " ")}`)
return JSON.stringify(result, null, " ")
}

inventoryItemDefinitionEndpoint = getInventoryItemDefinitionEndpoint(manifest)
const itemDefinitionsResponse = await fetch(inventoryItemDefinitionEndpoint)
itemDefinitions = await itemDefinitionsResponse.json()
}
return itemDefinitions
}
return itemDefinitions
}

// Get categories for weapon mods for sale
const [firstModCategory, secondModCategory] = categoryData[1].itemIndexes
const categories = [firstModCategory, secondModCategory]
// Get categories for weapon mods for sale
const [firstModCategory, secondModCategory] = categoryData[1].itemIndexes
const categories = [firstModCategory, secondModCategory]

const currentMods = []
for (const category of categories) {
const itemHash = salesData[category].itemHash
let mod = cachedMods[itemHash]

if (mod === undefined) {
usedCachedData = false
mod = {}
await getItemDefinitions()
mod.type = itemDefinitions[itemHash].itemTypeAndTierDisplayName
mod.name = itemDefinitions[itemHash].displayProperties.name
}

const currentMods = []
let usedCachedData = true
for (const category of categories) {
const itemHash = salesData[category].itemHash
let mod = cachedMods[itemHash]

if (mod === undefined) {
usedCachedData = false
mod = {}
await getItemDefinitions()
mod.type = itemDefinitions[itemHash].itemTypeAndTierDisplayName
mod.name = itemDefinitions[itemHash].displayProperties.name
mod.itemHash = itemHash
currentMods.push(mod)
}

mod.itemHash = itemHash
currentMods.push(mod)
return currentMods
}

const currentMods = await getCurrentAda1Mods()
const lastSoldMods = await getLastSoldAda1Mods()
const newInventory = await isNewInventory(currentMods, lastSoldMods)

let lastUpdated = undefined
if (newInventory) {
const timestamp = new Date().toISOString()
for (const mod of currentMods) {
await addMod(mod, timestamp)
const doubleCheckedMods = await getCurrentAda1Mods()
const confirmedNewMods = await isSameInventory(currentMods, doubleCheckedMods)
if (confirmedNewMods) {
const timestamp = new Date().toISOString()
for (const mod of currentMods) {
await addMod(mod, timestamp)
}
lastUpdated = timestamp
} else {
lastUpdated = lastSoldMods[lastSoldMods.length - 1].timestamp
}
lastUpdated = timestamp
} else {
lastUpdated = lastSoldMods[lastSoldMods.length - 1].timestamp
}
Expand Down
Loading

0 comments on commit f26f4d0

Please sign in to comment.