Guest User

keyworldhandler.lua

a guest
Nov 7th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | None | 0 0
  1. if(KeywordHandler == nil) then
  2.     KeywordNode = {
  3.         keywords = nil,
  4.         callback = nil,
  5.         parameters = nil,
  6.         children = nil,
  7.         parent = nil
  8.     }
  9.  
  10.     -- Created a new keywordnode with the given keywords, callback function and parameters and without any childNodes.
  11.     function KeywordNode:new(keys, func, param)
  12.         local obj = {}
  13.         obj.keywords = keys
  14.         obj.callback = func
  15.         obj.parameters = param
  16.         obj.children = {}
  17.         setmetatable(obj, self)
  18.         self.__index = self
  19.         return obj
  20.     end
  21.  
  22.     -- Calls the underlying callback function if it is not nil.
  23.     function KeywordNode:processMessage(cid, message)
  24.         return (self.callback == nil or self.callback(cid, message, self.keywords, self.parameters, self))
  25.     end
  26.  
  27.     -- Returns true if message contains all patterns/strings found in keywords.
  28.     function KeywordNode:checkMessage(message)
  29.         local ret = true
  30.         if(self.keywords.callback ~= nil) then
  31.             return self.keywords.callback(self.keywords, message)
  32.         end
  33.         for i, v in ipairs(self.keywords) do
  34.             if(type(v) == 'string') then
  35.                 local a, b = string.find(message, v)
  36.                 if(a == nil or b == nil) then
  37.                     ret = false
  38.                     break
  39.                 end
  40.             end
  41.         end
  42.         return ret
  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.         table.insert(self.children, childNode)
  69.         childNode.parent = self
  70.         return childNode
  71.     end
  72.  
  73.     KeywordHandler = {
  74.         root = nil,
  75.         lastNode = nil
  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()
  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
  132.                 self.lastNode = 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.                 else
  137.                     self.lastNode = oldLast
  138.                 end
  139.             end
  140.         end
  141.         return false
  142.     end
  143.  
  144.     -- Returns the root keywordnode
  145.     function KeywordHandler:getRoot()
  146.         return self.root
  147.     end
  148.  
  149.     -- Returns the last processed keywordnode or root if no last node is found.
  150.     function KeywordHandler:getLastNode()
  151.         return self.lastNode or self:getRoot()
  152.     end
  153.  
  154.     -- Adds a new keyword to the root keywordnode. Returns the new node.
  155.     function KeywordHandler:addKeyword(keys, callback, parameters)
  156.         return self:getRoot():addChildKeyword(keys, callback, parameters)
  157.     end
  158.  
  159.     -- Moves the current position in the keyword hierarchy count steps upwards. Count defalut value = 1.
  160.     --  This function MIGHT not work properly yet. Use at your own risk.
  161.     function KeywordHandler:moveUp(count)
  162.         local steps = count
  163.         if(steps == nil) then
  164.             steps = 1
  165.         end
  166.         for i = 1, steps,1 do
  167.             if(self.lastNode == nil) then
  168.                 break
  169.             else
  170.                 self.lastNode = self.lastNode:getParent() or self:getRoot()
  171.             end
  172.         end
  173.         return self.lastNode
  174.     end
  175. end
Advertisement
Add Comment
Please, Sign In to add comment