teeworlds_network/scripts/packet_template.rb

46 lines
652 B
Ruby
Raw Normal View History

2022-11-13 10:15:33 +00:00
# frozen_string_literal: true
require_relative '../packer'
##
2022-11-14 17:52:25 +00:00
# PacketName
2022-11-13 10:15:33 +00:00
#
2022-11-14 17:52:25 +00:00
# SENDER -> RECEIVER
class PacketName
2022-11-13 10:15:33 +00:00
attr_accessor :foo, :bar
def initialize(hash_or_raw)
if hash_or_raw.instance_of?(Hash)
init_hash(hash_or_raw)
else
init_raw(hash_or_raw)
end
end
def init_raw(data)
2022-11-14 17:52:25 +00:00
Unpacker.new(data)
2022-11-13 10:15:33 +00:00
end
def init_hash(attr)
@foo = attr[:foo] || 0
end
def to_h
2022-11-14 17:52:25 +00:00
{ foo: @foo, bar: @bar }
2022-11-13 10:15:33 +00:00
end
2022-11-15 10:03:13 +00:00
def foo?
!@foo.zero?
end
2022-11-13 10:15:33 +00:00
# basically to_network
2022-11-14 17:52:25 +00:00
# int array the SENDER sends to the RECEIVER
2022-11-13 10:15:33 +00:00
def to_a
Packer.pack_int(@foo) + Packer.pack_str(@bar)
2022-11-13 10:15:33 +00:00
end
def to_s
to_h
end
end