@@ -6,11 +6,12 @@ const axios = require('axios');
6
6
// TODO: Make this into class
7
7
/**
8
8
* @param {Number } start time to start plot
9
- * @param {Number } interval time interval to plot
10
- * @param {Number } end time to end plot
9
+ * @param {Number } interval time interval to plot, set to -1 or undefined to disable profit plot
10
+ * @param {Number } end time to end plot, set to -1 to disale profit timed and plot or undefined to disable just plot
11
+ * @param {Boolean } enableTrades enable return of profit data for each trade
11
12
* @return {Object }
12
13
*/
13
- exports . get = async function get ( start , interval , end ) {
14
+ exports . get = async function get ( start , interval , end , enableTrades ) {
14
15
const polldata = await fs . readJSON ( paths . files . polldata ) ;
15
16
const response = await axios (
16
17
{
@@ -44,12 +45,17 @@ exports.get = async function get(start, interval, end) {
44
45
return a - b ;
45
46
} ) ;
46
47
48
+ const tradeProfits = { } ;
49
+
47
50
let iter = 0 ; // to keep track of how many trades are accepted
48
51
for ( let i = 0 ; i < trades . length ; i ++ ) { // TODO: ADMIN TRADES
49
52
const trade = trades [ i ] ;
50
53
if ( ! ( trade . handledByUs === true && trade . isAccepted === true ) ) {
51
54
continue ; // trade was not accepted, go to next trade
52
55
}
56
+
57
+ let tradeProfit = 0 ;
58
+
53
59
iter ++ ;
54
60
let isGift = false ;
55
61
if ( ! Object . prototype . hasOwnProperty . call ( trade , 'dict' ) ) {
@@ -92,7 +98,7 @@ exports.get = async function get(start, interval, end) {
92
98
}
93
99
const prices = trade . prices [ sku ] . buy ;
94
100
95
- tracker . boughtItem ( itemCount , sku , prices , trade . value . rate , trade . time ) ;
101
+ tradeProfit += tracker . boughtItem ( itemCount , sku , prices , trade . value . rate , trade . time ) ;
96
102
}
97
103
}
98
104
}
@@ -105,23 +111,27 @@ exports.get = async function get(start, interval, end) {
105
111
continue ; // item is not in pricelist, so we will just skip it
106
112
}
107
113
const prices = trade . prices [ sku ] . sell ;
108
- tracker . soldItem ( itemCount , sku , prices , trade . value . rate , trade . time ) ;
114
+ tradeProfit += tracker . soldItem ( itemCount , sku , prices , trade . value . rate , trade . time ) ;
109
115
}
110
116
}
111
117
}
112
118
if ( ! isGift ) { // calculate overprice profit
119
+ tradeProfit += tracker . convert ( trade . value . their , trade . value . rate ) - tracker . convert ( trade . value . our , trade . value . rate ) ;
113
120
overpriceProfit += tracker . convert ( trade . value . their , trade . value . rate ) - tracker . convert ( trade . value . our , trade . value . rate ) ;
114
121
tracker . profitTrack . countProfit ( tracker . convert ( trade . value . their , trade . value . rate ) - tracker . convert ( trade . value . our , trade . value . rate ) , trade . time ) ;
115
122
}
123
+ tradeProfits [ trade . id ] = tracker . profitTrack . getFormated ( tradeProfit ) ;
116
124
}
117
- return {
125
+ const returnObj = {
118
126
profitTotal : tracker . profitTrack . getFormated ( tracker . profitTrack . profit ) ,
119
127
profitTimed : tracker . profitTrack . getFormated ( tracker . profitTrack . profitTimed ) ,
120
128
profitPlot : tracker . profitTrack . profitPlot ,
121
129
numberOfTrades : iter ,
122
130
overpriceProfit : tracker . profitTrack . getFormated ( overpriceProfit ) ,
123
131
keyValue : keyVal
124
132
} ;
133
+ if ( enableTrades ) returnObj [ 'tradeProfits' ] = tradeProfits ;
134
+ return returnObj ;
125
135
} ;
126
136
127
137
/**
@@ -200,7 +210,7 @@ class profitTracker {
200
210
metal : this . currentKey
201
211
} ) . toValue ( this . currentKey ) ; // get value in scrap
202
212
const metal = Currency . toRefined ( normalPrice % key ) ;
203
- const keys = Math . floor ( normalPrice / key ) ;
213
+ const keys = normalPrice > 0 ? Math . floor ( normalPrice / key ) : Math . ceil ( normalPrice / key ) ;
204
214
return new Currency ( { keys, metal} ) . toString ( ) ;
205
215
}
206
216
}
@@ -229,18 +239,21 @@ class itemTracker {
229
239
* @param {Object } prices prices for this item
230
240
* @param {Number } rate key rate
231
241
* @param {Number } time
242
+ * @return {Number } profit made on this item
232
243
*/
233
244
boughtItem ( itemCount , sku , prices , rate , time ) {
245
+ let itemProfit = 0 ;
234
246
if ( Object . prototype . hasOwnProperty . call ( this . overItems , sku ) ) { // if record for this item exists in overItems check it
235
247
if ( this . overItems [ sku ] . count > 0 ) {
236
248
if ( this . overItems [ sku ] . count >= itemCount ) {
237
249
this . overItems [ sku ] . count -= itemCount ;
238
250
this . profitTrack . countProfit ( ( this . overItems [ sku ] . price - this . convert ( prices , rate ) ) * itemCount , time ) ;
239
- return ; // everything is already sold no need to add to stock
251
+ return ( this . overItems [ sku ] . price - this . convert ( prices , rate ) ) * itemCount ; // everything is already sold no need to add to stock
240
252
} else {
241
253
const itemsOverOverItems = itemCount - this . overItems [ sku ] . count ;
242
254
this . overItems [ sku ] . count = 0 ;
243
255
this . profitTrack . countProfit ( ( this . overItems [ sku ] . price - this . convert ( prices , rate ) ) * ( itemCount - itemsOverOverItems ) , time ) ;
256
+ itemProfit += ( this . overItems [ sku ] . price - this . convert ( prices , rate ) ) * ( itemCount - itemsOverOverItems ) ;
244
257
itemCount = itemsOverOverItems ;
245
258
}
246
259
}
@@ -256,6 +269,7 @@ class itemTracker {
256
269
price : this . convert ( prices , rate )
257
270
} ;
258
271
}
272
+ return itemProfit ;
259
273
}
260
274
261
275
/**
@@ -265,14 +279,17 @@ class itemTracker {
265
279
* @param {Object } prices prices for item sold
266
280
* @param {Number } rate key rate
267
281
* @param {Number } time time of trade
282
+ * @return {Number } profit made on this item
268
283
*/
269
284
soldItem ( itemCount , sku , prices , rate , time ) {
285
+ let itemProfit = 0 ;
270
286
if ( Object . prototype . hasOwnProperty . call ( this . itemStock , sku ) ) { // have we bought this item already
271
287
if ( this . itemStock [ sku ] . count >= itemCount ) {
272
288
this . itemStock [ sku ] . count -= itemCount ;
273
289
this . profitTrack . countProfit ( ( this . convert ( prices , rate ) - this . itemStock [ sku ] . price ) * itemCount , time ) ;
274
- return ;
290
+ return ( this . convert ( prices , rate ) - this . itemStock [ sku ] . price ) * itemCount ;
275
291
} else {
292
+ itemProfit += ( this . convert ( prices , rate ) - this . itemStock [ sku ] . price ) * this . itemStock [ sku ] . count ;
276
293
this . profitTrack . countProfit ( ( this . convert ( prices , rate ) - this . itemStock [ sku ] . price ) * this . itemStock [ sku ] . count , time ) ;
277
294
itemCount -= this . itemStock [ sku ] . count ;
278
295
this . itemStock [ sku ] . count = 0 ;
@@ -289,6 +306,7 @@ class itemTracker {
289
306
price : this . convert ( prices , rate )
290
307
} ;
291
308
}
309
+ return itemProfit ;
292
310
}
293
311
294
312
/**
0 commit comments