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.
 
 
 

316 lines
6.7 KiB

  1. const chat = require('./social/chat');
  2. module.exports = {
  3. type: 'social',
  4. isPartyLeader: null,
  5. partyLeaderId: null,
  6. party: null,
  7. customChannels: null,
  8. blockedPlayers: [],
  9. actions: null,
  10. messageHistory: [],
  11. maxChatLength: 255,
  12. init: function (blueprint) {
  13. this.obj.extendComponent('social', 'socialCommands', {});
  14. if (blueprint)
  15. this.blockedPlayers = blueprint.blockedPlayers || [];
  16. },
  17. simplify: function (self) {
  18. const { party, customChannels, blockedPlayers, actions } = this;
  19. const res = {
  20. type: 'social',
  21. party
  22. };
  23. if (self) {
  24. Object.assign(res, {
  25. actions,
  26. customChannels,
  27. blockedPlayers
  28. });
  29. }
  30. return res;
  31. },
  32. save: function () {
  33. return {
  34. type: 'social',
  35. customChannels: this.customChannels,
  36. blockedPlayers: this.blockedPlayers
  37. };
  38. },
  39. sendMessage: function (msg, color, target) {
  40. (target || this.obj).socket.emit('event', {
  41. event: 'onGetMessages',
  42. data: {
  43. messages: [{
  44. class: color || 'q0',
  45. message: msg,
  46. type: 'chat'
  47. }]
  48. }
  49. });
  50. },
  51. chat: async function (msg) {
  52. await chat(this, msg);
  53. },
  54. dc: function () {
  55. if (!this.party)
  56. return;
  57. this.leaveParty();
  58. },
  59. //This gets called on the target player
  60. getInvite: function (msg) {
  61. if (this.party)
  62. return;
  63. let obj = this.obj;
  64. let sourceId = msg.data.sourceId;
  65. if (sourceId === obj.id)
  66. return;
  67. let source = cons.players.find(c => c.id === sourceId);
  68. if (!source || !source.social)
  69. return;
  70. source.social.sendMessage('invite sent', 'color-yellowB');
  71. this.sendMessage(source.name + ' has invited you to join a party', 'color-yellowB');
  72. this.obj.socket.emit('event', {
  73. event: 'onGetInvite',
  74. data: sourceId
  75. });
  76. },
  77. //This gets called on the player that initiated the invite
  78. acceptInvite: function (msg) {
  79. let sourceId = msg.data.sourceId;
  80. let source = cons.players.find(c => c.id === sourceId);
  81. if (!source)
  82. return;
  83. if (!this.party) {
  84. this.isPartyLeader = true;
  85. this.party = [this.obj.id];
  86. this.updateMainThread('party', this.party);
  87. }
  88. // Only add if not yet in party
  89. if (!this.party.find(id => (id === sourceId)))
  90. this.party.push(sourceId);
  91. this.updateMainThread('party', this.party);
  92. this.party.forEach(function (p) {
  93. let player = cons.players.find(c => c.id === p);
  94. player.social.party = this.party;
  95. player.social.updateMainThread('party', player.social.party);
  96. let returnMsg = source.name + ' has joined the party';
  97. if (p === sourceId)
  98. returnMsg = 'you have joined a party';
  99. player.social.sendMessage(returnMsg, 'color-yellowB');
  100. player
  101. .socket.emit('event', {
  102. event: 'onGetParty',
  103. data: this.party
  104. });
  105. }, this);
  106. },
  107. declineInvite: function (msg) {
  108. let targetId = msg.data.targetId;
  109. let target = cons.players.find(c => c.id === targetId);
  110. if (!target)
  111. return;
  112. this.sendMessage(target.name + ' declined your party invitation', 'color-redA');
  113. },
  114. //Gets called on the player that requested to leave
  115. leaveParty: function (msg) {
  116. if (!this.party)
  117. return;
  118. let name = this.obj.name;
  119. this.party.spliceWhere(p => p === this.obj.id);
  120. this.party.forEach(function (p) {
  121. let player = cons.players.find(c => c.id === p);
  122. let messages = [{
  123. class: 'q0',
  124. message: name + ' has left the party'
  125. }];
  126. let party = this.party;
  127. if (this.party.length === 1) {
  128. messages.push({
  129. class: 'q0',
  130. message: 'your group has been disbanded'
  131. });
  132. player.social.isPartyLeader = false;
  133. player.social.party = null;
  134. player.social.updateMainThread('party', player.social.party);
  135. party = null;
  136. }
  137. player.socket.emit('events', {
  138. onGetParty: [party],
  139. onGetMessages: [{
  140. messages: messages
  141. }]
  142. });
  143. }, this);
  144. this.obj.socket.emit('events', {
  145. onGetParty: [
  146. []
  147. ],
  148. onGetMessages: [{
  149. messages: {
  150. class: 'q0',
  151. message: 'you have left the party'
  152. }
  153. }]
  154. });
  155. if ((this.isPartyLeader) && (this.party.length >= 2)) {
  156. let newLeader = cons.players.find(c => c.id === this.party[0]).social;
  157. newLeader.isPartyLeader = true;
  158. this.party.forEach(function (p) {
  159. let returnMsg = newLeader.obj.name + ' is now the party leader';
  160. if (p === newLeader.obj.id)
  161. returnMsg = 'you are now the party leader';
  162. cons.players.find(c => c.id === p).socket.emit('events', {
  163. onGetMessages: [{
  164. messages: [{
  165. class: 'q0',
  166. message: returnMsg
  167. }]
  168. }]
  169. });
  170. }, this);
  171. }
  172. this.party = null;
  173. this.updateMainThread('party', this.party);
  174. },
  175. //Gets called on the player that requested the removal
  176. removeFromParty: function (msg) {
  177. if (!this.isPartyLeader) {
  178. this.sendMessage('you are not the party leader', 'color-redA');
  179. return;
  180. }
  181. let target = cons.players.find(c => c.id === msg.data.id);
  182. if (!target)
  183. return;
  184. this.party.spliceWhere(p => p === target.id);
  185. this.party.forEach(function (p) {
  186. cons.players.find(c => c.id === p)
  187. .socket.emit('events', {
  188. onGetParty: [this.party],
  189. onGetMessages: [{
  190. messages: [{
  191. class: 'color-yellowB',
  192. message: target.name + ' has been removed from the party'
  193. }]
  194. }]
  195. });
  196. }, this);
  197. target.socket.emit('events', {
  198. onGetMessages: [{
  199. messages: [{
  200. class: 'color-redA',
  201. message: 'you have been removed from the party'
  202. }]
  203. }],
  204. onPartyDisband: [{}]
  205. });
  206. target.social.party = null;
  207. target.social.isPartyLeader = false;
  208. target.social.updateMainThread('party', target.social.party);
  209. if (this.party.length === 1) {
  210. this.party = null;
  211. this.isPartyLeader = null;
  212. this.updateMainThread('party', this.party);
  213. this.sendMessage('your party has been disbanded', 'color-yellowB');
  214. }
  215. },
  216. updateMainThread: function (property, value) {
  217. atlas.updateObject(this.obj, {
  218. components: [{
  219. type: 'social',
  220. [property]: value
  221. }]
  222. });
  223. },
  224. //Sends a notification to yourself
  225. // arg1 = { message, className, type }
  226. notifySelf: function ({ message, className = 'color-redA', type = 'info', subType }) {
  227. const { obj: { id, serverId, instance } } = this;
  228. //Maybe we are in the main thread
  229. if (!instance)
  230. return;
  231. const { syncer } = instance;
  232. syncer.queue('onGetMessages', {
  233. id,
  234. messages: [{
  235. class: className,
  236. message,
  237. type,
  238. subType
  239. }]
  240. }, [serverId]);
  241. },
  242. //Sends multiple notifications to yourself
  243. // messages = [{ msg, className, type }]
  244. notifySelfArray: function (messages) {
  245. const { obj: { id, serverId, instance: { syncer } } } = this;
  246. messages.forEach(m => {
  247. const { className = 'color-redA', type = 'info', subType } = m;
  248. m.class = className;
  249. m.type = type;
  250. m.subType = subType;
  251. });
  252. syncer.queue('onGetMessages', {
  253. id,
  254. messages
  255. }, [serverId]);
  256. }
  257. };