diff --git a/lib/network.rb b/lib/network.rb index 4563c58..65a9876 100644 --- a/lib/network.rb +++ b/lib/network.rb @@ -117,3 +117,32 @@ MAX_PLAYERS = 16 MAP_CHUNK_SIZE = NET_MAX_PAYLOAD - NET_MAX_CHUNKHEADERSIZE - 4 # msg type SERVER_TICK_SPEED = 50 + +# should this be moved to some snap related file? + +NETOBJ_INVALID = 0 +NETOBJTYPE_PLAYERINPUT = 1 +NETOBJTYPE_PROJECTILE = 2 +NETOBJTYPE_LASER = 3 +NETOBJTYPE_PICKUP = 4 +NETOBJTYPE_FLAG = 5 +NETOBJTYPE_GAMEDATA = 6 +NETOBJTYPE_GAMEDATATEAM = 7 +NETOBJTYPE_GAMEDATAFLAG = 8 +NETOBJTYPE_CHARACTERCORE = 9 +NETOBJTYPE_CHARACTER = 10 +NETOBJTYPE_PLAYERINFO = 11 +NETOBJTYPE_SPECTATORINFO = 12 +NETOBJTYPE_DE_CLIENTINFO = 13 +NETOBJTYPE_DE_GAMEINFO = 14 +NETOBJTYPE_DE_TUNEPARAMS = 15 +NETEVENTTYPE_COMMON = 16 +NETEVENTTYPE_EXPLOSION = 17 +NETEVENTTYPE_SPAWN = 18 +NETEVENTTYPE_HAMMERHIT = 19 +NETEVENTTYPE_DEATH = 20 +NETEVENTTYPE_SOUNDWORLD = 21 +NETEVENTTYPE_DAMAGE = 22 +NETOBJTYPE_PLAYERINFORACE = 23 +NETOBJTYPE_GAMEDATARACE = 24 +NUM_NETOBJTYPE = 25 diff --git a/lib/snap_items/game_data.rb b/lib/snap_items/game_data.rb new file mode 100644 index 0000000..11391c4 --- /dev/null +++ b/lib/snap_items/game_data.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require_relative '../packer' + +class NetObj + class GameData + attr_accessor :game_start_tick, :game_state_flags, :game_state_end_tick + + def initialize(hash_or_raw) + if hash_or_raw.instance_of?(Hash) + init_hash(hash_or_raw) + else + init_raw(hash_or_raw) + end + @fields = instance_variables.map { |f| f.to_s[1..] } + @size = @fields.count + end + + def match_type?(type) + type == NETOBJTYPE_GAMEDATA + end + + def init_raw(data) + u = Unpacker.new(data) + @game_start_tick = u.get_int + @game_state_flags = u.get_int + @game_state_end_tick = u.get_int + end + + def init_hash(attr) + @game_start_tick = attr[:game_start_tick] || 0 + @game_state_flags = attr[:game_state_flags] || 0 + @game_state_end_tick = attr[:game_state_end_tick] || 0 + end + + def to_h + { + game_start_tick: @game_start_tick, + game_state_flags: @game_state_flags, + game_state_end_tick: @game_state_end_tick + } + end + + # basically to_network + # int array the server sends to the client + def to_a + Packer.pack_int(@game_start_tick) + + Packer.pack_int(@game_state_flags) + + Packer.pack_int(@game_state_end_tick) + end + + def to_s + to_h + end + end +end + +p NetObj::GameData.new([])