world.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. var glob = require('glob'),
  2. fs = require('fs'),
  3. crypto = require('crypto');
  4. session = {
  5. "updateRate": 3000,
  6. "players": {},
  7. "rooms": {},
  8. "mobiles": {},
  9. "mobileIds": 1,
  10. "items": {},
  11. "itemIds": 1
  12. };
  13. builder = require('./builder');
  14. admin = require('./admin');
  15. commands = require('./commands');
  16. rooms = require(config.worldDataPath + '/rooms');
  17. items = require(config.worldDataPath + '/items');
  18. mobiles = require(config.worldDataPath + '/mobiles');
  19. class World {
  20. constructor() {
  21. this.time = 0;
  22. this.blacklist = ["constructor"];
  23. this.blacklistPrefix = "mud_";
  24. this.timerId = -1;
  25. }
  26. mud_load() {
  27. if(this.timerId != -1 ) {
  28. clearInterval(this.timerId);
  29. }
  30. session.updateRate = 3000;
  31. session.rooms = {};
  32. session.items = {};
  33. session.mobiles = {};
  34. session.players = {};
  35. session.mobileIds = 1;
  36. session.itemIds = 1;
  37. //populate rooms
  38. for(var roomName in rooms) {
  39. var room = builder.mud_spawnRoom(roomName);
  40. if(typeof room.mud_init == 'function') {
  41. room.mud_init();
  42. }
  43. }
  44. //populate mobiles
  45. for(var mobileName in mobiles) {
  46. if(mobileName == "PlayerMobile") {
  47. continue;
  48. }
  49. var mobile = builder.mud_spawnMobile(mobileName);
  50. if(typeof mobile.mud_init == 'function') {
  51. mobile.mud_init();
  52. } else {
  53. world.mud_destroyMobile(mobile.id);
  54. }
  55. }
  56. //populate items
  57. for(var itemName in items) {
  58. var item = builder.mud_spawnItem(itemName);
  59. if(typeof item.mud_init == 'function') {
  60. item.mud_init();
  61. } else {
  62. world.mud_destroyItem(item.id);
  63. }
  64. }
  65. this.timerId = setInterval(function() {
  66. world.mud_tick(Date.now());
  67. }, session.updateRate);
  68. }
  69. mud_handleInput(roomName, mobileId, input) {
  70. var room = this.mud_getRoom(roomName);
  71. var mobile = this.mud_getMobile(mobileId);
  72. var command = input.shift();
  73. if(command.indexOf(this.blacklistPrefix) != -1) {
  74. libs.Output.toMobile(mobile.id, "I didn't understand that.");
  75. return true;
  76. }
  77. if(this.blacklist.indexOf(command) != -1) {
  78. libs.Output.toMobile(mobile.id, "I didn't understand that.");
  79. return true;
  80. }
  81. if (world.mud_mobileHasRole(mobile.id, "admin") && typeof admin[command] == 'function') {
  82. try {
  83. if(admin[command](room, mobile, input)) {
  84. return true;
  85. }
  86. } catch (error) {
  87. libs.Output.toServerError("Exception in admin tools - ", error);
  88. libs.Output.toMobile(mobile.id, "The administration tools are misbehaving.");
  89. if(world.mud_mobileHasRole(mobile.id, "admin")) {
  90. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  91. }
  92. }
  93. }
  94. if (world.mud_mobileHasRole(mobile.id, "builder") && typeof builder[command] == 'function') {
  95. try {
  96. if(builder[command](room, mobile, input)) {
  97. return true;
  98. }
  99. } catch (error) {
  100. libs.Output.toServerError("Exception in builder tools - ", error);
  101. libs.Output.toMobile(mobile.id, "The build tools are misbehaving.");
  102. if(world.mud_mobileHasRole(mobile.id, "admin")) {
  103. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  104. }
  105. }
  106. }
  107. for (var itemIndex in mobile.items) {
  108. var otherItem = this.mud_getItem(mobile.items[itemIndex]);
  109. if(typeof otherItem[command] == 'function') {
  110. try {
  111. if(otherItem[command](room, mobile, input)) {
  112. return true;
  113. }
  114. } catch (error) {
  115. libs.Output.toServerError("Exception in mobile item: " + mobile.constructor.name + ":" +mobile.id+","+otherItem.constructor.name+":"+otherItem.id+" - ", error);
  116. libs.Output.toMobile(mobile.id, otherItem.name + " you are holding {error}glitches{/error} for a second.");
  117. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  118. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  119. }
  120. }
  121. }
  122. }
  123. if(typeof mobile[command] == 'function') {
  124. try {
  125. if(mobile[command](room, mobile, input)) {
  126. return true;
  127. }
  128. } catch (error) {
  129. libs.Output.toServerError("Exception in mobile: " + mobile.constructor.name + ":" +mobile.id+" - ", error);
  130. libs.Output.toMobile(mobile.id, "You feel funny and {error}not quite right{/error}.");
  131. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  132. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  133. }
  134. }
  135. }
  136. for (var itemIndex in room.items) {
  137. var otherItem = this.mud_getItem(room.items[itemIndex]);
  138. if(typeof otherItem[command] == 'function') {
  139. try {
  140. if(otherItem[command](room, mobile, input)) {
  141. return true;
  142. }
  143. } catch (error) {
  144. libs.Output.toServerError("Exception in room item: " + room.constructor.name + ","+otherItem.constructor.name+":"+otherItem.id+" - ", error);
  145. libs.Output.toMobile(mobile.id, otherItem.name + " here {error}glitches{/error} for a second.");
  146. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  147. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  148. }
  149. }
  150. }
  151. }
  152. for (var mobileIndex in room.mobiles) {
  153. var otherMobile = this.mud_getMobile(room.mobiles[mobileIndex]);
  154. if(typeof otherMobile[command] == 'function') {
  155. try {
  156. if(otherMobile[command](room, mobile, input)) {
  157. return true;
  158. }
  159. } catch (error) {
  160. libs.Output.toServerError("Exception in room mobile: " + room.constructor.name + "," + otherMobile.constructor.name +":"+otherMobile.id+" - ", error);
  161. libs.Output.toMobile(mobile.id, otherMobile.name + " {error}staggers{/error} in and out of reality.");
  162. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  163. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  164. }
  165. }
  166. }
  167. }
  168. if(typeof room[command] == 'function') {
  169. try {
  170. if(room[command](room, mobile, input)) {
  171. return true;
  172. }
  173. } catch (error) {
  174. libs.Output.toServerError("Exception in room: " + room.id + " - ", error);
  175. libs.Output.toMobile(mobile.id, "You see strange {error}glitch errors{/error} in the surrounding area.");
  176. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  177. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  178. }
  179. }
  180. }
  181. for(var commanderClass in commands) {
  182. var commander = new commands[commanderClass]();
  183. if (typeof commander[command] == 'function') {
  184. try {
  185. if(commander[command](room, mobile, input)) {
  186. return true;
  187. }
  188. } catch (error) {
  189. libs.Output.toServerError("Exception in command: " + commanderClass + " - ", error);
  190. libs.Output.toMobile(mobile.id, commander.mud_getErrorMessage());
  191. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  192. libs.Output.toMobile(mobile.id, error.code + " " + error.stack + "\n");
  193. }
  194. }
  195. }
  196. }
  197. libs.Output.toMobile(mobile.id, "I didn't understand that.");
  198. return true;
  199. }
  200. mud_tick(time) {
  201. try {
  202. for(var roomName in session.rooms) {
  203. var room = this.mud_getRoom(roomName);
  204. if(typeof room.mud_tick == 'function') {
  205. room.mud_tick(time);
  206. }
  207. }
  208. } catch (error) {
  209. libs.Output.worldToMobiles("The flow of time seems to {error}stutter{/error} a second.");
  210. libs.Output.toServerError("Room tick error", error);
  211. }
  212. try {
  213. for(var mobileId in session.mobiles) {
  214. var mobile = this.mud_getMobile(mobileId);
  215. if(mobile == null) {
  216. console.log("null mobile", mobile);
  217. continue;
  218. }
  219. if(typeof mobile.mud_tick == 'function') {
  220. mobile.mud_tick(time);
  221. }
  222. }
  223. } catch (error) {
  224. libs.Output.worldToMobiles("Lifeforms in the universe {error}skip{/error} a unified heartbeat.");
  225. libs.Output.toServerError("Mobile tick error", error);
  226. }
  227. try {
  228. for(var itemId in session.items) {
  229. var item = this.mud_getItem(itemId);
  230. if(item == null) {
  231. console.log("null item", item);
  232. continue;
  233. }
  234. if(typeof item.mud_tick == 'function') {
  235. item.mud_tick(time);
  236. }
  237. }
  238. } catch (error) {
  239. libs.Output.worldToMobiles("Items everywhere {error}flicker{/error} out of reality for a second.");
  240. libs.Output.toServerError("Item tick error", error);
  241. }
  242. this.time++;
  243. this.time %= 640;
  244. switch(this.time) {
  245. case 0:
  246. //text must include dawn
  247. libs.Output.worldToMobiles("{global}Dawn breaks as the sun begins to rise.{/global}");
  248. break;
  249. case 160:
  250. //text must include midday
  251. libs.Output.worldToMobiles("{global}The sky is a clear, crisp cerulean to hold the bright midday sun.{/global}");
  252. break;
  253. case 320:
  254. //text must include twilight
  255. libs.Output.worldToMobiles("{global}Twilight creeps in slowly as the sun begins to set.{/global}");
  256. break;
  257. case 480:
  258. //text must include midnight
  259. libs.Output.worldToMobiles("{global}Stars twinkle in the midnight sky.{/global}");
  260. break;
  261. }
  262. }
  263. mud_moveMobile(mobileId, sourceRoom, destinationRoom, outDirection, inDirection) {
  264. if(session.rooms[destinationRoom] == null) {
  265. libs.Output.toMobile(mobileId, destinationRoom + " doesn't seem to exist yet.");
  266. return;
  267. }
  268. this.mud_moveMobileFromRoom(sourceRoom, mobileId, outDirection);
  269. this.mud_moveMobileToRoom(destinationRoom, mobileId, inDirection);
  270. }
  271. mud_moveMobileToRoom(roomName, mobileId, direction) {
  272. var mobileObj = this.mud_getMobile(mobileId);
  273. libs.Output.toRoom(roomName, mobileObj.mud_enterRoom(direction), mobileObj);
  274. this.mud_addMobileToRoom(roomName, mobileId);
  275. this.mud_handleInput(roomName, mobileId, ["look"]);
  276. }
  277. mud_moveMobileFromRoom(roomName, mobileId, direction) {
  278. var mobileObj = this.mud_getMobile(mobileId);
  279. this.mud_removeMobileFromRoom(roomName, mobileId);
  280. libs.Output.toRoom(roomName, mobileObj.mud_leaveRoom(direction), mobileObj);
  281. }
  282. mud_getItem(itemId) {
  283. return session.items[itemId];
  284. }
  285. mud_getMobile(mobileId) {
  286. return session.mobiles[mobileId];
  287. }
  288. mud_getRoom(roomName) {
  289. return session.rooms[roomName];
  290. }
  291. mud_addMobileToRoom(roomName, mobileId) {
  292. var mobileObj = this.mud_getMobile(mobileId);
  293. var roomObj = this.mud_getRoom(roomName);
  294. mobileObj.roomId = roomName;
  295. roomObj.mobiles.push(mobileId);
  296. }
  297. mud_removeMobileFromRoom(roomName, mobileId) {
  298. var room = this.mud_getRoom(roomName);
  299. room.mobiles.splice(room.mobiles.indexOf(mobileId), 1);
  300. }
  301. mud_addItemToRoom(roomName, itemId) {
  302. var room = this.mud_getRoom(roomName);
  303. if(room.items.indexOf(itemId) == -1) {
  304. room.items.push(itemId);
  305. }
  306. }
  307. mud_addItemToMobile(mobileId, itemId) {
  308. var mobile = this.mud_getMobile(mobileId);
  309. if(mobile.items.indexOf(itemId) == -1) {
  310. mobile.items.push(itemId);
  311. }
  312. }
  313. mud_removeItemFromMobile(mobileId, itemId) {
  314. var mobile = session.mobiles[mobileId];
  315. if(mobile.items.indexOf(itemId) != -1) {
  316. mobile.items.splice(mobile.items.indexOf(itemId), 1);
  317. }
  318. }
  319. mud_removeItemFromRoom(roomId, itemId) {
  320. var room = session.rooms[roomId];
  321. room.items.splice(room.items.indexOf(itemId), 1);
  322. }
  323. mud_destroyItem(itemId) {
  324. delete session.items[itemId];
  325. }
  326. mud_destroyMobile(mobileId) {
  327. delete session.mobiles[mobileId];
  328. }
  329. mud_mobileHasRole(mobileId, role) {
  330. var mobile = session.mobiles[mobileId];
  331. if(mobile.roles != null) {
  332. return mobile.roles.indexOf(role) != -1;
  333. }
  334. return false;
  335. }
  336. mud_generateHash(input) {
  337. return crypto.createHash('sha256').update(input).digest("base64");
  338. }
  339. mud_getUserData(username) {
  340. var playerFilename = config.playerDataPath+ "/"+username.toLowerCase().trim()+".json";
  341. var files = glob.sync(playerFilename, {} );
  342. if(files.length == 1) {
  343. var fileData = fs.readFileSync(files[0], "utf8");
  344. return JSON.parse(fileData);
  345. }
  346. }
  347. mud_isValidLogin(userData, password) {
  348. var hash = crypto.createHash('sha256').update(userData.username.toLowerCase().trim() + password).digest("base64");
  349. if(userData.password == hash) {
  350. return true;
  351. }
  352. return false;
  353. }
  354. mud_playerLoad(login, userData) {
  355. //TODO determine if this player is already playing and replace
  356. for(var activePlayerId in session.players) {
  357. var activePlayer = session.players[activePlayerId];
  358. if(activePlayer.password == userData.password) {
  359. libs.Output.toMobile(activePlayer.id, "You are being booted by another login.");
  360. var oldLogin = activePlayer.login.reconnect(login);
  361. login.player = activePlayer;
  362. login.setConnectionTimeout(activePlayer.connectionTimeout);
  363. return;
  364. }
  365. }
  366. var player = builder.mud_spawnMobile("PlayerMobile");
  367. player.login = login;
  368. player.login.setConnectionTimeout(userData.connectionTimeout);
  369. player.name = userData.username;
  370. player.description = userData.description;
  371. player.password = userData.password;
  372. player.roomId = userData.roomName;
  373. player.hintColor = userData.hintColor;
  374. player.terminalWidthPreference = userData.terminalWidthPreference;
  375. player.connectionTimeout = userData.connectionTimeout;
  376. player.keywords.push(player.name.toLowerCase());
  377. player.roles = userData.roles;
  378. player.title = userData.title;
  379. player.pubKeys = userData.pubKeys;
  380. for(var i = 0; i < userData.items.length; i++) {
  381. var itemName = userData.items[i];
  382. this.mud_addItemToMobile(player.id, builder.mud_spawnItem(itemName).id);
  383. }
  384. login.player = player;
  385. world.mud_attachPlayer(player);
  386. }
  387. mud_attachPlayer(player) {
  388. this.mud_addMobileToRoom(player.roomId, player.id);
  389. session.players[player.id] = player;
  390. var joinMessage = player.name + " joined the server.";
  391. libs.Output.toServer(joinMessage);
  392. libs.Output.worldToMobiles(joinMessage, player);
  393. this.mud_handleInput(player.roomId, player.id, ["look"]);
  394. }
  395. mud_playerSave(player) {
  396. var playerFilename = config.playerDataPath+ "/"+player.name.toLowerCase().trim()+".json";
  397. var userData = {};
  398. userData.username = player.name;
  399. userData.password = player.password,
  400. userData.roomName = session.rooms[player.roomId].constructor.name;
  401. userData.hintColor = player.hintColor;
  402. userData.description = player.description;
  403. userData.terminalWidthPreference = player.terminalWidthPreference;
  404. userData.connectionTimeout = player.connectionTimeout;
  405. userData.roles = player.roles;
  406. userData.title = player.title;
  407. userData.pubKeys = player.pubKeys;
  408. var items = [];
  409. for(var itemIdIndex in player.items) {
  410. items.push(session.items[player.items[itemIdIndex]].constructor.name);
  411. }
  412. userData.items = items;
  413. fs.writeFile(playerFilename, JSON.stringify(userData, null, "\t"), (err) => {
  414. if (err) {
  415. throw err;
  416. }
  417. libs.Output.toServer("saved player data " + playerFilename);
  418. });
  419. }
  420. mud_playerQuit(player) {
  421. this.mud_playerSave(player);
  422. var playerId = player.id;
  423. for(var itemIdIndex in player.items) {
  424. var item = session.items[player.items[itemIdIndex]];
  425. this.mud_removeItemFromMobile(player.id, item.id);
  426. this.mud_destroyItem(item.id);
  427. }
  428. this.mud_removeMobileFromRoom(player.roomId, player.id);
  429. delete session.players[player.id];
  430. this.mud_destroyMobile(playerId);
  431. libs.Output.toServer(player.name + " left the server.");
  432. libs.Output.worldToMobiles(player.name + " left the server.", player);
  433. return true;
  434. }
  435. }
  436. module.exports = new World();