forked from Arcana/node-dota2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
228 lines (190 loc) · 7.91 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
var steam = require("steam");
const DOTA_APP_ID = 570;
var EventEmitter = require('events').EventEmitter,
fs = require("fs"),
path = require("path"),
util = require("util"),
bignumber = require("bignumber.js"),
ProtoBuf = require('protobufjs'),
Dota2 = exports;
var builder = ProtoBuf.newBuilder();
var folder = fs.readdirSync(__dirname + '/proto');
folder.forEach(function(f) {
ProtoBuf.loadProtoFile(__dirname + '/proto/' + f, builder);
});
Dota2.schema = builder.build();
var Dota2Client = function Dota2Client(steamClient, debug, debugMore) {
EventEmitter.call(this);
this.debug = debug || false;
this.debugMore = debugMore || false;
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.chatChannels = []; // Map channel names to channel data.
this._gcReady = false;
this._gcClientHelloIntervalId = null;
this._gcConnectionStatus = Dota2.schema.GCConnectionStatus.GCConnectionStatus_NO_SESSION;
// This should probably be reworked to use a CMsgProtoBufHeader object
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;
if (self.debugMore) util.log("Dota2 fromGC: " + kMsg); // TODO: Turn type-protoMask into key name.
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);
}
});
this._sendClientHello = function() {
if (self._gcReady) {
if (self._gcClientHelloIntervalId) {
clearInterval(self._gcClientHelloIntervalId);
self._gcClientHelloIntervalId = null;
}
return;
}
if (self._gcClientHelloCount > 10) {
if (self.debug) util.log("ClientHello has taken longer than 30 seconds! Reporting timeout...");
self._gcClientHelloCount = 0;
self.emit("hellotimeout");
}
if (self.debug) util.log("Sending ClientHello");
if (!self._gc) {
util.log("Where the fuck is _gc?");
} else {
self._protoBufHeader.msg = Dota2.schema.EGCBaseClientMsg.k_EMsgGCClientHello;
var payload = new Dota2.schema.CMsgClientHello({});
payload.engine = 1;
payload.secret_key = "";
payload.client_session_need = 104;
self._gc.send(
self._protoBufHeader,
payload.toBuffer()
);
}
self._gcClientHelloCount++;
};
};
util.inherits(Dota2Client, EventEmitter);
// Expose enums
Dota2Client.prototype.ServerRegion = Dota2.ServerRegion;
Dota2Client.prototype.ToAccountID = function(accid) {
return new bignumber(accid).minus('76561197960265728') - 0;
};
Dota2Client.prototype.ToSteamID = function(accid) {
return new bignumber(accid).plus('76561197960265728') + "";
};
// Methods
Dota2Client.prototype.launch = function() {
/* Reports to Steam that we are running Dota 2. Initiates communication with GC with EMsgGCClientHello */
if (this.debug) util.log("Launching Dota 2");
this.AccountID = this.ToAccountID(this._client.steamID);
this.Party = null;
this.Lobby = null;
this.PartyInvite = null;
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);
};
Dota2Client.prototype.exit = function() {
/* Reports to Steam we are not running any apps. */
if (this.debug) util.log("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([]);
};
Dota2Client.prototype.sendToGC = function(type, payload, handler, callback) {
var self = this;
if (!this._gcReady) {
if (this.debug) util.log("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
);
}
// Handlers
var handlers = 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;
}
if (this.debug) util.log("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:
if (this.debug) util.log("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:
if (this.debug) util.log("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;
}
};
Dota2.Dota2Client = Dota2Client;
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");