-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-estimate.js
executable file
·111 lines (107 loc) · 3.24 KB
/
make-estimate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const csv = require('csv')
var assert = require('assert')
const ALPHABET = 'KMBTPEZY'.split('')
const TRESHOLD = 1e3
const humanNumber = function (num) {
var n = Math.abs(num)
var index = 0
while (n >= TRESHOLD && ++index < ALPHABET.length) { n /= TRESHOLD }
n = Math.round(Number.parseFloat(n))
var str = index === 0 ? n : n + ALPHABET[index - 1]
return String(str).toLowerCase()
}
var pasteParser = require('./parser');
module.exports = function (pasteText, cb) {
var parser = csv.parse()
//console.log('reading ./output.csv...')
var csvStream = require('fs').createReadStream('./output.csv', {flags: 'r'})
var i = 0
var nameIndex = {}
var headers;
parser.on('readable', function() {
let record;
while ((record = parser.read()) !== null) {
if (i++ === 0) {
headers = record
assert(headers)
assert.strictEqual(headers.length, 24)
headers.forEach(function (header, idx) {
assert(header, idx);
})
continue
}
assert.strictEqual(record.length, headers.length)
var item = {}
headers.forEach(function (header, idx) {
item[header] = record[idx]
})
nameIndex[item.typeName] = item
}
});
parser.on('end', function () {
//console.log('added', i - 1, 'docs')
var pasteItems = pasteParser(pasteText)
var estimate = {
totalMarketValue: 0,
itemsProcessed: 0,
itemsNotFound: 0,
itemsFound: 0,
itemsByID: {},
items: []
}
pasteItems.forEach(function (pasteItem) {
estimate.itemsProcessed++
var item = nameIndex[pasteItem.name]
if (!item) {
estimate.itemsNotFound++
console.error('warning: Item not found: "' + pasteItem.name + '"')
return;
}
var est
if (!estimate.itemsByID[item.typeID]) {
est = estimate.itemsByID[item.typeID] = JSON.parse(JSON.stringify(item))
Object.keys(est).forEach(function (k) {
est[k] = est[k].trim()
if (est[k] === '') {
est[k] = null
est[k + '_human'] = ''
}
else if (est[k].match(/^[0-9\.]+$/)) {
est[k] = Number(est[k])
est[k + '_human'] = humanNumber(est[k])
}
})
est.itemsFound = 0
est.totalMarketValue = 0
}
else {
est = estimate.itemsByID[item.typeID]
}
est.itemsFound += pasteItem.quantity
estimate.itemsFound += pasteItem.quantity
est.totalMarketValue += Number(item.minPrice) * pasteItem.quantity
est.totalMarketValue_human = humanNumber(est.totalMarketValue)
estimate.totalMarketValue += est.totalMarketValue
})
estimate.items = Object.values(estimate.itemsByID).sort(function (a, b) {
if (a.minPrice > b.minPrice) return -1;
if (a.minPrice === b.minPrice) return 0;
return 1;
})
delete estimate.itemsByID
estimate.totalMarketValue_human = humanNumber(estimate.totalMarketValue)
cb(null, estimate)
})
parser.once('error', function(err) {
//console.error(err.message);
return cb(err)
});
parser.on('end', function() {
//console.log('ended')
});
csvStream.on('end', function () {
//console.log('tsvStream end')
parser.end()
})
csvStream.pipe(parser)
}