Created
April 12, 2009 02:14
-
-
Save burke/93830 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # zorkmech.rb | |
| # ZorkMech | |
| # | |
| # Created by Burke Libbey on 11/04/09. | |
| # Copyright (c) 2009 Chromium 53. All rights reserved. | |
| # | |
| begin | |
| require 'rubygems' | |
| rescue LoadError | |
| end | |
| require 'mechanize' | |
| require 'logger' | |
| require 'json' | |
| require 'open-uri' | |
| $log = Logger.new($stdout) | |
| $log.level = Logger::INFO | |
| $mech = WWW::Mechanize.new | |
| $my = {} | |
| class ZorkMech | |
| attr_reader :zorkmids, :health, :level, :ap, :coconuts, :xp_to_level | |
| def initialize(username, password) | |
| $log.info("Logging in...") | |
| page = $mech.get("http://legendsofzork.com") | |
| page.forms[0].login = username | |
| page.forms[0].password = password | |
| @home = page.forms[0].submit | |
| $log.info("Logged in.") | |
| # We need to prime the character data cache, which we can't do | |
| # from the map overview page. | |
| get_page('/arena') | |
| end | |
| def get_page(url) | |
| page = $mech.get(url) | |
| update_character_info(page) | |
| page | |
| end | |
| private ############################################## | |
| def to_number(str) | |
| if str.index("K") | |
| return (1000 * str.to_f).ceil | |
| else | |
| return str.to_i | |
| end | |
| end | |
| def update_character_info(page) | |
| info_mappings = { | |
| "level" => "#character-level", | |
| "zorkmids" => "#current-zorkmids", | |
| "health" => "#health-remaining", | |
| "level" => "#character-level", | |
| "ap" => "#action-points-remaining", | |
| "coconuts" => ".coconuts .number", | |
| "encumbrance" => "#encumbrance", | |
| "fame" => "#character-fame", | |
| "xp_to_level" => "#character-xp" | |
| } | |
| info_mappings.each do |k,v| | |
| $my[k] = to_number(page.parser.css(v).text.strip.sub('Level ','')) | |
| end | |
| $my['id'] = page.parser.css('h2.char-name a').attr('href').split('/')[2] | |
| info = Opponent.new($my['id']).info | |
| $my['ar'] = info['ar'].to_i | |
| $my['dr'] = info['dr'].to_i | |
| end | |
| end | |
| class Arena | |
| def self.fight! | |
| (1..9).each do |n| | |
| opps = Arena.winnable_opponents(n) | |
| puts opps | |
| opps.each do |opponent| | |
| opponent.fight | |
| end | |
| end | |
| end | |
| def self.winnable_opponents(page) | |
| page = $mech.get("http://legendsofzork.com/arena?direction=asc&page=#{page}&sort=level") | |
| table = page.parser.css("#tab-arena-1-content table") | |
| opponents = [] | |
| (table/"tr").to_ary[1..-1].each do |tr| | |
| href = ((tr/"td").first/"a").attr('href') | |
| opponents << href.sub(/javascript: select\('(\d+)-.*'\);/,'\1') | |
| end | |
| opponents.map!{|o| Opponent.new(o)} | |
| opponents.reject!{|o|o.scary?} | |
| opponents | |
| end | |
| end | |
| class Opponent | |
| # Memoized. | |
| @@infos = {} | |
| attr_reader :info | |
| def initialize(id) | |
| @@infos[id] ||= JSON.parse($mech.get("/arena/select?opponent=#{id}").body) | |
| @info = @@infos[id] | |
| end | |
| def scary? | |
| return "fame" if @info['fame'].to_f > 50 | |
| return "level" if @info['level'].to_i > $my["level"] | |
| return "ar" if @info['ar'].to_i > $my['ar']*0.7 | |
| return "dr" if @info['dr'].to_i > $my['dr']*0.7 | |
| return false | |
| end | |
| def fight | |
| page = $mech.get('/arena') | |
| page.forms[1].opponent = @info['id'] | |
| page = page.forms[1].submit | |
| page.forms[0].wager = $my['level']*100 | |
| page = page.forms[0].submit | |
| page.parser.css("#combat-result").text | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment