Skip to content

Commit ad2e5d5

Browse files
authored
Merge pull request #72 from ZeusJunior/development
v2.1.0
2 parents 9d2743a + b5ddf8e commit ad2e5d5

File tree

8 files changed

+174
-101
lines changed

8 files changed

+174
-101
lines changed

app/profit.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const axios = require('axios');
66
// TODO: Make this into class
77
/**
88
* @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
1112
* @return {Object}
1213
*/
13-
exports.get = async function get(start, interval, end) {
14+
exports.get = async function get(start, interval, end, enableTrades) {
1415
const polldata = await fs.readJSON(paths.files.polldata);
1516
const response = await axios(
1617
{
@@ -44,12 +45,17 @@ exports.get = async function get(start, interval, end) {
4445
return a - b;
4546
});
4647

48+
const tradeProfits = {};
49+
4750
let iter = 0; // to keep track of how many trades are accepted
4851
for (let i = 0; i < trades.length; i++) { // TODO: ADMIN TRADES
4952
const trade = trades[i];
5053
if (!(trade.handledByUs === true && trade.isAccepted === true)) {
5154
continue;// trade was not accepted, go to next trade
5255
}
56+
57+
let tradeProfit = 0;
58+
5359
iter++;
5460
let isGift = false;
5561
if (!Object.prototype.hasOwnProperty.call(trade, 'dict')) {
@@ -92,7 +98,7 @@ exports.get = async function get(start, interval, end) {
9298
}
9399
const prices = trade.prices[sku].buy;
94100

95-
tracker.boughtItem(itemCount, sku, prices, trade.value.rate, trade.time);
101+
tradeProfit += tracker.boughtItem(itemCount, sku, prices, trade.value.rate, trade.time);
96102
}
97103
}
98104
}
@@ -105,23 +111,27 @@ exports.get = async function get(start, interval, end) {
105111
continue; // item is not in pricelist, so we will just skip it
106112
}
107113
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);
109115
}
110116
}
111117
}
112118
if (!isGift) { // calculate overprice profit
119+
tradeProfit += tracker.convert(trade.value.their, trade.value.rate) - tracker.convert(trade.value.our, trade.value.rate);
113120
overpriceProfit += tracker.convert(trade.value.their, trade.value.rate) - tracker.convert(trade.value.our, trade.value.rate);
114121
tracker.profitTrack.countProfit( tracker.convert(trade.value.their, trade.value.rate) - tracker.convert(trade.value.our, trade.value.rate), trade.time);
115122
}
123+
tradeProfits[trade.id] = tracker.profitTrack.getFormated(tradeProfit);
116124
}
117-
return {
125+
const returnObj = {
118126
profitTotal: tracker.profitTrack.getFormated(tracker.profitTrack.profit),
119127
profitTimed: tracker.profitTrack.getFormated(tracker.profitTrack.profitTimed),
120128
profitPlot: tracker.profitTrack.profitPlot,
121129
numberOfTrades: iter,
122130
overpriceProfit: tracker.profitTrack.getFormated(overpriceProfit),
123131
keyValue: keyVal
124132
};
133+
if (enableTrades) returnObj['tradeProfits'] = tradeProfits;
134+
return returnObj;
125135
};
126136

127137
/**
@@ -200,7 +210,7 @@ class profitTracker {
200210
metal: this.currentKey
201211
}).toValue(this.currentKey); // get value in scrap
202212
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);
204214
return new Currency({keys, metal}).toString();
205215
}
206216
}
@@ -229,18 +239,21 @@ class itemTracker {
229239
* @param {Object} prices prices for this item
230240
* @param {Number} rate key rate
231241
* @param {Number} time
242+
* @return {Number} profit made on this item
232243
*/
233244
boughtItem(itemCount, sku, prices, rate, time) {
245+
let itemProfit = 0;
234246
if (Object.prototype.hasOwnProperty.call(this.overItems, sku)) { // if record for this item exists in overItems check it
235247
if (this.overItems[sku].count > 0) {
236248
if (this.overItems[sku].count >= itemCount) {
237249
this.overItems[sku].count -= itemCount;
238250
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
240252
} else {
241253
const itemsOverOverItems = itemCount - this.overItems[sku].count;
242254
this.overItems[sku].count = 0;
243255
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);
244257
itemCount = itemsOverOverItems;
245258
}
246259
}
@@ -256,6 +269,7 @@ class itemTracker {
256269
price: this.convert(prices, rate)
257270
};
258271
}
272+
return itemProfit;
259273
}
260274

261275
/**
@@ -265,14 +279,17 @@ class itemTracker {
265279
* @param {Object} prices prices for item sold
266280
* @param {Number} rate key rate
267281
* @param {Number} time time of trade
282+
* @return {Number} profit made on this item
268283
*/
269284
soldItem(itemCount, sku, prices, rate, time) {
285+
let itemProfit = 0;
270286
if (Object.prototype.hasOwnProperty.call(this.itemStock, sku)) { // have we bought this item already
271287
if (this.itemStock[sku].count >= itemCount) {
272288
this.itemStock[sku].count -= itemCount;
273289
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;
275291
} else {
292+
itemProfit += (this.convert(prices, rate) - this.itemStock[sku].price) * this.itemStock[sku].count;
276293
this.profitTrack.countProfit( (this.convert(prices, rate) - this.itemStock[sku].price) * this.itemStock[sku].count, time);
277294
itemCount -= this.itemStock[sku].count;
278295
this.itemStock[sku].count = 0;
@@ -289,6 +306,7 @@ class itemTracker {
289306
price: this.convert(prices, rate)
290307
};
291308
}
309+
return itemProfit;
292310
}
293311

294312
/**

app/routes/trades.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@ router.get('/', (req, res) => {
1414
return;
1515
}
1616

17-
trades.get()
18-
.then((data) => {
19-
res.render('trades', {
20-
data: data,
21-
polldata: true
22-
});
23-
})
24-
.catch((err) => {
25-
throw err;
26-
});
17+
res.render('trades', {
18+
polldata: true
19+
});
2720
} else {
2821
if (!fs.existsSync(paths.files.polldata)) {
2922
res.json({
@@ -32,7 +25,7 @@ router.get('/', (req, res) => {
3225
return;
3326
}
3427

35-
trades.get()
28+
trades.get(Number(req.query.first), Number(req.query.count), Number(req.query.dir)==1)
3629
.then((data) => {
3730
res.json({
3831
success: 1,

app/trades.js

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,101 @@ const getName = require('../utils/getName');
44
const data = require('./data');
55
const moment = require('moment');
66
const getImage = require('../utils/getImage');
7-
const SKU = require('tf2-sku');
7+
const profit = require('./profit');
88

9+
/**
10+
*
11+
* @param {Number} first index of first trade to be included in results
12+
* @param {Number} count how many trades to include in results, set to -1 to return all
13+
* @param {Boolean} descending sort
14+
*/
15+
exports.get = async function(first, count, descending) {
16+
const polldata = await fs.readJSON(paths.files.polldata);
17+
const profitData = (await profit.get(undefined, undefined, undefined, true)).tradeProfits;
18+
let tradeList = Object.keys(polldata.offerData).map((key)=>{
19+
const ret = polldata.offerData[key];
20+
ret.id = key;
21+
return ret;
22+
});
23+
const tradeCount = tradeList.length;
24+
tradeList = tradeList.sort((a, b)=>{
25+
a = a.finishTimestamp;
26+
b = b.finishTimestamp;
927

10-
exports.get = function() {
11-
return fs.readJSON(paths.files.polldata)
12-
.then((polldata) => {
13-
return Object.keys(polldata.offerData).map((key)=>{
14-
const offer = polldata.offerData[key];
15-
const ret = {
16-
id: key,
17-
lastChange: polldata.timestamps[key],
18-
items: {
19-
our: [],
20-
their: []
21-
},
22-
action: offer.action,
23-
partner: offer.partner,
24-
accepted: offer.accepted,
25-
time: offer.finishTimestamp,
26-
datetime: moment.unix(Math.floor(offer.finishTimestamp/1000)).format('ddd D-M-YYYY HH:mm'),
27-
value: offer.value,
28-
prices: offer.prices,
29-
accepted: offer.handledByUs === true && offer.isAccepted === true
30-
};
28+
// check for undefined time, sort those at the end
29+
if ( (!a || isNaN(a)) && !(!b || isNaN(b))) return 1;
30+
if ( !(!a || isNaN(a)) && (!b || isNaN(b))) return -1;
31+
if ( (!a || isNaN(a)) && (!b || isNaN(b))) return 0;
3132

32-
if (typeof polldata.sent[key] != 'undefined') {
33-
ret.lastState = data.ETradeOfferState[polldata.sent[key]];
34-
} else if (typeof polldata.received[key] != 'undefined') {
35-
ret.lastState = data.ETradeOfferState[polldata.received[key]];
36-
}
37-
if (Object.prototype.hasOwnProperty.call(offer, 'dict')) {
38-
if (Object.keys(offer.dict.our).length > 0) {
39-
Object.keys(offer.dict.our).forEach((k)=>{
40-
ret.items.our.push(createTradeItem(k, offer.dict.our[k]));
41-
});
33+
if (descending) {
34+
b = [a, a = b][0];
35+
}
36+
37+
return a - b;
38+
});
39+
if (count != -1) tradeList = tradeList.slice(first, first + count);
40+
const items = {};
41+
const trades = tradeList.map((offer)=>{
42+
const ret = {
43+
id: offer.id,
44+
items: {
45+
our: [],
46+
their: []
47+
},
48+
profit: Object.prototype.hasOwnProperty.call(profitData, offer.id)?profitData[offer.id]: '',
49+
partner: offer.partner,
50+
accepted: offer.accepted,
51+
time: offer.finishTimestamp,
52+
datetime: moment.unix(Math.floor(offer.finishTimestamp/1000)).format('ddd D-M-YYYY HH:mm'),
53+
value: offer.value,
54+
accepted: offer.handledByUs === true && offer.isAccepted === true
55+
};
56+
if (typeof polldata.sent[offer.id] != 'undefined') {
57+
ret.lastState = data.ETradeOfferState[polldata.sent[offer.id]];
58+
} else if (typeof polldata.received[offer.id] != 'undefined') {
59+
ret.lastState = data.ETradeOfferState[polldata.received[offer.id]];
60+
}
61+
if (Object.prototype.hasOwnProperty.call(offer, 'dict')) {
62+
if (Object.keys(offer.dict.our).length > 0) {
63+
Object.keys(offer.dict.our).forEach((k)=>{
64+
if (!Object.prototype.hasOwnProperty.call(items, k)) {
65+
items[k] = createTradeItem(k);
4266
}
43-
if (Object.keys(offer.dict.their).length > 0) {
44-
Object.keys(offer.dict.their).forEach((k)=>{
45-
ret.items.their.push(createTradeItem(k, offer.dict.their[k]));
46-
});
67+
ret.items.our.push({
68+
sku: k,
69+
amount: offer.dict.our[k]
70+
});
71+
});
72+
}
73+
if (Object.keys(offer.dict.their).length > 0) {
74+
Object.keys(offer.dict.their).forEach((k)=>{
75+
if (!Object.prototype.hasOwnProperty.call(items, k)) {
76+
items[k] = createTradeItem(k);
4777
}
48-
}
49-
50-
return ret;
51-
});
52-
});
78+
ret.items.their.push({
79+
sku: k,
80+
amount: offer.dict.their[k]
81+
});
82+
});
83+
}
84+
}
85+
return ret;
86+
});
87+
return {
88+
trades,
89+
items,
90+
tradeCount
91+
};
5392
};
5493

5594
/**
5695
* Creates item object
5796
* @param {String} sku
58-
* @param {number} amount
5997
* @return {Object} item object created
6098
*/
61-
function createTradeItem(sku, amount) {
62-
const item = SKU.fromString(sku);
63-
item.sku = sku;
64-
item.name = getName(item.sku);
65-
item.style = getImage.getImageStyle(item.sku);
66-
item.amount = amount;
67-
return item;
99+
function createTradeItem(sku) {
100+
return {
101+
name: getName(sku),
102+
style: getImage.getImageStyle(sku)
103+
};
68104
}

assets/js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ const app = new Vue({
316316
case 2: // buy price
317317
return a.buy.total - b.buy.total;
318318
break;
319+
case 3: // buy price
320+
return a.intent - b.intent;
321+
break;
319322
}
320323
});
321324
}

assets/js/profit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const app = new Vue({
9999
metal: this.plotData.keyValue
100100
}).toValue(this.plotData.keyValue); // get value in scrap
101101
const metal = Currencies.toRefined(scrapVal % key);
102-
const keys = Math.floor(scrapVal / key);
102+
const keys = scrapVal>0 ? Math.floor(scrapVal / key) : Math.ceil(scrapVal / key);
103103
return new Currencies({keys, metal}).toString();
104104
}
105105
},

0 commit comments

Comments
 (0)