class LevelSet

Attributes

name[RW]
stats[RW]

Public Class Methods

new(name, defaults, neverball_scores) click to toggle source
# File lib/scores.rb, line 104
def initialize(name, defaults, neverball_scores)
  @name = name
  @defaults = defaults
  @neverball_scores = neverball_scores
  update
end

Public Instance Methods

to_pretty_string() click to toggle source
# File lib/scores.rb, line 122
def to_pretty_string
  s = StringIO.new
  s.write "## #{self.name} "
  if not @stats[:challenge][:completed]
    s.puts "{ ____ }"
  else
    k = []
    ["U", "C"].each_with_index { |r,i|
      if @stats[:challenge][:ranks][i] == 0
        k << r
      else
        k << "."
      end
    }
    s.puts "{ #{'%-4.4s' % k.join(" ")} }"
  end
  0.upto(4) { |i|
    0.upto(4) { |j|
      l = @stats[:levels][i*5+j]
      if l[:state] == :locked
        s.write "{ /////// }"
      elsif l[:state] == :unlocked
        s.write "{ _______ }"
      elsif l[:total] >= 3
        s.write "{         }"
      else
        k = []
        ["T", "U", "C"].each_with_index { |r,i|
          if l[:ranks][i] == 0
            k << r
          else
            k << "."
          end
        }
        s.write "{ #{'%-7.7s' % k.join(" ")} }"
      end
      if j == 4
        s.write "\n"
      else
        s.write " "
      end
    }
  }
  scm = "#{@stats[:completed]}/#{@stats[:maxcompleted]}"
  spr = "#{Integer((100.0/@stats[:maxcompleted])*@stats[:completed])}"
  tcm = "#{@stats[:total]}/#{@stats[:maxtotal]}"
  tpr = "#{Integer((100.0/@stats[:maxtotal])*@stats[:total])}"
  s.write ">> completed: #{scm} (#{spr}%), total: #{tcm} (#{tpr}%)\n"
  return s.string
end
update() click to toggle source
update

reread and count and diff

# File lib/scores.rb, line 111
def update()
  f = @defaults
  puts @defaults
  f = @neverball_scores+"/#{@name}.txt" if File.exist?(@neverball_scores+"/#{@name}.txt")
  data = nil
  File.open(f) { |f|
    data = parse_scores(f)
  }
  stats = count_stats(data)
  @stats = stats
end

Private Instance Methods

count_stats(a) click to toggle source
# File lib/scores.rb, line 69
def count_stats(a)
  stats = { completed: 0, maxcompleted: 0, total: 0, maxtotal: 0, challenge: { completed: false, ranks: [], total: 0 }, levels: [] }

  0.upto(1) { |i|
    stats[:maxtotal] += 1
    r = get_rank(a[0][i])
    stats[:challenge][:ranks] << r
    stats[:challenge][:total] += r
    stats[:total] += r
  }

  stats[:maxcompleted] += 1
  if get_r(a[0][0]) > 0 or get_r(a[0][1]) > 0
    stats[:challenge][:completed] = true
    stats[:completed] += 1
  end


  a[1..].each { |e|
    ll = { name: e[0][1], state: e[0][0], ranks:[] , total: 0 }
    stats[:maxcompleted] += 1
    stats[:completed] += 1 if ll[:state] == :completed
    1.upto(3) { |i|
      stats[:maxtotal] += 1
      r = get_rank(e[i])
      ll[:ranks] << r
      stats[:total] += r
      ll[:total] += r
    }
    stats[:levels] << ll
  }
  return stats
end
get_chomp(u) click to toggle source
# File lib/scores.rb, line 19
def get_chomp(u)
  s = u.gets("\n")
  return nil if s == nil
  return s.chomp
end
get_r(o) click to toggle source
# File lib/scores.rb, line 54
def get_r(o)
  i = 3
  o.each { |e|
    break unless DIFFICULTIES.include? e[2]
    i -= 1
  }
  return i
end
get_rank(o) click to toggle source
# File lib/scores.rb, line 62
def get_rank(o)
  if get_r(o) == 3
    return 1
  else
    return 0
  end
end
parse_scores(e) click to toggle source
# File lib/scores.rb, line 24
def parse_scores(e)
  raise unless get_chomp(e) == "version 2"
  raise unless get_chomp(e).split(" ")[0]  == "set"
  a = []
  o = []
  1.upto(2) {
    k = []
    1.upto(3) {
      k << take_score(get_chomp(e))
    }
    o << k
  }
  a << o
  loop do
    level = get_chomp(e)
    return a if level == nil
    _, st, _, path = level.split(" ")
    st_ = [:unlocked, :locked, :completed][Integer(st)]
    o = [[st_,path]]
    1.upto(3) {
      k = []
      1.upto(3) {
        k << take_score(get_chomp(e))
      }
      o << k
    }
    a << o
  end
  raise
end
take_score(h) click to toggle source
# File lib/scores.rb, line 15
def take_score(h)
  t_,c_,n_ = h.split(" ")
  return [Integer(t_)/100, Integer(c_), n_]
end