From 49b2806966ca2b342d209b0381296f3135f70199 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 17 Sep 2023 11:47:23 +0200 Subject: [PATCH] Use rconline class --- lib/game_client.rb | 8 +++---- lib/messages/rcon_line.rb | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 lib/messages/rcon_line.rb diff --git a/lib/game_client.rb b/lib/game_client.rb index 92b64ac..b0d3c0b 100644 --- a/lib/game_client.rb +++ b/lib/game_client.rb @@ -3,6 +3,7 @@ require_relative 'models/player' require_relative 'models/chat_message' require_relative 'messages/input_timing' +require_relative 'messages/rcon_line' require_relative 'messages/sv_client_drop' require_relative 'messages/rcon_cmd_add' require_relative 'messages/rcon_cmd_rem' @@ -144,11 +145,8 @@ def on_disconnect(data) end def on_rcon_line(chunk) - u = Unpacker.new(chunk.data[1..]) - context = Context.new( - nil, - line: u.get_string - ) + message = RconLine.new(chunk.data[1..]) + context = Context.new(message) call_hook(:rcon_line, context) end diff --git a/lib/messages/rcon_line.rb b/lib/messages/rcon_line.rb new file mode 100644 index 0000000..3ac7192 --- /dev/null +++ b/lib/messages/rcon_line.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require_relative '../packer' + +## +# RconLine +# +# Server -> Client +class RconLine + attr_accessor :command + + 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) + @command = u.get_string + end + + def init_hash(attr) + @command = attr[:command] || 'hello world' + end + + def to_h + { + command: @command + } + end + + # basically to_network + # int array the Server sends to the Client + def to_a + Packer.pack_str(@command) + end + + def to_s + to_h + end +end