conversation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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."]
  7. },
  8. {
  9. "trigger": ["help", "oh no", "oops"],
  10. "response": ["say Are you ok, {source.name}?", "say What's wrong?", "say How can I help?"]
  11. },
  12. {
  13. "trigger": ["whats your name", "who are you"],
  14. "response": ["say I'm {listener.name}", "say {listener.name}", "say Oh! It's {listener.name}!", "say Me?"]
  15. },
  16. {
  17. "trigger": ["how are you", "you ok"],
  18. "response": ["say I'm fine", "say Pretty good", "say peachy", "say ehh", "say I'm good. How are you, {source.name}?"]
  19. },
  20. {
  21. "trigger": ["whats up", "whats wrong"],
  22. "response": ["say Not much", "say nothing", "say Everything is great", "say ehh"]
  23. },
  24. {
  25. "trigger": ["bye", "goodbye", "good bye", "see you"],
  26. "response": ["say Bye!", "say Goodbye {source.name}", "say It was a pleasure talking to you.", "emote waves."]
  27. },
  28. {
  29. "trigger": ["good luck"],
  30. "response": ["say Thanks."]
  31. },
  32. {
  33. "trigger": ["thanks", "thank you"],
  34. "response": ["say You're welcome!", "say No problem.", "say The pleasure was mine."]
  35. },
  36. {
  37. "trigger": ["how do you know"],
  38. "response": ["say Lucky guess, I guess", "emote shrugs."]
  39. },
  40. {
  41. "trigger": ["do you know", "who i am"],
  42. "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}."]
  43. },
  44. {
  45. "trigger": ["i like you", "youre great", "your great"],
  46. "response": ["say Aww.", "say I like you too!", "say That's nice of you to say."]
  47. },
  48. ];
  49. this.orders = [
  50. {
  51. "trigger": ["go east"],
  52. "response": ["east"]
  53. },
  54. {
  55. "trigger": ["go west"],
  56. "response": ["west"]
  57. },
  58. {
  59. "trigger": ["go north"],
  60. "response": ["north"]
  61. },
  62. {
  63. "trigger": ["go south"],
  64. "response": ["south"]
  65. },
  66. {
  67. "trigger": ["go northwest"],
  68. "response": ["nw"]
  69. },
  70. {
  71. "trigger": ["go northeast"],
  72. "response": ["ne"]
  73. },
  74. {
  75. "trigger": ["go southwest"],
  76. "response": ["sw"]
  77. },
  78. {
  79. "trigger": ["go southeast"],
  80. "response": ["se"]
  81. },
  82. {
  83. "trigger": ["go up"],
  84. "response": ["up"]
  85. },
  86. {
  87. "trigger": ["go down"],
  88. "response": ["down"]
  89. },
  90. {
  91. "trigger": ["enter"],
  92. "response": ["enter"]
  93. },
  94. {
  95. "trigger": ["leave"],
  96. "response": ["leave"]
  97. },
  98. {
  99. "trigger": ["inventory"],
  100. "response": ["inventory"]
  101. },
  102. ];
  103. }
  104. basicDecency(source, message, listener) {
  105. return this.mobileInstructionRules(source, message, listener, this.simple);
  106. }
  107. followsOrders(source, message, listener) {
  108. return this.mobileInstructionRules(source, message, listener, this.orders);
  109. }
  110. mobileInstructionRules(source, message, listener, instructionset) {
  111. if(source == null) {
  112. return false;
  113. }
  114. if(message.indexOf("shout") != -1) {
  115. return false;
  116. }
  117. for(var i = 0; i < instructionset.length; i++) {
  118. var response = this.triggerResponse(
  119. instructionset[i].trigger,
  120. instructionset[i].response,
  121. message
  122. );
  123. if(response != null) {
  124. var cleanResponse = this.parsedResponse(response, source, listener, message);
  125. if(typeof listener['mud_addCommand'] == 'function') {
  126. listener.mud_addCommand(listener, cleanResponse);
  127. return true;
  128. }
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. parsedResponse(response, source, listener) {
  135. response = response.replace("{source.name}", source.name);
  136. response = response.replace("{listener.name}", listener.name);
  137. return response;
  138. }
  139. triggerResponse(triggers, responses, message) {
  140. if(this.contains(triggers, message)) {
  141. return this.random(responses);
  142. }
  143. }
  144. random(responses) {
  145. var index = parseInt(Math.random() * responses.length);
  146. return responses[index];
  147. }
  148. contains(keys, message) {
  149. var cleanMessage = message.replace(/'/g, "");
  150. for(var i = 0; i < keys.length; i++) {
  151. var regex = new RegExp("\\b" + keys[i] + "\\b", "i");
  152. if(cleanMessage.search(regex) != -1) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. }
  159. module.exports = new Conversation();