-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (153 loc) · 5.93 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
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
const { app } = require("./depends.js");
const socket = require("socket.io");
require('dotenv').config();
// Objects
const _user_ = require("./backend/classes/user.js");
const _character_ = require("./backend/classes/character.js");
const _chat_message_ = require("./backend/classes/chatMessage.js");
const { World } = require("./backend/classes/world.js");
// Commands
const say = require("./backend/commands/say.js");
const look = require("./backend/commands/look.js");
const move = require("./backend/commands/move.js");
const grab = require("./backend/commands/grab");
const drop = require("./backend/commands/drop.js");
// test world
// const world = require("./backend/testWorld.js").world;
const world = new World("aGrxbWZ_map.json");
// START SERVER
const PORT = process.env.PORT || 8088;
const server = app.listen(PORT, function () {
console.log("----- MUD -----");
console.log(`listening at https://localhost:${PORT}`);
});
// Socket Setup
const io = socket(server);
// Server Data
var users = {};
var chat = [];
var loggedInUsers = [];
io.on("connection", function (socket) {
console.log("--> Creating new Session.");
let user = null; // Initialize user pointer to null
// ===== ACCOUNT AND CONNECTION MANAGEMENT ===== //
socket.on("disconnect", function () {
let today = new Date();
if (user == null) return
let newchat = new _chat_message_.ChatMessage( // generate disconnect chat msg
color = "yellow",
body = `Player ${user.username} disconnected.`,
sender = "SERVER",
timestamp = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds()
);
chat.push(newchat); // push it to server chat history
io.emit("chat_update", newchat); // send the msg to all users
console.log("--> Removing Session.");
});
socket.on("on_check_auth", function(cached_auth, callback) {
console.log(cached_auth);
var result = false;
var err = null;
for (let [_, value] of Object.entries(users)) {
if (value.checkAuthToken(cached_auth)) {
result = true;
}
}
callback(err, result);
});
socket.on("on_login", function(user_id) {
// try to find an existing user with the given id
user = users[user_id];
// if no user with the given id was found, create a new one
if (user == null) {
console.log("--> Creating new User.")
user = new _user_.User( // create the user object
user_id = user_id,
username = "user_" + (Object.keys(users).length + 1), // TODO: replace with user-defined username (with validation)
socket = socket,
auth_token = "hello"
);
users[user_id] = user;
}
else{ // adding this so that a reconnecting player with a new socket is not ignored.
user.socket.disconnect();
user.socket = socket;
users[user_id] = user;
}
// add user to logged in users
loggedInUsers.push(user_id);
socket.emit("user_update", user.emitUser());
})
socket.on("change_username", function(data) {
username = data.username;
user_id = data.user_id;
users[user_id].setUsername(username);
io.emit("user_update", users[user_id].emitUser());
});
// ===== CHARACTER MANAGEMENT ===== //
socket.on("create_character", function(data){
let char_name = data[0];
let user_id = data[1];
let user = users[user_id];
// check if player name is valid TBD
// create character attached to user
let new_char = new _character_.Character(char_name)
new_char.room = world.getStartRoom();
user.characters.push(new_char);
user.current_character = user.characters.length - 1;
socket.emit("user_update", user.emitUser());
});
// ===== COMMANDS ===== //
socket.on("message", function(message_data) {
switch (message_data.cmd){
// SAY COMMAND
case "say":
say.process(message_data, user, users, chat, io)
break;
// LOOK COMMAND
case "l":
case "look":
look.process(message_data, user, users, chat, io)
break;
// MOVE NORTH
case "n":
case "north":
move.moveNorth(world, message_data, user, users, chat, io);
break;
// MOVE SOUTH
case "s":
case "south":
move.moveSouth(world, message_data, user, users, chat, io);
break;
// MOVE EAST
case "e":
case "east":
move.moveEast(world, message_data, user, users, chat, io);
break;
// MOVE WEST
case "w":
case "west":
move.moveWest(world, message_data, user, users, chat, io);
break;
case "grab":
grab.process(message_data, user, users, chat, io);
break;
case "drop":
drop.process(message_data, user, users, chat, io);
break;
// INVALID COMMAND ENTERED
default:
let today = new Date();
let newchat = new _chat_message_.ChatMessage( // generate disconnect chat msg
color = "red",
body = `Invalid command: ` + message_data,
sender = "ERR",
timestamp = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds()
);
newchat.body = message_data.cmd + " is not a valid command.";
newchat.color = "red";
chat.push(newchat);
socket.emit("chat_update", newchat);
}
});
});