Class: Natural20::Session
- Inherits:
-
Object
- Object
- Natural20::Session
- Defined in:
- lib/natural_20/session.rb
Constant Summary collapse
- VALID_SETTINGS =
%i[manual_dice_roll].freeze
Instance Attribute Summary collapse
-
#game_properties ⇒ Object
readonly
Returns the value of attribute game_properties.
-
#game_time ⇒ Object
readonly
Returns the value of attribute game_time.
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Class Method Summary collapse
- .current_session ⇒ Natural20::Session
- .new_session(root_path = nil) ⇒ Natural20::Session
- .set_session(session) ⇒ Object
Instance Method Summary collapse
- #has_save_game? ⇒ Boolean
- #increment_game_time!(seconds = 6) ⇒ Object
-
#initialize(root_path = nil) ⇒ Session
constructor
A new instance of Session.
- #load_characters ⇒ Object
- #load_class(klass) ⇒ Object
- #load_classes ⇒ Object
- #load_equipment(item) ⇒ Object
- #load_npcs ⇒ Object
- #load_object(object_name) ⇒ Object
- #load_races ⇒ Object
- #load_save ⇒ Natural20::Battle
- #load_spell(spell) ⇒ Object
- #load_state(state_type) ⇒ Object
- #load_thing(item) ⇒ Object
- #load_weapon(weapon) ⇒ Object
- #load_weapons ⇒ Object
- #npc(npc_type, options = {}) ⇒ Object
- #save_character(name, data) ⇒ Object
- #save_game(battle) ⇒ Object
-
#save_state(state_type, value = {}) ⇒ Object
store a state.
- #setting(k) ⇒ Object
- #t(token, options = {}) ⇒ Object
- #update_settings(settings = {}) ⇒ Object
Constructor Details
#initialize(root_path = nil) ⇒ Session
Returns a new instance of Session.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/natural_20/session.rb', line 23 def initialize(root_path = nil) @root_path = root_path.presence || '.' @session_state = {} @weapons = {} @equipment = {} @objects = {} @thing = {} @char_classes = {} @spells = {} @settings = { manual_dice_roll: false } @game_time = 0 # game time in seconds I18n.load_path << Dir[File.join(@root_path, 'locales') + '/*.yml'] I18n.default_locale = :en if File.exist?(File.join(@root_path, 'game.yml')) @game_properties = YAML.load_file(File.join(@root_path, 'game.yml')).deep_symbolize_keys! else raise t(:missing_game) end end |
Instance Attribute Details
#game_properties ⇒ Object (readonly)
Returns the value of attribute game_properties.
4 5 6 |
# File 'lib/natural_20/session.rb', line 4 def game_properties @game_properties end |
#game_time ⇒ Object (readonly)
Returns the value of attribute game_time.
4 5 6 |
# File 'lib/natural_20/session.rb', line 4 def game_time @game_time end |
#root_path ⇒ Object (readonly)
Returns the value of attribute root_path.
4 5 6 |
# File 'lib/natural_20/session.rb', line 4 def root_path @root_path end |
Class Method Details
.current_session ⇒ Natural20::Session
14 15 16 |
# File 'lib/natural_20/session.rb', line 14 def self.current_session @session end |
.new_session(root_path = nil) ⇒ Natural20::Session
8 9 10 11 |
# File 'lib/natural_20/session.rb', line 8 def self.new_session(root_path = nil) @session = Natural20::Session.new(root_path) @session end |
.set_session(session) ⇒ Object
19 20 21 |
# File 'lib/natural_20/session.rb', line 19 def self.set_session(session) @session = session end |
Instance Method Details
#has_save_game? ⇒ Boolean
87 88 89 |
# File 'lib/natural_20/session.rb', line 87 def has_save_game? File.exist?(File.join(@root_path, 'savegame.yml')) end |
#increment_game_time!(seconds = 6) ⇒ Object
61 62 63 |
# File 'lib/natural_20/session.rb', line 61 def increment_game_time!(seconds = 6) @game_time += seconds end |
#load_characters ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/natural_20/session.rb', line 65 def load_characters files = Dir[File.join(@root_path, 'characters', '*.yml')] @characters ||= files.map do |file| YAML.load_file(file) end @characters.map do |char_content| Natural20::PlayerCharacter.new(self, char_content) end end |
#load_class(klass) ⇒ Object
140 141 142 143 144 |
# File 'lib/natural_20/session.rb', line 140 def load_class(klass) @char_classes[klass.to_sym] ||= begin YAML.load_file(File.join(@root_path, 'char_classes', "#{klass}.yml")).deep_symbolize_keys! end end |
#load_classes ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/natural_20/session.rb', line 125 def load_classes files = Dir[File.join(@root_path, 'char_classes', '*.yml')] files.map do |fname| class_name = File.basename(fname, '.yml') [class_name, YAML.load_file(fname).deep_symbolize_keys!] end.to_h end |
#load_equipment(item) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/natural_20/session.rb', line 163 def load_equipment(item) @equipment[item.to_sym] ||= begin equipment = YAML.load_file(File.join(@root_path, 'items', 'equipment.yml')).deep_symbolize_keys! equipment[item.to_sym] end end |
#load_npcs ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/natural_20/session.rb', line 109 def load_npcs files = Dir[File.join(@root_path, 'npcs', '*.yml')] files.map do |fname| npc_name = File.basename(fname, '.yml') Natural20::Npc.new(self, npc_name, rand_life: true) end end |
#load_object(object_name) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/natural_20/session.rb', line 170 def load_object(object_name) @objects[object_name.to_sym] ||= begin objects = YAML.load_file(File.join(@root_path, 'items', 'objects.yml')).deep_symbolize_keys! objects[object_name.to_sym] end end |
#load_races ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/natural_20/session.rb', line 117 def load_races files = Dir[File.join(@root_path, 'races', '*.yml')] files.map do |fname| race_name = File.basename(fname, '.yml') [race_name, YAML.load_file(fname).deep_symbolize_keys!] end.to_h end |
#load_save ⇒ Natural20::Battle
101 102 103 |
# File 'lib/natural_20/session.rb', line 101 def load_save YAML.load_file(File.join(@root_path, 'savegame.yml')) end |
#load_spell(spell) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/natural_20/session.rb', line 133 def load_spell(spell) @spells[spell.to_sym] ||= begin spells = YAML.load_file(File.join(@root_path, 'items', "spells.yml")).deep_symbolize_keys! spells[spell.to_sym].merge(id: spell) end end |
#load_state(state_type) ⇒ Object
83 84 85 |
# File 'lib/natural_20/session.rb', line 83 def load_state(state_type) @session_state[state_type.to_sym] || {} end |
#load_thing(item) ⇒ Object
157 158 159 160 161 |
# File 'lib/natural_20/session.rb', line 157 def load_thing(item) @thing[item.to_sym] ||= begin load_weapon(item) || load_equipment(item) || load_object(item) end end |
#load_weapon(weapon) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/natural_20/session.rb', line 146 def load_weapon(weapon) @weapons[weapon.to_sym] ||= begin weapons = YAML.load_file(File.join(@root_path, 'items', 'weapons.yml')).deep_symbolize_keys! weapons[weapon.to_sym] end end |
#load_weapons ⇒ Object
153 154 155 |
# File 'lib/natural_20/session.rb', line 153 def load_weapons YAML.load_file(File.join(@root_path, 'items', 'weapons.yml')).deep_symbolize_keys! end |
#npc(npc_type, options = {}) ⇒ Object
105 106 107 |
# File 'lib/natural_20/session.rb', line 105 def npc(npc_type, = {}) Natural20::Npc.new(self, npc_type, ) end |
#save_character(name, data) ⇒ Object
96 97 98 |
# File 'lib/natural_20/session.rb', line 96 def save_character(name, data) File.write(File.join(@root_path, 'characters', "#{name}.yml"), data.to_yaml) end |
#save_game(battle) ⇒ Object
92 93 94 |
# File 'lib/natural_20/session.rb', line 92 def save_game(battle) File.write(File.join(@root_path, 'savegame.yml'), battle.to_yaml) end |
#save_state(state_type, value = {}) ⇒ Object
store a state
78 79 80 81 |
# File 'lib/natural_20/session.rb', line 78 def save_state(state_type, value = {}) @session_state[state_type.to_sym] ||= {} @session_state[state_type.to_sym].deep_merge!(value) end |
#setting(k) ⇒ Object
55 56 57 58 59 |
# File 'lib/natural_20/session.rb', line 55 def setting(k) raise 'invalid settings' unless VALID_SETTINGS.include?(k.to_sym) @settings[k.to_sym] end |
#t(token, options = {}) ⇒ Object
177 178 179 |
# File 'lib/natural_20/session.rb', line 177 def t(token, = {}) I18n.t(token, **) end |
#update_settings(settings = {}) ⇒ Object
49 50 51 52 53 |
# File 'lib/natural_20/session.rb', line 49 def update_settings(settings = {}) settings.each_key { |k| raise 'invalid settings' unless VALID_SETTINGS.include?(k.to_sym) } @settings.deep_merge!(settings.deep_symbolize_keys) end |