This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
/
Client.js
117 lines (103 loc) · 3.08 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
var nl = "\r\n";
function Client (socket, world) {
this.socket = socket;
this.world = world;
this.name = 'User ' + world.usercount;
this.visited = {}; // rooms we've been to.
this.inventory = []; // our stuff.
}
Client.prototype.gotoRoom = function (roomid) {
var room = this.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.socket.end();
this.world.delClient(this);
}
Client.prototype.msg = function (message) {
this.socket.write(message+nl);
}
Client.prototype.showPrompt = function () {
this.socket.write('['+this.name+'] ' + this.room.name + '> ');
}
Client.prototype.say = function (message) {
var umsg = "You say \""+message+"\"";
var omsg = this.name + " says \"" + message + "\"";
this.world.message(this, umsg, omsg, this.room);
}
Client.prototype.whisper = function (to, message) {
var target = this.world.getUser(to);
if (target)
{ this.msg("You whisper \""+message+"\" to "+to);
target.msg(nl+this.name + " whispers \""+message+"\" to you.");
target.showPrompt();
}
else
this.msg("No such user \""+to+"\"");
}
Client.prototype.emote = function (message) {
var umsg = "* "+message;
var omsg = this.name + " " + message;
this.world.message(this, umsg, omsg, this.room);
}
Client.prototype.rename = function (newname) {
if (this.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;
this.world.message(this, umsg, omsg);
}
}
// Commands for all clients.
Client.prototype.parse = function (data) {
var matches;
var str = data.toString('utf8').replace(/(\n|\r)+$/, '');
if (str.match(/^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+) (called)? ([\w\s'\.]+)?/)) {
var roomid = matches[1];
var roomname = matches[3];
var init = {};
if (roomname)
init.name = roomname;
if (this.world.roomExists(roomid))
this.msg("Sorry, a room called "+roomid+" already exists.");
else {
this.world.addRoom(roomid, init);
this.gotoRoom(roomid);
}
}
else if (str.match(/^hi$/)) this.say("hello");
else if (str.match(/^bye$/)) this.say("goodbye");
else
this.msg("I don't understand \""+str+"\"");
this.showPrompt();
}
exports.newClient = function (socket, world) {
return new Client(socket, world);
}
// End of library.