Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. require 'ostruct'
  2.  
  3. class Bounty
  4. class Parser
  5. @@task_assignment_regexs = {
  6. /It appears they have a creature problem they'd like you to solve/ => :cull,
  7. /It appears they need your help in tracking down some kind of lost heirloom/ => :heirloom,
  8. /The local furrier .+ has an order to fill and wants our help/ => :skins,
  9. /The local gem dealer, [^,]+, has an order to fill and wants our help/ => :gem,
  10. # /to provide a protective escort/ => :escort,
  11. /Hmm, I've got a task here from the town of ([^.]+?). The local [^,]+?, [^,]+, has asked for our aid. Head over there and see what you can do. Be sure to ASK about BOUNTIES./ => :herb,
  12. /It appears that a local resident urgently needs our help in some matter/ => :rescue,
  13. /It appears they have a bandit problem they'd like you to solve/ => :bandit,
  14. }
  15.  
  16. @@task_completed_regexs = {
  17. /^You have succeeded in your task and can return to the Adventurer's Guild/ => :taskmaster,
  18. /^You have located the heirloom and should bring it back/ => :heirloom,
  19. /^You succeeded in your task and should report back to/ => :dangerous,
  20. }
  21.  
  22. @@task_triggered_regexs = {
  23. /^You have made contact with the child you are to rescue and you must get (?:him|her) back alive to one of the (?:guardsmen) just inside ([^.]+)\.$/ => :rescue,
  24. /^You have been tasked to hunt down and kill a particularly dangerous ([^.]+) that has established a territory [oi]n (?:the\s+)?([^.]+?)(?: near [^.]+)?\. You have provoked (?:his|her|its) attention and now you must(?: return to where you left (?:him|her|it) and)? kill (?:him|her|it)!$/ => :dangerous,
  25. }
  26.  
  27. @@task_description_regexs = {
  28. /^You have been tasked to(?: help \w+)? suppress (bandit) activity (?:on|in) (?:the\s+)?([^.]+?)(?:\s+near [^.]+)?\. You need to kill (\d+) (?:more\s+)?of them to complete your task\.$/ => :bandit,
  29. /^You have been tasked to(?: help \w+)? suppress ([^.]+) activity (?:on|in) (?:the\s+)?([^.]+?)(?:\s+near [^.]+)?\. You need to kill (\d+) (?:more\s+)?of them to complete your task\.$/ => :cull,
  30. /^You have been tasked to recover (?:an?|some) ([^.]+) that an unfortunate citizen lost after being attacked by an? ([^.]+) [oi]n (?:the\s+)?([^.]+?)(?:\s+near [^.]+)?\. The heirloom can be identified by the initials \w+ engraved upon it\. [^.]*?(LOOT|SEARCH)[^.]+\.$/ => :heirloom,
  31. /^You have been tasked to retrieve (\d+) ([^.]+?)s? of at least ([^.]+) quality for [^.]+ in ([^.]+?)\. You can SKIN them off the corpse of an? ([^.]+) or purchase them from another adventurer\. You can SELL the skins to the furrier as you collect them\."$/ => :skins,
  32. /^The gem dealer in [^,]+, [^,]+, has received orders from multiple customers requesting (?:an?|some) ([^.]+)\. You have been tasked to retrieve (\d+) (?:more\s+)?of them\. You can SELL them to the gem dealer as you find them\.$/ => :gem,
  33. /^(?:The taskmaster told you: ")?I've got a special mission for you\. A certain client has hired us to provide a protective escort on (?:his|her) upcoming journey\. Go to ([^.]+) and WAIT for (?:him|her) to meet you there\. You must guarantee (?:his|her) safety to ([^.]+) as soon as you can, being ready for any dangers that the two of you may face\. Good luck!"?$/ => :escort,
  34. /^The .+? in ([^,]+?), [^,]+?, is working on a concoction that requires (?:an?|some) ([^.]+?) found [oi]n (?:the\s+)?([^.]+?)(?:\s+near [^.]+)?\. These samples must be in pristine condition\. You have been tasked to retrieve (\d+) (?:more\s+)?samples?\.$/ => :herb,
  35. /^You have been tasked to (?: help \w+|hunt down and) kill a (?:particularly )?dangerous ([^.]+) that has established a territory [oi]n (?:the\s+)?([^.]+?)(?: near [^.]+)?\. You can get its attention by killing other creatures of the same type in its territory\.$/ => :dangerous,
  36. /^You have been tasked to rescue the young runaway (?:son|daughter) of a local citizen\. A local divinist has had visions of the child fleeing from an? ([^.]+) [oi]n (?:the\s+)?([^.]+?)(?:\s+near [^.]+)?\. Find the area where the child was last seen and clear out the creatures that have been tormenting (?:him|her) in order to bring (?:him|her) out of hiding\.$/ => :rescue,
  37. }
  38.  
  39. @@regex_for_task_description_of = @@task_description_regexs.invert
  40. @@regex_for_trigger_description_of = @@task_triggered_regexs.invert
  41. @@regex_for_assignment_of = @@task_assignment_regexs.invert
  42.  
  43. @@requirement_labels_for_task = {
  44. :cull => [ :creature, :area, :number ],
  45. :heirloom => [ :item, :creature, :area, :action ],
  46. :skins => [ :number, :skin, :quality, :town, :creature ],
  47. :gem => [ :gem, :number ],
  48. :escort => [ :start, :destination ],
  49. :herb => [ :town, :herb, :area, :number ],
  50. :rescue => [ :creature, :area ],
  51. :dangerous => [ :creature, :area ],
  52. :bandit => [ :creature, :area, :number ],
  53. }
  54.  
  55. @@task_failed_regexs = {
  56. /^You have failed in your task/ => :taskmaster,
  57. /^The child you were tasked to rescue is gone and your task is failed. Report this failure to the Adventurer's Guild./ => :taskmaster,
  58. }
  59.  
  60. def self.parse(desc)
  61. return nil if desc.nil? or desc.empty?
  62. @description = desc
  63. @requirements = nil #Hash.new
  64. @task = nil
  65. @status = nil
  66.  
  67. # Figure task type and status from description
  68. if @description =~ /^You are not currently assigned a task/
  69. @task = :taskmaster
  70. elsif check_task_description( desc )
  71. @status = :unfinished
  72. @task = check_task_description( desc )
  73. @requirements = Hash.new
  74. elsif check_task_assignment( desc )
  75. @status = :assigned
  76. @task = check_task_assignment( desc )
  77. if [:herb].include? @task
  78. match_data = @@regex_for_assignment_of[@task].match( @description )
  79. @requirements = { town: match_data[1] }
  80. end
  81. elsif check_task_completed( desc )
  82. @status = :done
  83. @task = check_task_completed( desc )
  84. if :heirloom == @task and defined? @@last_heirloom_item and not @@last_heirloom_item.nil?
  85. @requirements = { :item => @@last_heirloom_item }
  86. end
  87. elsif check_task_failed( desc )
  88. @status = :failed
  89. @task = check_task_failed( desc )
  90. elsif check_task_triggered( desc )
  91. @status = :triggered
  92. @task = check_task_triggered( desc )
  93. @requirements = Hash.new
  94.  
  95. if :dangerous == @task
  96. if @description =~ @@regex_for_trigger_description_of[@task]
  97. match_data = @@regex_for_trigger_description_of[@task].match( @description ).to_a.flatten
  98. match_data.shift
  99. @@requirement_labels_for_task[@task].each_with_index { |label, index|
  100. @requirements[label] = match_data[index]
  101. }
  102. else
  103. echo "ERROR: couldn't parse triggered dangerous task requirements"
  104. end
  105. elsif :heirloom == @task and defined? @@last_heirloom_item and not @@last_heirloom_item.nil?
  106. @requirements = { :item => @@last_heirloom_item }
  107. end
  108. else
  109. #echo "ERROR: did not recognize bounty description:"
  110. #echo desc
  111. return nil
  112. end
  113.  
  114. if @task.nil?
  115. # we don't have a bounty, so it doesn't have any requirements
  116. elsif :unfinished == @status and @description =~ @@regex_for_task_description_of[@task]
  117. match_data = @@regex_for_task_description_of[@task].match( @description ).to_a.flatten
  118. match_data.shift
  119. @@requirement_labels_for_task[@task].each_with_index { |label, index|
  120. if :action == label
  121. @requirements[label] = match_data[index].downcase
  122. elsif :number == label
  123. @requirements[label] = match_data[index].to_i
  124. elsif :creature == label
  125. # Clean up creature value if we need to remove extra adjectives (eg beings, centaurs)
  126. critter = match_data[index]
  127. critter = 'being' if critter =~ /^\w+ being$/
  128. @requirements[label] = critter
  129. else
  130. @requirements[label] = match_data[index]
  131. end
  132. }
  133. if :heirloom == @task
  134. @@last_heirloom_item = @requirements[:item]
  135. end
  136. elsif :unfinished == @status
  137. echo "ERROR: couldn't parse requirements from description"
  138. end
  139.  
  140. return { description: @description, requirements: @requirements, task: @task, status: @status }
  141. end
  142.  
  143.  
  144. def self.check_task_description(desc)
  145. return nil if desc.nil? or desc.empty?
  146. for regex in @@task_description_regexs.keys
  147. if desc =~ regex
  148. return :bandit if desc =~ /bandit/
  149. return @@task_description_regexs[regex]
  150. end
  151. end
  152. return nil
  153. end
  154.  
  155. def self.check_task_assignment(desc)
  156. return nil if desc.nil? or desc.empty?
  157. for regex in @@task_assignment_regexs.keys
  158. return @@task_assignment_regexs[regex] if desc =~ regex
  159. end
  160. return nil
  161. end
  162.  
  163. def self.check_task_completed(desc)
  164. return nil if desc.nil? or desc.empty?
  165. for regex in @@task_completed_regexs.keys
  166. return @@task_completed_regexs[regex] if desc =~ regex
  167. end
  168. return nil
  169. end
  170.  
  171. def self.check_task_failed(desc)
  172. return nil if desc.nil? or desc.empty?
  173. for regex in @@task_failed_regexs.keys
  174. return @@task_failed_regexs[regex] if desc =~ regex
  175. end
  176. return nil
  177. end
  178.  
  179. def self.check_task_triggered(desc)
  180. return nil if desc.nil? or desc.empty?
  181. for regex in @@task_triggered_regexs.keys
  182. return @@task_triggered_regexs[regex] if desc =~ regex
  183. end
  184. return nil
  185. end
  186. end
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement