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