You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

483 lines
9.2 KiB

  1. let roles = require('../../config/roles');
  2. let atlas = require('../../world/atlas');
  3. let generator = require('../../items/generator');
  4. let random = require('../../misc/random');
  5. let configSlots = require('../../items/config/slots');
  6. let io = require('../../security/io');
  7. let factions = require('../../config/factions');
  8. let connections = require('../../security/connections');
  9. let commandRoles = {
  10. //Regular players
  11. join: 0,
  12. leave: 0,
  13. unEq: 0,
  14. //Mods
  15. mute: 5,
  16. unmute: 5,
  17. //Admin
  18. getItem: 10,
  19. getGold: 10,
  20. setLevel: 10,
  21. godMode: 10,
  22. clearInventory: 10,
  23. completeQuests: 10,
  24. getReputation: 10,
  25. loseReputation: 10,
  26. setStat: 10,
  27. die: 10,
  28. getXp: 10
  29. };
  30. let localCommands = [
  31. 'join',
  32. 'leave',
  33. 'mute',
  34. 'unmute'
  35. ];
  36. module.exports = {
  37. customChannels: [],
  38. roleLevel: null,
  39. init: function (blueprint) {
  40. if (this.customChannels) {
  41. this.customChannels = this.customChannels
  42. .filter((c, i) => (this.customChannels.indexOf(c) === i));
  43. }
  44. this.roleLevel = roles.getRoleLevel(this.obj);
  45. },
  46. onBeforeChat: function (msg) {
  47. let messageText = msg.message;
  48. if (messageText[0] !== '/')
  49. return;
  50. messageText = messageText.substr(1).split(' ');
  51. let actionName = messageText.splice(0, 1)[0].toLowerCase();
  52. actionName = Object.keys(commandRoles).find(a => (a.toLowerCase() === actionName));
  53. if (!actionName)
  54. return;
  55. else if (this.roleLevel < commandRoles[actionName])
  56. return;
  57. msg.ignore = true;
  58. let config = {};
  59. if ((messageText.length === 1) && (messageText[0].indexOf('=') === -1))
  60. config = messageText[0];
  61. else {
  62. messageText.forEach(function (m) {
  63. m = m.split('=');
  64. config[m[0]] = m[1];
  65. });
  66. }
  67. if (localCommands.indexOf(actionName) > -1)
  68. this[actionName].call(this, config);
  69. else {
  70. atlas.performAction(this.obj, {
  71. cpn: 'social',
  72. method: actionName,
  73. data: config
  74. });
  75. }
  76. },
  77. //actions
  78. join: function (value) {
  79. if (typeof (value) !== 'string')
  80. return;
  81. value = value.split(' ').join('');
  82. if (value.lengh === 0)
  83. return;
  84. let obj = this.obj;
  85. let channels = obj.auth.customChannels;
  86. if (!channels.some(c => (c === value)))
  87. channels.push(value);
  88. else
  89. return;
  90. channels.push(value);
  91. let charname = obj.auth.charname;
  92. io.set({
  93. ent: charname,
  94. field: 'customChannels',
  95. value: JSON.stringify(channels)
  96. });
  97. obj.socket.emit('events', {
  98. onGetMessages: [{
  99. messages: [{
  100. class: 'color-yellowB',
  101. message: 'joined channel: ' + value,
  102. type: 'info'
  103. }]
  104. }]
  105. });
  106. obj.socket.emit('event', {
  107. event: 'onJoinChannel',
  108. data: value
  109. });
  110. },
  111. leave: function (value) {
  112. if (typeof (value) !== 'string')
  113. return;
  114. let obj = this.obj;
  115. let channels = obj.auth.customChannels;
  116. if (!channels.some(c => (c === value))) {
  117. obj.socket.emit('events', {
  118. onGetMessages: [{
  119. messages: [{
  120. class: 'color-redA',
  121. message: 'you are not currently in that channel',
  122. type: 'info'
  123. }]
  124. }]
  125. });
  126. return;
  127. }
  128. let channels = obj.auth.customChannels;
  129. channels.spliceWhere(c => (c === value));
  130. let charname = obj.auth.charname;
  131. io.set({
  132. ent: charname,
  133. field: 'customChannels',
  134. value: JSON.stringify(channels)
  135. });
  136. obj.socket.emit('event', {
  137. event: 'onLeaveChannel',
  138. data: value
  139. });
  140. this.obj.socket.emit('events', {
  141. onGetMessages: [{
  142. messages: [{
  143. class: 'color-yellowB',
  144. message: 'left channel: ' + value,
  145. type: 'info'
  146. }]
  147. }]
  148. });
  149. },
  150. isInChannel: function (character, channel) {
  151. return character.auth.customChannels.some(c => (c === channel));
  152. },
  153. unEq: function () {
  154. let eq = this.obj.equipment;
  155. Object.keys(eq.eq).forEach(function (slot) {
  156. eq.unequip(eq.eq[slot]);
  157. });
  158. },
  159. mute: function (target, reason) {
  160. if (typeof (target) === 'object') {
  161. let keys = Object.keys(target);
  162. target = keys[0];
  163. reason = keys[1];
  164. }
  165. if (target === this.obj.name)
  166. return;
  167. let o = connections.players.find(o => (o.name === target));
  168. if (!o)
  169. return;
  170. let role = roles.getRoleLevel(o);
  171. if (role >= this.roleLevel)
  172. return;
  173. let social = o.social;
  174. if (social.muted) {
  175. this.sendMessage('That player has already been muted', 'color-redA');
  176. return;
  177. }
  178. let reasonMsg = '';
  179. if (reason)
  180. reasonMsg = ' (' + reason + ')';
  181. social.muted = true;
  182. this.sendMessage('Successfully muted ' + target, 'color-yellowB');
  183. this.sendMessage('You have been muted' + reasonMsg, 'color-yellowB', o);
  184. atlas.updateObject(o, {
  185. components: [{
  186. type: 'social',
  187. muted: true
  188. }]
  189. });
  190. io.set({
  191. ent: new Date(),
  192. field: 'modLog',
  193. value: JSON.stringify({
  194. source: this.obj.name,
  195. command: 'mute',
  196. target: target,
  197. reason: reason
  198. })
  199. });
  200. },
  201. unmute: function (target, reason) {
  202. if (typeof (target) === 'object') {
  203. let keys = Object.keys(target);
  204. target = keys[0];
  205. reason = keys[1];
  206. }
  207. if (target === this.obj.name)
  208. return;
  209. let o = connections.players.find(o => (o.name === target));
  210. if (!o)
  211. return;
  212. let role = roles.getRoleLevel(o);
  213. if (role >= this.roleLevel)
  214. return;
  215. let social = o.social;
  216. if (!social.muted) {
  217. this.sendMessage('That player is not muted', 'color-redA');
  218. return;
  219. }
  220. let reasonMsg = '';
  221. if (reason)
  222. reasonMsg = ' (' + reason + ')';
  223. delete social.muted;
  224. this.sendMessage('Successfully unmuted ' + target, 'color-yellowB');
  225. this.sendMessage('You have been unmuted' + reasonMsg, 'color-yellowB', o);
  226. atlas.updateObject(o, {
  227. components: [{
  228. type: 'social',
  229. muted: null
  230. }]
  231. });
  232. io.set({
  233. ent: new Date(),
  234. field: 'modLog',
  235. value: JSON.stringify({
  236. source: this.obj.name,
  237. command: 'unmute',
  238. target: target,
  239. reason: reason
  240. })
  241. });
  242. },
  243. clearInventory: function () {
  244. let inventory = this.obj.inventory;
  245. inventory.items
  246. .filter(i => !i.eq)
  247. .map(i => i.id)
  248. .forEach(i => inventory.destroyItem(i, null, true));
  249. },
  250. getItem: function (config) {
  251. if (config.slot === 'set') {
  252. configSlots.slots.forEach(function (s) {
  253. if (s === 'tool')
  254. return;
  255. let newConfig = extend(true, {}, config, {
  256. slot: s
  257. });
  258. this.getItem(newConfig);
  259. }, this);
  260. return;
  261. }
  262. if (config.stats)
  263. config.stats = config.stats.split(',');
  264. if (config.name)
  265. config.name = config.name.split('_').join(' ');
  266. if (config.spellName)
  267. config.spellName = config.spellName.split('_').join(' ');
  268. if (config.type)
  269. config.type = config.type.split('_').join(' ');
  270. if (config.sprite)
  271. config.sprite = config.sprite.split('_');
  272. let spritesheet = config.spritesheet;
  273. delete config.spritesheet;
  274. let factions = (config.factions || '').split(',');
  275. delete config.factions;
  276. let safe = config.safe;
  277. delete config.safe;
  278. let eq = config.eq;
  279. delete config.eq;
  280. let item = generator.generate(config);
  281. if (safe) {
  282. item.noDrop = true;
  283. item.noDestroy = true;
  284. item.noSalvage = true;
  285. }
  286. factions.forEach(function (f) {
  287. if (f === '')
  288. return;
  289. let faction = factions.getFaction(f);
  290. faction.uniqueStat.generate(item);
  291. item.factions = [];
  292. item.factions.push({
  293. id: f,
  294. tier: 3
  295. });
  296. });
  297. if (spritesheet)
  298. item.spritesheet = spritesheet;
  299. let newItem = this.obj.inventory.getItem(item);
  300. if (eq)
  301. this.obj.equipment.equip(newItem.id);
  302. },
  303. getGold: function (amount) {
  304. let newGold = this.obj.trade.gold + ~~amount;
  305. newGold = Math.max(-1000000000, Math.min(1000000000, newGold));
  306. this.obj.trade.gold = newGold;
  307. this.obj.syncer.set(true, 'trade', 'gold', newGold);
  308. },
  309. setLevel: function (level) {
  310. let obj = this.obj;
  311. let syncer = obj.syncer;
  312. level = Math.max(1, ~~level);
  313. let stats = obj.stats;
  314. let values = stats.values;
  315. let oldLevel = values.level;
  316. values.level = level;
  317. let delta = level - oldLevel;
  318. values.hpMax += (40 * delta);
  319. syncer.setObject(true, 'stats', 'values', 'level', level);
  320. syncer.setObject(true, 'stats', 'values', 'hpMax', values.hpMax);
  321. process.send({
  322. method: 'object',
  323. serverId: obj.serverId,
  324. obj: {
  325. level: level
  326. }
  327. });
  328. stats.calcXpMax();
  329. },
  330. godMode: function () {
  331. let obj = this.obj;
  332. let statValues = obj.stats.values;
  333. let newValues = {
  334. int: 10000000,
  335. str: 10000000,
  336. dex: 10000000,
  337. hpMax: 10000000,
  338. hp: 10000000,
  339. manaMax: 10000000,
  340. mana: 10000000,
  341. sprintChance: 100,
  342. vit: 10000000
  343. };
  344. let syncer = obj.syncer;
  345. for (let s in newValues) {
  346. let newValue = newValues[s];
  347. statValues[s] = newValue;
  348. syncer.setObject(true, 'stats', 'values', s, newValue);
  349. }
  350. obj.spellbook.calcDps();
  351. },
  352. completeQuests: function () {
  353. let obj = this.obj;
  354. let quests = obj.quests;
  355. quests.quests.forEach(function (q) {
  356. q.isReady = true;
  357. q.complete();
  358. }, this);
  359. quests.quests = [];
  360. obj.instance.questBuilder.obtain(obj);
  361. },
  362. getReputation: function (faction) {
  363. if (typeof (faction) !== 'string')
  364. return;
  365. this.obj.reputation.getReputation(faction, 50000);
  366. },
  367. loseReputation: function (faction) {
  368. if (typeof (faction) !== 'string')
  369. return;
  370. this.obj.reputation.getReputation(faction, -50000);
  371. },
  372. setStat: function (config) {
  373. this.obj.stats.values[config.stat] = ~~config.value;
  374. },
  375. getXp: function (amount) {
  376. this.obj.stats.getXp(amount, this.obj, this.obj);
  377. },
  378. die: function () {
  379. this.obj.stats.takeDamage({
  380. amount: 99999
  381. }, 1, this.obj);
  382. }
  383. };