Skip to content

Commit

Permalink
feat(*): improve networking, caching, space usage
Browse files Browse the repository at this point in the history
fixes #67, #66, #65
precursor requirement for #57
  • Loading branch information
nijikokun committed Jun 25, 2018
1 parent 13c2582 commit 8c55053
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 185 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"js-base64": "2.4.3",
"jsoncomp": "1.6.1",
"material-design-icons": "3.0.1",
"moment": "^2.22.2",
"moment-shortformat": "^2.1.0",
"react-select": "1.1.0",
"slug": "^0.9.1",
"tiny-emitter": "2.0.2"
Expand Down
2 changes: 1 addition & 1 deletion src/classes/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Item {
}

// Impresence's
if (this.name.indexOf('Impresence') > -1) {
if (this.fullName.indexOf('Impresence') > -1) {
let variants = [
// 0 = modifier, 1 = variant name
['physical damage', 'Physical'],
Expand Down
153 changes: 110 additions & 43 deletions src/classes/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
getPercentageChange
} from '../helpers'

import moment from 'moment-shortformat'
import { Base64 } from 'js-base64'
import Item from '../classes/item'
import Ago from '../classes/ago'

const HOUR = 1000 * 60 * 60

Expand Down Expand Up @@ -40,11 +40,37 @@ class Portfolio {
this.isUpdating = false
this.isOld = false

// Convert history elements to classes
this.history.forEach(record => {
record.items.forEach(entry => {
entry.item = Item.toItem(entry.item)
})
this.migrate()
}

migrate () {
this.history = this.history.map(record => {
let firstItem = record.items[0]

if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
record.items = record.items.map((v, index) => {
v.item = Item.toItem(v.item)
return this.convertReportItemObjectToArray(v)
})
}

if (!record.hash) {
record.hash = encode({
total: record.total,
items: record.items.map(v => [ v[1], v[5] ])
})
}

return record
})
}

// remove old tab item hashes
cleanup () {
let { tabs, tabItems } = this
Object.keys(tabItems).forEach(id => {
let tab = tabs.find(tab => tab.value === id)
if (!tab) delete tabItems[id]
})
}

Expand All @@ -71,32 +97,24 @@ class Portfolio {
// ran only when a tab is updated
update (tab, tabItems) {
let tabItemsHash = encode(tabItems)

this.lastChecked = Date.now()
this.isUpdating = this.shouldUpdateTabItems(tab.value, tabItemsHash)
if (this.isUpdating) {
this.tabItems[tab.value] = tabItemsHash
this.onUpdate()
return true
return this.onUpdate()
}

return false
}

// remove old tab item hashes
cleanup () {
let { tabs, tabItems } = this
Object.keys(tabItems).forEach(id => {
if (!tabs.find(tab => tab.value === id)) {
delete tabItems[id]
}
})
}

onUpdate () {
// Cleanup tab item hashes before updating
this.cleanup()

// Update portfolio items
let {tabItems, history, league} = this
let previous = this.latestReport()
let prices = CC.Prices[league]
let cluster = []

Expand All @@ -111,44 +129,86 @@ class Portfolio {
return
}

let clusterItem = cluster.find(entry => {
return entry.item.fullName === item.fullName
})

if (!clusterItem || clusterItem.stackSize == null) {
clusterItem = {}
clusterItem.item = item
clusterItem.price = itemPrice
clusterItem.stackSize = item.stackSize
clusterItem.chaosValue = item.stackSize * itemPrice.chaosValue
clusterItem.children = []
let clusterItemIndex = cluster.findIndex(v =>
v[1] === item.fullName &&
v[2] === item.variant &&
v[6] === item.links
)

return cluster.push(clusterItem)
if (clusterItemIndex < 0) {
return cluster.push(this.buildReportItem(item, itemPrice))
}

if (clusterItem) {
clusterItem.stackSize += item.stackSize
clusterItem.chaosValue += item.stackSize * itemPrice.chaosValue
clusterItem.children.push(item)
}
let clusterItem = cluster[clusterItemIndex]
clusterItem[3] += item.stackSize
clusterItem[5] += item.stackSize * itemPrice.chaosValue
clusterItem[clusterItem.length - 1].push([
[item.stackSize, [item.source.inventoryId, item.source.w, item.source.x, item.source.y]]
])
})
})

let report = {
createdAt: Date.now(),
total: cluster.reduce((p, c) => p + c.chaosValue, 0),
items: cluster.sort((a, b) => {
return b.chaosValue - a.chaosValue
})
total: cluster.reduce((p, c) => p + c[5], 0),
items: cluster.sort((a, b) => b[5] - a[5])
}

this.history.push(report)
report.hash = encode({
total: report.total,
items: report.items.map(v => [ v[1], v[5] ])
})

let pushReport = true

while (this.history.length > 24) {
this.history.shift()
if (previous && previous.hash === report.hash) {
pushReport = false
}

if (pushReport) {
this.history.push(report)
}

if (previous) {
while (this.history.length > 24) {
this.history.shift()
}
}

this.isUpdating = false

return pushReport
}

buildReportItem (item, itemPrice) {
return [
itemPrice.icon || item.icon,
item.fullName,
item.variant,
item.stackSize,
itemPrice.chaosValue,
item.stackSize * itemPrice.chaosValue,
item.links,
itemPrice.gemQuality,
itemPrice.gemLevel,
[ this.buildReportItemChild(item) ]
]
}

buildReportItemChild (item) {
return [item.stackSize, [item.source.inventoryId, item.source.w, item.source.x, item.source.y]]
}

convertReportItemObjectToArray (v) {
let reportItem = this.buildReportItem(v.item, v.price)

reportItem[3] = v.stackSize
reportItem[5] = v.chaosValue

let children = reportItem[reportItem.length - 1]
v.children.forEach(cv => children.push(this.buildReportItemChild(cv)))

return reportItem
}

latestReport () {
Expand All @@ -161,8 +221,15 @@ class Portfolio {
}

getLastUpdateTime () {
if (this.isUpdating) {
return 'updating...'
}

let lastUpdatedAt = this.lastUpdatedAt()
return lastUpdatedAt ? Ago(lastUpdatedAt) : 'syncing'

return lastUpdatedAt
? moment(lastUpdatedAt).short()
: 'building...'
}

getCurrencyRate (currency) {
Expand Down
11 changes: 11 additions & 0 deletions src/classes/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,20 @@ class Requester {
delete this.listeners[id]
}

offByRequestName (name) {
Object.keys(this.listeners).forEach(id => {
if (name === this.listeners[id].name) {
this.off(id)
}
})
}

fire (o) {
let now = Date.now()

Object.keys(this.listeners).forEach(id => {
let listener = this.listeners[id]

if (o.name === listener.name) {
if (!listener.interval || !listener.lastCalled) {
listener.callable(o.response)
Expand All @@ -254,6 +264,7 @@ class Requester {
// Modifying
remove (name) {
delete this.requests[name]
this.offByRequestName(name)
}

add ({ name, method }) {
Expand Down
Loading

0 comments on commit 8c55053

Please sign in to comment.