-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (103 loc) · 4.04 KB
/
index.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
const net = require("net")
require("dotenv").config()
const port = process.env.PORT || 0
let clients = []
let names = new Map()
let usedNames = []
let rooms = new Map()
const server = net.createServer()
let userNumber = 0
server.on("connection", stream => {
clients.push(stream)
names.set(stream, `<User-${++userNumber}>`)
usedNames.push(names.get(stream))
rooms.set(stream, "general")
stream.on("data", data => {
data = data.toString()
data = data.replace(/\n$/g, "")
if(data == "/rooms" || data == "/list-rooms") {
let usedRooms = []
rooms.forEach((userRoom, userStream) => {
usedRooms.push(userRoom)
})
usedRooms = [...new Set(usedRooms)]
stream.write(usedRooms.join(", ") + "\n")
} else if(data == "/users" || data == "/list-users") {
let usedRooms = {}
rooms.forEach((userRoom, userStream) => {
if(usedRooms[userRoom]) {
usedRooms[userRoom].push(names.get(userStream))
} else {
usedRooms[userRoom] = [names.get(userStream)]
}
})
let output = []
for(let i = 0; i < Object.keys(usedRooms).length; i++) {
let room = Object.keys(usedRooms)[i]
output.push(`"${room}": `)
usedRooms[room].forEach(user => {
output.push(`${user}, `)
})
output.push("\n")
}
output = output.join("")
stream.write(output)
} else if(data.startsWith("/join") || data.startsWith("/room")) {
let room = data.split(" ")
room.shift()
room = room.join(" ")
rooms.set(stream, room)
stream.write(`Switched to room '${room}' \n`)
} else if(data.startsWith("/nick ") || data.startsWith("/nickname ")) {
let name = data.split(" ")
name.shift()
name = name.join(" ")
if(usedNames.includes(`<${name}>`)) {
stream.write(`The name '${name}' is already in use. Please choose a different name.\n`)
} else if(name.includes(" ")) {
stream.write(`The name '${name}' includes spaces, which makes it invalid. Names can't include spaces.\n`)
} else {
let oldNameIndex = usedNames.indexOf(names.get(stream))
names.set(stream, `<${name}>`)
usedNames[oldNameIndex] = names.get(stream)
stream.write(`Your name was set to '${name}'\n`)
}
} else if(data.startsWith("/msg")) {
let splitMsg = data.split(" ")
let receiver = `<${splitMsg[1]}>`
let message = splitMsg
message.shift()
message.shift()
message = message.join(" ")
for(let [receiverStream, receiverName] of names.entries()) {
if(receiverName == receiver) {
receiverStream.write(`<DM from ${names.get(stream)} to ${receiver}> ${message}\n`)
}
}
} else if(data == "/quit" || data == "/exit" || data == "/close") {
stream.end()
usedNames.splice(usedNames.indexOf(names.get(stream)), 1)
if(clients.indexOf(stream) > -1) {
clients.splice(clients.indexOf(stream), 1)
}
} else {
let messageRoom = rooms.get(stream)
clients.forEach(client => {
if(messageRoom == rooms.get(client)) {
if(client != stream) client.write(`${names.get(stream)} ${data}\n`)
}
})
}
})
stream.on("close", err => {
usedNames.splice(usedNames.indexOf(names.get(stream)), 1)
if(err) console.log(err)
if(clients.indexOf(stream) > -1) {
clients.splice(clients.indexOf(stream), 1)
}
})
})
server.listen(port)
server.on("listening", () => {
console.log(`Listening on port ${server.address().port}`)
})