conversation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. class Conversation {
  2. constructor() {
  3. this.simple = [
  4. {
  5. "trigger": ["hello", "hey", "hi"],
  6. "response": ["say Hello {source.name}", "say hi {source.name}", "say hello", "say hey", "say hi", "emote waves.", "emote smiles.", "How are you, {source.name}?", "Ahh, nice to see you {source.name}."],
  7. "topic": "conversation-greeting"
  8. },
  9. {
  10. "trigger": ["help", "oh no", "oops"],
  11. "response": ["say Are you ok, {source.name}?", "say What's wrong?", "say How can I help?"],
  12. "topic": "conversation-assist"
  13. },
  14. {
  15. "trigger": ["whats your name", "who are you"],
  16. "response": ["say I'm {listener.name}", "say {listener.name}", "say Oh! It's {listener.name}!", "say Me?"],
  17. "topic": "conversation-identify"
  18. },
  19. {
  20. "trigger": ["how are you", "you ok"],
  21. "response": ["say I'm fine", "say Pretty good", "say peachy", "say ehh", "say I'm good. How are you, {source.name}?"],
  22. "topic": "conversation-status"
  23. },
  24. {
  25. "trigger": ["whats up", "whats wrong"],
  26. "response": ["say Not much", "say nothing", "say Everything is great", "say ehh"],
  27. "topic": "conversation-status"
  28. },
  29. {
  30. "trigger": ["bye", "goodbye", "good bye", "see you"],
  31. "response": ["say Bye!", "say Goodbye {source.name}", "say It was a pleasure talking to you.", "emote waves."],
  32. "topic": "conversation-end"
  33. },
  34. {
  35. "trigger": ["good luck"],
  36. "response": ["say Thanks."],
  37. "topic": "conversation-luck"
  38. },
  39. {
  40. "trigger": ["thanks", "thank you"],
  41. "response": ["say You're welcome!", "say No problem.", "say The pleasure was mine."],
  42. "topic": "conversation-thanks"
  43. },
  44. {
  45. "trigger": ["how do you know"],
  46. "response": ["say Lucky guess, I guess", "emote shrugs."],
  47. "topic": null
  48. },
  49. {
  50. "trigger": ["do you know", "who i am"],
  51. "response": ["say You're kinda famous, {source.name}. I imagine everyone knows you.", "say I mean, you are {source.name}, right?", "say You're {source.name}."],
  52. "topic": null
  53. },
  54. {
  55. "trigger": ["i like you", "youre great", "your great"],
  56. "response": ["say Aww.", "say I like you too!", "say That's nice of you to say."],
  57. "topic": null
  58. },
  59. ];
  60. this.orders = [
  61. {
  62. "trigger": ["go east"],
  63. "response": ["east"]
  64. },
  65. {
  66. "trigger": ["go west"],
  67. "response": ["west"]
  68. },
  69. {
  70. "trigger": ["go north"],
  71. "response": ["north"]
  72. },
  73. {
  74. "trigger": ["go south"],
  75. "response": ["south"]
  76. },
  77. {
  78. "trigger": ["go northwest"],
  79. "response": ["nw"]
  80. },
  81. {
  82. "trigger": ["go northeast"],
  83. "response": ["ne"]
  84. },
  85. {
  86. "trigger": ["go southwest"],
  87. "response": ["sw"]
  88. },
  89. {
  90. "trigger": ["go southeast"],
  91. "response": ["se"]
  92. },
  93. {
  94. "trigger": ["go up"],
  95. "response": ["up"]
  96. },
  97. {
  98. "trigger": ["go down"],
  99. "response": ["down"]
  100. },
  101. {
  102. "trigger": ["enter"],
  103. "response": ["enter"]
  104. },
  105. {
  106. "trigger": ["leave"],
  107. "response": ["leave"]
  108. },
  109. {
  110. "trigger": ["inventory"],
  111. "response": ["inventory"]
  112. },
  113. ];
  114. }
  115. basicDecency(source, message, listener) {
  116. return this.mobileInstructionRules(source, message, listener, this.simple);
  117. }
  118. followsOrders(source, message, listener) {
  119. return this.mobileInstructionRules(source, message, listener, this.orders);
  120. }
  121. canWander(mobile) {
  122. var responses = this.getRoomExits(mobile);
  123. return responses[parseInt(Math.random() * responses.length)];
  124. }
  125. getRoomExits(mobile) {
  126. var roomId = mobile.roomId;
  127. var room = session.rooms[roomId];
  128. return room.roomExits;
  129. }
  130. mobileInstructionRules(source, message, listener, instructionset) {
  131. if(source == null) {
  132. return false;
  133. }
  134. if(message.indexOf("shout") != -1) {
  135. return false;
  136. }
  137. for(var i = 0; i < instructionset.length; i++) {
  138. var response = this.triggerResponse(
  139. instructionset[i],
  140. message
  141. );
  142. if(response != null) {
  143. var cleanResponse = this.parsedResponse(response.action, source, listener, message);
  144. if(typeof listener['mud_addCommand'] == 'function') {
  145. var remembers = false;
  146. if(typeof listener['mud_checkMemory'] == 'function') {
  147. if(response.topic != null) {
  148. remembers = listener.mud_checkMemory(response.topic, source.name);
  149. }
  150. }
  151. if (!remembers) {
  152. listener.mud_addCommand(listener, cleanResponse);
  153. }
  154. if(typeof listener['mud_addMemory'] == 'function') {
  155. if(response.topic != null) {
  156. listener.mud_addMemory(response.topic, source.name);
  157. }
  158. }
  159. return true;
  160. }
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. parsedResponse(response, source, listener) {
  167. response = response.replace("{source.name}", source.name);
  168. response = response.replace("{listener.name}", listener.name);
  169. return response;
  170. }
  171. triggerResponse(instructionset, message) {
  172. var triggers = instructionset.trigger;
  173. var responses = instructionset.response;
  174. if(this.contains(triggers, message)) {
  175. var response = {
  176. "action": this.random(responses),
  177. "topic": instructionset.topic
  178. };
  179. return response;
  180. }
  181. }
  182. random(responses) {
  183. var index = parseInt(Math.random() * responses.length);
  184. return responses[index];
  185. }
  186. contains(keys, message) {
  187. var cleanMessage = message.replace(/'/g, "");
  188. for(var i = 0; i < keys.length; i++) {
  189. var regex = new RegExp("\\b" + keys[i] + "\\b", "i");
  190. if(cleanMessage.search(regex) != -1) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. }
  197. module.exports = new Conversation();