-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.js
256 lines (220 loc) · 9.26 KB
/
scheduler.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
var steam = require("steam"),
util = require("util"),
fs = require("fs"),
childProcess = require("child_process"),
steamClient = new steam.SteamClient(),
steamUser = new steam.SteamUser(steamClient),
steamFriends = new steam.SteamFriends(steamClient),
config = require("./app.js"),
utility = require("./utility.js"),
botname = config.botname,
logOnDetails = config.logOnDetails,
commands = config.commands,
commands_dota = config.commands_dota,
commandChar = config.commandChar,
dota_bots = config.dota_bots,
chatID = config.chatID,
region = config.region,
randomstring = require("randomstring");
var isReady = true; // just in case you want to check if steam is ready for some reason
var introMsg = false;
var lobbies = []; // manage all lobbies from the steam group, remove lobbies when the game is sent back as started
var matches = []; // add games when they are sent back by the bots, remove them when stats are checked
var bots = {}; // Add bots when they are spawned to this object using the botname as the identifier
// count how many bots we can have maximally
var maxBots = Object.keys(dota_bots).length;
// the handler that all IPC messages should be bound to
var messageHandler = function messageHandler(data) {
var sentry = data.sentry;
var match = data.match;
if (data.match !== undefined) {
matches.push(match);
dotaBots[sentry].inuse = "false";
bots[sentry].kill();
} else {
dotaBots[sentry].inuse = "false";
bots[sentry].kill();
}
console.log(JSON.stringify(match));
};
var onSteamLogOn = function onSteamLogOn(logonResp) {
if (logonResp.eresult == steam.EResult.OK) {
initBots();
steamFriends.setPersonaState(steam.EPersonaState.Online); // to display your bot"s status as "Online"
steamFriends.setPersonaName(botname); // to change its nickname
steamFriends.joinChat(chatID); // ID for the group chat
util.log("Logged on.");
}
},
onSteamSentry = function onSteamSentry(sentry) {
util.log("Received sentry.");
require("fs").writeFileSync("sentry", sentry);
},
onSteamServers = function onSteamServers(servers) {
util.log("Received servers.");
fs.writeFile("servers", JSON.stringify(servers));
},
onSteamLogOff = function onSteamLogOff(eresult) {
util.log("Logged off from Steam.");
},
onSteamError = function onSteamError(error) {
util.log("Connection closed by server.");
},
onWebSessionID = function onWebSessionID(webSessionID) {
util.log("Received web session id.");
// steamTrade.sessionID = webSessionID;
steamFriends.webLogOn(function onWebLogonSetTradeCookies(cookies) {
util.log("Received cookies.");
for (var i = 0; i < cookies.length; i++) {
// steamTrade.setCookie(cookies[i]);
}
});
},
onChatEnter = function onChatEnter(id, response) {
util.log("Entered chat with ID: " + id + " with response: " + response);
},
onChatMsg = function onChatMsg(chatID, message, type, sender) {
util.log("Recieved message: " + message + " from: " + sender + " in chatroom: " + chatID);
if (message.lastIndexOf(config.commandChar, 0) === 0) { // checks if string starts with ., which means command
for (var command of Object.keys(commands)) {
if (message.lastIndexOf(command, 0) === 0) { // compare the entire list of commands to the message
if (steamFriends.chatRooms[chatID][sender].permissions & commands[command]) { // kick permission is all you need to use commands
handles[command.substr(1)](message.split(command)[1], sender);
}
}
}
}
//util.log(bot.chatRooms);
},
onChatStateChange = function onChatStateChange(stateChange, doeeID, chatID, doerID) {
util.log(doeeID + " has been " + stateChange + " by " + doerID + " in group: " + chatID);
};
var handles = {};
handles.test = function(message, sender) {
util.log("Test command. The rest of the message was: " + message);
steamFriends.sendMessage(chatID, "Test command. The rest of the message was: " + message);
};
handles.host = function(message, sender) {
// check if there is even a bot available to make a lobby
var b = getNextBot();
if (b !== "") {
//util.log("Host command. The rest of the message was: " + message);
var name = "MyLeague_" + randomstring.generate(3); // format should be " name region (garbage text)"
var password = randomstring.generate(6);
steamFriends.sendMessage(chatID, "Hosting Lobby with name: " + name);
steamFriends.sendMessage(sender, name + " password is: " + password);
lobbies[name] = {
"password": password,
"leaderId": sender,
"members": [sender]
};
// This is where we actually do the logic for hosting the lobby
// Dota2Bot(logOnDetails, sentryNum, botname, commands_dota, commandChar, region, name, lobby)
var args = ["./Dota2Bot.js", b.logOnDetails, b.sentryNum, b.botname, commands_dota, commandChar, region, name, lobbies[name]];
bots[b.sentryNum] = childProcess.spawn(process.execPath, args, {
stdio: [null, null, null, "ipc"]
});
bots[b.sentryNum].on("message", messageHandler);
b.inuse = true;
} else {
bot.sendMessage(sender, "All bots are currently occupied, please try again later.")
}
};
var newGame = function newGame(name, password, mode, region, leader) {
var b = getNextBot();
if (b !== "") {
//util.log("Host command. The rest of the message was: " + message);
var name = name || "MyLeague_" + randomstring.generate(3); // format should be " name region (garbage text)"
var password = password || randomstring.generate(6);
//bot.sendMessage(chatID, "Hosting Lobby with name: " + name);
//bot.sendMessage(sender, name + " password is: " + password);
lobbies[name] = {
"password": password,
"leaderId": leader,
"members": [leader]
};
// This is where we actually do the logic for hosting the lobby
// Dota2Bot(logOnDetails, sentryNum, botname, commands_dota, commandChar, region, name, lobby)
util.log("logOnDetails pre-spawn: " + JSON.stringify(b.logOnDetails));
util.log("mode pre-spawm: " + JSON.stringify(mode));
/*util.log("sentryNum " + b.sentryNum)
util.log("botname " + b.botname)
util.log("commands_dota " + commands_dota)
util.log("commandChar " + commandChar)
util.log("region " + region)
util.log("name " + name)
util.log("lobbies " + lobbies[name])*/
var args = ["./Dota2Bot.js", JSON.stringify(b.logOnDetails), b.sentryNum, b.botname, mode, JSON.stringify(commands_dota), commandChar, region, name, JSON.stringify(lobbies[name])];
bots[b.sentryNum] = childProcess.spawn(process.execPath, args, {
stdio: [process.stdin, process.stdout, process.stderr, "ipc"]
});
bots[b.sentryNum].on("message", messageHandler);
b.inuse = true;
return true;
} else {
util.log("All bots are currently occupied, please try again later.")
return -1;
}
}
handles.join = function(message, sender) {
for (var lobby of lobbies) {
if (message.split(" ")[0] === lobby) {
for (var member of lobby["members"]) {
if (member !== sender) {
lobby["members"].push(sender);
steamFriends.sendMessage(sender, "Password for " + lobby + " is " + lobby["password"]);
}
}
} else {
steamFriends.sendMessage(sender, "Lobby not found.");
}
}
};
// init steam instances for bots
// Dota2Bot(logOnDetails, sentryNum, botname, commands_dota, commandChar, region)
var dotaBots = {};
var initBots = function initBots() {
for (var i = 0; i < maxBots; i++) {
dotaBots[i] = {
inuse: "false",
logOnDetails: dota_bots["" + i + ""],
botname: "IHB_" + i,
sentryNum: i
};
}
};
var getNextBot = function getNextBot() {
var b;
for (var i = 0; i < maxBots; i++) {
if (dotaBots[i]["inuse"] === "false") {
b = dotaBots[i];
} else {
b = "";
}
}
return b;
};
process.on("exit", function(code) {
console.log("\n");
util.log("Exiting gracefully.");
});
steamClient.connect();
steamClient.on("connected", function() {
steamUser.logOn(logOnDetails);
});
steamUser.on('updateMachineAuth', function(sentry, callback) {
fs.writeFileSync('sentry', sentry.bytes)
util.log("sentryfile saved");
callback({
sha_file: crypto.createHash('sha1').update(sentry.bytes).digest()
});
});
steamClient.on('logOnResponse', onSteamLogOn)
.on('loggedOff', onSteamLogOff)
.on('error', onSteamError)
.on('servers', onSteamServers);
steamFriends.on("webSessionID", onWebSessionID)
.on('chatEnter', onChatEnter)
.on('chatMsg', onChatMsg)
.on('chatStateChange', onChatStateChange);
module.exports.newGame = newGame;