2024-06-07 14:38:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Stat < ApplicationRecord
|
2024-06-18 07:43:39 +00:00
|
|
|
has_one :character_sheet_feature, as: :featurable, dependent: :destroy
|
2024-06-17 13:53:09 +00:00
|
|
|
has_one :character_sheet_section, through: :character_sheet_feature
|
|
|
|
accepts_nested_attributes_for :character_sheet_feature, allow_destroy: true
|
2024-06-12 15:13:07 +00:00
|
|
|
|
2024-06-13 12:24:54 +00:00
|
|
|
has_one :character, through: :character_sheet_section
|
2024-06-13 07:41:45 +00:00
|
|
|
has_many :dice_rolls, as: :rollable, dependent: :destroy
|
2024-06-21 14:47:07 +00:00
|
|
|
has_many :dice_roll_types, as: :rollable, dependent: :destroy
|
2024-06-07 14:38:10 +00:00
|
|
|
|
|
|
|
validates :name, presence: true,
|
2024-06-17 13:53:09 +00:00
|
|
|
length: { maximum: 100 }
|
2024-06-21 11:46:12 +00:00
|
|
|
validates :min_allowed, numericality: true, allow_nil: true
|
|
|
|
validates :max_allowed, numericality: true, allow_nil: true
|
2024-06-07 14:38:10 +00:00
|
|
|
validates :value, presence: true,
|
|
|
|
numericality: true
|
2024-06-11 13:55:39 +00:00
|
|
|
validate :validate_roll_command
|
2024-06-07 14:38:10 +00:00
|
|
|
|
2024-06-21 11:46:12 +00:00
|
|
|
def rollable?
|
|
|
|
roll_command.present?
|
|
|
|
end
|
|
|
|
|
2024-06-13 12:03:34 +00:00
|
|
|
def roll(table)
|
2024-06-20 07:26:33 +00:00
|
|
|
roller = DiceRoller.new(roll_command, stat: self)
|
|
|
|
result = roller.roll
|
|
|
|
dice_rolls.create(result: roller.roll, table:, dice: roller.dice)
|
2024-06-13 12:03:34 +00:00
|
|
|
result
|
2024-06-11 13:55:39 +00:00
|
|
|
end
|
|
|
|
|
2024-06-07 14:38:10 +00:00
|
|
|
private
|
|
|
|
|
2024-06-11 13:55:39 +00:00
|
|
|
def validate_roll_command
|
|
|
|
return if roll_command.blank?
|
|
|
|
|
|
|
|
DiceRoller.new(roll_command).valid?
|
|
|
|
end
|
2024-06-07 14:38:10 +00:00
|
|
|
end
|