Class: CSstats::Handler
- Inherits:
-
Object
- Object
- CSstats::Handler
- Defined in:
- lib/csstats/handler.rb
Instance Attribute Summary collapse
-
#fileversion ⇒ Object
Returns the value of attribute fileversion.
-
#path ⇒ Object
Returns the value of attribute path.
-
#players ⇒ Object
Returns the value of attribute players.
-
#position ⇒ Object
Returns the value of attribute position.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Handler
constructor
Initialize file.
-
#player(id) ⇒ Object
Get the player information of specified id.
-
#players_count ⇒ Object
Get total players count.
-
#read_int_data(handle) ⇒ Object
Get the 32bit integer from file.
-
#read_player(handle) ⇒ Object
Read player information from file.
-
#read_short_data(handle) ⇒ Object
Get the 16bit integer from file.
-
#read_string_data(handle, length) ⇒ Object
Get the string from file.
-
#search_by_name(name) ⇒ Object
Get player by specified name.
Constructor Details
#initialize(options = {}) ⇒ Handler
Initialize file.
Options:
path - The String of csstats.dat file.
maxplayers - The Integer of how many players to return.
Returns nothing.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/csstats/handler.rb', line 14 def initialize(={}) @path = [:path] @players = [] @fileversion = 0x00 @position = 1 maxplayers = [:maxplayers] || 0 return if @path.nil? handle = File.new(@path, "r") @fileversion = read_short_data(handle); i = 0 while (!handle.eof? && (maxplayers == 0 || i < maxplayers)) player = read_player(handle) if player player['rank'] = i + 1 players[i] = player end i = i + 1 end end |
Instance Attribute Details
#fileversion ⇒ Object
Returns the value of attribute fileversion.
5 6 7 |
# File 'lib/csstats/handler.rb', line 5 def fileversion @fileversion end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/csstats/handler.rb', line 5 def path @path end |
#players ⇒ Object
Returns the value of attribute players.
5 6 7 |
# File 'lib/csstats/handler.rb', line 5 def players @players end |
#position ⇒ Object
Returns the value of attribute position.
5 6 7 |
# File 'lib/csstats/handler.rb', line 5 def position @position end |
Instance Method Details
#player(id) ⇒ Object
Get the player information of specified id.
id - The Integer of player id.
Returns the Hash of player information.
125 126 127 128 129 |
# File 'lib/csstats/handler.rb', line 125 def player(id) unless (@players[id-1].nil?) @players[id-1] end end |
#players_count ⇒ Object
Get total players count.
Returns the Integer of player count.
134 135 136 |
# File 'lib/csstats/handler.rb', line 134 def players_count @players.count end |
#read_int_data(handle) ⇒ Object
Get the 32bit integer from file.
handle - The File which was opened for reading data.
Returns the integer.
90 91 92 93 94 |
# File 'lib/csstats/handler.rb', line 90 def read_int_data(handle) data = handle.read(4) raise CSstats::Error, "Cannot read int data." unless data return data.unpack("V").first end |
#read_player(handle) ⇒ Object
Read player information from file.
handle - The File which was opened for reading data.
Returns The Hash (Mash) of player information.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/csstats/handler.rb', line 43 def read_player(handle) length = read_short_data(handle); return nil if (length == 0) mash = Hashie::Mash.new mash.nick = read_string_data(handle, length) length = read_short_data(handle) mash.uniq = read_string_data(handle, length) mash.teamkill = read_int_data(handle) mash.damage = read_int_data(handle) mash.deaths = read_int_data(handle) mash.kills = read_int_data(handle) mash.shots = read_int_data(handle) mash.hits = read_int_data(handle) mash.headshots = read_int_data(handle) mash.defusions = read_int_data(handle) mash.defused = read_int_data(handle) mash.plants = read_int_data(handle) mash.explosions = read_int_data(handle) read_int_data(handle) # 0x00000000 mash.head = read_int_data(handle) mash.chest = read_int_data(handle) mash.stomach = read_int_data(handle) mash.leftarm = read_int_data(handle) mash.rightarm = read_int_data(handle) mash.leftleg = read_int_data(handle) mash.rightleg = read_int_data(handle) mash.acc = mash.hits / mash.shots * 100 mash.eff = mash.kills / (mash.kills + mash.deaths) * 100 read_int_data(handle) # 0x00000000 return mash end |
#read_short_data(handle) ⇒ Object
Get the 16bit integer from file.
handle - The File which was opened for reading data.
Returns the integer.
101 102 103 104 105 |
# File 'lib/csstats/handler.rb', line 101 def read_short_data(handle) data = handle.read(2) raise CSstats::Error, "Cannot read short data." unless data return data.unpack("v").first end |
#read_string_data(handle, length) ⇒ Object
Get the string from file.
handle - The File which was opened for reading data. len - length of string to read.
Returns the string.
113 114 115 116 117 118 |
# File 'lib/csstats/handler.rb', line 113 def read_string_data(handle, length) data = handle.read(length) raise CSstats::Error, "Cannot read string data." unless data data = data.strip return data end |
#search_by_name(name) ⇒ Object
Get player by specified name.
name - The String of player name.
Returns the Hash of player information.
143 144 145 146 147 |
# File 'lib/csstats/handler.rb', line 143 def search_by_name(name) @players.each do |player| return player if (name == player.nick) end end |