world.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. if(typeof mobileObj.mud_enterRoom == "function") {
  274. libs.Output.toRoom(roomName, mobileObj.mud_enterRoom(direction), mobileObj);
  275. } else {
  276. if(direction != null) {
  277. libs.Output.toRoom(roomName, this.name + " " + direction + ".");
  278. } else {
  279. libs.Output.toRoom(roomName, this.name + " enters the room.", mobileObj);
  280. }
  281. }
  282. this.mud_addMobileToRoom(roomName, mobileId);
  283. this.mud_handleInput(roomName, mobileId, ["look"]);
  284. }
  285. mud_moveMobileFromRoom(roomName, mobileId, direction) {
  286. var mobileObj = this.mud_getMobile(mobileId);
  287. this.mud_removeMobileFromRoom(roomName, mobileId);
  288. if(typeof mobileObj.mud_leaveRoom == "function") {
  289. libs.Output.toRoom(roomName, mobileObj.mud_leaveRoom(direction), mobileObj);
  290. } else {
  291. if(direction != null) {
  292. libs.Output.toRoom(roomName, this.name + " " + direction + ".");
  293. } else {
  294. libs.Output.toRoom(roomName, this.name + " leaves.", mobileObj);
  295. }
  296. }
  297. }
  298. mud_getItem(itemId) {
  299. return session.items[itemId];
  300. }
  301. mud_getMobile(mobileId) {
  302. return session.mobiles[mobileId];
  303. }
  304. mud_getRoom(roomName) {
  305. return session.rooms[roomName];
  306. }
  307. mud_addMobileToRoom(roomName, mobileId) {
  308. var mobileObj = this.mud_getMobile(mobileId);
  309. var roomObj = this.mud_getRoom(roomName);
  310. mobileObj.roomId = roomName;
  311. roomObj.mobiles.push(mobileId);
  312. }
  313. mud_removeMobileFromRoom(roomName, mobileId) {
  314. var room = this.mud_getRoom(roomName);
  315. room.mobiles.splice(room.mobiles.indexOf(mobileId), 1);
  316. }
  317. mud_addItemToRoom(roomName, itemId) {
  318. var room = this.mud_getRoom(roomName);
  319. if(room.items.indexOf(itemId) == -1) {
  320. room.items.push(itemId);
  321. }
  322. }
  323. mud_addItemToMobile(mobileId, itemId) {
  324. var mobile = this.mud_getMobile(mobileId);
  325. if(mobile.items.indexOf(itemId) == -1) {
  326. mobile.items.push(itemId);
  327. }
  328. }
  329. mud_removeItemFromMobile(mobileId, itemId) {
  330. var mobile = session.mobiles[mobileId];
  331. if(mobile.items.indexOf(itemId) != -1) {
  332. mobile.items.splice(mobile.items.indexOf(itemId), 1);
  333. }
  334. }
  335. mud_removeItemFromRoom(roomId, itemId) {
  336. var room = session.rooms[roomId];
  337. room.items.splice(room.items.indexOf(itemId), 1);
  338. }
  339. mud_destroyItem(itemId) {
  340. delete session.items[itemId];
  341. }
  342. mud_destroyMobile(mobileId) {
  343. delete session.mobiles[mobileId];
  344. }
  345. mud_mobileHasRole(mobileId, role) {
  346. var mobile = session.mobiles[mobileId];
  347. if(mobile.roles != null) {
  348. return mobile.roles.indexOf(role) != -1;
  349. }
  350. return false;
  351. }
  352. mud_generateHash(input) {
  353. return crypto.createHash('sha256').update(input).digest("base64");
  354. }
  355. mud_getUserData(username) {
  356. var playerFilename = config.playerDataPath+ "/"+username.toLowerCase().trim()+".json";
  357. var files = glob.sync(playerFilename, {} );
  358. if(files.length == 1) {
  359. var fileData = fs.readFileSync(files[0], "utf8");
  360. return JSON.parse(fileData);
  361. }
  362. }
  363. mud_isValidLogin(userData, password) {
  364. var hash = crypto.createHash('sha256').update(userData.username.toLowerCase().trim() + password).digest("base64");
  365. if(userData.password == hash) {
  366. return true;
  367. }
  368. return false;
  369. }
  370. mud_playerLoad(login, userData) {
  371. for(var activePlayerId in session.players) {
  372. var activePlayer = session.players[activePlayerId];
  373. if(activePlayer.password == userData.password) {
  374. libs.Output.toMobile(activePlayer.id, "You are being booted by another login.");
  375. var oldLogin = activePlayer.login.reconnect(login);
  376. login.player = activePlayer;
  377. login.setConnectionTimeout(activePlayer.connectionTimeout);
  378. return;
  379. }
  380. }
  381. var player = builder.mud_spawnMobile("PlayerMobile");
  382. player.login = login;
  383. player.login.setConnectionTimeout(userData.connectionTimeout);
  384. player.name = userData.username;
  385. player.description = userData.description;
  386. player.password = userData.password;
  387. player.roomId = userData.roomName;
  388. player.hintColor = userData.hintColor;
  389. player.terminalWidthPreference = userData.terminalWidthPreference;
  390. player.connectionTimeout = userData.connectionTimeout;
  391. player.keywords.push(player.name.toLowerCase());
  392. player.roles = userData.roles;
  393. player.title = userData.title;
  394. player.pubKeys = userData.pubKeys;
  395. for(var itemIdIndex in userData.items) {
  396. var loadedItemData = userData.items[itemIdIndex];
  397. var loadedItem = builder.mud_spawnItem(loadedItemData.classname);
  398. if(typeof loadedItem.mud_loadItem == "function") {
  399. loadedItem.mud_loadItem(loadedItemData);
  400. }
  401. this.mud_addItemToMobile(player.id, loadedItem.id);
  402. }
  403. login.player = player;
  404. world.mud_attachPlayer(player);
  405. }
  406. mud_attachPlayer(player) {
  407. this.mud_addMobileToRoom(player.roomId, player.id);
  408. session.players[player.id] = player;
  409. var joinMessage = player.name + " joined the server.";
  410. libs.Output.toServer(joinMessage);
  411. libs.Output.worldToMobiles(joinMessage, player);
  412. this.mud_handleInput(player.roomId, player.id, ["look"]);
  413. }
  414. mud_playerSave(player) {
  415. var playerFilename = config.playerDataPath+ "/"+player.name.toLowerCase().trim()+".json";
  416. var userData = {};
  417. userData.username = player.name;
  418. userData.password = player.password,
  419. userData.roomName = session.rooms[player.roomId].constructor.name;
  420. userData.hintColor = player.hintColor;
  421. userData.description = player.description;
  422. userData.terminalWidthPreference = player.terminalWidthPreference;
  423. userData.connectionTimeout = player.connectionTimeout;
  424. userData.roles = player.roles;
  425. userData.title = player.title;
  426. userData.pubKeys = player.pubKeys;
  427. var items = [];
  428. for(var itemIdIndex in player.items) {
  429. var savedItem = {};
  430. if(typeof session.items[player.items[itemIdIndex]].mud_saveItem == "function") {
  431. savedItem = session.items[player.items[itemIdIndex]].mud_saveItem();
  432. }
  433. savedItem.classname = session.items[player.items[itemIdIndex]].constructor.name;
  434. items.push(savedItem);
  435. }
  436. userData.items = items;
  437. fs.writeFile(playerFilename, JSON.stringify(userData, null, "\t"), (err) => {
  438. if (err) {
  439. throw err;
  440. }
  441. libs.Output.toServer("saved player data " + playerFilename);
  442. });
  443. }
  444. mud_playerQuit(player) {
  445. this.mud_playerSave(player);
  446. var playerId = player.id;
  447. for(var itemIdIndex in player.items) {
  448. var item = session.items[player.items[itemIdIndex]];
  449. this.mud_removeItemFromMobile(player.id, item.id);
  450. this.mud_destroyItem(item.id);
  451. }
  452. this.mud_removeMobileFromRoom(player.roomId, player.id);
  453. delete session.players[player.id];
  454. this.mud_destroyMobile(playerId);
  455. libs.Output.toServer(player.name + " left the server.");
  456. libs.Output.worldToMobiles(player.name + " left the server.", player);
  457. return true;
  458. }
  459. }
  460. module.exports = new World();