From c11a7ae2d22fd9e478fa91a1417d5a9a0f3fb10d Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Tue, 15 Nov 2022 12:13:48 +0100 Subject: [PATCH] Generate SvClientDrop twnet g sv SvClientDrop client_id:int reason:str silent:bool --- lib/models/sv_client_drop.rb | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/models/sv_client_drop.rb diff --git a/lib/models/sv_client_drop.rb b/lib/models/sv_client_drop.rb new file mode 100644 index 0000000..d22ee31 --- /dev/null +++ b/lib/models/sv_client_drop.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require_relative '../packer' + +## +# SvClientDrop +# +# Server -> Client +class SvClientDrop + attr_accessor :client_id, :reason, :silent + + 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) + u = Unpacker.new(data) + @client_id = u.get_int + @reason = u.get_string + @silent = u.get_int + end + + def init_hash(attr) + @client_id = attr[:client_id] || 0 + @reason = attr[:reason] || '' + @silent = attr[:silent] || false + end + + def to_h + { + client_id: @client_id, + reason: @reason, + silent: @silent + } + end + + def silent? + !@silent.zero? + end + + # basically to_network + # int array the Server sends to the Client + def to_a + Packer.pack_int(@client_id) + + Packer.pack_str(@reason) + + Packer.pack_int(@silent) + end + + def to_s + to_h + end +end