Class: Pathfinder::CharacterSheet

Inherits:
Object
  • Object
show all
Includes:
RC, Tools
Defined in:
lib/pathfinder_dnd/character_sheet.rb

Overview

The whole of the Pathfinder Spreadsheet-tools environment. The character-sheet provides one-word lookup of almost all essential character statistics.

On a technical level, a CharacterSheet wraps access to a GoogleDrive::Worksheet with friendly helper methods. Skill accessor methods are dynamically added at object instantiation.

CharacterSheet is currently the gameplay interface, so it includes Pathfinder::Tools for easy dice rolling, skill checks, and ninja business.

Constant Summary collapse

STATS_SHEET =

What sheet title to pull stats from?

'Stats, Skills, Weapons'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools

#average, #check, #roll, #single_roll, #sum, #verbose

Constructor Details

#initialize(session, key) ⇒ CharacterSheet

session: a google_drive session key: the Drive identifier for the character sheet document

(it's the key= query param when you load the char sheet in the browser)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pathfinder_dnd/character_sheet.rb', line 84

def initialize(session, key)

    # This is where failure will occur if Oauth is fucked
    @doc = session.spreadsheet_by_key(key)


    # all we need for now
    @stats = @doc.worksheet_by_title(STATS_SHEET)
    @sheets = [@stats]

    if @stats.nil?
        raise "Couldn't load the Stats charsheet"
    end

    # set starting HP
    @hp = self.max_hp

    # write in skill values
    inject_instance_properties(get_raw_skills())
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



29
30
31
# File 'lib/pathfinder_dnd/character_sheet.rb', line 29

def doc
  @doc
end

#hpObject

Returns the value of attribute hp.



30
31
32
# File 'lib/pathfinder_dnd/character_sheet.rb', line 30

def hp
  @hp
end

#statsObject (readonly)

Returns the value of attribute stats.



29
30
31
# File 'lib/pathfinder_dnd/character_sheet.rb', line 29

def stats
  @stats
end

Class Method Details

.cell_reader(name, row_or_coord, col = nil, sheet_index = 0) ⇒ Object

reads a cell directly from the spreadsheet.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pathfinder_dnd/character_sheet.rb', line 33

def self.cell_reader(name, row_or_coord, col = nil, sheet_index = 0)
    define_method(name) do 
        sheets = instance_variable_get('@sheets')
        sheet = sheets[sheet_index]

        if row_or_coord.is_a? Numeric
            return sheet[row_or_coord, col].to_i
        else
            return sheet[row_or_coord].to_i
        end
    end
end

Instance Method Details

#get_raw_skills(start_loc = 'AH16', offset = 6) ⇒ Object

Builds a <String> => <Integer> hash of skill scores from the character spreadsheet.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pathfinder_dnd/character_sheet.rb', line 113

def get_raw_skills(start_loc = 'AH16', offset = 6)
    # scrape the spreadsheet skills list
    skills = {}
    start, names_col = @stats.cell_name_to_row_col(start_loc)
    skills_col = names_col + offset
    (start..start+38).each do |row|
        # more clear to split this up
        skill_name = @stats[row, names_col]
        skill_val  = @stats[row, skills_col]
        skills[skill_name] = skill_val.to_i
    end

    skills
end

#inject_instance_properties(props) ⇒ Object

Injects a <String> => <Any> hash of properties into this instance as attribute accessors. If the accessor methods already exist, then the local variables they wrap are updated.

This method is used in conjuction with ‘get_raw_skill` to populate the intance with skill fields at runtime.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pathfinder_dnd/character_sheet.rb', line 133

def inject_instance_properties(props)
    # for adding these properties to only THIS instance
    metaclass = (class << self; self; end)

    props.each do |name, value|
        safe_name = name.downcase.gsub(/\s/, '_').gsub(/[^a-zA-Z0-9_]/, '').to_sym

        # define the accessor for this skill if we haven't already
        if not self.respond_to? safe_name
            metaclass.class_eval { attr_reader safe_name }
        end

        # update the skill value
        instance_variable_set("@#{safe_name}", value)
    end
end

#refreshObject

Refeshes this instance with new data from the online character sheet, including updates to skills.



107
108
109
110
# File 'lib/pathfinder_dnd/character_sheet.rb', line 107

def refresh
    @sheets.each {|s| s.reload()}
    inject_instance_properties(get_raw_skills())
end