Class: CSstats::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/csstats/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Handler

Public: Initialize file.

options - The Hash options:

:path       - The String of csstats.dat file path (required).
:maxplayers - The Integer of how many players to return (optional).

Returns nothing.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/csstats/handler.rb', line 14

def initialize(options = {})
  @path = options[:path]
  @players = []

  maxplayers = options[:maxplayers] || 0

  return if @path.nil?

  handle = File.new(@path, 'r')

  @file_version = 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

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/csstats/handler.rb', line 5

def path
  @path
end

#playersObject (readonly)

Returns the value of attribute players.



5
6
7
# File 'lib/csstats/handler.rb', line 5

def players
  @players
end

Instance Method Details

#player(id) ⇒ Object

Public: Get the player information of specified id.

id - The Integer of player id.

Returns the Mash of player information.



42
43
44
45
46
# File 'lib/csstats/handler.rb', line 42

def player(id)
  unless @players[id - 1].nil?
    @players[id - 1]
  end
end

#players_countObject

Public: Get total players count.

Returns the Integer of player count.



51
52
53
# File 'lib/csstats/handler.rb', line 51

def players_count
  @players.count
end

#search_by_name(name) ⇒ Object

Public: Get player by specified name.

name - The String of player name.

Returns the Mash of player information.



60
61
62
63
64
# File 'lib/csstats/handler.rb', line 60

def search_by_name(name)
  @players.each do |player|
    return player if name == player.nick
  end
end