summaryrefslogtreecommitdiff
path: root/app/models/character.rb
blob: b06220bdc2cd5eea901401ad054e3b99ebf31a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
class Character < ApplicationRecord
  belongs_to :user
  belongs_to :activity, optional: true
  has_many :title_awards
  has_many :titles, through: :title_awards
  belongs_to :active_title, class_name: "Title", optional: true
  has_one :hearth
  has_many :equipment
  has_many :character_items
  has_many :learned_activities
  has_many :items, through: :character_items
  has_many :character_skills
  has_many :chat_messages
  has_many :bazaar_orders
  validates :name, presence: true
  validates_length_of :name, maximum: 15, message: "can't be longer than 15 characters"
  validates_uniqueness_of :name, message: "is already being used"
  validates_format_of :name, with: /\A[a-z]+\z/i, message: "must consist of letters only"

  attribute :wounds, :integer, default: 0

  enum offensive_style: [:balanced, :precise, :brutal]
  enum defensive_style: [:centered, :elusive, :protective]

  after_create :create_skills, :set_combat_styles
  after_create { Hearth.create(character: self) }

  def beastslay_level; skill_level("beastslay"); end
  def fluxseethe_level; skill_level("fluxseethe"); end
  def havencast_level; skill_level("havencast"); end
  def hexcarve_level; skill_level("hexcarve"); end
  def magiculture_level; skill_level("magiculture"); end
  def manatrawl_level; skill_level("manatrawl"); end
  def omenbind_level; skill_level("omenbind"); end
  def otherforge_level; skill_level("otherforge"); end
  def planequarry_level; skill_level("planequarry"); end
  def spicework_level; skill_level("spicework"); end
  def synthsever_level; skill_level("synthsever"); end
  def veilreach_level; skill_level("veilreach"); end
  def wealdreap_level; skill_level("wealdreap"); end
  def wildscour_level; skill_level("wildscour"); end
  def worldcall_level; skill_level("worldcall"); end

  def vestige
    vestige = self.character_items.find_by(item: Item.find_by_gid("vestige"))
    vestige ? vestige.quantity : 0
  end

  def shift_item(item, amount)
    item = Item.find_by_gid(item) if item.is_a? String
    CharacterItem.transaction do
      ci = self.character_items.find_or_initialize_by(item: item)
      ci.increment(:quantity, amount)
      ci.save!
    end
  end

  def pay_cost_for(activity)
    CharacterItem.transaction do
      activity.whatnot[:cost]&.each do |cost|
        case cost[:type]
        when "item"
          self.shift_item(cost[:gid], -cost[:quantity])
        end
      end
    end
  end

  def has_item?(item, quantity = 1)
    item = Item.find_by_gid(item) if item.is_a? String
    ci = self.character_items.find_by(item: item)
    ci && ci.quantity >= quantity
  end

  def open_slots_for(item)
    full_slots = self.equipment.map { |e| e.slot }
    item.equip_slots.reject { |slot| full_slots.include?(slot) }
  end

  def equip(item)
    Character.transaction do
      open_slots = self.open_slots_for(item)
      raise EquipmentError unless open_slots.any?
      self.shift_item(item, -1)
      self.equipment.create!(item: item, slot: open_slots.first)
    end
  end

  def unequip(slot)
    Character.transaction do
      equipment = self.equipment.find_by(slot: slot)
      raise EquipmentError unless equipment
      item = equipment.item
      equipment.destroy!
      self.shift_item(item, 1)
    end
  end

  def add_skill_xp(skill, amount)
    skill = Skill.find_by_gid(skill) if skill.is_a? String
    Character.transaction do
      cs = self.character_skills.find_by(skill: skill)
      cs.xp += amount
      cs.save!
    end
  end

  def skill_level(skill)
    skill = Skill.find_by_gid(skill) if skill.is_a? String
    self.character_skills.find_by(skill: skill).level
  end

  def activity_time_remaining
    return nil unless self.activity
    duration_data = self.activity.whatnot[:duration]
    duration = duration_data[:base]
    duration_data[:scaling]&.each do |scaling_entry|
      case scaling_entry[:type]
      when "skill"
        duration -= self.skill_level(scaling_entry[:gid]) * scaling_entry[:scale_value]
      when "stat"
        duration -= self.total_stat_change(scaling_entry[:gid]) * scaling_entry[:scale_value]
      else
        raise "Invalid scaling type." # TODO: Improve this
      end
    end
    duration = [duration, duration_data[:minimum] || 10].max
    duration - (Time.now - self.activity_started_at)
  end

  def can_do_activity?(activity, ignore_cost: false, ignore_requirements: false)
    return false unless activity.innate? || self.learned_activities.exists?(activity: activity)
    unless ignore_cost
      activity.whatnot[:cost]&.each do |cost|
        case cost[:type]
        when "item"
          return false unless self.has_item?(cost[:gid], cost[:quantity])
        end
      end
    end
    unless ignore_requirements
      activity.whatnot[:requirements]&.each do |requirement|
        case requirement[:type]
        when "skill"
          return false unless self.skill_level(requirement[:gid]) >= requirement[:level]
        when "hearth_amenity"
          return false unless self.hearth.has_amenity?(requirement[:gid], requirement[:level])
        end
      end
    end
    true
  end

  def start_activity(activity, queued_actions: nil)
    if self.can_do_activity?(activity)
      self.update(activity: activity, activity_started_at: Time.now, queued_actions: queued_actions)
    else
      false
    end
  end

  def stop_activity
    self.update(activity: nil, activity_started_at: nil, queued_actions: nil)
  end

  def award_title(title)
    title = Title.find_by_gid(title) if title.is_a? String
    # TODO: Simplify these lines?
    return false if self.title_awards.exists?(title: title)
    self.title_awards.create(title: title)
  end

  def equipment_stats
    stats = {}
    self.equipment.each do |eq|
      eq.item.whatnot[:equip_effects]&.each do |effect|
        if effect[:type] == "stat_change"
          stats[effect[:gid]] ||= 0
          stats[effect[:gid]] += effect[:modifier]
        end
      end
    end
    stats
  end

  def effects
    # TODO: Review this filter_map to see if it can be simplified
    hearth_amenity_effects = self.hearth.built_hearth_amenities.filter_map { |a| a.effects if a.effects }
    equipment_effects = self.equipment.filter_map { |a| a.effects if a.effects }
    (hearth_amenity_effects + equipment_effects).flatten
  end

  def total_stat_change(gid)
    effects.filter_map { |e| e[:modifier] if e[:type] == "stat_change" && e[:gid] == gid }.sum
  end

  def can_fight?
    self.wounds < max_wounds
  end

  def max_wounds
    1 + total_stat_change("max_wounds")
  end

  def max_hp
    9 + self.beastslay_level + (equipment_stats["max_hp"] || 0)
  end

  def speed
    self.beastslay_level + (equipment_stats["speed"] || 0)
  end

  def accuracy(with_combat_style: false)
    base = self.beastslay_level + (equipment_stats["accuracy"] || 0)
    if with_combat_style && self.precise?
      base = (base * 1.25).floor
    elsif with_combat_style && self.brutal?
      base = (base * 0.75).ceil
    end
    base
  end

  def power(with_combat_style: false)
    base = self.beastslay_level + (equipment_stats["power"] || 0)
    if with_combat_style && self.precise?
      base = (base * 0.75).ceil
    elsif with_combat_style && self.brutal?
      base = (base * 1.25).floor
    end
    base
  end

  def evasion(with_combat_style: false)
    base = self.beastslay_level + (equipment_stats["evasion"] || 0)
    if with_combat_style && self.elusive?
      base = (base * 1.25).floor
    elsif with_combat_style && self.protective?
      base = (base * 0.75).ceil
    end
    base
  end

  def block(with_combat_style: false)
    base = self.beastslay_level + (equipment_stats["block"] || 0)
    if with_combat_style && self.elusive?
      base = (base * 0.75).ceil
    elsif with_combat_style && self.protective?
      base = (base * 1.25).floor
    end
    base
  end

  def block_value
    equipment_stats["block_value"] || 0
  end

  private
    def create_skills
      Skill.all.each { |skill| self.character_skills.create(skill: skill, xp: 0) }
    end

    def set_combat_styles
      self.update(offensive_style: :balanced, defensive_style: :centered)
    end
end