class Mythal::Stats

Constants

VALID_ATTRS
VALID_CHALLENGE_RATINGS

Attributes

challenge_rating[R]
options[R]

Public Class Methods

new(challenge_rating: "1/4", options: {}) click to toggle source
# File lib/mythal/stats.rb, line 24
def initialize(challenge_rating: "1/4", options: {})
  @challenge_rating = init_challenge_rating(challenge_rating)
  @options = options
  post_initialize
end

Public Instance Methods

attributes() click to toggle source
# File lib/mythal/stats.rb, line 30
def attributes
  {
    challenge_rating: challenge_rating,
    proficiency_bonus: stats_by_cr["proficiency_bonus"],
    armor_class: stats_by_cr["armor_class"],
    hit_points: init_hit_points,
    damage_per_round: init_damage_per_round,
    attack_bonus: stats_by_cr["attack_bonus"],
    save_dc: stats_by_cr["save_dc"],
    speed: 30,
    str: 15,
    dex: 14,
    con: 13,
    int: 12,
    wis: 10,
    cha: 8,
  }.merge(user_overrides)
end

Private Instance Methods

config() click to toggle source
# File lib/mythal/stats.rb, line 51
def config
  Mythal::Config
end
init_challenge_rating(cr) click to toggle source
# File lib/mythal/stats.rb, line 66
def init_challenge_rating(cr)
  default_cr = "1/4"
  return default_cr unless VALID_CHALLENGE_RATINGS.include?(cr)
  cr
end
init_damage_per_round() click to toggle source
# File lib/mythal/stats.rb, line 55
def init_damage_per_round
  Range.new(*stats_by_cr["damage_per_round"].split("..").map(&:to_i)).to_a.sample
end
init_hit_points() click to toggle source
# File lib/mythal/stats.rb, line 72
def init_hit_points
  Range.new(*stats_by_cr["hit_points"].split("..").map(&:to_i)).to_a.sample
end
post_initialize() click to toggle source
# File lib/mythal/stats.rb, line 82
def post_initialize
  attributes.each do |attr_name, attr_value|
    define_singleton_method(attr_name) { attr_value }
  end
end
stats_by_cr() click to toggle source
# File lib/mythal/stats.rb, line 59
def stats_by_cr
  @stats_by_cr ||= begin
    raw_stats = config.npc_stats_by_challenge_rating
    raw_stats["Headers"].zip(raw_stats[challenge_rating]).to_h
  end
end
user_overrides() click to toggle source
# File lib/mythal/stats.rb, line 76
def user_overrides
  options.select do |k, _v|
    VALID_ATTRS.include?(k)
  end
end