Advertisement
t_a_w

Labyrinth Explorer

Feb 7th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.38 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
  9.   def initialize(url, path)
  10.     url = URI.parse(url) if url.is_a?(String)
  11.     @url = url
  12.     @path = path
  13.   end
  14.  
  15.   def text
  16.     @text ||= game_area.text.strip.gsub("\r", "").gsub(/\n+N\n+W  E\n+S\n+Start Over/, "")
  17.   end
  18.  
  19.   def links
  20.     @links ||= game_area.css("a").map{|a| [a.text, @url+a[:href]]}.to_h
  21.   end
  22.  
  23.   def doc
  24.     @doc ||= fetch(url)
  25.   end
  26.  
  27. private
  28.  
  29.   def game_area
  30.     Nokogiri::HTML(doc[/<pre>(.*)<\/pre>/m, 1])
  31.   end
  32.  
  33.   def fetch(url)
  34.     `curl -s --cookie #{cookie.shellescape} #{url.to_s.shellescape}`
  35.   end
  36.  
  37.   def cookie
  38.     "THE COOKIE"
  39.   end
  40. end
  41.  
  42. class Adventure
  43.   def initialize
  44.     @states = {}
  45.     @queue = []
  46.   end
  47.  
  48.   def add!(url, path)
  49.     return if @states[url]
  50.     @states[url] = State.new(url, path)
  51.     @queue << url
  52.   end
  53.  
  54.   def explore!
  55.     state = @states[@queue.shift]
  56.     state.links.each do |direction, url|
  57.       add! url, state.path + [direction]
  58.     end
  59.   end
  60.  
  61.   def start!
  62.     start_url = "http://mirror.otp22.com/chal/yxunomei.php?reset=yes"
  63.     add! start_url, []
  64.     until @queue.empty?
  65.       explore!
  66.     end
  67.     @states.each do |url, state|
  68.       puts "State: #{state.path.join(" ")}"
  69.       puts state.text
  70.       puts ""
  71.     end
  72.     binding.pry
  73.   end
  74. end
  75.  
  76.  
  77. Adventure.new.start!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement