Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.47 KB | None | 0 0
  1. var Logger = require('./Logger');
  2. var UserRoleEnum = require("../enum/UserRoleEnum");
  3.  
  4. var ErrorTextInvalidCommand = "ERROR: Unknown command, type /help for command list";
  5. var ErrorTextBadCommand = "ERROR: Bad command!";
  6.  
  7.  
  8. function PlayerCommand(gameServer, playerTracker) {
  9. this.gameServer = gameServer;
  10. this.playerTracker = playerTracker;
  11. }
  12.  
  13. module.exports = PlayerCommand;
  14.  
  15. PlayerCommand.prototype.writeLine = function (text) {
  16. this.gameServer.sendChatMessage(null, this.playerTracker, text);
  17. };
  18.  
  19. PlayerCommand.prototype.executeCommandLine = function (commandLine) {
  20. if (!commandLine) return;
  21. var command = commandLine;
  22. var args = "";
  23. var index = commandLine.indexOf(' ');
  24. if (index >= 0) {
  25. command = commandLine.slice(0, index);
  26. args = commandLine.slice(index + 1, commandLine.length);
  27. }
  28. command = command.trim().toLowerCase();
  29. if (command.length > 16) {
  30. this.writeLine(ErrorTextInvalidCommand);
  31. return;
  32. }
  33. for (var i = 0; i < command.length; i++) {
  34. var c = command.charCodeAt(i);
  35. if (c < 0x21 || c > 0x7F) {
  36. this.writeLine(ErrorTextInvalidCommand);
  37. return;
  38. }
  39. }
  40. if (!playerCommands.hasOwnProperty(command)) {
  41. this.writeLine(ErrorTextInvalidCommand);
  42. return;
  43. }
  44. var execute = playerCommands[command];
  45. if (typeof execute == 'function') {
  46. execute.bind(this)(args);
  47. } else {
  48. this.writeLine(ErrorTextBadCommand);
  49. }
  50. };
  51.  
  52. PlayerCommand.prototype.userLogin = function (ip, password) {
  53. if (!password) return null;
  54. password = password.trim();
  55. if (!password) return null;
  56. for (var i = 0; i < this.gameServer.userList.length; i++) {
  57. var user = this.gameServer.userList[i];
  58. if (user.password != password)
  59. continue;
  60. if (user.ip && user.ip != ip)
  61. continue;
  62. return user;
  63. }
  64. return null;
  65. };
  66.  
  67. var playerCommands = {
  68. id: function (args) {
  69. this.writeLine("ID: " + this.playerTracker.pID);
  70. },
  71. help: function (args) {
  72. if (this.playerTracker.userRole == UserRoleEnum.ADMIN || this.playerTracker.userRole == UserRoleEnum.MODER) {
  73. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  74. this.writeLine("/skin %shark - change skin");
  75. this.writeLine("/kill - self kill");
  76. this.writeLine("/help - this command list");
  77. this.writeLine("/mass - gives mass to yourself - Must be ADMIN or MODER");
  78. this.writeLine("/spawnmass - gives yourself spawnmass - Must be ADMIN");
  79. this.writeLine("/minion - gives yourself minions - Must be ADMIN or MODER");
  80. this.writeLine("/minion remove - removes all of your minions - Must be ADMIN or MODER");
  81. this.writeLine("/addbot - Adds Bots to the server - Must be ADMIN");
  82. this.writeLine("/shutdown - SHUTDOWNS THE SERVER - MUST BE ADMIN");
  83. this.writeLine("/status - Shows Status of the Server - Must be ADMIN or Moder");
  84. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  85. } else {
  86. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  87. this.writeLine("/skin %shark - change skin");
  88. this.writeLine("/kill - self kill");
  89. this.writeLine("/help - this command list");
  90. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  91. }
  92. },
  93. skin: function (args) {
  94. if (this.playerTracker.cells.length > 0) {
  95. this.writeLine("ERROR: Cannot change skin while player in game!");
  96. return;
  97. }
  98. var skinName = "";
  99. if (args) skinName = args.trim();
  100. this.playerTracker.setSkin(skinName);
  101. if (skinName == "")
  102. this.writeLine("Your skin was removed");
  103. else
  104. this.writeLine("Your skin set to " + skinName);
  105. },
  106. kill: function (args) {
  107. if (this.playerTracker.cells.length < 1) {
  108. this.writeLine("You cannot kill yourself, because you're still not joined to the game!");
  109. return;
  110. }
  111. while (this.playerTracker.cells.length > 0) {
  112. var cell = this.playerTracker.cells[0];
  113. this.gameServer.removeNode(cell);
  114. // replace with food
  115. var food = require('../entity/Food');
  116. food = new food(this.gameServer, null, cell.position, cell._size);
  117. food.setColor(cell.color);
  118. this.gameServer.addNode(food);
  119. }
  120. this.writeLine("You killed yourself");
  121. },
  122. mass: function (args) {
  123. if (this.playerTracker.userRole != UserRoleEnum.ADMIN && this.playerTracker.userRole != UserRoleEnum.MODER) {
  124. this.writeLine("ERROR: access denied!");
  125. return;
  126. }
  127. var mass = (args || "").trim();
  128. if (mass.length < 1) {
  129. this.writeLine("ERROR: missing mass argument!");
  130. return;
  131. }
  132. var size = Math.sqrt(mass * 100);
  133. for (var i in this.playerTracker.cells) {
  134. this.playerTracker.cells[i].setSize(size);
  135. }
  136. this.writeLine("Set mass of " + this.playerTracker._name + " to " + (size * size / 100).toFixed(3));
  137.  
  138. },
  139. spawnmass: function (args) {
  140. if (this.playerTracker.userRole != UserRoleEnum.ADMIN) {
  141. this.writeLine("ERROR: access denied!");
  142. return;
  143. }
  144. var mass = (args || "").trim();
  145. if (mass.length < 1) {
  146. this.writeLine("ERROR: missing mass argument!");
  147. return;
  148. }
  149. var size = Math.sqrt(mass * 100);
  150. this.playerTracker.spawnmass = size;
  151. this.writeLine("Set spawnmass of " + this.playerTracker._name + " to " + (size * size / 100).toFixed(3));
  152. },
  153. minion: function(args) {
  154. var add = (args || "").trim();
  155. if (this.playerTracker.userRole != UserRoleEnum.ADMIN && this.playerTracker.userRole != UserRoleEnum.MODER) {
  156. this.writeLine("ERROR: access denied!");
  157. return;
  158. }
  159. // Remove minions
  160. if (this.playerTracker.minionControl === true && add == "remove") {
  161. this.playerTracker.minionControl = false;
  162. this.playerTracker.miQ = 0;
  163. this.writeLine("Succesfully removed minions for " + this.playerTracker._name);
  164. // Add minions
  165. } else {
  166. this.playerTracker.minionControl = true;
  167. // Add minions for client
  168. if (isNaN(add)) add = 1;
  169.  
  170. for (var i = 0; i < add; i++) {
  171. this.gameServer.bots.addMinion(this.playerTracker);
  172. }
  173. this.writeLine("Added " + add + " minions for " + this.playerTracker._name);
  174. }
  175. },
  176. addbot: function(args) {
  177. var add = (args || "").trim();
  178. if (this.playerTracker.userRole != UserRoleEnum.ADMIN) {
  179. this.writeLine("ERROR: access denied!");
  180. return;
  181. }
  182. for (var i = 0; i < add; i++) {
  183. this.gameServer.bots.addBot();
  184. }
  185. Logger.warn(this.playerTracker.socket.remoteAddress + "ADDED " + add + " BOTS");
  186. this.writeLine("Added " + add + " Bots");
  187. },
  188. status: function(args) {
  189. if (this.playerTracker.userRole != UserRoleEnum.ADMIN && this.playerTracker.userRole != UserRoleEnum.MODER) {
  190. this.writeLine("ERROR: access denied!");
  191. return;
  192. }
  193. var ini = require('./ini.js');
  194. // Get amount of humans/bots
  195. var humans = 0,
  196. bots = 0;
  197. for (var i = 0; i < this.gameServer.clients.length; i++) {
  198. if ('_socket' in this.gameServer.clients[i]) {
  199. humans++;
  200. } else {
  201. bots++;
  202. }
  203. }
  204. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  205. this.writeLine("Connected players: " + this.gameServer.clients.length + "/" + this.gameServer.config.serverMaxConnections);
  206. this.writeLine("Players: " + humans + " - Bots: " + bots);
  207. this.writeLine("Server has been running for " + Math.floor(process.uptime() / 60) + " minutes");
  208. this.writeLine("Current memory usage: " + Math.round(process.memoryUsage().heapUsed / 1048576 * 10) / 10 + "/" + Math.round(process.memoryUsage().heapTotal / 1048576 * 10) / 10 + " mb");
  209. this.writeLine("Current game mode: " + this.gameServer.gameMode.name);
  210. this.writeLine("Current update time: " + this.gameServer.updateTimeAvg.toFixed(3) + " [ms] (" + ini.getLagMessage(this.gameServer.updateTimeAvg) + ")");
  211. this.writeLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  212. },
  213. login: function (args) {
  214. var password = (args || "").trim();
  215. if (password.length < 1) {
  216. this.writeLine("ERROR: missing password argument!");
  217. return;
  218. }
  219. var user = this.userLogin(this.playerTracker.socket.remoteAddress, password);
  220. if (!user) {
  221. this.writeLine("ERROR: login failed!");
  222. return;
  223. }
  224. Logger.write("LOGIN " + this.playerTracker.socket.remoteAddress + ":" + this.playerTracker.socket.remotePort + " as \"" + user.name + "\"");
  225. this.playerTracker.userRole = user.role;
  226. this.playerTracker.userAuth = user.name;
  227. this.writeLine("Login done as \"" + user.name + "\"");
  228. return;
  229. },
  230. logout: function (args) {
  231. if (this.playerTracker.userRole == UserRoleEnum.GUEST) {
  232. this.writeLine("ERROR: not logged in");
  233. return;
  234. }
  235. Logger.write("LOGOUT " + this.playerTracker.socket.remoteAddress + ":" + this.playerTracker.socket.remotePort + " as \"" + this.playerTracker.userAuth + "\"");
  236. this.playerTracker.userRole = UserRoleEnum.GUEST;
  237. this.playerTracker.userAuth = null;
  238. this.writeLine("Logout done");
  239. },
  240. shutdown: function (args) {
  241. if (this.playerTracker.userRole != UserRoleEnum.ADMIN) {
  242. this.writeLine("ERROR: access denied!");
  243. return;
  244. }
  245. Logger.warn("SHUTDOWN REQUEST FROM " + this.playerTracker.socket.remoteAddress + " as " + this.playerTracker.userAuth);
  246. process.exit(0);
  247. },
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement