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 | var mongo = require('mongodb').MongoClient;
|
36 | var natural = require('natural');
|
37 | var i18n = new (require('i18n-2'))({ locales: ['en']});
|
38 | var _ = require('underscore');
|
39 | var tokenizer = new natural.WordTokenizer();
|
40 |
|
41 | var advtxt = {};
|
42 |
|
43 | exports = module.exports = advtxt.Server = function(connectionString) {
|
44 | var self = this;
|
45 | self.mongodb = null;
|
46 |
|
47 | console.info('Connecting to MongoDB database…');
|
48 |
|
49 | mongo.connect(connectionString, function(err, db) {
|
50 | if (err) throw err;
|
51 |
|
52 | if (db) {
|
53 | console.info('Connected.');
|
54 | self.mongodb = db;
|
55 |
|
56 | }
|
57 | else {
|
58 | console.error('Connected, but failed to get a database…');
|
59 | process.exit(1);
|
60 | }
|
61 | });
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | advtxt.Server.prototype.movePlayer = function(command, direction) {
|
74 | var self = this;
|
75 |
|
76 | var move = [];
|
77 |
|
78 | if (direction === i18n.__('north') || direction === 'n')
|
79 | move = [0, -1];
|
80 | else if (direction === i18n.__('south') || direction === 's')
|
81 | move = [0, 1];
|
82 | else if (direction === i18n.__('east') || direction === 'e')
|
83 | move = [1, 0];
|
84 | else if (direction === i18n.__('west') || direction === 'w')
|
85 | move = [-1, 0];
|
86 | else
|
87 | move = [0, 0];
|
88 |
|
89 |
|
90 | command.player.x += move[0];
|
91 | command.player.y += move[1];
|
92 |
|
93 |
|
94 | var playerCollection = self.mongodb.collection('player');
|
95 | playerCollection.update({_id: command.player._id},
|
96 | {$set: {x: command.player.x, y: command.player.y}}, {w:1},
|
97 | function(err, success) {
|
98 | if (err) throw err;
|
99 |
|
100 |
|
101 | if (success) {
|
102 | self.getCurrentLocation(command, true);
|
103 | }
|
104 | });
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | advtxt.Server.prototype.updatePlayerItems = function(player, next) {
|
117 | var self = this;
|
118 |
|
119 | var playerCollection = self.mongodb.collection('player');
|
120 | playerCollection.update({_id: player._id},
|
121 | {$set: {items: player.items}}, {w:1},
|
122 | function(err, success) {
|
123 | if (err) throw err;
|
124 |
|
125 |
|
126 | if (!success) {
|
127 | console.error("Couldn't save player: " + player.username);
|
128 | console.error(player);
|
129 | }
|
130 | if (typeof next !== 'undefined' && next) next(success);
|
131 | });
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | advtxt.Server.prototype.doCommand = function(command) {
|
149 | var self = this;
|
150 | command.command = tokenizer.tokenize(command.command.toLowerCase());
|
151 |
|
152 |
|
153 | var verb = command.command[0];
|
154 | var obj = command.command[1];
|
155 | var player = command.player;
|
156 | var room = player.room;
|
157 |
|
158 | if (verb === i18n.__('get')) {
|
159 |
|
160 | if (typeof room.items[obj] !== 'undefined') {
|
161 | var available = room.items[obj].available(player);
|
162 | if (available === true) {
|
163 |
|
164 | player.items[obj] = room.items[obj].name;
|
165 | command.reply(room.items[obj].get(player));
|
166 |
|
167 | self.updatePlayerItems(player);
|
168 | }
|
169 |
|
170 | else {
|
171 | command.reply(available);
|
172 | }
|
173 | }
|
174 |
|
175 | else {
|
176 | command.reply(i18n.__('You can\'t find a "%s" in this room!', obj));
|
177 | }
|
178 | }
|
179 | else if (verb === i18n.__('go')) {
|
180 |
|
181 | if (typeof room.exits[obj] !== 'undefined') {
|
182 | var available = room.exits[obj].available(player);
|
183 | if (available === true) {
|
184 |
|
185 | command.reply(room.exits[obj].go(player));
|
186 |
|
187 | self.movePlayer(command, obj);
|
188 | }
|
189 |
|
190 | else {
|
191 | command.reply(available);
|
192 | }
|
193 | }
|
194 |
|
195 | else {
|
196 |
|
197 | command.reply(i18n.__('You can\'t go "%s", it just doesn\'t work.', obj));
|
198 | }
|
199 | }
|
200 |
|
201 | else if (typeof room.commands[verb] !== 'undefined') {
|
202 | command.reply(room.commands[verb](player));
|
203 | }
|
204 |
|
205 | else if (verb === i18n.__('exits')) {
|
206 | var exits = [];
|
207 | for (var key in room.exits) {
|
208 | exits.push(room.exits[key].name);
|
209 | }
|
210 |
|
211 | var exitNames = i18n.__('Available exits: ');
|
212 | for (var i = 0; i < exits.length; i++) {
|
213 | exitNames += exits[i];
|
214 | if (i !== exits.length - 1) exitNames += i18n.__(", ");
|
215 | }
|
216 | command.reply(exitNames);
|
217 | }
|
218 | else {
|
219 | command.reply(i18n.__('Sorry, I don\'t know how to "%s" in this room.', verb));
|
220 | }
|
221 | }
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | advtxt.Server.prototype.getCurrentLocation = function(command, enterRoom) {
|
233 | var self = this;
|
234 |
|
235 | var roomCollection = self.mongodb.collection('room');
|
236 | roomCollection.findOne({x: command.player.x, y: command.player.y, map: command.player.map}, function (err, room) {
|
237 | if (err) throw err;
|
238 |
|
239 | if (room) {
|
240 |
|
241 |
|
242 | command.player.room = room;
|
243 |
|
244 |
|
245 |
|
246 | var player = command.player;
|
247 |
|
248 |
|
249 | eval(player.room.commands);
|
250 | eval(player.room.items);
|
251 | eval(player.room.exits);
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | if (enterRoom) {
|
257 | command.reply(player.room.description);
|
258 | }
|
259 |
|
260 | else {
|
261 | self.doCommand(command);
|
262 | }
|
263 | }
|
264 | });
|
265 | }
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | advtxt.Server.prototype.processCommand = function(command) {
|
275 | var self = this;
|
276 |
|
277 | var playerCollection = self.mongodb.collection('player');
|
278 | playerCollection.findOne({username: command.player, map: "default"}, function(err, player) {
|
279 | if (err) throw err;
|
280 |
|
281 | if (player) {
|
282 | command.player = player;
|
283 | self.getCurrentLocation(command);
|
284 | }
|
285 | else {
|
286 | playerCollection.insert({
|
287 | username: command.player,
|
288 | map: "default",
|
289 | x: 0,
|
290 | y: 0,
|
291 | room: {},
|
292 | items: {}
|
293 | }, function(err, player) {
|
294 | if (err) throw err;
|
295 |
|
296 | command.player = player[0];
|
297 | self.getCurrentLocation(command, true);
|
298 | });
|
299 | }
|
300 | });
|
301 | }
|
302 |
|