Class: RecorderCommand

Inherits:
CLAide::Command
  • Object
show all
Defined in:
lib/hearthstone/commands/recorder_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ RecorderCommand

Returns a new instance of RecorderCommand.



25
26
27
28
29
30
31
# File 'lib/hearthstone/commands/recorder_command.rb', line 25

def initialize(argv)
  @input = argv.option('input-path', "#{Dir.home}/Library/Logs/Unity/Player.log")
  @output = argv.option('output-path', "#{Dir.home}/Documents/hearthstone-recorder")
  @config = argv.option('config-path', "#{Dir.home}/Library/Preferences/Blizzard/Hearthstone/log.config")
  @complete = argv.flag?('complete', false)
  super
end

Instance Attribute Details

#completeObject (readonly)

Returns the value of attribute complete.



10
11
12
# File 'lib/hearthstone/commands/recorder_command.rb', line 10

def complete
  @complete
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/hearthstone/commands/recorder_command.rb', line 10

def config
  @config
end

#inputObject (readonly)

Returns the value of attribute input.



10
11
12
# File 'lib/hearthstone/commands/recorder_command.rb', line 10

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



10
11
12
# File 'lib/hearthstone/commands/recorder_command.rb', line 10

def output
  @output
end

Class Method Details

.optionsObject



16
17
18
19
20
21
22
23
# File 'lib/hearthstone/commands/recorder_command.rb', line 16

def self.options
  [
    ['--input-path', 'File path of the Hearthstone log files.'],
    ['--config-path', 'Hearthstone config file path. (Default: ~/Library/Preferences/Blizzard/Hearthstone/log.config)'],
    ['--output-path', 'File path of the output record files. (Default: ~/Documents/hearthstone-recorder)'],
    ['--complete', 'Read the log file completely and re-create every games. Default: only watch for new games.']
  ].concat(super)
end

Instance Method Details

#on_event(event, data) ⇒ Object



83
84
85
# File 'lib/hearthstone/commands/recorder_command.rb', line 83

def on_event(event, data)
  # puts "event: #{event.to_s} -> #{data}"
end

#on_game_mode(mode) ⇒ Object



79
80
81
# File 'lib/hearthstone/commands/recorder_command.rb', line 79

def on_game_mode(mode)
  puts "Game mode detected: #{mode}"
end

#on_game_over(game) ⇒ Object

when game over, write the logs to output folder



69
70
71
72
73
74
75
76
77
# File 'lib/hearthstone/commands/recorder_command.rb', line 69

def on_game_over(game)
  json = game.to_json
  filename = game.filename + ".json"

  puts "Game recorded: #{game.players.first.name} vs #{game.players.last.name}"
  File.open(File.join(self.output, filename), "w") do |f|
    f.write(json)
  end
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hearthstone/commands/recorder_command.rb', line 47

def run
  logger = Hearthstone::Log::GameLogger.new(self)

  if self.complete
    logger.log_file(File.open(self.input).read)
  end

  params = {}
  params[:backward] = 10 unless self.complete

  File::Tail::Logfile.open(self.input, params) do |file|
    begin
      file.interval = 10
      file.tail do |line|
        logger.log_line(line)
      end
    rescue Interrupt
    end
  end
end

#validate!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hearthstone/commands/recorder_command.rb', line 33

def validate!
  super

  unless Dir.exists? self.output
    Dir.mkdir(self.output)
  end

  configurator = Hearthstone::Log::Configurator.new(self.config)
  if configurator.needs_config?
    puts "Configure Hearthstone, if hearthstone is running, please restart it. (#{self.config})"
    configurator.configure
  end
end