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
|
class Character < ApplicationRecord
belongs_to :user
belongs_to :activity, optional: true
has_one :hearth
has_many :character_items
has_many :items, through: :character_items
has_many :character_skills
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"
after_create :create_skills
after_create { Hearth.create(character: self) }
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 add_skill_xp(skill, amount)
CharacterSkill.find_by(skill: skill).increment!(:xp, amount)
end
def skill_level(gid)
self.character_skills.find_by(skill: Skill.find_by_gid(gid.to_s)).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 |skill, scaling_amount|
duration -= self.skill_level(skill) * scaling_amount
end
duration = [duration, duration_data[:minimum] || 10].max
duration - (Time.now - self.activity_started_at)
end
def can_do_activity?(activity)
activity.whatnot[:cost]&.each do |cost|
case cost[:type]
when "item"
return false unless self.has_item?(cost[:gid], cost[:quantity])
end
end
activity.whatnot[:requirements]&.each do |requirement|
case requirement[:type]
when "hearth_amenity"
return false unless self.hearth.has_amenity?(requirement[:gid], requirement[:level])
end
end
true
end
def start_activity(activity)
self.update(activity: activity, activity_started_at: Time.now) if self.can_do_activity?(activity)
end
private
def create_skills
Skill.all.each { |skill| self.character_skills.create(skill: skill, xp: 0) }
end
end
|