Class: SnapItemBase
- Inherits:
-
Object
- Object
- SnapItemBase
- Defined in:
- lib/snapshot/snap_item_base.rb
Direct Known Subclasses
NetObj::Character, NetObj::ClientInfo, NetObj::Flag, NetObj::GameData, NetObj::GameDataFlag, NetObj::GameDataTeam, NetObj::Laser, NetObj::Pickup, NetObj::PlayerInfo, NetObj::PlayerInput, NetObj::Projectile, NetObj::SpectatorInfo, SnapEventBase
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
Instance Method Summary collapse
- #init_hash(attr) ⇒ Object
- #init_raw(data) ⇒ Object
- #init_unpacker(u) ⇒ Object
-
#initialize(hash_or_raw) ⇒ SnapItemBase
constructor
A new instance of SnapItemBase.
-
#to_a ⇒ Object
basically to_network int array the server sends to the client.
- #to_h ⇒ Object
- #to_s ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(hash_or_raw) ⇒ SnapItemBase
Returns a new instance of SnapItemBase.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/snapshot/snap_item_base.rb', line 8 def initialize(hash_or_raw) @fields = @field_names.map do |_| 0 end @size = @fields.count @name = self.class.name @notes = [] # hexdump annotation notes if hash_or_raw.instance_of?(Hash) init_hash(hash_or_raw) elsif hash_or_raw.instance_of?(Unpacker) init_unpacker(hash_or_raw) else init_raw(hash_or_raw) end end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/snapshot/snap_item_base.rb', line 6 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/snapshot/snap_item_base.rb', line 6 def name @name end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
6 7 8 |
# File 'lib/snapshot/snap_item_base.rb', line 6 def notes @notes end |
Instance Method Details
#init_hash(attr) ⇒ Object
58 59 60 61 62 |
# File 'lib/snapshot/snap_item_base.rb', line 58 def init_hash(attr) @fields_names.each do |name| instance_variable_set("@#{name}", attr[name] || 0) end end |
#init_raw(data) ⇒ Object
53 54 55 56 |
# File 'lib/snapshot/snap_item_base.rb', line 53 def init_raw(data) u = Unpacker.new(data) init_unpacker(u) end |
#init_unpacker(u) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/snapshot/snap_item_base.rb', line 28 def init_unpacker(u) @id = u.get_int p = u.parsed.last @notes.push([:cyan, p[:pos], p[:len], "id=#{@id}"]) i = 0 @fields.map! do |_| # TODO: as of right now it can get nil values here # the fix would be "u.get_int || 0" # but fixing it would probably make it harder # to debug invalid data # # but do rethink this in a later point please :) # for now call .validate() everywhere val = u.get_int p = u.parsed.last color = (i % 2).zero? ? :yellow : :pink desc = @field_names[i] @notes.push([color, p[:pos], p[:len], "#{desc}=#{val}"]) i += 1 val end end |
#to_a ⇒ Object
basically to_network int array the server sends to the client
75 76 77 78 79 80 81 |
# File 'lib/snapshot/snap_item_base.rb', line 75 def to_a arr = [] @fields.each do |value| arr += Packer.pack_int(value) end arr end |
#to_h ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/snapshot/snap_item_base.rb', line 64 def to_h hash = {} hash[:id] = @id @field_names.each_with_index do |name, index| hash[name] = @fields[index] end hash end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/snapshot/snap_item_base.rb', line 83 def to_s to_h end |
#validate ⇒ Object
24 25 26 |
# File 'lib/snapshot/snap_item_base.rb', line 24 def validate @fields.select(&:nil?).empty? end |