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.
 
 
 

340 lines
7.3 KiB

  1. define([
  2. 'world/atlas',
  3. 'config/roles'
  4. ], function(
  5. atlas,
  6. roles
  7. ) {
  8. return {
  9. type: 'social',
  10. isPartyLeader: null,
  11. partyLeaderId: null,
  12. party: null,
  13. init: function() {},
  14. simplify: function() {
  15. return {
  16. type: 'social',
  17. party: this.party
  18. };
  19. },
  20. sendMessage: function(msg) {
  21. this.obj.socket.emit('event', {
  22. event: 'onGetMessages',
  23. data: {
  24. messages: [{
  25. class: 'q0',
  26. message: msg,
  27. type: 'chat'
  28. }]
  29. }
  30. });
  31. },
  32. sendPartyMessage: function(msg) {
  33. if (!this.party) {
  34. this.obj.socket.emit('events', {
  35. onGetMessages: [{
  36. messages: [{
  37. class: 'q0',
  38. message: 'you are not in a party',
  39. type: 'info'
  40. }]
  41. }]
  42. });
  43. return;
  44. }
  45. var charname = this.obj.auth.charname;
  46. var message = msg.data.message.substr(1);
  47. this.party.forEach(function(p) {
  48. var player = cons.players.find(c => c.id == p);
  49. player.socket.emit('events', {
  50. onGetMessages: [{
  51. messages: [{
  52. class: 'q0',
  53. message: '(party: ' + charname + '): ' + message,
  54. type: 'chat'
  55. }]
  56. }]
  57. });
  58. }, this);
  59. },
  60. chat: function(msg) {
  61. var charname = this.obj.auth.charname;
  62. var level = this.obj.stats.values.level;
  63. if (level >= 10)
  64. level = 4;
  65. else if (level >= 6)
  66. level = 2;
  67. else
  68. level = 0;
  69. var msgStyle = roles.getRoleMessageStyle(this.obj) || ('q' + level);
  70. var messageString = msg.data.message;
  71. if (messageString[0] == '@') {
  72. var playerName = '';
  73. //Check if there's a space in the name
  74. if (messageString[1] == "'") {
  75. playerName = messageString.substring(2, messageString.indexOf("'", 2));
  76. messageString = messageString.replace("@'" + playerName + "' ", '');
  77. } else {
  78. playerName = messageString.substring(1, messageString.indexOf(' '));
  79. messageString = messageString.replace('@' + playerName + ' ', '');
  80. }
  81. if (playerName == this.obj.name)
  82. return;
  83. var target = cons.players.find(p => p.name == playerName);
  84. if (!target)
  85. return;
  86. this.obj.socket.emit('event', {
  87. event: 'onGetMessages',
  88. data: {
  89. messages: [{
  90. class: msgStyle,
  91. message: '(you to ' + playerName + '): ' + messageString,
  92. type: 'chat'
  93. }]
  94. }
  95. });
  96. target.socket.emit('event', {
  97. event: 'onGetMessages',
  98. data: {
  99. messages: [{
  100. class: msgStyle,
  101. message: '(' + this.obj.name + ' to you): ' + messageString,
  102. type: 'chat'
  103. }]
  104. }
  105. });
  106. } else if (messageString[0] == '%') {
  107. this.sendPartyMessage(msg);
  108. } else {
  109. var prefix = roles.getRoleMessagePrefix(this.obj) || '';
  110. io.sockets.emit('event', {
  111. event: 'onGetMessages',
  112. data: {
  113. messages: [{
  114. class: msgStyle,
  115. message: prefix + charname + ': ' + msg.data.message,
  116. type: 'chat'
  117. }]
  118. }
  119. });
  120. }
  121. },
  122. dc: function() {
  123. if (!this.party)
  124. return;
  125. this.leaveParty();
  126. },
  127. //This gets called on the target player
  128. getInvite: function(msg) {
  129. if (this.party)
  130. return;
  131. var obj = this.obj;
  132. var sourceId = msg.data.sourceId;
  133. if (sourceId == obj.id)
  134. return;
  135. var source = cons.players.find(c => c.id == sourceId);
  136. if (!source)
  137. return;
  138. source.social.sendMessage('invite sent');
  139. this.sendMessage(source.name + ' has invited you to join a party');
  140. this.obj.socket.emit('event', {
  141. event: 'onGetInvite',
  142. data: sourceId
  143. });
  144. },
  145. //This gets called on the player that initiated the invite
  146. acceptInvite: function(msg) {
  147. var sourceId = msg.data.sourceId;
  148. var source = cons.players.find(c => c.id == sourceId);
  149. if (!source)
  150. return;
  151. if (!this.party) {
  152. this.isPartyLeader = true;
  153. this.party = [this.obj.id];
  154. this.updatePartyOnThread();
  155. }
  156. this.party.push(sourceId);
  157. this.updatePartyOnThread();
  158. this.party.forEach(function(p) {
  159. var player = cons.players.find(c => c.id == p);
  160. player.social.party = this.party;
  161. player.social.updatePartyOnThread();
  162. var msg = source.name + ' has joined the party';
  163. if (p == sourceId)
  164. msg = 'you have joined a party';
  165. player.social.sendMessage(msg);
  166. player
  167. .socket.emit('event', {
  168. event: 'onGetParty',
  169. data: this.party
  170. });
  171. }, this);
  172. },
  173. declineInvite: function(msg) {
  174. var targetId = msg.data.targetId;
  175. var target = cons.players.find(c => c.id == targetId);
  176. if (!target)
  177. return;
  178. this.sendMessage(target.name + ' declined your party invitation');
  179. },
  180. //Gets called on the player that requested to leave
  181. leaveParty: function(msg) {
  182. var name = this.obj.name;
  183. this.party.spliceWhere(p => p == this.obj.id);
  184. this.party.forEach(function(p) {
  185. var player = cons.players.find(c => c.id == p);
  186. var messages = [{
  187. class: 'q0',
  188. message: name + ' has left the party'
  189. }];
  190. var party = this.party;
  191. if (this.party.length == 1) {
  192. messages.push({
  193. class: 'q0',
  194. message: 'your group has been disbanded'
  195. });
  196. player.social.isPartyLeader = false;
  197. player.social.party = null;
  198. player.social.updatePartyOnThread();
  199. party = null;
  200. }
  201. player.socket.emit('events', {
  202. onGetParty: [party],
  203. onGetMessages: [{
  204. messages: messages
  205. }]
  206. });
  207. }, this);
  208. this.obj.socket.emit('events', {
  209. onGetParty: [
  210. []
  211. ],
  212. onGetMessages: [{
  213. messages: {
  214. class: 'q0',
  215. message: 'you have left the party'
  216. }
  217. }]
  218. });
  219. if ((this.isPartyLeader) && (this.party.length >= 2)) {
  220. var newLeader = cons.players.find(c => c.id == this.party[0]).social;
  221. newLeader.isPartyLeader = true;
  222. this.party.forEach(function(p) {
  223. var msg = newLeader.obj.name + ' is now the party leader';
  224. if (p == newLeader.obj.id)
  225. msg = 'you are now the party leader';
  226. cons.players.find(c => c.id == p).socket.emit('events', {
  227. onGetMessages: [{
  228. messages: [{
  229. class: 'q0',
  230. message: msg
  231. }]
  232. }]
  233. });
  234. }, this);
  235. }
  236. this.party = null;
  237. this.updatePartyOnThread();
  238. },
  239. //Gets called on the player that requested the removal
  240. removeFromParty: function(msg) {
  241. if (!this.isPartyLeader) {
  242. this.sendMessage('you are not the party leader');
  243. return;
  244. }
  245. var target = cons.players.find(c => c.id == msg.data);
  246. if (!target)
  247. return;
  248. this.party.spliceWhere(p => p == target.id);
  249. this.party.forEach(function(p) {
  250. cons.players.find(c => c.id == p)
  251. .socket.emit('events', {
  252. onGetParty: [this.party],
  253. onGetMessages: [{
  254. messages: [{
  255. class: 'q0',
  256. message: target.name + ' has been removed from the party'
  257. }]
  258. }]
  259. });
  260. }, this);
  261. target.socket.emit('events', {
  262. onGetMessages: [{
  263. messages: [{
  264. class: 'q0',
  265. message: 'you have been removed from the party'
  266. }]
  267. }],
  268. onPartyDisband: [{}]
  269. });
  270. target.social.party = null;
  271. target.social.isPartyLeader = false;
  272. target.social.updatePartyOnThread();
  273. if (this.party.length == 1) {
  274. this.party = null
  275. this.isPartyLeader = null;
  276. this.updatePartyOnThread();
  277. this.sendMessage('your party has been disbanded');
  278. }
  279. },
  280. updatePartyOnThread: function() {
  281. atlas.updateObject(this.obj, {
  282. components: [{
  283. type: 'social',
  284. party: this.party
  285. }]
  286. });
  287. }
  288. };
  289. });