Class: Hunter

Inherits:
ApplicationRecord show all
Defined in:
app/models/hunter.rb

Overview

Schema Information

Table name: hunters

id          :bigint           not null, primary key
charm       :integer
cool        :integer
experience  :integer
harm        :integer
luck        :integer
name        :string
retired     :boolean
sharp       :integer
tough       :integer
weird       :integer
created_at  :datetime         not null
updated_at  :datetime         not null
playbook_id :bigint
user_id     :bigint

Indexes

index_hunters_on_playbook_id  (playbook_id)
index_hunters_on_user_id      (user_id)

Foreign Keys

fk_rails_...  (playbook_id => playbooks.id)
fk_rails_...  (user_id => users.id)

The character appearing in the mystery

Constant Summary collapse

MAX_LUCK =
7

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.policy_classObject

This is the Pundit Policy that governs Hunter access

See Also:


61
62
63
# File 'app/models/hunter.rb', line 61

def self.policy_class
  HunterPolicy
end

Instance Method Details

#advanced?(move) ⇒ Boolean

Check whether a move has been advanced by an improvement for this hunter

Parameters:

  • move (Move)

    the move to check

Returns:

  • (Boolean)

123
124
125
# File 'app/models/hunter.rb', line 123

def advanced?(move)
  !!(hunters_move_for(move_id: move.id)&.advanced)
end

#available_improvementsActiveRecord::Collection

List all improvements that are available based on the hunter's playbook, and excludes improvements the hunter has already taken, and excludes advanced improvement if the hunter does not qualify

Returns:

  • (ActiveRecord::Collection)

72
73
74
75
76
77
78
# File 'app/models/hunter.rb', line 72

def available_improvements
  availables = playbook.improvements.where.not(id: improvements.select(:id))
  unless Improvement.advanced_eligible?(self)
    availables = availables.not_advanced
  end
  availables
end

#gain_experience(exp) ⇒ Object

Add a negative or positive amount of experience to the hunter.

Parameters:

  • exp (Numeric)

    the amount of experience to give the player


84
85
86
87
# File 'app/models/hunter.rb', line 84

def gain_experience(exp)
  self.experience += exp
  save
end

#hunters_move_for(move_id:) ⇒ Object

Returns the Hunters Move otherwise known as the object that represents the relationship between the hunter and the move.

Parameters:

  • move_id (Move_id)

    the id of the move


93
94
95
# File 'app/models/hunter.rb', line 93

def hunters_move_for(move_id:)
  hunters_moves.find_by(move_id: move_id)
end

#rating=(rating) ⇒ Object

Sets the Hunter's stats to those of the rating. Intended for use in creating the Hunter.

Parameters:

  • rating (Rating)

    the hunter will assume these stats


106
107
108
109
# File 'app/models/hunter.rb', line 106

def rating=(rating)
  rate_attrs = rating.attributes.with_indifferent_access.slice(*Rating::LIST)
  assign_attributes(rate_attrs)
end

#rating_id=(rating_id) ⇒ Object

Sets the Hunter's stats to those of the rating. Intended for use in creating the Hunter.

Parameters:

  • rating (Rating)

    the hunter will assume these stats


115
116
117
# File 'app/models/hunter.rb', line 115

def rating_id=(rating_id)
  self.rating = Rating.find rating_id
end

#retire!Object

Transitions the Hunter to a retired state


98
99
100
# File 'app/models/hunter.rb', line 98

def retire!
  update!(retired: true)
end

#unstable?Boolean

Check whether the hunter has lost enough health to fall unstable

Returns:

  • (Boolean)

128
129
130
# File 'app/models/hunter.rb', line 128

def unstable?
  harm > 3
end