This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
index.js
378 lines (335 loc) · 14.2 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Dota 2 module
* @module Dota2
*/
/**
* A Long class for representing a 64 bit two's-complement integer value
* derived from the Closure Library for stand-alone use and extended with unsigned support.
* @external Long
* @see {@link https://www.npmjs.com/package/long|long} npm package
*/
/**
* The Steam for Node JS package, allowing interaction with Steam.
* @external steam
* @see {@link https://www.npmjs.com/package/steam|steam} npm package
*/
const util = require("util");
const Long = require("long");
const steam = require("steam");
const steam_resources = require("steam-resources");
const { createLogger, format, transports } = require('winston');
const { EventEmitter } = require('events');
const DOTA_APP_ID = 570;
var Dota2 = exports;
/**
* Protobuf schema created by Steam Resources. This is an alias of `steam.GC.Dota.Internal`.
* This object can be used to obtain Dota2 specific protobuf types.
* Object types can be created by `new Dota2.schema.<TypeName>(payload :Object);`.
* Enum types can be referenced by `Dota2.schema.<EnumName>`, which returns an object array representing the enum.
* @alias module:Dota2.schema
*/
Dota2.schema = steam_resources.GC.Dota.Internal;
/**
* The Dota 2 client that communicates with the GC
* @class
* @alias module:Dota2.Dota2Client
* @param {Object} steamClient - Node-steam client instance
* @param {boolean} debug - Print debug information to console
* @param {boolean} debugMore - Print even more debug information to console
* @extends {EventEmitter} EventEmitter
* @fires module:Dota2.Dota2Client#event:ready
* @fires module:Dota2.Dota2Client#event:unhandled
* @fires module:Dota2.Dota2Client#event:hellotimeout
* @fires module:Dota2.Dota2Client#event:popup
* @fires module:Dota2.Dota2Client#event:sourceTVGamesData
* @fires module:Dota2.Dota2Client#event:inventoryUpdate
* @fires module:Dota2.Dota2Client#event:practiceLobbyUpdate
* @fires module:Dota2.Dota2Client#event:practiceLobbyCleared
* @fires module:Dota2.Dota2Client#event:lobbyInviteUpdate
* @fires module:Dota2.Dota2Client#event:lobbyInviteCleared
* @fires module:Dota2.Dota2Client#event:practiceLobbyJoinResponse
* @fires module:Dota2.Dota2Client#event:practiceLobbyListData
* @fires module:Dota2.Dota2Client#event:practiceLobbyResponse
* @fires module:Dota2.Dota2Client#event:lobbyDestroyed
* @fires module:Dota2.Dota2Client#event:friendPracticeLobbyListData
* @fires module:Dota2.Dota2Client#event:inviteCreated
* @fires module:Dota2.Dota2Client#event:partyUpdate
* @fires module:Dota2.Dota2Client#event:partyCleared
* @fires module:Dota2.Dota2Client#event:partyInviteUpdate
* @fires module:Dota2.Dota2Client#event:partyInviteCleared
* @fires module:Dota2.Dota2Client#event:joinableCustomGameModes
* @fires module:Dota2.Dota2Client#event:chatChannelsData
* @fires module:Dota2.Dota2Client#event:chatJoin
* @fires module:Dota2.Dota2Client#event:chatJoined
* @fires module:Dota2.Dota2Client#event:chatLeave
* @fires module:Dota2.Dota2Client#event:chatMessage
* @fires module:Dota2.Dota2Client#event:profileCardData
* @fires module:Dota2.Dota2Client#event:playerMatchHistoryData
* @fires module:Dota2.Dota2Client#event:playerInfoData
* @fires module:Dota2.Dota2Client#event:playerStatsData
* @fires module:Dota2.Dota2Client#event:trophyListData
* @fires module:Dota2.Dota2Client#event:hallOfFameData
* @fires module:Dota2.Dota2Client#event:playerCardRoster
* @fires module:Dota2.Dota2Client#event:playerCardDrafted
* @fires module:Dota2.Dota2Client#event:liveLeagueGamesUpdate
* @fires module:Dota2.Dota2Client#event:topLeagueMatchesData
* @fires module:Dota2.Dota2Client#event:teamData
* @fires module:Dota2.Dota2Client#event:matchesData
* @fires module:Dota2.Dota2Client#event:matchDetailsData
* @fires module:Dota2.Dota2Client#event:matchMinimalDetailsData
* @fires module:Dota2.Dota2Client#event:matchmakingStatsData
* @fires module:Dota2.Dota2Client#event:topFriendMatchesData
* @fires module:Dota2.Dota2Client#event:tipResponse
* @fires module:Dota2.Dota2Client#event:tipped
*/
Dota2.Dota2Client = function Dota2Client(steamClient, debug, debugMore) {
EventEmitter.call(this);
this.debug = debug || false;
this.debugMore = debugMore || false;
/**
* The logger used to write debug messages. This is a WinstonJS logger,
* feel free to configure it as you like
* @type {winston.Logger}
*/
this.Logger = createLogger({
transports: [new transports.Console()],
format: format.combine(
format.colorize(),
format.timestamp({
format: 'D MMM HH:mm:ss'
}),
format.printf(nfo => {
return `${nfo.timestamp} - ${nfo.message}`
})
)
});
if(debug) this.Logger.level = "debug";
if(debugMore) this.Logger.level = "silly";
/** The current state of the bot's inventory. Contains cosmetics, player cards, ...
* @type {CSOEconItem[]}
*/
this.Inventory = [];
/** The chat channels the bot has joined
* @type {CMsgDOTAJoinChatChannelResponse[]}
*/
this.chatChannels = []; // Map channel names to channel data.
/** The lobby the bot is currently in. Falsy if the bot isn't in a lobby.
* @type {CSODOTALobby}
*/
this.Lobby = null;
/** The currently active lobby invitation. Falsy if the bot has not been invited.
* @type {CSODOTALobbyInvite}
*/
this.LobbyInvite = null;
/** The party the bot is currently in. Falsy if the bot isn't in a party.
* @type {CSODOTAParty}
*/
this.Party = null;
/** The currently active party invitation. Falsy if the bot has not been invited.
* @type {CSODOTAPartyInvite}
*/
this.PartyInvite = null;
var steamUser = new steam.SteamUser(steamClient);
this._user = steamUser;
this._client = steamClient;
this._gc = new steam.SteamGameCoordinator(steamClient, DOTA_APP_ID);
this._appid = DOTA_APP_ID;
this._gcReady = false;
this._gcClientHelloIntervalId = null;
this._gcConnectionStatus = Dota2.schema.GCConnectionStatus.GCConnectionStatus_NO_SESSION;
// node-steam wants this as a simple object, so we can't use CMsgProtoBufHeader
this._protoBufHeader = {
"msg": "",
"proto": {
"client_steam_id": this._client.steamID,
"source_app_id": this._appid
}
};
var self = this;
this._gc.on("message", function fromGC(header, body, callback) {
/* Routes messages from Game Coordinator to their handlers. */
callback = callback || null;
var kMsg = header.msg;
self.Logger.silly("Dota2 fromGC: " + Dota2._getMessageName(kMsg));
if (kMsg in self._handlers) {
if (callback) {
self._handlers[kMsg].call(self, body, callback);
} else {
self._handlers[kMsg].call(self, body);
}
} else {
self.emit("unhandled", kMsg, Dota2._getMessageName(kMsg));
}
});
this._sendClientHello = function() {
if (self._gcReady) {
if (self._gcClientHelloIntervalId) {
clearInterval(self._gcClientHelloIntervalId);
self._gcClientHelloIntervalId = null;
}
return;
}
if (self._gcClientHelloCount > 10) {
self.Logger.warn("ClientHello has taken longer than 30 seconds! Reporting timeout...")
self._gcClientHelloCount = 0;
self.emit("hellotimeout");
}
self.Logger.debug("Sending ClientHello");
if (!self._gc) {
self.Logger.error("Where the fuck is _gc?");
} else {
self._gc.send(
{msg: Dota2.schema.EGCBaseClientMsg.k_EMsgGCClientHello, proto: {}},
new Dota2.schema.CMsgClientHello({}).toBuffer()
);
}
self._gcClientHelloCount++;
};
};
util.inherits(Dota2.Dota2Client, EventEmitter);
// Methods
/**
* Converts a 64bit Steam ID to a Dota2 account ID by deleting the 32 most significant bits
* @alias module:Dota2.Dota2Client.ToAccountID
* @param {string} steamID - String representation of a 64bit Steam ID
* @returns {number} Dota2 account ID corresponding with steamID
*/
Dota2.Dota2Client.prototype.ToAccountID = function(steamID) {
return new Long.fromString(""+steamID).sub('76561197960265728').toNumber();
};
/**
* Converts a Dota2 account ID to a 64bit Steam ID
* @alias module:Dota2.Dota2Client.ToSteamID
* @param {string} accid - String representation of a Dota 2 account ID
* @returns {external:Long} 64bit Steam ID corresponding to the given Dota 2 account ID
*/
Dota2.Dota2Client.prototype.ToSteamID = function(accid) {
return new Long.fromString(accid+"").add('76561197960265728');
};
/**
* Reports to Steam that you're playing Dota 2, and then initiates communication with the Game Coordinator.
* @alias module:Dota2.Dota2Client#launch
*/
Dota2.Dota2Client.prototype.launch = function() {
/* Reports to Steam that we are running Dota 2. Initiates communication with GC with EMsgGCClientHello */
this.Logger.debug("Launching Dota 2");
this.AccountID = this.ToAccountID(this._client.steamID);
this.Party = null;
this.Lobby = null;
this.PartyInvite = null;
this.Inventory = [];
this.chatChannels = [];
this._user.gamesPlayed([{
"game_id": this._appid
}]);
// Keep knocking on the GCs door until it accepts us.
// This is very bad practice and quite trackable.
// The real client tends to send only one of these.
// Really we should just send one when the connection status is GC online
this._gcClientHelloCount = 0;
this._gcClientHelloIntervalId = setInterval(this._sendClientHello, 6000);
//Also immediately send clienthello
setTimeout(this._sendClientHello, 1000);
};
/**
* Stop sending a heartbeat to the GC and report to steam you're no longer playing Dota 2
* @alias module:Dota2.Dota2Client#exit
*/
Dota2.Dota2Client.prototype.exit = function() {
/* Reports to Steam we are not running any apps. */
this.Logger.debug("Exiting Dota 2");
/* stop knocking if exit comes before ready event */
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
}
this._gcReady = false;
if (this._client.loggedOn) this._user.gamesPlayed([]);
};
Dota2.Dota2Client.prototype.sendToGC = function(type, payload, handler, callback) {
var self = this;
if (!this._gcReady) {
this.Logger.warn("GC not ready, please listen for the 'ready' event.");
if (callback) callback(-1, null); // notify user that something went wrong
return null;
}
this._protoBufHeader.msg = type;
this._gc.send(this._protoBufHeader, // protobuf header, same for all messages
payload.toBuffer(), // payload of the message
Dota2._convertCallback.call(self, handler, callback) // let handler treat callback so events are triggered
);
}
// Events
/**
* Emitted when the connection with the GC has been established
* and the client is ready to take requests.
* @event module:Dota2.Dota2Client#ready
*/
/**
* Emitted when the GC sends a message that isn't yet treated by the library.
* @event module:Dota2.Dota2Client#unhandled
* @param {number} kMsg - Proto message type ID
* @param {string} kMsg_name - Proto message type name
*/
/**
* Emitted when the connection with the GC takes longer than 30s
* @event module:Dota2.Dota2Client#hellotimeout
*/
// Handlers
var handlers = Dota2.Dota2Client.prototype._handlers = {};
handlers[Dota2.schema.EGCBaseClientMsg.k_EMsgGCClientWelcome] = function clientWelcomeHandler(message) {
/* Response to our k_EMsgGCClientHello, now we can execute other GC commands. */
// Only execute if _gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'ready');
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
}
this.Logger.debug("Received client welcome.");
// Parse any caches
this._gcReady = true;
this._handleWelcomeCaches(message);
this.emit("ready");
};
handlers[Dota2.schema.EGCBaseClientMsg.k_EMsgGCClientConnectionStatus] = function gcClientConnectionStatus(message) {
/* Catch and handle changes in connection status, cuz reasons u know. */
var status = Dota2.schema.CMsgConnectionStatus.decode(message).status;
if (status) this._gcConnectionStatus = status;
switch (status) {
case Dota2.schema.GCConnectionStatus.GCConnectionStatus_HAVE_SESSION:
this.Logger.debug("GC Connection Status regained.");
// Only execute if _gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'ready');
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
this._gcReady = true;
this.emit("ready");
}
break;
default:
this.Logger.debug(Object.keys(Dota2.schema.GCConnectionStatus).find(key => Dota2.schema.GCConnectionStatus[key] === status));
this.Logger.debug("GC Connection Status unreliable - " + status);
// Only execute if !_gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'unready');
if (!this._gcClientHelloIntervalId) {
this._gcClientHelloIntervalId = setInterval(this._sendClientHello, 5000); // Continually try regain GC session
this._gcReady = false;
this.emit("unready");
}
break;
}
};
require("./handlers/cache");
require("./handlers/inventory");
require("./handlers/chat");
require("./handlers/guild");
require("./handlers/community");
require("./handlers/helper");
require("./handlers/match");
require("./handlers/lobbies");
require("./handlers/parties");
require("./handlers/leagues");
require("./handlers/sourcetv");
require("./handlers/team");
require("./handlers/custom");
require("./handlers/general");
require("./handlers/fantasy");
require("./handlers/compendium");