Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "uri"
- require "nokogiri"
- require "shellwords"
- class State
- attr_reader :url, :path
- def initialize(url, path)
- url = URI.parse(url) if url.is_a?(String)
- @url = url
- @path = path
- end
- def text
- @text ||= game_area.text.strip.gsub("\r", "").gsub(/\n+N\n+W E\n+S\n+Start Over/, "")
- end
- def links
- @links ||= game_area.css("a").map{|a| [a.text, @url+a[:href]]}.to_h
- end
- def doc
- @doc ||= fetch(url)
- end
- private
- def game_area
- Nokogiri::HTML(doc[/<pre>(.*)<\/pre>/m, 1])
- end
- def fetch(url)
- `curl -s --cookie #{cookie.shellescape} #{url.to_s.shellescape}`
- end
- def cookie
- "THE COOKIE"
- end
- end
- class Adventure
- def initialize
- @states = {}
- @queue = []
- end
- def add!(url, path)
- return if @states[url]
- @states[url] = State.new(url, path)
- @queue << url
- end
- def explore!
- state = @states[@queue.shift]
- state.links.each do |direction, url|
- add! url, state.path + [direction]
- end
- end
- def start!
- start_url = "http://mirror.otp22.com/chal/yxunomei.php?reset=yes"
- add! start_url, []
- until @queue.empty?
- explore!
- end
- @states.each do |url, state|
- puts "State: #{state.path.join(" ")}"
- puts state.text
- puts ""
- end
- binding.pry
- end
- end
- Adventure.new.start!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement