-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
94 lines (78 loc) · 2.17 KB
/
client.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
'use strict';
// Environment
require('dotenv').config();
const superagent = require('superagent');
// Set server url based on .env
const SERVER_URL = require('./server-url.js'); // if NODE_ENV=development, use localhost:3000
// Command logic
const { handleCommand } = require('./lib/handle-command.js');
// Interface modules
const emojic = require('emojic');
const log = require('./lib/log.js');
const rl = require('./lib/readline-interface.js');
// Socket.io
const io = require('socket.io-client');
let socket = io.connect(SERVER_URL);
log(`Client running on ${SERVER_URL}...`);
// Client will accept a line and pass it to handlers
rl.on('line', handleLine);
// Client will accept and print
// any payload.display
socket.on('output', log);
socket.on('clear', console.clear);
rl.on('close', handleReadlineClose);
// Helper functions
function clear() {
console.clear();
}
function changeApp(game) {
superagent
.get(`${SERVER_URL}/api/v1/app-info/${game}`)
.then(results => {
let url = results.body[0].url;
console.log(url);
socket.disconnect(true);
socket = io.connect(url);
socket.on('output', log);
socket.on('clear', clear);
})
.catch(() => {
console.error('Game does not exist');
});
}
function switchConnection(url) {
socket.disconnect(true);
socket = io.connect(url);
socket.on('clear', console.clear);
socket.on('output', log);
socket.on('clear', clear);
}
function exitCommand() {
socket.disconnect(true);
rl.close();
}
function handleReadlineClose() {
log(`${emojic.smiley} Have a great day! ${emojic.wave}`);
process.exit(0);
}
function handleLine(line) {
if (line[0] === '/' && line.length > 1) {
const launch = /^\/launch/gi;
if (launch.test(line)) {
let app = line.trim().slice(8);
console.log(app);
changeApp(app);
} else if (line === '/lobby') {
switchConnection(SERVER_URL);
} else if (line === '/exit') {
exitCommand();
} else if (line === '/dev') {
switchConnection('http://localhost:4444');
} else {
handleCommand(line, socket);
}
} else {
socket.emit('input', line);
}
rl.prompt(true); // Show the readline prompt
}