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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
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 :conditions, through: :states
has_many :states
has_many :chat_messages
has_many :bazaar_orders
validates :name, presence: true
# TODO: Make defaults better. This has to allow nil so the `attribute` default works, and I don't like that.
validates :rested_duration, numericality: { greater_than_or_equal_to: 0, only_integer: true }, allow_nil: 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
attribute :rested_duration, :integer, default: 0
enum offensive_style: [:balanced, :precise, :brutal], _default: "balanced"
enum defensive_style: [:centered, :elusive, :protective], _default: "centered"
after_create :create_skills, :set_combat_styles
after_create { Hearth.create(character: self) }
def self.sorted_by_total_xp
all.sort_by(&:total_xp).reverse
end
def self.sorted_by_total_level
all.sort_by(&:total_level).reverse
end
def self.top_total_xp
sorted_by_total_xp.first(5)
end
def self.top_total_level
sorted_by_total_level.first(5)
end
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 total_xp
character_skills.sum(:xp).to_i
end
def total_level
count = 0
character_skills.each do |cs|
count += cs.level
end
count
end
def total_xp_rank
Character.sorted_by_total_xp.map(&:id).index(self.id) + 1
end
def total_level_rank
Character.sorted_by_total_level.map(&:id).index(self.id) + 1
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 active_states
self.states.all.select { |s| !s.expired? }
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] || 1))
when "hearth_planting"
hp = hearth.ripe_hearth_plantings_of(cost[:gid]).first
raise HearthPlantingError unless hp
hp.destroy
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 equipment_with_gid(item)
item = Item.find_by_gid(item) if item.is_a? String
self.equipment.find_by(item: item)
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 trophy_monster_gid
equipment = self.equipment.joins(:item).where("items.gid like ?", "%_trophy").first
if equipment
equipment.item.gid.split("_trophy").first
else
nil
end
end
def can_equip?(item)
item.whatnot[:equip_requirements]&.each do |requirement|
case requirement[:type]
when "skill"
return false unless self.skill_level(requirement[:gid]) >= requirement[:level]
else
raise "Invalid requirement type string (#{requirement[:type]})"
end
end
true
end
def equip(item)
Character.transaction do
raise EquipmentError unless self.can_equip?(item)
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
time = duration_of_activity - (Time.now - self.activity_started_at)
time -= rested_duration_to_spend_on_activity if self.rested_duration > 0
time
end
def duration_of_activity
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_data[:minimum] || 10].max
end
def rested_duration_to_spend_on_activity
return nil unless self.activity
[(duration_of_activity / 2).floor, self.rested_duration].min
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] || 1)
when "hearth_planting"
return false unless hearth.ripe_hearth_plantings_of(cost[:gid]).first
else
raise "Invalid cost type string (#{cost[:type]})"
end
end
end
unless ignore_requirements
activity.whatnot[:requirements]&.each do |requirement|
case requirement[:type]
when "equipment"
return false unless self.equipment_with_gid(requirement[:gid])
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])
else
raise "Invalid requirement type string (#{requirement[:type]})"
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 resting?
self.started_resting_at
end
def start_resting
return false if self.started_resting_at
self.update(started_resting_at: Time.now)
end
def stop_resting
return false unless self.started_resting_at
Character.transaction do
seconds_of_this_rest = (Time.now - self.started_resting_at).to_i
new_rested_duration = [(seconds_of_this_rest + self.rested_duration), (60 * 60 * 12)].min
self.update(started_resting_at: nil, rested_duration: new_rested_duration)
end
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 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 }
state_effects = self.states.filter_map { |a| a.effects if a.effects && !a.expired? }
(hearth_amenity_effects + equipment_effects + state_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 planting_spots
[total_stat_change("planting_spots"), 0].max
end
def can_fight?
self.wounds < max_wounds
end
def max_wounds
[1 + total_stat_change("max_wounds"), 0].max
end
def max_hp
[9 + self.beastslay_level + total_stat_change("max_hp"), 1].max
end
def speed
[self.beastslay_level + total_stat_change("speed"), 0].max
end
def accuracy(with_combat_style: false)
base = [self.beastslay_level + total_stat_change("accuracy"), 0].max
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 + total_stat_change("power"), 0].max
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 + total_stat_change("evasion"), 0].max
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 + total_stat_change("block"), 0].max
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
[total_stat_change("block_value"), 0].max
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
|