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.
 
 
 

454 regels
10 KiB

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