Class: Smogon::Movesetdex

Inherits:
Object
  • Object
show all
Defined in:
lib/smogon/movesetdex.rb

Class Method Summary collapse

Class Method Details

.get(name, tier = nil, metagame = nil, fields = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smogon/movesetdex.rb', line 22

def self.get(name, tier = nil, metagame = nil, fields = nil)
  incapsulate = fields == nil

  fields ||= [
    'name',
    'movesets' => [
      'name',
      { 'tags'      => %w(shorthand) },
      { 'items'     => %w(name)      },
      { 'abilities' => %w(name)      },
      { 'natures'   => %w(hp patk pdef spatk spdef spe) },
      { 'moveslots' => [ 'slot', { 'move' => %(name) } ] },
      { 'evconfigs' => %w(hp patk pdef spatk spdef spe) }
    ]
  ]

  response = if metagame
    API.using_metagame(metagame) do
      API.request 'pokemon', name, fields
    end
  else
    API.request 'pokemon', name, fields
  end
  return nil      if response.is_a?(String) || response.empty? || response.first.empty?
  return response if not incapsulate

  response = response.first

  results = [].tap do |movesets|
    response['movesets'].each do |movesetdex|
      movesets << Moveset.new.tap do |moveset|
        moveset.pokemon = response['name']
        moveset.name    = movesetdex['name']
        moveset.tier    = movesetdex['tags'][0]['shorthand']
        moveset.item    = movesetdex['items'].collect(&:values).flatten
        moveset.ability = movesetdex['abilities'].collect(&:values).flatten
        moveset.nature = movesetdex['natures'].map { |nature| Naturedex.get(nature) }

        moveset.moves = []
        movesetdex['moveslots'].each do |moveslot|
          slot = moveslot['slot'] - 1

          if moveset.moves[slot]
            moveset.moves[slot] << moveslot['move_name']
          else
            moveset.moves << [ moveslot['move_name'] ]
          end
        end

        moveset.evs = [].tap do |evs|
          ['hp', 'patk', 'pdef', 'spatk', 'spdef', 'spe'].each do |stat|
            evs << movesetdex['evconfigs'].first[stat]
          end
        end.join ' / '
      end
    end
  end

  tier ? results.reject { |moveset| moveset.tier.downcase != tier.downcase } : results
end