forked from vankruptgames/PavlovVR-Rcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
235 lines (221 loc) · 7.32 KB
/
Copy pathinit.js
File metadata and controls
235 lines (221 loc) · 7.32 KB
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
const inquirer = require('inquirer');
const net = require('net')
const fs = require('fs');
var servers = JSON.parse(fs.readFileSync("./servers.json"));
var CMDS = [];
async function init() {
new Promise((res, rej) => {
return fs.readdir('./commands', (err, files) => err != null ? rej(err) : res(files))
}).then(async (res) => {
let files = res.filter(file => file.endsWith('.js'));
files.forEach((file) => {
let f = require(`./commands/${file}`)
if (f.config.enabled) {
console.log(`Loaded CMD: ${f.config.name} (${file})`);
CMDS.push({filename: file, run: f.run, view: f.config.view, name: f.config.name, params: f.config.params})
}
})
serverPrompt()
}).catch((res) => { console.log("Failed to load commands folder: " + res) })
}
init()
function serverPrompt() {
inquirer.prompt([{
type: 'list',
name: 'serverIp',
message: 'Select a server',
choices: servers.map(server => server.ip + ':' + server.port),
}, ]).then(async (selected) => {
console.info('Connecting to Server:', selected.serverIp);
if (servers.filter(serv => serv.ip === selected.serverIp.split(':')[0] && serv.port === selected.serverIp.split(':')[1])[0]) {
activeServer = servers.filter(serv => serv.ip === selected.serverIp.split(':')[0] && serv.port === selected.serverIp.split(':')[1])[0];
activeSocket = await spinServer(activeServer)
if (!activeSocket) {
console.log('Couldn\'t connect\n')
serverPrompt()
}
if (activeSocket) commandPrompt(activeSocket);
}
});
}
function commandHandler(socket, command, params) {
return new Promise(resolve => {
findCommand = CMDS.filter(cmd => cmd.view === command.split(' ')[0])[0]
if (findCommand) {
socket.write(command)
socket.once('data', function(data) {
return resolve(data.toString())
});
}
});
}
function runCmd(cmd, functions, options, socket) {
let c = CMDS.filter(c => c.view == cmd)[0]
if (!c) return commandPrompt(socket)
try {
c.run(functions, options, socket)
} catch (e) {
console.log("CMD Failed: " + e)
}
}
function commandPrompt(socket) {
inquirer.prompt([{
type: 'list',
name: 'command',
message: 'Commands:',
pageSize: CMDS.length <= 20 ? CMDS.length : 20,
choices: CMDS.map(cmd => cmd.view),
}, ]).then(async (selected) => {
runCmd(selected.command, FUNCTIONS, {cmd: selected.command}, socket)
});
}
function playerPrompt(socket, command, option, option2) {
// find a good delimiter
if (!socket.playerList.PlayerList) {
console.log('No players online :(')
return commandPrompt(socket);
}
inquirer.prompt([{
type: 'list',
name: 'player',
message: 'Select a Player',
choices: socket.playerList.PlayerList.map(player => player.Username + ' | ' + player.UniqueId),
}, ]).then(async (selected) => {
steam64Id = selected.player.split(' | ')[1]
switch (command) {
case 'kick':
await commandHandler(socket, 'Kick ' + steam64Id)
console.log(selected.player.Username + " Was Kicked")
break;
case 'kick':
await commandHandler(socket, 'Kill ' + steam64Id)
console.log(selected.player.Username + " Was Killed")
break;
case 'inspectplayer':
console.log(await commandHandler(socket, 'InspectPlayer ' + steam64Id))
break;
case 'switchteam':
await commandHandler(socket, `SwitchTeam ${steam64Id} ${option}`)
break;
case 'giveitem':
await commandHandler(socket, `GiveItem ${steam64Id} ${option}`)
break;
case 'setplayerskin':
await commandHandler(socket, `SetPlayerSkin ${steam64Id} ${option}`)
}
commandPrompt(socket)
});
}
function teamPrompt() {
return new Promise(resolve => {
inquirer.prompt([{
type: 'list',
name: 'team',
message: 'Select a Team',
choices: ["Blue Team (Defenders)", "Red Team (Attackers)"],
}, ]).then(async (selected) => {
resolve(selected.team)
});
});
}
function gmPrompt() {
return new Promise(resolve => {
inquirer.prompt([{
type: 'list',
name: 'gamemode',
message: 'Select a Gamemode',
choices: ["SND", "TDM", "DM", "GUN"],
}, ]).then(async (selected) => {
resolve(selected.team)
});
});
}
function textPrompt(type, goBack) {
return new Promise(resolve => {
inquirer.prompt([{
message: "",
type: "input",
name: "input",
}, ]).then(async (selected) => {
if (type === 'int' && selected.input.match(/^\d+$/)) {
resolve(selected.input)
} else if (type === 'steamId64' && selected.input.length === 17 && selected.input.match(/^\d+$/)) {
resolve(selected.input)
} else if (type === 'string') {
resolve(selected.input)
} else {
resolve(false)
}
});
});
}
function reconnect(socket) {
//reconnect on loss (max retries)
}
//mark couldnt get authentication method to use json
function spinServer(server) {
return new Promise(resolve => {
socket = net.Socket();
socket.connect(server.port, server.ip, () => {});
socket.on('error', function(err) {
console.log(err)
resolve(false)
});
socket.on('data', function(data) {
if (data.toString().startsWith('Password:')) {
socket.write(server.password)
console.log(data.toString())
}
if (data.toString().startsWith('Authenticated=1')) {
console.log('Logged in!');
(async() => {
resolve(socket);
socket.playerList = JSON.parse(await commandHandler(socket, 'RefreshList'))
})();
setInterval(async () => {
socket.playerList = JSON.parse(await commandHandler(socket, 'RefreshList'))
}, 60000);
}
if (data.toString().startsWith('Authenticated=0')) {
console.log('Login wrong!');
}
});
});
}
const FUNCTIONS = {serverPrompt, commandHandler, commandPrompt, playerPrompt, teamPrompt, gmPrompt, textPrompt, reconnect, spinServer}
process.on("UnhandledPromiseRejectionWarning", e => {
console.log(e + "\n")
inquirer.prompt([{
message: "Unhandled Rejection Warning: Restart server or exit?",
type: 'list',
name: 'choice',
choices: ["Restart", "Exit"],
}, ]).then(async (selected) => {
if (selected.choice == "Restart") serverPrompt()
else if (selected.choice == "CommandHandler") process.exit()
})
})
.on("uncaughtException", e => {
console.log(e)
inquirer.prompt([{
message: "Unhandled Exception: Restart server or exit?",
type: 'list',
name: 'choice',
choices: ["Restart", "Exit"],
}, ]).then(async (selected) => {
if (selected.choice == "Restart") serverPrompt()
else if (selected.choice == "CommandHandler") process.exit()
})
})
.on("unhandledRejection", e => {
console.log(e)
inquirer.prompt([{
message: "Unhandled Rejection: Restart server or exit?",
type: 'list',
name: 'choice',
choices: ["Restart", "Exit"],
}, ]).then(async (selected) => {
if (selected.choice == "Restart") serverPrompt()
else if (selected.choice == "CommandHandler") process.exit()
})
})