-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (240 loc) · 7.32 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const axios = require('axios');
const EventEmitter = require('events').EventEmitter;
const SteamUser = require('steam-user');
const GlobalOffensive = require('globaloffensive');
/**
* Contains the unique id and current quantity of the storage unit
*/
class StorageUnit {
constructor(id, currentQuantity) {
this.id = id;
this.currentQuantity = currentQuantity;
}
}
class StorageHelper extends EventEmitter {
constructor() {
super();
this._isLoggedIn = false;
this._inventory = {};
this._user = new SteamUser();
this._csgo = new GlobalOffensive(this._user);
this._registerErrorHandling();
this._registerItemCustomizationNotificationHandling();
}
/**
* Login to Steam and connect to GC
* @param {string} username
* @param {string} password
*/
login = (username, password) => {
const that = this;
this._user.on('loggedOn', function () {
console.log('Logged into Steam');
that._isLoggedIn = true;
that._user.gamesPlayed([730]);
});
this._csgo.on('connectedToGC', function () {
that.emit('ready');
});
this._user.logOn({
'accountName': username,
'password': password
});
};
/**
* Async function to add items of one type to a storage unit
* @param {string} storageUnitName Name of the storage unit, defined by the user
* @param {string} itemName Full name of the item which will be added to the storage unit
* @param {number} max Amount of items to add, default will be used if this number exceeds the available space
*/
addItemsToStorageUnit = async (storageUnitName, itemName, max = -1) => {
const storageUnitMaxCapacity = 1000;
if(!this._isReady()){
return;
}
const steamId = this._user.steamID.getSteamID64();
await this._fetchInventory(steamId);
if (this._inventory) {
const storageUnit = this._getStorageUnitByName(storageUnitName);
if (!storageUnit) {
return;
}
const assetIds = this._getAssetidsForName(itemName);
let maxItems = storageUnitMaxCapacity - storageUnit.currentQuantity;
if (assetIds.length === 0) {
return console.log(`No items found for given name ${itemName}`);
}
if (assetIds.length < maxItems) {
maxItems = assetIds.length;
}
if (max > 0 && max < maxItems) {
maxItems = max;
}
console.log(`Adding ${maxItems} ${itemName} to ${storageUnitName}`);
for (let i = 0; i < maxItems; i++) {
this._csgo.addToCasket(storageUnit.id, assetIds[i]);
await this._sleep();
}
}
else {
console.log('Failed to fetch inventory');
}
};
/**
* Async function to retrieve items of one type from a storage unit
* @param {string} storageUnitName Name of the storage unit, defined by the user
* @param {number} max Amount of items to retrieve, default will be used if this number exceeds the amount of items in storage unit
*/
getItemsFromStorageUnit = async (storageUnitName, max = -1) => {
if(!this._isReady()){
return;
}
const steamId = this._user.steamID.getSteamID64();
await this._fetchInventory(steamId);
if (this._inventory) {
const storageUnit = this._getStorageUnitByName(storageUnitName);
if (!storageUnit) {
return;
}
const assetIds = this._getAssetidsFromStorageUnit(storageUnit.id);
let maxItems = assetIds.length;
if (assetIds.length === 0) {
return console.log(`No items found in storage unit`);
}
if (max > 0 && max < maxItems) {
maxItems = max;
}
console.log(`Withdrawing ${maxItems} items from ${storageUnitName}`);
for (let i = 0; i < maxItems; i++) {
this._csgo.removeFromCasket(storageUnit.id, assetIds[i]);
await this._sleep();
}
}
};
/**
* Fetches the user's inventory from Steam
* @param {string} steamId
* @private
*/
_fetchInventory = async (steamId) => {
const url = `https://steamcommunity.com/inventory/${steamId}/730/2?l=english&count=1000`;
try {
const response = await axios.get(url);
if (response.data && response.data.assets) {
this._inventory = response.data;
}
} catch (error) {
console.log(error);
}
};
/**
* Returns storage unit information from the user's inventory for the given name
* @param {string} name
* @private
*/
_getStorageUnitByName = (name) => {
const inventory = this._inventory;
const descriptions = inventory.descriptions;
const assets = inventory.assets;
const storageUnits = descriptions.filter(item => item.name === 'Storage Unit');
if (storageUnits && storageUnits.length > 0) {
const nameTagTemplate = `Name Tag: ''${name}''`;
const storageUnit = storageUnits.find(item => item.fraudwarnings[0] === nameTagTemplate);
if (storageUnit) {
const classId = storageUnit.classid;
const storageUnitAsset = assets.find(asset => asset.classid === classId);
let currentQuantity = storageUnit.descriptions[2];
currentQuantity = currentQuantity.value.replace('Number of Items: ', '');
return new StorageUnit(parseInt(storageUnitAsset.assetid), parseInt(currentQuantity));
}
}
else {
console.log(`No storage unit found with name ${name}`);
}
};
/**
* Returns an array with assetids for all items which match the given name
* @param {string} name
* @private
*/
_getAssetidsForName = (name) => {
const inventory = this._inventory;
const assets = inventory.assets;
const descriptions = inventory.descriptions;
let item = descriptions.find(item => item.name === name);
if (item) {
const classId = item.classid;
return assets.filter(asset => asset.classid === classId && !asset.hasOwnProperty('casket_id')).map(function (obj) {
return parseInt(obj.assetid);
});
}
return [];
};
/**
* Returns an array with assetids for all items in a storage unit
* @param {Number} storageUnitId
* @private
*/
_getAssetidsFromStorageUnit = (storageUnitId) => {
let inventory = [];
this._csgo.getCasketContents(storageUnitId, (err, items) => {
if (err) {
console.log(err.message);
return [];
}
else {
inventory = items;
}
});
return inventory.map(function (obj) {
return parseInt(obj.id);
});
};
/**
* Logs errors returned from the SteamUser object
* @private
*/
_registerErrorHandling = () => {
this._user.on('error', function (e) {
console.log(e);
});
};
/**
* Deals with events related to storage units
* @private
*/
_registerItemCustomizationNotificationHandling = () => {
this._csgo.on('itemCustomizationNotification', (itemIds, notificationType) => {
if (notificationType === GlobalOffensive.ItemCustomizationNotification.CasketInvFull) {
console.log(`Storage unit ${itemIds[0]} is full`);
}
if (notificationType === GlobalOffensive.ItemCustomizationNotification.CasketAdded) {
console.log('Item added to Storage unit');
}
if (notificationType === GlobalOffensive.ItemCustomizationNotification.CasketRemoved) {
console.log('Item removed from Storage unit');
}
});
};
/**
* Returns Promise which is resolved after the timeout defined in ms has passed
* @private
*/
_sleep = () => {
return new Promise(resolve => setTimeout(resolve, 500));
};
/**
* Returns boolean which indicates if the user is logged in and has a connection to GC
* @private
*/
_isReady = () =>{
if (!this._isLoggedIn) {
console.log('Not logged in');
}
else if (!this._csgo.haveGCSession) {
console.log('Not connected to GC');
}
return this._isLoggedIn && this._csgo.haveGCSession;
};
}
module.exports.StorageHelper = StorageHelper;