-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (132 loc) · 4.75 KB
/
server.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
require('dotenv').config();
const { Nuxt, Builder } = require('nuxt');
const bot = require('./assets/js/twitch-bot');
const app = require('./routes');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const isProd = process.env.NODE_ENV === 'production';
const isPrecommit = process.env.PRECOMMIT === 'true';
// We instantiate Nuxt.js with the options
let config = require('./nuxt.config.js');
config.dev = !isProd;
const nuxt = new Nuxt(config);
// Listen the server
server.listen(process.env.PORT, process.env.HOST);
console.log(
`The Windfish is dreaming on ${process.env.HOST}:${process.env.PORT}\n\n`
.warn
);
// Start build process in dev mode
if (config.dev && !isPrecommit) {
const builder = new Builder(nuxt);
builder.build();
}
app.use(nuxt.render);
let sessions = {};
/**
* Socket.io
*
* Here is where we set up all out socket connections, handlers, events
* responses and any other behaviour we need to happen during a connection
*/
io.on('connection', socket => {
let connectedCount = Object.keys(socket.client.server.sockets.connected)
.length;
console.log(`User connected with ID: ${socket.id}`.info);
console.log(`Currently connected users: ${connectedCount}`.info);
// Make sure we disconnect everything possible for a user, session, etc.
socket.on('disconnect', () => {
console.log(`User disconnected with ID: ${socket.id}`.warn);
for (let key in sessions) {
let session = sessions[key];
let position = session.users.indexOf(socket.id);
if (position > -1) {
// Remove the user from the stored session users
session.users.splice(position, 1);
// If no users attached to the session
if (session.users.length < 1) {
// remove the session from memory
console.log(`Deleting session ID: ${key}`.warn);
delete sessions[key];
}
}
}
});
/**
* Send the complete tracker data between two multiple connections
*/
socket.on('send broadcast data', data => {
let currentSocket = socket;
let connectedSocketIds = Object.keys(
currentSocket.server.sockets.connected
);
for (let key in sessions) {
let session = sessions[key];
let position = session.users.indexOf(socket.id);
if (position > -1) {
session.users.forEach(userSocketId => {
let socket =
currentSocket.server.sockets.connected[userSocketId];
socket.emit('update broadcast data', data);
});
}
}
});
/**
* Connect sockets, or rather, bind them to the instance of the WebSocket
*/
socket.on('connection broadcast', sessionId => {
console.log(`User connected with session ID: ${sessionId}`.warn);
sessions[sessionId] = sessions[sessionId] || {};
sessions[sessionId].users = sessions[sessionId].users || [];
sessions[sessionId].users.push(socket.id);
console.log(sessions);
});
/**
* Connect the bot and and initialize all that requires of it
*/
socket.on('connection bot', config => {
let channels = [];
let twitchConnection = new bot(config);
channels.push(`#${config.channel}`);
twitchConnection.connect({
options: {
debug: true,
},
connection: {
reconnect: true,
},
identity: {
username: process.env.TWITCH_BOT_NAME,
password: process.env.TWITCH_BOT_TOKEN,
},
channels,
socket,
});
// Kill the bot
socket.on('disconnect bot', () => {
twitchConnection.disconnect();
});
// Update the bot whitelist. More details in the actual bot code
socket.on('update bot whitelist', whitelist => {
twitchConnection.updateWhitelist(whitelist);
});
/**
* We dequeue certain bot actions so we can effectively "promise".
* This inner logic can be refactors based on JS Promises but for
* now we just dequeue in a classic callback way.
* IE: Chat activate sword and another chat user activates shield...
* We just make sure we complete one before the other.
*/
socket.on('bot dequeue', userData => {
twitchConnection.dequeue(userData);
});
});
});
/**
* Just kind of a way to check for any issues before we commit to Git.
*/
if (isPrecommit) {
console.log('No issues found in start mock'.warn);
process.exit();
}