admin.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var crypto = require('crypto');
  2. class Admin {
  3. help(room, mobile, input) {
  4. if(input.join(" ").trim().length == 0) {
  5. libs.Output.toMobile(mobile.id, "Admin commands:");
  6. libs.Output.toMobile(mobile.id, "reload, hash, time, permission");
  7. return false;
  8. }
  9. var topic = input.join(" ").trim();
  10. switch(topic) {
  11. case "reload":
  12. libs.Output.toMobile(mobile.id, "* reload world - reloads the core processing");
  13. libs.Output.toMobile(mobile.id, "* reload builder - reloads the building tools");
  14. libs.Output.toMobile(mobile.id, "* reload libs - reloads common utilities like output");
  15. libs.Output.toMobile(mobile.id, "* reload admin - reloads the administration tools");
  16. libs.Output.toMobile(mobile.id, "* reload commands - reloads the collection of player input commands");
  17. return false;
  18. case "hash":
  19. libs.Output.toMobile(mobile.id, "hash [input to hash] - will generate a sha256-base64 hash");
  20. return true;
  21. case "time":
  22. libs.Output.toMobile(mobile.id, "time - displays the current server time tick (0-640)");
  23. libs.Output.toMobile(mobile.id, "time [value] - sets the server to this time tick");
  24. return true;
  25. case "permission":
  26. libs.Output.toMobile(mobile.id, "permission see [playerName] - lists the permissions of the player");
  27. libs.Output.toMobile(mobile.id, "permission add [playerName] [admin|builder] - adds the permission to a player");
  28. libs.Output.toMobile(mobile.id, "permission remove [playerName] [admin|builder] - removes a permission from a player");
  29. return true;
  30. }
  31. }
  32. reload(room, mobile, input) {
  33. switch(input[0]) {
  34. case "world":
  35. return this.reloadWorld(room, mobile, input);
  36. case "builder":
  37. return this.reloadBuilder(room, mobile, input);
  38. case "libs":
  39. return this.reloadLibs(room, mobile, input);
  40. case "admin":
  41. return this.reloadAdmin(room, mobile, input);
  42. case "commands":
  43. return this.reloadCommands(room, mobile, input);
  44. //default:
  45. //return this.help(room, mobile, ["reload"]);
  46. }
  47. }
  48. reloadWorld(room, mobile, input) {
  49. var tempTime = world.time;
  50. var tempSession = session;
  51. libs.Output.toMobile(mobile.id,"Preparing to reload world.");
  52. try {
  53. delete require.cache[require.resolve('./world')];
  54. world = require('./world');
  55. world.time = tempTime;
  56. session = tempSession;
  57. libs.Output.toMobile(mobile.id,"World reloaded");
  58. libs.Output.toServer("World reloaded.\n");
  59. } catch (error) {
  60. libs.Output.toMobile(mobile.id, "Error when attempting to reload world.");
  61. libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n");
  62. }
  63. return true;
  64. }
  65. reloadBuilder(room, mobile, input) {
  66. libs.Output.toMobile(mobile.id,"Preparing to reload build tools.");
  67. try {
  68. delete require.cache[require.resolve('./builder')];
  69. builder = require('./builder');
  70. libs.Output.toMobile(mobile.id,"Build tools reloaded");
  71. libs.Output.toServer("Build tools reloaded.\n");
  72. } catch (error) {
  73. libs.Output.toMobile(mobile.id, "Error when attempting to reload build tools.");
  74. libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n");
  75. }
  76. return true;
  77. }
  78. reloadLibs(room, mobile, input) {
  79. libs.Output.toMobile(mobile.id,"Preparing to reload common libraries.");
  80. try {
  81. delete require.cache[require.resolve('./libs')];
  82. libs = require('./libs');
  83. for(var library in libs){
  84. libs.Output.toMobile(mobile.id, "\t" + library);
  85. delete require.cache[require.resolve('./libs/'+library.toLowerCase()+".js")];
  86. libs[library] = require('./libs/' + library.toLowerCase() + ".js");
  87. }
  88. libs.Output.toMobile(mobile.id,"Common libraries reloaded");
  89. libs.Output.toServer("Common libraries reloaded.\n");
  90. } catch (error) {
  91. libs.Output.toMobile(mobile.id, "Error when attempting to reload common libraries.");
  92. libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n");
  93. }
  94. return true;
  95. }
  96. reloadCommands(room, mobile, input) {
  97. libs.Output.toMobile(mobile.id,"Preparing to reload commands.");
  98. try {
  99. delete require.cache[require.resolve('./commands')];
  100. commands = require('./commands');
  101. for(var commandClass in commands){
  102. libs.Output.toMobile(mobile.id, "\t" + commandClass);
  103. delete require.cache[require.resolve('./commands/'+commandClass.toLowerCase()+".js")];
  104. commands[commandClass] = require('./commands/' + commandClass.toLowerCase() + ".js");
  105. }
  106. libs.Output.toMobile(mobile.id,"Commands reloaded");
  107. libs.Output.toServer("Commands reloaded.\n");
  108. } catch (error) {
  109. libs.Output.toMobile(mobile.id, "Error when attempting to reload commands.");
  110. libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n");
  111. }
  112. return true;
  113. }
  114. reloadAdmin(room, mobile, input) {
  115. libs.Output.toMobile(mobile.id,"Preparing to reload admin tools.");
  116. try {
  117. delete require.cache[require.resolve('./admin')];
  118. admin = require('./admin');
  119. libs.Output.toMobile(mobile.id,"Admin tools reloaded.");
  120. libs.Output.toServer("Admin tools reloaded.\n");
  121. } catch (error) {
  122. libs.Output.toMobile(mobile.id, "Error when attempting to reload admin tools.");
  123. libs.Output.toMobile(mobile.id, error.code + " " + error.stack +"\n");
  124. }
  125. return true;
  126. }
  127. hash(room, mobile, input) {
  128. var hash = world.mud_generateHash(input.join(" "));
  129. libs.Output.toMobile(mobile.id,"hash: " + hash);
  130. return true;
  131. }
  132. time(room, mobile, input) {
  133. if(input.join(" ") == null || input.join(" ") == "") {
  134. libs.Output.toMobile(mobile.id,"The time is " + world.time);
  135. return true;
  136. } else {
  137. var newTime = parseInt(input.join(" "));
  138. world.time = newTime;
  139. libs.Output.toMobile(mobile.id,"The time is " + world.time);
  140. return true;
  141. }
  142. }
  143. permission(room, mobile, input) {
  144. switch(input.shift()) {
  145. case "add":
  146. this.mud_addPermission(room, mobile, input);
  147. return true;
  148. case "remove":
  149. this.mud_removePermission(room, mobile, input);
  150. return true;
  151. case "see":
  152. this.mud_seePermission(room, mobile, input);
  153. return true;
  154. default:
  155. return this.help(room, mobile, ["permission"]);
  156. }
  157. }
  158. mud_addPermission(room, mobile, input) {
  159. var playerIds = [];
  160. for(var index in session.players) {
  161. var player = session.players[index];
  162. playerIds.push(player.id);
  163. }
  164. var username = input.join(" ").trim().toLowerCase();
  165. var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles);
  166. if(player != null) {
  167. var role = input[1].trim();
  168. if(player.roles.indexOf(role) == -1) {
  169. player.roles.push(role);
  170. libs.Output.toMobile(mobile.id, "Adding permission '"+ role + "' to player '"+player.name+"'");
  171. libs.Output.toMobile(player.id, "You now have '" + role + "' access.");
  172. player.save();
  173. world.mud_handleInput(player.roomId, player.id, ["help", role]);
  174. }
  175. libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:");
  176. for(var index in player.roles) {
  177. libs.Output.toMobile(mobile.id, "\t"+player.roles[index]);
  178. }
  179. return true;
  180. }
  181. libs.Output.toMobile(mobile.id, "That player is not online.");
  182. return true;
  183. }
  184. mud_seePermission(room, mobile, input) {
  185. var playerIds = [];
  186. for(var index in session.players) {
  187. var player = session.players[index];
  188. playerIds.push(player.id);
  189. }
  190. var username = input.join(" ").trim().toLowerCase();
  191. var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles);
  192. if(player != null) {
  193. libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:");
  194. for(var index in player.roles) {
  195. libs.Output.toMobile(mobile.id, "\t"+player.roles[index]);
  196. }
  197. return true;
  198. }
  199. libs.Output.toMobile(mobile.id, "That player is not online.");
  200. return true;
  201. }
  202. mud_removePermission(room, mobile, input) {
  203. var playerIds = [];
  204. for(var index in session.players) {
  205. var player = session.players[index];
  206. playerIds.push(player.id);
  207. }
  208. var username = input.join(" ").trim().toLowerCase();
  209. var player = libs.Utilities.getSpecific(username, playerIds, session.mobiles);
  210. if(player != null) {
  211. var role = input[1].trim();
  212. if(player.roles.indexOf(role) != -1) {
  213. player.roles.splice(player.roles.indexOf(role), 1);
  214. libs.Output.toMobile(mobile.id, "Removing permission '"+ role + "' from player '"+player.name+"'");
  215. libs.Output.toMobile(player.id, "You no longer have '" + role + "' access.");
  216. player.save();
  217. }
  218. libs.Output.toMobile(mobile.id, "Player '"+player.name+"' roles:");
  219. for(var index in player.roles) {
  220. libs.Output.toMobile(mobile.id, "\t"+player.roles[index]);
  221. }
  222. return true;
  223. }
  224. libs.Output.toMobile(mobile.id, "That player is not online.");
  225. return true;
  226. }
  227. }
  228. module.exports = new Admin();