Class: ProconBypassMan::ExternalInput::ExternalData

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/external_input/external_data.rb

Constant Summary collapse

UNPRESS_BUTTONS =
Set.new(ProconBypassMan::Procon::ButtonCollection.available.map { |x| "un#{x}".to_sym })

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex:, buttons:, raw_data: nil) ⇒ ExternalData

Returns a new instance of ExternalData.

Parameters:

  • hex (String, NilClass)
  • buttons (Array<String>, NilClass)
  • raw_data (String, NilClass) (defaults to: nil)


38
39
40
41
42
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 38

def initialize(hex: , buttons: , raw_data: nil)
  @hex = hex
  @buttons = buttons || []
  @raw_data = raw_data
end

Instance Attribute Details

#hexString, NilClass (readonly)

Returns 16進数表現のデータ.

Returns:

  • (String, NilClass)

    16進数表現のデータ



7
8
9
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 7

def hex
  @hex
end

#raw_dataString, NilClass (readonly)

Returns ログに表示する用.

Returns:

  • (String, NilClass)

    ログに表示する用



10
11
12
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 10

def raw_data
  @raw_data
end

Class Method Details

.is_json(raw_data) ⇒ Boolean

Parameters:

  • raw_data (String)

Returns:

  • (Boolean)


31
32
33
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 31

def self.is_json(raw_data)
  raw_data.start_with?('{')
end

.parse!(raw_data) ⇒ ExternalData

Returns JSON か カンマ区切りのbuttons.

Returns:

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 14

def self.parse!(raw_data)
  raise ParseError unless raw_data.ascii_only?

  if is_json(raw_data)
    begin
      json = JSON.parse(raw_data)
      return new(hex: json['hex'], buttons: json['buttons'])
    rescue JSON::ParserError
      raise ParseError
    end
  end

  return new(hex: nil, buttons: raw_data.scan(/:\w+:/).map { |x| x.gsub(':', '') }, raw_data: raw_data)
end

Instance Method Details

#buttonsArray<Symbol>

NOTE: ログに表示する用

Returns:

  • (Array<Symbol>)


66
67
68
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 66

def buttons
  @buttons.map(&:to_sym)
end

#press_buttonsArray<Symbol>

Returns:

  • (Array<Symbol>)


51
52
53
54
55
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 51

def press_buttons
  buttons.select do |button|
    ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP[button]
  end
end

#to_binaryString, NilClass

Returns:

  • (String, NilClass)


45
46
47
48
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 45

def to_binary
  return nil if @hex.nil?
  [@hex].pack('H*')
end

#unpress_buttonsArray<Symbol>

Returns:

  • (Array<Symbol>)


58
59
60
61
62
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 58

def unpress_buttons
  buttons.select { |button|
    UNPRESS_BUTTONS.include?(button)
  }.map { |b| to_button(b.to_s).to_sym }
end