Class: RumbleTools::Loadout

Inherits:
Object
  • Object
show all
Defined in:
lib/rumble_tools/loadout.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(leader = nil, troops = []) ⇒ Loadout

Returns a new instance of Loadout.



10
11
12
13
# File 'lib/rumble_tools/loadout.rb', line 10

def initialize(leader = nil, troops = [])
  @leader = leader || Leader.new
  @troops = troops || []
end

Instance Attribute Details

#leaderObject

Returns the value of attribute leader.



8
9
10
# File 'lib/rumble_tools/loadout.rb', line 8

def leader
  @leader
end

#troopsObject

Returns the value of attribute troops.



8
9
10
# File 'lib/rumble_tools/loadout.rb', line 8

def troops
  @troops
end

Class Method Details

.from_code(code) ⇒ Object

Parse a loadout code and return a loadout object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rumble_tools/loadout.rb', line 16

def self.from_code(code)
  # Remove rumblo: prefix if present
  code = code.sub(/^rumblo:/, '')
  
  # Decode the base64 string
  bytes = Base64.strict_decode64(code)
  
  # Parse the protocol buffer message
  proto = LoadoutProto.decode(bytes)
  
  # Create a new loadout
  loadout = Loadout.new
  
  # Create the leader
  loadout.leader = Leader.new(proto.leader_id, proto.leader_talent_id)
  
  # Create the troops
  proto.troops.each do |troop_proto|
    loadout.troops << Troop.new(troop_proto.mini_id, troop_proto.talent_id)
  end
  
  loadout
end

Instance Method Details

#to_codeObject

Generate a loadout code



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rumble_tools/loadout.rb', line 41

def to_code
  # Create a protocol buffer message
  proto = LoadoutProto.new(
    leader_id: @leader.mini,
    leader_talent_id: @leader.talent_id,
    troops: @troops.map { |troop| TroopProto.new(mini_id: troop.mini, talent_id: troop.talent_id) }
  )
  
  # Encode the message to binary
  bytes = LoadoutProto.encode(proto)
  
  # Return the base64 encoded string with rumblo: prefix
  "rumblo:" + Base64.strict_encode64(bytes)
end

#to_hObject

Convert to hash for serialization



57
58
59
60
61
62
# File 'lib/rumble_tools/loadout.rb', line 57

def to_h
  {
    leader: @leader.to_h,
    troops: @troops.map(&:to_h)
  }
end

#to_json(*args) ⇒ Object

Convert to JSON



65
66
67
# File 'lib/rumble_tools/loadout.rb', line 65

def to_json(*args)
  to_h.to_json(*args)
end