1 | var formatters = require('../utils/formatters.js');
|
2 |
|
3 | exports.command = {
|
4 | name: "dig",
|
5 | autoload: true,
|
6 | unloadable: true,
|
7 | min_rank: 5,
|
8 | display: "creates a new place, next to where you are",
|
9 | help: ".dig <direction> <name> will create a new place next to here, " +
|
10 | "called <name>, in <direction>'s direction. If that place already exists, " +
|
11 | "but a passage doesn't, just create the passage. More info about directions in .file directions.",
|
12 | usage: ".dig <direction> <name>",
|
13 | weigth: 0,
|
14 |
|
15 |
|
16 | execute: function(socket, command, command_access) {
|
17 | var chalk = require('chalk');
|
18 | direction = command.split(' ')[0];
|
19 | name = command.split(' ').slice(1).join(" ");
|
20 |
|
21 | if (typeof direction !== 'string' || direction.length === 0) {
|
22 | socket.write(chalk.bold("Syntax: ") + ".dig <direction> <name>\r\n");
|
23 | return;
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 | valid = false;
|
29 | if (direction.toUpperCase() === direction) {
|
30 | for (var i in command_access.getUniverse()) {
|
31 | if (i === direction &&
|
32 | typeof command_access.getUniverse()[i] === 'number'
|
33 | )
|
34 | valid = true;
|
35 | }
|
36 | }
|
37 | if (!valid) {
|
38 | socket.write(chalk.red(":: " + chalk.bold(direction) + " is not a valid direction.\r\n"));
|
39 | return;
|
40 | }
|
41 |
|
42 | if ((typeof name !== 'string') ||
|
43 | (name.length === 0) ||
|
44 | !(/^[a-zA-Z\u00C0-\u00ff]+$/.test(name.substring(0,1)))
|
45 | ) {
|
46 | socket.write(chalk.red(":: Place names need to start with a letter!\r\n"));
|
47 | return;
|
48 | }
|
49 | if (name === command_access.getUniverse().get(socket.db.where).name) {
|
50 | socket.write(chalk.yellow(":: You're already there!\r\n"));
|
51 | return;
|
52 | }
|
53 |
|
54 | var neighbours = command_access.getUniverse().get_neighbours(
|
55 | command_access.getUniverse().get(socket.db.where));
|
56 |
|
57 | for (var n = 0; n < neighbours.length; n++) {
|
58 | if (neighbours[n] !== null && neighbours[n].name === name) {
|
59 | socket.write(chalk.yellow(":: That place already exists!\r\n"));
|
60 | return;
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | target = socket.db.where.slice(0);
|
69 | if (direction.search("W") !== -1) target[0]--;
|
70 | if (direction.search("E") !== -1) target[0]++;
|
71 | if (direction.search("N") !== -1) target[1]++;
|
72 | if (direction.search("S") !== -1) target[1]--;
|
73 | if (direction.search("U") !== -1) target[2]++;
|
74 | if (direction.search("D") !== -1) target[2]--;
|
75 | targObj = command_access.getUniverse().get(target);
|
76 |
|
77 | if (targObj !== null) {
|
78 |
|
79 | if (targObj.name !== name) {
|
80 | socket.write(formatters.text_wrap(chalk.red(":: You're trying to create a " +
|
81 | "place called " + chalk.bold(name) + " where there's already another " +
|
82 | "place called " + chalk.bold(targObj.name) + "! ") + chalk.yellow("Maybe you want to " +
|
83 | chalk.bold(".destroy") + " that one first, or recheck your " + chalk.bold(".map ") + "and make " +
|
84 | "sure of what you're trying to do?\r\n")));
|
85 | return;
|
86 | }
|
87 | var updateMe = command_access.getUniverse().get(socket.db.where);
|
88 |
|
89 | var newPassage = eval("command_access.getUniverse()."+direction);
|
90 | if ((updateMe.passages & newPassage) === newPassage) {
|
91 | socket.write(chalk.yellow("You cannot create a passage that already exists!\r\n"));
|
92 | return;
|
93 | }
|
94 | updateMe.passages += newPassage;
|
95 | if (!command_access.getUniverse().update(updateMe)) {
|
96 | socket.write("You should have been able to create a passage to " +
|
97 | name + ". However, that didn't work. Please let an " +
|
98 | command_access.ranks.list[
|
99 | command_access.ranks.list.length - 1
|
100 | ] + " know about this!\r\n");
|
101 | } else {
|
102 | socket.write(chalk.green(":: ") + "You just created a passage to " + chalk.bold(name) + ".\r\n");
|
103 | }
|
104 | } else {
|
105 |
|
106 | var opposite = command_access.getUniverse().opposites[Math.log(
|
107 | eval("command_access.getUniverse()."+direction)
|
108 | ) / Math.log(2)][1];
|
109 | if (!command_access.getUniverse().create(target, opposite)) {
|
110 |
|
111 | socket.write("You should have been able to dig towards " +
|
112 | direction + " from here and create a place called " + name +
|
113 | ". However, that didn't work. Please let an " +
|
114 | command_access.ranks.list[
|
115 | command_access.ranks.list.length - 1
|
116 | ] +
|
117 | " know about this!\r\n");
|
118 | return;
|
119 | }
|
120 | done = command_access.getUniverse().get(target);
|
121 | if (done !== null) done.name = name;
|
122 | if (!command_access.getUniverse().update(done)) {
|
123 |
|
124 | socket.write("You dug, but you weren't able to make the new place " +
|
125 | "how you wanted it to be. It's not your fault... and you " +
|
126 | "should warn an " +
|
127 | command_access.ranks.list[
|
128 | command_access.ranks.list.length - 1
|
129 | ] +
|
130 | "about this!\r\n");
|
131 | return;
|
132 | }
|
133 | socket.write(chalk.green(":: ") + "You dig towards " + chalk.bold(direction) +
|
134 | ", and create a new place called " + chalk.bold(name) + ".\r\n");
|
135 | }
|
136 |
|
137 | command_access.saveUniverse();
|
138 | }
|
139 | }
|