Advertisement
Guest User

Untitled

a guest
Apr 15th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. -- Advanced NPC System by Jiddo
  2.  
  3. if KeywordHandler == nil then
  4. KeywordNode = {
  5. keywords = nil,
  6. callback = nil,
  7. parameters = nil,
  8. children = nil,
  9. parent = nil
  10. }
  11.  
  12. -- Created a new keywordnode with the given keywords, callback function and parameters and without any childNodes.
  13. function KeywordNode:new(keys, func, param)
  14. local obj = {}
  15. obj.keywords = keys
  16. obj.callback = func
  17. obj.parameters = param
  18. obj.children = {}
  19. setmetatable(obj, self)
  20. self.__index = self
  21. return obj
  22. end
  23.  
  24. -- Calls the underlying callback function if it is not nil.
  25. function KeywordNode:processMessage(cid, message)
  26. return (self.callback == nil or self.callback(cid, message, self.keywords, self.parameters, self))
  27. end
  28.  
  29. -- Returns true if message contains all patterns/strings found in keywords.
  30. function KeywordNode:checkMessage(message)
  31. if self.keywords.callback ~= nil then
  32. return self.keywords.callback(self.keywords, message)
  33. end
  34. for i,v in ipairs(self.keywords) do
  35. if type(v) == 'string' then
  36. local a, b = string.find(message, v)
  37. if a == nil or b == nil then
  38. return false
  39. end
  40. end
  41. end
  42. return true
  43. end
  44.  
  45. -- Returns the parent of this node or nil if no such node exists.
  46. function KeywordNode:getParent()
  47. return self.parent
  48. end
  49.  
  50. -- Returns an array of the callback function parameters assosiated with this node.
  51. function KeywordNode:getParameters()
  52. return self.parameters
  53. end
  54.  
  55. -- Returns an array of the triggering keywords assosiated with this node.
  56. function KeywordNode:getKeywords()
  57. return self.keywords
  58. end
  59.  
  60. -- Adds a childNode to this node. Creates the childNode based on the parameters (k = keywords, c = callback, p = parameters)
  61. function KeywordNode:addChildKeyword(keywords, callback, parameters)
  62. local new = KeywordNode:new(keywords, callback, parameters)
  63. return self:addChildKeywordNode(new)
  64. end
  65.  
  66. -- Adds a pre-created childNode to this node. Should be used for example if several nodes should have a common child.
  67. function KeywordNode:addChildKeywordNode(childNode)
  68. self.children[#self.children + 1] = childNode
  69. childNode.parent = self
  70. return childNode
  71. end
  72.  
  73. KeywordHandler = {
  74. root = nil,
  75. lastNode = {}
  76. }
  77.  
  78. -- Creates a new keywordhandler with an empty rootnode.
  79. function KeywordHandler:new()
  80. local obj = {}
  81. obj.root = KeywordNode:new(nil, nil, nil)
  82. setmetatable(obj, self)
  83. self.__index = self
  84. return obj
  85. end
  86.  
  87. -- Resets the lastNode field, and this resetting the current position in the node hierarchy to root.
  88. function KeywordHandler:reset(cid)
  89. if self.lastNode[cid] then
  90. self.lastNode[cid] = nil
  91. end
  92. end
  93.  
  94. -- Makes sure the correct childNode of lastNode gets a chance to process the message.
  95. function KeywordHandler:processMessage(cid, message)
  96. local node = self:getLastNode(cid)
  97. if node == nil then
  98. error('No root node found.')
  99. return false
  100. end
  101.  
  102. local ret = self:processNodeMessage(node, cid, message)
  103. if ret then
  104. return true
  105. end
  106.  
  107. if node:getParent() then
  108. node = node:getParent() -- Search through the parent.
  109. local ret = self:processNodeMessage(node, cid, message)
  110. if ret then
  111. return true
  112. end
  113. end
  114.  
  115. if node ~= self:getRoot() then
  116. node = self:getRoot() -- Search through the root.
  117. local ret = self:processNodeMessage(node, cid, message)
  118. if ret then
  119. return true
  120. end
  121. end
  122. return false
  123. end
  124.  
  125. -- Tries to process the given message using the node parameter's children and calls the node's callback function if found.
  126. -- Returns the childNode which processed the message or nil if no such node was found.
  127. function KeywordHandler:processNodeMessage(node, cid, message)
  128. local messageLower = string.lower(message)
  129. for i, childNode in pairs(node.children) do
  130. if childNode:checkMessage(messageLower) then
  131. local oldLast = self.lastNode[cid]
  132. self.lastNode[cid] = childNode
  133. childNode.parent = node -- Make sure node is the parent of childNode (as one node can be parent to several nodes).
  134. if childNode:processMessage(cid, message) then
  135. return true
  136. end
  137. self.lastNode[cid] = oldLast
  138. end
  139. end
  140. return false
  141. end
  142.  
  143. -- Returns the root keywordnode
  144. function KeywordHandler:getRoot()
  145. return self.root
  146. end
  147.  
  148. -- Returns the last processed keywordnode or root if no last node is found.
  149. function KeywordHandler:getLastNode(cid)
  150. return self.lastNode[cid] or self:getRoot()
  151. end
  152.  
  153. -- Adds a new keyword to the root keywordnode. Returns the new node.
  154. function KeywordHandler:addKeyword(keys, callback, parameters)
  155. return self:getRoot():addChildKeyword(keys, callback, parameters)
  156. end
  157.  
  158. -- Moves the current position in the keyword hierarchy count steps upwards. Count defalut value = 1.
  159. -- This function MIGHT not work properly yet. Use at your own risk.
  160. function KeywordHandler:moveUp(count)
  161. local steps = count
  162. if steps == nil or type(steps) ~= "number" then
  163. steps = 1
  164. end
  165. for i = 1, steps, 1 do
  166. if self.lastNode[cid] == nil then
  167. break
  168. end
  169. self.lastNode[cid] = self.lastNode[cid]:getParent() or self:getRoot()
  170. end
  171. return self.lastNode[cid]
  172. end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement