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.
 
 
 

452 lines
9.8 KiB

  1. let atlas = require('../world/atlas');
  2. let roles = require('../config/roles');
  3. let events = require('../misc/events');
  4. module.exports = {
  5. type: 'social',
  6. isPartyLeader: null,
  7. partyLeaderId: null,
  8. party: null,
  9. customChannels: null,
  10. messageHistory: [],
  11. maxChatLength: 255,
  12. init: function (blueprint) {
  13. this.obj.extendComponent('social', 'socialCommands', {});
  14. },
  15. simplify: function (self) {
  16. return {
  17. type: 'social',
  18. party: this.party,
  19. customChannels: self ? this.customChannels : null,
  20. muted: this.muted
  21. };
  22. },
  23. sendMessage: function (msg, color, target) {
  24. (target || this.obj).socket.emit('event', {
  25. event: 'onGetMessages',
  26. data: {
  27. messages: [{
  28. class: color || 'q0',
  29. message: msg,
  30. type: 'chat'
  31. }]
  32. }
  33. });
  34. },
  35. sendPartyMessage: function (msg) {
  36. if (!this.party) {
  37. this.obj.socket.emit('events', {
  38. onGetMessages: [{
  39. messages: [{
  40. class: 'color-redA',
  41. message: 'you are not in a party',
  42. type: 'info'
  43. }]
  44. }]
  45. });
  46. return;
  47. }
  48. let charname = this.obj.auth.charname;
  49. let message = msg.data.message.substr(1);
  50. this.party.forEach(function (p) {
  51. let player = cons.players.find(c => c.id === p);
  52. player.socket.emit('events', {
  53. onGetMessages: [{
  54. messages: [{
  55. class: 'color-grayB',
  56. message: '(party: ' + charname + '): ' + message,
  57. type: 'chat'
  58. }]
  59. }]
  60. });
  61. }, this);
  62. },
  63. sendCustomChannelMessage: function (msg) {
  64. let pList = cons.players;
  65. let pLen = pList.length;
  66. let origMessage = msg.data.message.substr(1);
  67. let channel = origMessage.split(' ')[0];
  68. let message = origMessage.substr(channel.length);
  69. if ((!channel) || (!message)) {
  70. this.obj.socket.emit('events', {
  71. onGetMessages: [{
  72. messages: [{
  73. class: 'color-redA',
  74. message: 'syntax: $channel message',
  75. type: 'info'
  76. }]
  77. }]
  78. });
  79. return;
  80. } else if (!this.isInChannel(this.obj, channel)) {
  81. this.obj.socket.emit('events', {
  82. onGetMessages: [{
  83. messages: [{
  84. class: 'color-redA',
  85. message: 'you are not currently in channel: ' + channel,
  86. type: 'info'
  87. }]
  88. }]
  89. });
  90. return;
  91. } else if (pLen > 0) {
  92. for (let i = 0; i < pLen; i++) {
  93. if (this.isInChannel(pList[i], channel)) {
  94. pList[i].socket.emit('events', {
  95. onGetMessages: [{
  96. messages: [{
  97. class: 'color-grayB',
  98. message: '[' + channel + '] ' + this.obj.auth.charname + ': ' + message,
  99. type: channel.trim()
  100. }]
  101. }]
  102. });
  103. }
  104. }
  105. }
  106. },
  107. chat: function (msg) {
  108. if (!msg.data.message)
  109. return;
  110. msg.data.message = msg.data.message
  111. .split('<')
  112. .join('&lt;')
  113. .split('>')
  114. .join('&gt;');
  115. if (!msg.data.message)
  116. return;
  117. if (msg.data.message.trim() === '')
  118. return;
  119. this.onBeforeChat(msg.data);
  120. if (msg.data.ignore)
  121. return;
  122. if (this.muted) {
  123. this.sendMessage('You have been muted from talking', 'color-redA');
  124. return;
  125. }
  126. let messageString = msg.data.message;
  127. if (messageString.length > this.maxChatLength)
  128. return;
  129. let history = this.messageHistory;
  130. let time = +new Date();
  131. history.spliceWhere(h => ((time - h.time) > 5000));
  132. if (history.length > 0) {
  133. if (history[history.length - 1].msg === messageString) {
  134. this.sendMessage('You have already sent that message', 'color-redA');
  135. return;
  136. } else if (history.length >= 3) {
  137. this.sendMessage('You are sending too many messages', 'color-redA');
  138. return;
  139. }
  140. }
  141. history.push({
  142. msg: messageString,
  143. time: time
  144. });
  145. let charname = this.obj.auth.charname;
  146. let msgStyle = roles.getRoleMessageStyle(this.obj) || ('color-grayB');
  147. let msgEvent = {
  148. source: charname,
  149. msg: messageString
  150. };
  151. events.emit('onBeforeSendMessage', msgEvent);
  152. messageString = msgEvent.msg;
  153. if (messageString[0] === '@') {
  154. let playerName = '';
  155. //Check if there's a space in the name
  156. if (messageString[1] === "'") {
  157. playerName = messageString.substring(2, messageString.indexOf("'", 2));
  158. messageString = messageString.replace("@'" + playerName + "' ", '');
  159. } else {
  160. playerName = messageString.substring(1, messageString.indexOf(' '));
  161. messageString = messageString.replace('@' + playerName + ' ', '');
  162. }
  163. if (playerName === this.obj.name)
  164. return;
  165. let target = cons.players.find(p => p.name === playerName);
  166. if (!target)
  167. return;
  168. this.obj.socket.emit('event', {
  169. event: 'onGetMessages',
  170. data: {
  171. messages: [{
  172. class: 'color-yellowB',
  173. message: '(you to ' + playerName + '): ' + messageString,
  174. type: 'chat'
  175. }]
  176. }
  177. });
  178. target.socket.emit('event', {
  179. event: 'onGetMessages',
  180. data: {
  181. messages: [{
  182. class: 'color-yellowB',
  183. message: '(' + this.obj.name + ' to you): ' + messageString,
  184. type: 'chat'
  185. }]
  186. }
  187. });
  188. } else if (messageString[0] === '$')
  189. this.sendCustomChannelMessage(msg);
  190. else if (messageString[0] === '%')
  191. this.sendPartyMessage(msg);
  192. else {
  193. let prefix = roles.getRoleMessagePrefix(this.obj) || '';
  194. global.io.sockets.emit('event', {
  195. event: 'onGetMessages',
  196. data: {
  197. messages: [{
  198. class: msgStyle,
  199. message: prefix + charname + ': ' + msg.data.message,
  200. item: msg.data.item,
  201. type: 'chat'
  202. }]
  203. }
  204. });
  205. }
  206. },
  207. dc: function () {
  208. if (!this.party)
  209. return;
  210. this.leaveParty();
  211. },
  212. //This gets called on the target player
  213. getInvite: function (msg) {
  214. if (this.party)
  215. return;
  216. let obj = this.obj;
  217. let sourceId = msg.data.sourceId;
  218. if (sourceId === obj.id)
  219. return;
  220. let source = cons.players.find(c => c.id === sourceId);
  221. if (!source)
  222. return;
  223. source.social.sendMessage('invite sent', 'color-yellowB');
  224. this.sendMessage(source.name + ' has invited you to join a party', 'color-yellowB');
  225. this.obj.socket.emit('event', {
  226. event: 'onGetInvite',
  227. data: sourceId
  228. });
  229. },
  230. //This gets called on the player that initiated the invite
  231. acceptInvite: function (msg) {
  232. let sourceId = msg.data.sourceId;
  233. let source = cons.players.find(c => c.id === sourceId);
  234. if (!source)
  235. return;
  236. if (!this.party) {
  237. this.isPartyLeader = true;
  238. this.party = [this.obj.id];
  239. this.updatePartyOnThread();
  240. }
  241. // Only add if not yet in party
  242. if (!this.party.find(id => (id === sourceId)))
  243. this.party.push(sourceId);
  244. this.updatePartyOnThread();
  245. this.party.forEach(function (p) {
  246. let player = cons.players.find(c => c.id === p);
  247. player.social.party = this.party;
  248. player.social.updatePartyOnThread();
  249. let msg = source.name + ' has joined the party';
  250. if (p === sourceId)
  251. msg = 'you have joined a party';
  252. player.social.sendMessage(msg, 'color-yellowB');
  253. player
  254. .socket.emit('event', {
  255. event: 'onGetParty',
  256. data: this.party
  257. });
  258. }, this);
  259. },
  260. declineInvite: function (msg) {
  261. let targetId = msg.data.targetId;
  262. let target = cons.players.find(c => c.id === targetId);
  263. if (!target)
  264. return;
  265. this.sendMessage(target.name + ' declined your party invitation', 'color-redA');
  266. },
  267. //Gets called on the player that requested to leave
  268. leaveParty: function (msg) {
  269. let name = this.obj.name;
  270. this.party.spliceWhere(p => p === this.obj.id);
  271. this.party.forEach(function (p) {
  272. let player = cons.players.find(c => c.id === p);
  273. let messages = [{
  274. class: 'q0',
  275. message: name + ' has left the party'
  276. }];
  277. let party = this.party;
  278. if (this.party.length === 1) {
  279. messages.push({
  280. class: 'q0',
  281. message: 'your group has been disbanded'
  282. });
  283. player.social.isPartyLeader = false;
  284. player.social.party = null;
  285. player.social.updatePartyOnThread();
  286. party = null;
  287. }
  288. if (!player) {
  289. console.log('no player');
  290. console.log(this.party);
  291. console.log(this.obj.name);
  292. }
  293. player.socket.emit('events', {
  294. onGetParty: [party],
  295. onGetMessages: [{
  296. messages: messages
  297. }]
  298. });
  299. }, this);
  300. this.obj.socket.emit('events', {
  301. onGetParty: [
  302. []
  303. ],
  304. onGetMessages: [{
  305. messages: {
  306. class: 'q0',
  307. message: 'you have left the party'
  308. }
  309. }]
  310. });
  311. if ((this.isPartyLeader) && (this.party.length >= 2)) {
  312. let newLeader = cons.players.find(c => c.id === this.party[0]).social;
  313. newLeader.isPartyLeader = true;
  314. this.party.forEach(function (p) {
  315. let msg = newLeader.obj.name + ' is now the party leader';
  316. if (p === newLeader.obj.id)
  317. msg = 'you are now the party leader';
  318. cons.players.find(c => c.id === p).socket.emit('events', {
  319. onGetMessages: [{
  320. messages: [{
  321. class: 'q0',
  322. message: msg
  323. }]
  324. }]
  325. });
  326. }, this);
  327. }
  328. this.party = null;
  329. this.updatePartyOnThread();
  330. },
  331. //Gets called on the player that requested the removal
  332. removeFromParty: function (msg) {
  333. if (!this.isPartyLeader) {
  334. this.sendMessage('you are not the party leader', 'color-redA');
  335. return;
  336. }
  337. let target = cons.players.find(c => c.id === msg.data);
  338. if (!target)
  339. return;
  340. this.party.spliceWhere(p => p === target.id);
  341. this.party.forEach(function (p) {
  342. cons.players.find(c => c.id === p)
  343. .socket.emit('events', {
  344. onGetParty: [this.party],
  345. onGetMessages: [{
  346. messages: [{
  347. class: 'color-yellowB',
  348. message: target.name + ' has been removed from the party'
  349. }]
  350. }]
  351. });
  352. }, this);
  353. target.socket.emit('events', {
  354. onGetMessages: [{
  355. messages: [{
  356. class: 'color-redA',
  357. message: 'you have been removed from the party'
  358. }]
  359. }],
  360. onPartyDisband: [{}]
  361. });
  362. target.social.party = null;
  363. target.social.isPartyLeader = false;
  364. target.social.updatePartyOnThread();
  365. if (this.party.length === 1) {
  366. this.party = null;
  367. this.isPartyLeader = null;
  368. this.updatePartyOnThread();
  369. this.sendMessage('your party has been disbanded', 'color-yellowB');
  370. }
  371. },
  372. updatePartyOnThread: function () {
  373. atlas.updateObject(this.obj, {
  374. components: [{
  375. type: 'social',
  376. party: this.party
  377. }]
  378. });
  379. }
  380. };