Advertisement
t_a_w

Labyrinth

Feb 7th, 2017
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.28 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "uri"
  4. require "nokogiri"
  5. require "shellwords"
  6.  
  7. class State
  8.   attr_reader :url, :path, :items
  9.   def initialize(url, path, items)
  10.     url = URI.parse(url) if url.is_a?(String)
  11.     @url = url
  12.     @path = path
  13.     @items = items
  14.   end
  15.  
  16.   def text
  17.     @text ||= game_area.text.strip.gsub("\r", "").gsub(/\n+/, "\n").gsub(/\n+N\n+W  E\n+S\n+Start Over/, "")
  18.   end
  19.  
  20.   def found_items
  21.     @found_items ||= text.scan(/You take the (\w+)\.|(\w+) and take it/).flatten.compact
  22.   end
  23.  
  24.   def links
  25.     @links ||= game_area.css("a").map{|a| [a.text, [@url+a[:href], (items+found_items)]] if a.text != "Start Over"}.compact.to_h
  26.   end
  27.  
  28.   def doc
  29.     @doc ||= fetch(url)
  30.   end
  31.  
  32.   def key
  33.     [url, items]
  34.   end
  35.  
  36.   def print!
  37.     puts "Path: #{path.join(" ")}"
  38.     puts "Items: #{items.join(" ")}"
  39.     puts text
  40.     puts ""
  41.   end
  42.  
  43. private
  44.  
  45.   def game_area
  46.     Nokogiri::HTML(doc[/<pre>(.*)<\/pre>/m, 1])
  47.   end
  48.  
  49.   def fetch(url)
  50.     `curl -s --cookie #{cookie.shellescape} #{url.to_s.shellescape}`
  51.   end
  52.  
  53.   def cookie
  54.     "THE COOKIE"
  55.   end
  56. end
  57.  
  58. class Adventure
  59.   def initialize
  60.     @states = {}
  61.     @queue = []
  62.     # To see just the road taken
  63.     @path = %W[W S E E N S W W N E N N N E N S E N N E E S W S E E S N W W N E N N N E N S W N N N]
  64.     # To see all possibilities (doesn't actually get all as it can accidentally get items)
  65.     # @path = []
  66.   end
  67.  
  68.   def add!(url, path, items)
  69.     state = State.new(url, path, items)
  70.     return if @states[state.key]
  71.     @states[state.key] = state
  72.     @queue << state.key
  73.   end
  74.  
  75.   def explore!
  76.     state = @states[@queue.shift]
  77.     state.links.each do |direction, (url, items)|
  78.       add! url, state.path + [direction], items
  79.     end
  80.   end
  81.  
  82.   def follow_path!
  83.     next_direction = @path.shift
  84.     state = @states[@queue.shift]
  85.     url, items = state.links[next_direction]
  86.     add! url, state.path + [next_direction], items
  87.   end
  88.  
  89.   def start!
  90.     start_url = "http://mirror.otp22.com/chal/yxunomei.php?reset=yes"
  91.     add! start_url, [], []
  92.     # First follow the path
  93.     until @path.empty?
  94.       follow_path!
  95.     end
  96.     # Then explore
  97.     until @queue.empty?
  98.       explore!
  99.     end
  100.     @states.each do |url, state|
  101.       state.print!
  102.     end
  103.   end
  104. end
  105.  
  106. Adventure.new.start!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement