Class: TextPlayer::Session
- Inherits:
-
Object
- Object
- TextPlayer::Session
- Defined in:
- lib/text_player/session.rb
Overview
Mid-level: Manages game session lifecycle and output formatting
Instance Method Summary collapse
-
#call(cmd) ⇒ Object
We intentionally intercept certain commands.
-
#initialize(gamefile, dfrotz: nil) ⇒ Session
constructor
A new instance of Session.
- #quit ⇒ Object
- #restore(slot = nil) ⇒ Object
- #run ⇒ Object
- #running? ⇒ Boolean
- #save(slot = nil) ⇒ Object
- #score ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(gamefile, dfrotz: nil) ⇒ Session
Returns a new instance of Session.
6 7 8 9 10 11 |
# File 'lib/text_player/session.rb', line 6 def initialize(gamefile, dfrotz: nil) @gamefile = gamefile @game = Dfrotz.new(gamefile.full_path, dfrotz:) @started = false @interrupt_count = 0 end |
Instance Method Details
#call(cmd) ⇒ Object
We intentionally intercept certain commands. Because the intention of this library is automated play, allowing an agent to save to any file path on the system is a security risk at worst, and a nuisance at best.
We automatically save to “autosave” when the game is quit.
Quit is also intercepted to make sure we shut down the game cleanly.
46 47 48 49 |
# File 'lib/text_player/session.rb', line 46 def call(cmd) command = Commands.create(cmd, game_name: @gamefile.name) execute_command(command) end |
#quit ⇒ Object
66 67 68 69 |
# File 'lib/text_player/session.rb', line 66 def quit command = Commands::Quit.new execute_command(command) end |
#restore(slot = nil) ⇒ Object
61 62 63 64 |
# File 'lib/text_player/session.rb', line 61 def restore(slot = nil) command = Commands::Restore.new(save: Save.new(game_name: @gamefile.name, slot:)) execute_command(command) end |
#run ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/text_player/session.rb', line 13 def run(&) result = start while running? command = yield result break if command.nil? result = call(command) end end |
#running? ⇒ Boolean
34 35 36 |
# File 'lib/text_player/session.rb', line 34 def running? @started && @game.running? end |
#save(slot = nil) ⇒ Object
56 57 58 59 |
# File 'lib/text_player/session.rb', line 56 def save(slot = nil) command = Commands::Save.new(save: Save.new(game_name: @gamefile.name, slot:)) execute_command(command) end |
#score ⇒ Object
51 52 53 54 |
# File 'lib/text_player/session.rb', line 51 def score command = Commands::Score.new execute_command(command) end |
#start ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/text_player/session.rb', line 23 def start return @start_result if @started setup_interrupt_handling @game.start @started = true start_command = Commands::Start.new @start_result = execute_command(start_command) end |