This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
/
Client.js
246 lines (223 loc) · 6.26 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var nl = "\r\n";
function Client (socket, username, init) {
this.socket = socket;
this.name = username;
this.visited = {}; // rooms we've been to. This isn't stored.
// If we are loaded from a config, assume we're not connected.
// Otherwise, assume we are.
if (init)
this.connected = false;
else
this.connected = true;
if (init && init.desc)
this.desc = init.desc;
else
this.desc = "Looks like an uninteresting person.";
// Our inventory, this can be stored.
if (init && init.inventory)
this.inventory = init.inventory;
else
this.inventory = [];
// Permissions.
if (init && init.can)
this.can = init.can;
else
this.can = {
"build" : false,
"admin" : false
};
// The ultimate permission, cannot be "granted" or "revoked".
if (init && init.god)
this.god = init.god;
// Passphrase support
if (init && init.pass)
this.pass = init.pass;
}
Client.prototype.gotoRoom = function (roomid) {
//console.log(this.name + " is going to " + roomid);
var room = world.getRoom(roomid);
if (room) {
this.room = room;
if (!this.visited[roomid])
{ this.room.look(this);
this.visited[roomid] = true;
}
}
else
this.msg("No such room: "+roomid);
}
Client.prototype.logout = function () {
this.connected = false;
this.socket.end();
}
Client.prototype.msg = function (message) {
if (this.connected)
this.socket.write(message+nl);
}
Client.prototype.showPrompt = function () {
if (this.connected) {
this.socket.write('['+this.name+'] ' + this.room.name);
if (this.can.build)
this.socket.write(' ('+this.room.id+')');
this.socket.write('> ');
}
}
Client.prototype.say = function (message) {
var umsg = "You say \""+message+"\"";
var omsg = this.name + " says \"" + message + "\"";
world.message(this, umsg, omsg, this.room);
}
Client.prototype.whisper = function (to, message) {
var target = world.getUser(to);
if (target)
{ this.msg("You whisper \""+message+"\" to "+to);
target.update(this.name + " whispers \""+message+"\" to you.");
}
else
this.msg("No such user \""+to+"\"");
}
Client.prototype.emote = function (message) {
var umsg = "* "+message;
var omsg = this.name + " " + message;
world.message(this, umsg, omsg, this.room);
}
Client.prototype.update = function (message) {
this.msg(nl+message);
this.showPrompt();
}
Client.prototype.rename = function (newname) {
if (world.userExists(newname)) {
this.msg("Sorry, that name has already been taken.");
}
else {
var oldname = this.name;
this.name = newname;
var umsg = "You have changed your name to: " + newname;
var omsg = oldname + " has changed their name to: " + newname;
world.message(this, umsg, omsg);
}
}
// Commands for all clients.
Client.prototype.parse = function (data) {
var matches;
var str = world.dataString(data);
if (str == 'exit') return this.logout();
else if (this.room.parse(this, str)) true; // We found a Room call.
else if (matches = str.match(/^say "(.*?)" to (.*?)$/)) {
this.whisper(matches[2], matches[1]);
}
else if (matches = str.match(/^say (.*?)$/)) {
this.say(matches[1]);
}
else if (matches = str.match(/^emote (.*?)$/)) {
this.emote(matches[1]);
}
else if (matches = str.match(/^call me ([\w\s'\.]+)/)) {
this.rename(matches[1]);
}
else if (matches = str.match(/^make room (\w+)\s?(?:called)?\s?([\w\s'\.]+)?/)) {
if (this.can.build) {
var roomid = matches[1];
var roomname = matches[2];
this.buildRoom(roomid, roomname);
}
else
this.youCant();
}
else if (matches = str.match(/^goto (\w+)$/)) {
if (this.can.build) {
var go = matches[1];
if (world.roomExists(go))
this.gotoRoom(roomid);
else
this.msg("That room does not exist.");
}
else
this.youCant();
}
else if (matches = str.match(/^grant (\w+) (?:to )?([\w\s'\.]+)/)) {
if (this.can.admin || this.god) { // a god can always grant.
var permission = matches[1];
var username = matches[2];
var user = world.getUser(username);
if (user) {
user.can[permission] = true;
user.update(this.name+" has granted you the ability to "+permission+".");
}
else
this.msg("No such user.");
}
else
this.youCant();
}
else if (matches = str.match(/^revoke (\w+) (?:from )?([\w\s'\.]+)/)) {
if (this.can.admin) {
var permission = matches[1];
var username = matches[2];
var user = world.getUser(username);
if (user) {
if (!this.god && user.god) {
user.update(this.name + " tried to revoke "+permission+" on you!");
this.msg("You cannot revoke the powers of a god!");
}
else {
user.can[permission] = false;
user.update(this.name+" has revoked your ability to "+permission+".");
}
}
else
this.msg("No such user.");
}
else
this.youCant();
}
else if (matches = str.match(/^kill user ([\w\s'\.]+)/)) {
if (this.can.admin) {
var username = matches[1];
var user = world.getUser(username);
if (user) {
if (user.god) {
user.update(this.name + " tried to kill you!");
this.msg("You cannot kill a god!");
}
else {
user.update("You have been killed, goodbye.");
user.logout();
world.delClient(user);
}
}
else
this.msg("No such user.");
}
else
this.youCant();
}
else if (matches = str.match(/^passwd (\w+)/)) {
this.pass = matches[1];
}
else if (str == 'hi') this.say("hello");
else if (str == 'bye') this.say("goodbye");
else
this.msg("I don't understand \""+str+"\"");
this.showPrompt();
}
// A user isn't allowed to use a certain command
Client.prototype.youCant = function () {
this.msg("You can't do that.");
}
// Build a room
Client.prototype.buildRoom = function (roomid, roomname) {
if (world.roomExists(roomid))
this.msg("Sorry, a room called "+roomid+" already exists.");
else {
var init = {};
if (roomname)
init.name = roomname;
world.addRoom(roomid, init);
this.gotoRoom(roomid);
}
}
exports.newClient = function (socket, username, init) {
return new Client(socket, username, init);
}
// End of library.