-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dota2Bot.js
255 lines (220 loc) · 8.49 KB
/
Dota2Bot.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
var steam = require("steam"),
util = require("util"),
fs = require("fs"),
dota2 = require("dota2"),
steamClient = new steam.SteamClient(),
steamUser = new steam.SteamUser(steamClient),
steamFriends = new steam.SteamFriends(steamClient),
dota2 = require("dota2"),
Dota2 = new dota2.Dota2Client(steamClient, true),
randomstring = require("randomstring"),
utility = require("./utility.js");
var dotaReady = false;
var steamReady = false;
// this is the object we are going to give back to scheduler
// if our match actually starts
var match = {};
var logOnDetails = JSON.parse(process.argv[2]),
sentryNum = process.argv[3],
botname = process.argv[4],
mode = process.argv[5],
commands_dota = JSON.parse(process.argv[6]),
commandChar = process.argv[7],
region = process.argv[8],
name = process.argv[9],
lobby = JSON.parse(process.argv[10]);
util.log("Mode-after: " + mode);
/*util.log("logOnDetails =" + logOnDetails);
util.log("sentryNum =" + sentryNum);
util.log("botname =" + botname);
util.log("commands_dota =" + commands_dota);
util.log("commandChar =" + commandChar);
util.log("region =" + region);
util.log("name =" + name);
util.log("lobby =" + lobby);*/
// constructor, just in case we need an instance
/*function Dota2Bot(logOnDetails, sentryNum, botname, commands_dota, commandChar, region) {
this.logOnDetails = logOnDetails;
this.botname = botname;
this.commands_dota = commands_dota;
this.commandChar = commandChar;
this.region = region;
this.sentryNum = sentryNum;
}
/*Dota2Bot.prototype.logOn = function logOn() {
bot.logOn(logOnDetails);
}
Dota2Bot.prototype.hostGame = function hostGame() {
}
// Handles all IPC with the scheduler
/*process.on("message", function(data){
if (data.msg === "done") {
}
});*/
// Handles all of the steam communication
var onSteamLogOn = function onSteamLogOn(logonResp) {
if (logonResp.eresult == steam.EResult.OK) {
steamReady = true;
steamFriends.setPersonaState(steam.EPersonaState.Online); // to display your bot"s status as "Online"
steamFriends.setPersonaName(botname); // to change its nickname
util.log("Logged on.");
Dota2.launch();
Dota2.on("ready", function() {
dotaReady = true;
Dota2.leavePracticeLobby(); // just incase we have a lingering lobby open from a network DC or something
Dota2.leavePracticeLobby(); // just incase we have a lingering lobby open from a network DC or something
Dota2.leavePracticeLobby(); // just incase we have a lingering lobby open from a network DC or something
Dota2.leavePracticeLobby(); // just incase we have a lingering lobby open from a network DC or something
console.log("Node-dota2 ready.");
host(name, mode, region, lobby);
});
Dota2.on("unready", function onUnready() {
dotaReady = false;
console.log("Node-dota2 unready.");
});
Dota2.on("unhandled", function(kMsg) {
util.log("UNHANDLED MESSAGE " + kMsg);
});
}
},
onSteamLogOff = function onSteamLogOff(eresult) {
util.log("Logged off from Steam.");
},
onSteamError = function onSteamError(error) {
util.log("Connection closed by server.");
};
// Dota 2 event handlers
var onPracticeLobbyUpdate = function onPracticeLobbyUpdate(practiceLobbyUpdate, lobby) {
var lobbyId = utility.bigInt(lobby["lobbyId"]["high"], lobby["lobbyId"]["low"]);
if (lobby["gameName"] !== undefined) {
Dota2.joinChat("Lobby_" + lobbyId, 3); // 3 is a lobby
//Dota2.sendMessage("Lobby_" + lobbyId, "Waiting for lobby to fill up with the proper members. Will await confirmation from leader when full.");
}
util.log(lobby);
if (match["lobbyId"] !== undefined) {
match["lobbyId"] = lobbyId;
}
// If the lobby has a matchId, it means the lobby has been launched, which means we need the bot to leave the lobby and send
// the match Object to the scheduler. Before we do that, we have to make a note of what the Match ID is though, so we can
// see who won later on.
if (lobby["matchId"] !== null) {
if (match["matchId"] === undefined) {
match["matchId"] = lobby["matchId"];
}
}
util.log("Lobby " + lobby["gameName"] + " with ID " + lobbyId + " updated.");
},
onPracticeLobbyCreateResponse = function onPracticeLobbyCreateResponse(practiceLobbyCreateResponse, id) {
//Dota2.joinChat(practiceLobbyCreateResponse.channelName, 3);
},
onChatMessage = function onChatMessage(channelName, personaName, text, chatData) {
//util.log(JSON.stringify(chatData));
util.log(personaName + "(" + chatData["accountId"] + ") in channel: " + utility.bigInt(chatData["channelId"]["high"], chatData["channelId"]["low"]) + " said: " + text);
if (text.lastIndexOf(commandChar, 0) === 0) { // checks if string starts with ., which means command
for (var command of Object.keys(commands_dota)) {
if (text.lastIndexOf(command, 0) === 0) { // compare the entire list of commands to the message.
if (match["leaderId"] === utility.steam3To64(chatData["accountId"])) {
handles[command.substr(1)](text.split(command)[1], lobby["leaderId"]);
}
}
}
}
};
var host = function host(name, mode, region, lobby) {
Dota2.leavePracticeLobby();
Dota2.leavePracticeLobby();
util.log("inside host func, lobby.password: " + lobby.password);
util.log("name: " + name);
util.log("mode: " + mode);
util.log("region: " + region)
/*Available regions
UNSPECIFIED: 0,
USWEST: 1,
USEAST: 2,
EUROPE: 3,
KOREA: 4,
SINGAPORE: 5,
EUWEST: 6,
AUSTRALIA: 7,
STOCKHOLM: 8,
AUSTRIA: 9,
BRAZIL: 10,
SOUTHAFRICA: 11,
PERFECTWORLDTELECOM: 12,
PERFECTWORLDUNICOM: 13,
CHILE: 14,
PERU: 15
*/
/*var servers = {
"USW": 1,
"USE": 2,
"EUE": 3,
"EUW": 6,
"RUS": 8,
"PW": 12,
"KR": 4,
"SEA": 5,
"AUS": 9,
"PER": 15,
"BRA": 10,
"CHI": 14,
"SA": 11
};*/
//var mode = 2;
// This is where we actually do the logic for hosting the lobby
Dota2.createPracticeLobby(name, lobby.password, parseInt(mode), parseInt(region), function(success, response) {
if (success == 1) {
util.log("Lobby successfully created.");
} else { // find out why we are not getting a 1 response (no idea why this would happen)
Dota2.practiceLobbyListRequest(function(isNull, response) {
util.log(response["lobbies"]);
});
}
//util.log(success + " " + response["lobby_details"]);
});
match = {
"name": name,
"password": lobby.password,
"leaderId": lobby.leaderId,
"members": [lobby.leaderId]
};
};
var sendMatch = function sendMatch() {
process.send({
sentry: sentryNum,
match: match
});
}
// handlers for the in-game lobby chat commands
var handles = {};
handles.start = function start(message, sender) {
Dota2.launchPracticeLobby(function() {});
sendMatch();
};
handles.stop = function stop(message, sender) {
};
// When Ctrl+C is registered, run this before termination of the process
process.on("exit", function(code) {
console.log("\n");
util.log("Exiting gracefully.");
Dota2.leavePracticeLobby();
});
steamClient.connect();
steamClient.on("connected", function() {
steamUser.logOn(logOnDetails);
});
steamUser.on('updateMachineAuth', function(sentry, callback) {
fs.writeFileSync('sentry_' + sentryNum, sentry.bytes)
util.log("sentryfile saved");
callback({
sha_file: crypto.createHash('sha1').update(sentry.bytes).digest()
});
});
// binding listeners
steamClient.on('logOnResponse', onSteamLogOn)
.on('loggedOff', onSteamLogOff)
.on('error', onSteamError)
.on('servers', onSteamServers);
Dota2.on("practiceLobbyUpdate", onPracticeLobbyUpdate)
.on("practiceLobbyCreateResponse", onPracticeLobbyCreateResponse)
.on("chatMessage", onChatMessage);