1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var natural = require('natural');
|
12 | var i18n = new (require('i18n-2'))({ locales: ['en']});
|
13 | var _ = require('underscore');
|
14 | var Hoek = require('hoek');
|
15 | var tokenizer = new natural.WordTokenizer();
|
16 | var Parser = require('./parser');
|
17 |
|
18 | var advtxt = {};
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | exports = module.exports = advtxt.Server = function() {
|
28 | var self = this;
|
29 |
|
30 | self.db = null;
|
31 | self.initialized = false;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | advtxt.Server.prototype.initialize = function(db) {
|
42 | var self = this;
|
43 |
|
44 | self.db = db;
|
45 | self.initialized = true;
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | advtxt.Server.prototype.movePlayer = function(command, direction) {
|
58 | var self = this;
|
59 |
|
60 | var move = [];
|
61 |
|
62 | if (direction === i18n.__('north') || direction === 'n')
|
63 | move = [0, -1];
|
64 | else if (direction === i18n.__('south') || direction === 's')
|
65 | move = [0, 1];
|
66 | else if (direction === i18n.__('east') || direction === 'e')
|
67 | move = [1, 0];
|
68 | else if (direction === i18n.__('west') || direction === 'w')
|
69 | move = [-1, 0];
|
70 | else
|
71 | move = [0, 0];
|
72 |
|
73 |
|
74 | command.player.x += move[0];
|
75 | command.player.y += move[1];
|
76 |
|
77 |
|
78 | self.updatePlayerPosition(command, self.getCurrentLocation.bind(self));
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | advtxt.Server.prototype.resetPlayer = function(command, clearItems) {
|
90 | var self = this;
|
91 |
|
92 |
|
93 | command.player.x = 0;
|
94 | command.player.y = 0;
|
95 |
|
96 | command.player.status = "alive";
|
97 |
|
98 |
|
99 | if (typeof clearItems !== 'undefined' && clearItems) {
|
100 | command.player.items = {};
|
101 | command.replies.push(i18n.__("Giving you a blank slate…"));
|
102 | }
|
103 | else {
|
104 | command.replies.push(i18n.__("Moving you to the origin room…"));
|
105 | }
|
106 |
|
107 |
|
108 | self.updatePlayerPosition(command, function(command) {
|
109 | var self = this;
|
110 |
|
111 | if (clearItems) {
|
112 | self.updatePlayerItems(command, self.getCurrentLocation.bind(self));
|
113 | }
|
114 |
|
115 |
|
116 | else {
|
117 | self.getCurrentLocation(command, true);
|
118 | }
|
119 | }.bind(self));
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | advtxt.Server.prototype.updatePlayerPosition = function(command, next) {
|
131 | var self = this;
|
132 |
|
133 | var selector = {_id: command.player._id};
|
134 | var data = {x: command.player.x, y: command.player.y};
|
135 |
|
136 | self.db.update('player', selector, data, function(err, success){
|
137 |
|
138 | if (success) {
|
139 | if(typeof next !== 'undefined' && next) next(command, true);
|
140 | }
|
141 | else {
|
142 | if(typeof next !== 'undefined' && next) next(false);
|
143 | }
|
144 | }.bind(self));
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | advtxt.Server.prototype.updatePlayerItems = function(command, next) {
|
156 | var self = this;
|
157 |
|
158 | var selector = {_id: command.player._id};
|
159 | var data = {items: command.player.items};
|
160 |
|
161 | self.db.update('player', selector, data, function(err, success) {
|
162 |
|
163 | if (!success) {
|
164 | console.error("Couldn't save player: " + command.player.username);
|
165 | console.error(command.player);
|
166 | }
|
167 | if (typeof next !== 'undefined' && next) next(command, true);
|
168 | }.bind(self));
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | advtxt.Server.prototype.updatePlayerStatus = function(command, next) {
|
190 | var self = this;
|
191 |
|
192 | var selector = {_id: command.player._id};
|
193 | var data = {status: command.player.status};
|
194 |
|
195 | self.db.update('player', selector, data, function(err, success) {
|
196 | if (!success) {
|
197 | console.error("Couldn't save player: " + command.player.username);
|
198 | console.error(command.player);
|
199 | }
|
200 | if (typeof next !== 'undefined' && next) next(command, true);
|
201 | }.bind(self));
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | advtxt.Server.prototype.doCommand = function(command) {
|
212 | var self = this;
|
213 |
|
214 | var parser = new Parser(command);
|
215 | if (!parser.fail) {
|
216 | command.command = parser.command;
|
217 | }
|
218 | else {
|
219 | command.replies.push(__.i18n("I don't know what you mean."));
|
220 | }
|
221 |
|
222 |
|
223 | var verb = command.command.verb;
|
224 | var object = command.command.object;
|
225 | var player = command.player;
|
226 | var room = player.room;
|
227 | var commands = parser.commands;
|
228 |
|
229 |
|
230 |
|
231 | if (verb === commands.RESET && object === '') {
|
232 | self.resetPlayer(command, false);
|
233 | }
|
234 | else if (verb === commands.RESET && object === i18n.__('all')) {
|
235 | self.resetPlayer(command, true);
|
236 | }
|
237 | else if (player.status === 'dead' || player.status === 'win') {
|
238 | self.finalize(command);
|
239 | }
|
240 | else if (verb === commands.GET) {
|
241 |
|
242 | if (typeof room.items[object] !== 'undefined') {
|
243 | var available = room.items[object].available(player);
|
244 | if (available === true) {
|
245 |
|
246 | player.items[object] = room.items[object].name;
|
247 | command.replies.push(room.items[object].get(player));
|
248 |
|
249 | self.updatePlayerItems(command, self.finalize.bind(self));
|
250 | }
|
251 |
|
252 | else {
|
253 | command.replies.push(available);
|
254 | self.finalize(command);
|
255 | }
|
256 | }
|
257 |
|
258 | else {
|
259 | command.replies.push(i18n.__('You can\'t find a "%s" in this room!', object));
|
260 | self.finalize(command);
|
261 | }
|
262 | }
|
263 | else if (verb === commands.GO) {
|
264 |
|
265 | if (typeof room.exits[object] !== 'undefined') {
|
266 | var available = room.exits[object].available(player);
|
267 | if (available === true) {
|
268 |
|
269 | command.replies.push(room.exits[object].go(player));
|
270 |
|
271 | self.movePlayer(command, object);
|
272 | }
|
273 |
|
274 | else {
|
275 | command.replies.push(available);
|
276 | self.finalize(command);
|
277 | }
|
278 | }
|
279 |
|
280 | else {
|
281 |
|
282 | command.replies.push(i18n.__('You can\'t go "%s", it just doesn\'t work.', object));
|
283 | self.finalize(command);
|
284 | }
|
285 | }
|
286 |
|
287 | else if (typeof room.commands[verb] !== 'undefined') {
|
288 | command.replies.push(room.commands[verb](player));
|
289 | self.finalize(command);
|
290 | }
|
291 |
|
292 | else if (verb === commands.EXITS) {
|
293 | var exits = [];
|
294 | for (var key in room.exits) {
|
295 | exits.push(room.exits[key].name);
|
296 | }
|
297 |
|
298 | var exitNames = i18n.__('Available exits: ');
|
299 | for (var i = 0; i < exits.length; i++) {
|
300 | exitNames += exits[i];
|
301 | if (i !== exits.length - 1) exitNames += i18n.__(", ");
|
302 | }
|
303 | command.replies.push(exitNames);
|
304 | self.finalize(command);
|
305 | }
|
306 | else {
|
307 | command.replies.push(i18n.__('Sorry, I don\'t know how to "%s" in this room.', verb));
|
308 | self.finalize(command);
|
309 | }
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | advtxt.Server.prototype.getCurrentLocation = function(command, enterRoom) {
|
322 | var self = this;
|
323 |
|
324 |
|
325 |
|
326 | if (typeof command.status === 'undefined')
|
327 | command.status = Hoek.clone(command.player.status);
|
328 |
|
329 | self.db.findOne('room', {x: command.player.x, y: command.player.y, map: command.player.map}, function (err, room) {
|
330 | if (err) throw err;
|
331 |
|
332 | if (room) {
|
333 |
|
334 |
|
335 | command.player.room = room;
|
336 |
|
337 |
|
338 |
|
339 | var player = command.player;
|
340 |
|
341 |
|
342 | eval(player.room.commands);
|
343 | eval(player.room.items);
|
344 | eval(player.room.exits);
|
345 |
|
346 |
|
347 |
|
348 | if (enterRoom) {
|
349 | command.replies.push(player.room.description);
|
350 | self.finalize(command);
|
351 | }
|
352 |
|
353 | else {
|
354 | self.doCommand(command);
|
355 | }
|
356 | }
|
357 | });
|
358 | }
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | advtxt.Server.prototype.processCommand = function(command) {
|
368 | var self = this;
|
369 |
|
370 | self.db.findOne('player', {username: command.player, map: "default"}, function(err, player) {
|
371 | if (err) throw err;
|
372 |
|
373 | if (player) {
|
374 | command.player = player;
|
375 | self.getCurrentLocation(command);
|
376 | }
|
377 | else {
|
378 | self.db.insertOne('player', {
|
379 | username: command.player,
|
380 | map: "default",
|
381 | x: 0,
|
382 | y: 0,
|
383 | status: "alive",
|
384 | room: {},
|
385 | items: {}
|
386 | }, function(err, player) {
|
387 | if (err) throw err;
|
388 |
|
389 | command.player = player;
|
390 | self.getCurrentLocation(command, true);
|
391 | });
|
392 | }
|
393 | });
|
394 | }
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | advtxt.Server.prototype.finalize = function(command) {
|
402 | var self = this;
|
403 |
|
404 |
|
405 |
|
406 | if (command.status !== command.player.status) {
|
407 | if (command.player.status === 'dead') {
|
408 | command.replies.push(i18n.__('You\'ve died! Send the command `reset all` to try again!'));
|
409 | }
|
410 | else if (command.player.status === 'win') {
|
411 | command.replies.push(i18n.__('You\'ve won the game! How impressive! Send the command `reset all` to play again!'));
|
412 | }
|
413 | self.updatePlayerStatus(command);
|
414 | }
|
415 | else if (command.player.status === 'dead') {
|
416 | command.replies.push(i18n.__('You\'re still dead! Send the command `reset all` to try again!'));
|
417 | }
|
418 | else if (command.player.status === 'win') {
|
419 | command.replies.push(i18n.__('Yes, your win was glorious, but send the command `reset all` to play again!'));
|
420 | }
|
421 |
|
422 |
|
423 | command.done(command);
|
424 | }
|
425 |
|