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, :items
- def initialize(url, path, items)
- url = URI.parse(url) if url.is_a?(String)
- @url = url
- @path = path
- @items = items
- end
- def text
- @text ||= game_area.text.strip.gsub("\r", "").gsub(/\n+/, "\n").gsub(/\n+N\n+W E\n+S\n+Start Over/, "")
- end
- def found_items
- @found_items ||= text.scan(/You take the (\w+)\.|(\w+) and take it/).flatten.compact
- end
- def links
- @links ||= game_area.css("a").map{|a| [a.text, [@url+a[:href], (items+found_items)]] if a.text != "Start Over"}.compact.to_h
- end
- def doc
- @doc ||= fetch(url)
- end
- def key
- [url, items]
- end
- def print!
- puts "Path: #{path.join(" ")}"
- puts "Items: #{items.join(" ")}"
- puts text
- puts ""
- 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 = []
- # To see just the road taken
- @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]
- # To see all possibilities (doesn't actually get all as it can accidentally get items)
- # @path = []
- end
- def add!(url, path, items)
- state = State.new(url, path, items)
- return if @states[state.key]
- @states[state.key] = state
- @queue << state.key
- end
- def explore!
- state = @states[@queue.shift]
- state.links.each do |direction, (url, items)|
- add! url, state.path + [direction], items
- end
- end
- def follow_path!
- next_direction = @path.shift
- state = @states[@queue.shift]
- url, items = state.links[next_direction]
- add! url, state.path + [next_direction], items
- end
- def start!
- start_url = "http://mirror.otp22.com/chal/yxunomei.php?reset=yes"
- add! start_url, [], []
- # First follow the path
- until @path.empty?
- follow_path!
- end
- # Then explore
- until @queue.empty?
- explore!
- end
- @states.each do |url, state|
- state.print!
- end
- end
- end
- Adventure.new.start!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement