-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
129 lines (109 loc) · 4.1 KB
/
app.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
// Including libraries
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const http = require('http');
const xss = require("xss");
var path = require('path');
// Usernames
const nameListAdjective = ["Creepy", "Deadly", "Eerie", "Howling", "Icy", "Invisible", "Jumpy", "Dark", "Savage",
"Quiet", "Grim", "Dangerous", "Cursed", "Frightful", "Bitter", "Gloomy", "Menacing", "Crackling", "Magical",
"Lurking", "Gravestone", "Wicked", "Phantom", "Scarlet", "Haunted", "Witchy", "Spooky", "Scary", "Weird", "Nerdy"];
const nameListNoun = ["Spider", "Ghost", "Pumpkin", "Skull", "Vampire", "Goblin", "Potion", "Shadow", "Monster", "Fog",
"Owl", "Bat", "Hunter", "Stalker", "Cat", "Candy", "Ghoul", "Zombie", "Moon", "Werewolf", "Skeleton", "Web",
"Thief", "Eyeball", "Eyes", "Candle", "Mist", "Midnight", "Creature", "Nerd"];
const playerModelList = ["ghost", "vampire", "werewolf"];
function randomName() {
return nameListAdjective[nameListAdjective.length * Math.random() | 0] + nameListNoun[nameListNoun.length * Math.random() | 0] + Math.floor(Math.random().toFixed(2)*100);
}
function randomPlayerModel() {
return playerModelList[playerModelList.length * Math.random() | 0];
}
// Basic client list (to keep track of currently connected client IDs and names only, nothing more)
const clients = [];
const sockets = [];
// Listen for incoming connections from clients
io.on('connection', (socket) => {
clients[socket.id] = {};
sockets[socket.id] = socket;
// Listen for disconnect events
socket.on('disconnect', (data) => {
// Only broadcast to other clients that this one has left IF it has triggered a join event
if (clients[socket.id].id !== undefined) {
socket.broadcast.emit('otherDisconnect', clients[socket.id].id);
}
delete clients[socket.id];
});
// Listen for client joining and assign a name
socket.on('join', (data) => {
let _id = data.id || 0;
let _name = randomName();
let _model = randomPlayerModel();
// Check to see if a client with the same ID has already joined the server
let clientsKeys = Object.keys(clients)
clientsKeys.forEach((key, index) => {
// If the ID matches, then drop the existing socket connection
if (`${clients[key].id}` == _id) {
let clientToDisconnect = `${[key]}`;
sockets[clientToDisconnect].disconnect(); // boot the existing client off the server
}
})
if (_id !== 0) {
// Add data to current client list
clients[socket.id].id = _id;
clients[socket.id].name = _name;
clients[socket.id].model = _model;
// Broadcast join to all others (which will then respond with name for original client)
socket.broadcast.emit('otherJoin', clients[socket.id]);
// Note that client has joined in the console
console.log(_id, "joined as a " + _model + " named " + _name);
// Tell user their assigned name
sockets[socket.id].emit('selfIdentity', {
name: _name,
model: _model
})
}
});
// Listen for name change events
socket.on('identity', (data) => {
let _id = data.id;
let _name = data.name;
let _model = data.model;
_name = xss(_name.substr(0, 20)); // Make sure name is XSS safe
// Update name in client list
clients[socket.id].name = _name;
clients[socket.id].model = _model;
// If there's a target, only send the name to that target
if (data.target !== undefined) {
// Iterate over connected clients to find target and send them the message
for (let c in clients) {
if (clients[c].id == data.target) {
sockets[c].emit('otherIdentity', {
id: _id,
name: _name,
model: _model
})
}
}
} else {
// Otherwise broadcast name change to all other users
socket.broadcast.emit('otherIdentity', {
id: _id,
name: _name,
model: _model
});
}
});
// Listen for movement events
socket.on('move', (data) => {
socket.broadcast.emit('otherMove', data);
});
});
// Http server
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.use(express.static('static'));
// Open server to manage server things
server.listen(process.env.PORT || 80);