Overengineer snap items (but not in a good way)

This commit is contained in:
ChillerDragon 2022-11-18 09:57:43 +01:00
parent cb48c1028d
commit 396b539cca

View file

@ -7,46 +7,66 @@ class NetObj
attr_accessor :game_start_tick, :game_state_flags, :game_state_end_tick attr_accessor :game_start_tick, :game_state_flags, :game_state_end_tick
def initialize(hash_or_raw) def initialize(hash_or_raw)
@field_names = %i[
game_start_tick
game_state_flags
game_state_end_tick
]
@fields = @field_names.map do |_|
0
end
@size = @fields.count
if hash_or_raw.instance_of?(Hash) if hash_or_raw.instance_of?(Hash)
init_hash(hash_or_raw) init_hash(hash_or_raw)
else else
init_raw(hash_or_raw) init_raw(hash_or_raw)
end end
@fields = instance_variables.map { |f| f.to_s[1..] }
@size = @fields.count
end end
def match_type?(type) def match_type?(type)
type == NETOBJTYPE_GAMEDATA type == NETOBJTYPE_GAMEDATA
end end
def validate
@fields.select(&:nil?).empty?
end
def init_raw(data) def init_raw(data)
u = Unpacker.new(data) u = Unpacker.new(data)
@game_start_tick = u.get_int @fields.map! do |_|
@game_state_flags = u.get_int # TODO: as of right now it can get nil values here
@game_state_end_tick = u.get_int # 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
u.get_int
end
end end
def init_hash(attr) def init_hash(attr)
@game_start_tick = attr[:game_start_tick] || 0 @fields_names.each do |name|
@game_state_flags = attr[:game_state_flags] || 0 instance_variable_set("@#{name}", attr[name] || 0)
@game_state_end_tick = attr[:game_state_end_tick] || 0 end
end end
def to_h def to_h
{ hash = {}
game_start_tick: @game_start_tick, @field_names.each_with_index do |name, index|
game_state_flags: @game_state_flags, hash[name] = @fields[index]
game_state_end_tick: @game_state_end_tick end
} hash
end end
# basically to_network # basically to_network
# int array the server sends to the client # int array the server sends to the client
def to_a def to_a
Packer.pack_int(@game_start_tick) + arr = []
Packer.pack_int(@game_state_flags) + @fields.each do |value|
Packer.pack_int(@game_state_end_tick) arr += Packer.pack_int(value)
end
arr
end end
def to_s def to_s
@ -54,5 +74,3 @@ class NetObj
end end
end end
end end
p NetObj::GameData.new([])