class UVaTools::User

Public Class Methods

load(username) click to toggle source
# File lib/uva-tools/user.rb, line 3
def self.load(username)
  if File.directory?(UVaTools::USER_SAVE_LOCATION) && File.exists?("#{UVaTools::USER_SAVE_LOCATION}/#{username}")
    Marshal.load(File.binread("#{UVaTools::USER_SAVE_LOCATION}/#{username}"))
  else
    nil
  end
end
new(username) click to toggle source
# File lib/uva-tools/user.rb, line 11
def initialize(username)
  @username = username
end

Public Instance Methods

has_solved?(prob_nums) click to toggle source
# File lib/uva-tools/user.rb, line 32
def has_solved?(prob_nums)
  res = Array(prob_nums).map do |prob_num|
    solved_by_number.has_key? prob_num
  end
  res.length < 2 ? res[0] : res
end
reload() click to toggle source
# File lib/uva-tools/user.rb, line 39
def reload
  @solved_pids = nil
  @solved = nil
  @unsolved = nil
  self
end
save() click to toggle source
# File lib/uva-tools/user.rb, line 46
def save
  FileUtils::mkdir_p UVaTools::USER_SAVE_LOCATION
  File.open("#{UVaTools::USER_SAVE_LOCATION}/#{@username}", 'w') {|f| f.write(Marshal.dump(self))}
  true
end
solved() click to toggle source
# File lib/uva-tools/user.rb, line 15
def solved
  solved_hash.values
end
solved_by_number() click to toggle source
# File lib/uva-tools/user.rb, line 19
def solved_by_number
  solved_hash
end
unsolved() click to toggle source
# File lib/uva-tools/user.rb, line 23
def unsolved
  @unsolved ||= begin
    prob_by_id = UVaTools.problems_by_id
    unsolved_pids.map do |pid|
      prob_by_id[pid]
    end.sort{ |a, b| b.dacu <=> a.dacu }
  end
end

Private Instance Methods

get_solved_pids() click to toggle source
# File lib/uva-tools/user.rb, line 80
def get_solved_pids
  uri = URI("http://uhunt.felix-halim.net/api/solved-bits/#{uid}")
  res = JSON.parse(Net::HTTP.get(uri))[0]['solved']
  res.each_with_index.map do |num, j|
    num.to_s(2).reverse.scan(/./).each_with_index.map do |c, i|
      j*32 + i if c == '1'
    end.compact
  end.flatten.sort
end
marshal_dump() click to toggle source
# File lib/uva-tools/user.rb, line 98
def marshal_dump
  [uid, @username, solved_pids]
end
marshal_load(array) click to toggle source
# File lib/uva-tools/user.rb, line 94
def marshal_load array
  @uid, @username, @solved_pids = array
end
reload_solved_pids() click to toggle source
# File lib/uva-tools/user.rb, line 76
def reload_solved_pids
  @solved_pids = get_solved_pids
end
solved_hash() click to toggle source
# File lib/uva-tools/user.rb, line 64
def solved_hash
  @solved ||= begin
    prob_by_id = UVaTools.problems_by_id
    h = {}
    solved_pids.each do |pid|
      problem = prob_by_id[pid]
      h[problem.number] = problem
    end
    h
  end
end
solved_pids() click to toggle source
# File lib/uva-tools/user.rb, line 60
def solved_pids
  @solved_pids ||= get_solved_pids
end
uid() click to toggle source
# File lib/uva-tools/user.rb, line 53
def uid
  @uid ||= begin
    uri = URI("http://uhunt.felix-halim.net/api/uname2uid/#{@username}")
    Net::HTTP.get(uri)
  end
end
unsolved_pids() click to toggle source
# File lib/uva-tools/user.rb, line 90
def unsolved_pids
  UVaTools.problems.map(&:id) - solved_pids
end