UNPKG

3.23 kBJavaScriptView Raw
1exports.command = {
2 name: "go", // Name of command to be executed (Max 10 chars)
3 autoload: true, // Should the command be autoloaded at startup
4 unloadable: true, // Can the command be unloaded dynamically
5 min_rank: 0, // Minimum rank to use to execute the command
6 display: "go somewhere else", // Summary help text to show in the .help command (Max 60 chars)
7 help: "After you .look you'll know where you can go to. Then, you'll just have to .go <place>.", // Full help text when .help <command> is used
8 usage: ".go <place>",
9
10 // Function to execute the command
11 execute: function(socket, command, command_access) {
12 var chalk = require('chalk');
13 var to = command.split(' ')[0];
14 if ((typeof to === 'undefined') || (to.length < 1)) return socket.write(chalk.yellow(":: Where do you want to go to?\r\n"));
15 if (to.toLowerCase() === command_access.getUniverse().get(socket.db.where).name.toLowerCase()) return socket.write(chalk.red(":: You are already there!\r\n"));
16 var neighbours = command_access.getUniverse().get_neighbours(command_access.getUniverse().get(socket.db.where));
17 if (neighbours.length == 0) return socket.write(chalk.yellow(":: You don't see anywhere to go to.\r\n"));
18 var toId = null;
19 var abrev = [];
20 for (var i=0; i < neighbours.length; i++) {
21 if (neighbours[i].name.toLowerCase() == to.toLowerCase()) toId = i;
22 if (neighbours[i].name.toLowerCase().substring(0,to.length) == to.toLowerCase()) abrev.push(i);
23 }
24 if (toId == null) {
25 if (abrev.length === 0) return socket.write(chalk.red(":: I don't know where " + chalk.bold(to) + " is...\r\n"));
26 if (abrev.length > 1) {
27 possibilities = "";
28 for (var p = 0; p < abrev.length-1; p++) {
29 possibilities += chalk.bold(neighbours[p].name) + ", ";
30 }
31 possibilities += neighbours[abrev[abrev.length-1]].name;
32 return socket.write(chalk.yellow(":: There are several possible exits you might mean: " + possibilities + ". Can you be more specific?\r\n"));
33 }
34 toId = abrev[0];
35 }
36
37 var math=require('mathjs');
38 var movement = math.subtract(socket.db.where,neighbours[toId].coords);
39 var direction = "";
40 if (movement[2] > 0) direction = "down";
41 if (movement[2] < 0) direction = "up";
42 if ((movement[0] !== 0 || movement[1] !== 0) && movement[2] != 0) {
43 direction += " and ";
44 }
45 if (movement[1] > 0) direction += "south";
46 if (movement[1] < 0) direction += "north";
47 if (movement[0] > 0) direction += "west";
48 if (movement[0] < 0) direction += "east";
49
50 command_access.allHereButMe(socket,function(me,t){t.write(chalk.bold(":: " + chalk.yellow(me.username) + " starts walking to " + chalk.green(direction) + " towards " + chalk.teal(to.toLowerCase()) + "...\r\n"));});
51 socket.write(chalk.bold(":: You start walking to " + chalk.green(direction) + " towards " + chalk.teal(neighbours[toId].name) + "...\r\n"));
52 socket.db.where = neighbours[toId].coords;
53 var tmp = command_access.getUser(socket.username);
54 tmp.where = socket.db.where;
55 command_access.updateUser(socket.username, tmp);
56 command_access.allHereButMe(socket,function(me,to){to.write(chalk.bold(":: " + chalk.yellow(me.username) + " walks in.\r\n"));});
57 socket.write(chalk.green(":: You arrive.\r\n"));
58 }
59}