class Natural20::Session
Constants
- VALID_SETTINGS
Attributes
game_properties[R]
game_time[R]
root_path[R]
Public Class Methods
current_session()
click to toggle source
@return [Natural20::Session]
# File lib/natural_20/session.rb, line 14 def self.current_session @session end
new(root_path = nil)
click to toggle source
# 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
new_session(root_path = nil)
click to toggle source
@param root_path
[String] The current adventure working folder @return [Natural20::Session]
# 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)
click to toggle source
@param session [Natural20::Session]
# File lib/natural_20/session.rb, line 19 def self.set_session(session) @session = session end
Public Instance Methods
has_save_game?()
click to toggle source
# 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)
click to toggle source
# File lib/natural_20/session.rb, line 61 def increment_game_time!(seconds = 6) @game_time += seconds end
load_characters()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
@return [Natural20::Battle]
# File lib/natural_20/session.rb, line 101 def load_save YAML.load_file(File.join(@root_path, 'savegame.yml')) end
load_spell(spell)
click to toggle source
# 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)
click to toggle source
# File lib/natural_20/session.rb, line 83 def load_state(state_type) @session_state[state_type.to_sym] || {} end
load_thing(item)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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 = {})
click to toggle source
# File lib/natural_20/session.rb, line 105 def npc(npc_type, options = {}) Natural20::Npc.new(self, npc_type, options) end
save_character(name, data)
click to toggle source
# 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)
click to toggle source
@param battle [Natural20::BattleMap]
# 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 = {})
click to toggle source
store a state @param state_type [String,Symbol] @param value [Hash]
# 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)
click to toggle source
# 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 = {})
click to toggle source
# File lib/natural_20/session.rb, line 177 def t(token, options = {}) I18n.t(token, **options) end
update_settings(settings = {})
click to toggle source
@options settings manual_dice_roll [Boolean]
# 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