Class: Pktool::Pokemon

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/models/pokemon.rb

Constant Summary collapse

@@ways =
[:AS, :CS, :hAS, :hCS, :HB, :HD, :HAs, :HCs]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abilityObject

Returns the value of attribute ability.



14
15
16
# File 'lib/models/pokemon.rb', line 14

def ability
  @ability
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/models/pokemon.rb', line 15

def description
  @description
end

#effort_valueObject Also known as: ev

Returns the value of attribute effort_value.



14
15
16
# File 'lib/models/pokemon.rb', line 14

def effort_value
  @effort_value
end

#individual_valueObject Also known as: iv

Returns the value of attribute individual_value.



14
15
16
# File 'lib/models/pokemon.rb', line 14

def individual_value
  @individual_value
end

#itemObject

Returns the value of attribute item.



14
15
16
# File 'lib/models/pokemon.rb', line 14

def item
  @item
end

#natureObject

Returns the value of attribute nature.



14
15
16
# File 'lib/models/pokemon.rb', line 14

def nature
  @nature
end

Class Method Details

.fetch(name, feature = {}) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/models/pokemon.rb', line 21

def self.fetch(name, feature = {})
  pokemon = self.find_by_name(name)
  raise Error, "存在ないポケモンです。" unless pokemon
  pokemon.set(feature)
  return pokemon
end

.waysObject



28
29
30
# File 'lib/models/pokemon.rb', line 28

def self.ways
  return @@ways
end

Instance Method Details

#base_statObject



32
33
34
# File 'lib/models/pokemon.rb', line 32

def base_stat
  { H: self.H, A: self.A, B: self.B, C: self.C, D: self.D, S: self.S, 重さ: self.weight}
end

#effected_statistics(name) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/models/pokemon.rb', line 84

def effected_statistics(name)
  effected = statistics(name)
  effected *= rank_effect
  effected *= 1.5 if name == :A && @item == "こだわりハチマキ"
  effected *= 1.5 if name == :C && @item == "こだわりメガネ"

  effected
end

#rank_effectObject



93
94
95
96
97
98
99
# File 'lib/models/pokemon.rb', line 93

def rank_effect
  if @rank == 1
    1.0
  else
    @rank > 0 ? (@rank.abs + 2.0) / 2.0 : 2.0 / (@rank.abs + 2.0)
  end
end

#set(feature) ⇒ Object



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
# File 'lib/models/pokemon.rb', line 36

def set(feature)
  @description = feature[:description] || ""
  @nature = Nature.find_by_name(feature[:nature]) || Nature.find_by_name("がんばりや")
  @effort_value = feature[:effort_value] ||  { H: 0, A: 0, B: 0, C: 0, D: 0, S: 0 }
  @individual_value = feature[:individual_value] ||  { H: 31, A: 31, B: 31, C: 31, D: 31, S: 31 }
  @ability = feature[:ability] ||  1
  @item = feature[:item] ||  ""
  @level = feature[:level] || 50
  @rank = feature[:rank] || 1

  if effort_value.instance_of?(Symbol) && @@ways.include?(effort_value)
    case effort_value
    when :AS
      @effort_value     = { H: 0, A: 252, B: 0, C: 0, D: 0, S: 252 }
    when :CS
      @effort_value     = { H: 0, A: 0, B: 0, C: 252, D: 0, S: 252 }
    when :hAS
      @effort_value     = { H: 6, A: 252, B: 0, C: 0, D: 0, S: 252 }
    when :hCS
      @effort_value     = { H: 6, A: 0, B: 0, C: 252, D: 0, S: 252 }
    when :HB
      @effort_value     = { H: 252, A: 0, B: 252, C: 0, D: 0, S: 0 }
    when :HD
      @effort_value     = { H: 252, A: 0, B: 0, C: 0, D: 252, S: 0 }
    when :HAs
      @effort_value     = { H: 252, A: 252, B: 0, C: 0, D: 0, S: 6 }
    when :HCs
      @effort_value     = { H: 252, A: 0, B: 0, C: 252, D: 0, S: 6 }
    else
      @effort_value     = { H: 0, A: 0, B: 0, C: 0, D: 0, S: 0 }
    end
  end
end

#statistics(name) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/models/pokemon.rb', line 70

def statistics(name)
  case name
  when :H
    statistics = ((self.H * 2 + @individual_value[:H] + @effort_value[:H] / 4 ) * @level / 100 ) + 10 + @level
  else
    statistics = (((self.send(name) * 2 + @individual_value[name] + @effort_value[name] / 4 ) * @level / 100 ) + 5) * @nature.send(name)
  end
  statistics.floor
end

#statsObject



80
81
82
# File 'lib/models/pokemon.rb', line 80

def stats
  [:H, :A, :B, :C, :D, :S].map { |name| [name, statistics(name)]}.to_h
end

#to_hObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/models/pokemon.rb', line 116

def to_h
  {
    name: name,
    description: @description,
    nature: @nature.to_sym,
    effort_value: @effort_value,
    individual_value: @individual_value,
    ability: @ability,
    item: @item
  }
end

#type_ranked_movesObject



108
109
110
111
112
113
114
# File 'lib/models/pokemon.rb', line 108

def type_ranked_moves
  list = moves.group("move_type, attack_type").having("max(power)").order("power desc")
  {
    "物理": list.where(move_type: "物理"),
    "特殊": list.where(move_type: "特殊"),
  }
end

#typesObject



101
102
103
104
105
106
# File 'lib/models/pokemon.rb', line 101

def types
  type_effect = open("data/type.json") do |io|
    JSON.load(io)
  end
  Hash[type_effect.keys.map { |t| [t, type_effect[type1][t] * (type2.present? ? type_effect[type2][t]: 1.0)] }]
end