This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
173 lines (142 loc) · 4.39 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
const async = require('async');
const semver = require('semver');
const inherits = require('util').inherits;
const EventEmitter = require('events').EventEmitter;
const version = require('./package.json').version;
const Schema = require('./lib/schema.js');
class TF2 {
/**
* @param {Object} options
* @param {String} options.apiKey Steam API key
* @param {Number} options.updateTime
*/
constructor(options) {
EventEmitter.call(this);
this.apiKey = options.apiKey;
this.updateTime = options.updateTime || 24 * 60 * 60 * 1000;
this.ready = false;
this.schema = null;
}
/**
* @param {String} apiKey Steam API key
*/
setAPIKey(apiKey) {
this.apiKey = apiKey;
}
/**
* Initializes the class
* @param {Function} callback
*/
init(callback) {
if (this.ready) {
callback(null);
return;
}
if (this.schema !== null && this._updateWait() !== 0) {
this._startUpdater();
this.ready = true;
this.emit('ready');
callback(null);
return;
}
this.getSchema(err => {
if (err) {
return callback(err);
}
this._startUpdater();
this.ready = true;
this.emit('ready');
return callback(null);
});
}
/**
* Sets the schema using known data. If the schema data does not contain a version, or the version does not satify our version, then the schema will be ignored
* @param {Object} data Schema data
* @param {Boolean} fromUpdate If the schema is new or not
*/
setSchema(data, fromUpdate) {
// Ignore the schema if it does not contain a version, or if the schema has a higher version (major)
if ((!data.version && !fromUpdate) || semver.major(data.version) > semver.major(version)) {
return;
}
if (this.schema !== null) {
this.schema.raw = data.raw;
this.schema.time = data.time || new Date().getTime();
} else {
this.schema = new Schema(data);
}
if (fromUpdate) {
this.emit('schema', this.schema);
}
}
/**
* Gets the schema from the TF2 API
* @param {Function} callback
*/
getSchema(callback) {
if (this.apiKey === undefined) {
throw new Error('Missing API key');
}
async.parallel(
{
overview: callback => {
Schema.getOverview(this.apiKey, callback);
},
items: callback => {
Schema.getItems(this.apiKey, callback);
},
paintkits: function (callback) {
Schema.getPaintKits(callback);
},
items_game: function (callback) {
Schema.getItemsGame(callback);
}
},
(err, result) => {
if (err) {
return callback(err);
}
const raw = {
schema: Object.assign(result.overview, { items: result.items, paintkits: result.paintkits }),
items_game: result.items_game
};
this.setSchema({ version: version, raw: raw }, true);
callback(null, this.schema);
}
);
}
/**
* Starts schema updater
*/
_startUpdater() {
if (this.updateTime === -1) {
return;
}
clearTimeout(this._updateTimeout);
clearInterval(this._updateInterval);
this._updateTimeout = setTimeout(() => {
// Update the schema
this.getSchema(err => {
this.emit('failed', err);
});
// Set update interval
this._updateInterval = setInterval(
TF2.prototype.getSchema.bind(this, function () {}),
this.updateTime
);
}, this._updateWait());
}
_updateWait() {
if (this.updateTime === -1) {
return -1;
}
let wait = this.schema.time - new Date().getTime() + this.updateTime;
if (wait < 0) {
wait = 0;
}
return wait;
}
}
inherits(TF2, EventEmitter);
module.exports = TF2;
module.exports.Schema = Schema;