csstats 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9065ccdcb64130e7cccf2af1c3abfdd313991478
4
- data.tar.gz: d814405281dbabd4d3bc638ff5f759927b60bfd9
3
+ metadata.gz: 9da3fbfae32bbf6cae85ac94caf59a66fb9fb4f8
4
+ data.tar.gz: 76547cef1effba619c15eded091132196cafe537
5
5
  SHA512:
6
- metadata.gz: e97ce568ccb03bd622e5d354f5ca5f97e8bf2cfa83975f5e1361e92b30f141de711fe17b5a8873fb69da1d73d5b41d34dbc1a4d2135912abfd6a44aefb804532
7
- data.tar.gz: 224d61e39d3cc63c63c1be20b054ed892f568d399a5412a886eae3eb15b92beea9fc96cacad7088a27e1d0dade71612dcc8eb0ca856285f695906c910828e712
6
+ metadata.gz: a9d31ba0a0059ad47e1d0bf66f7377b29edba4266151c2402cc886dc9690c3beac93aa002e2d3db8e5de0606b84729fd9bf543a5c0bb601da44eb9de78323f54
7
+ data.tar.gz: e533af087daa8172936c493196263c9f0264dac75966af80e4e3a1ea1ceb33868769106c7fd12991c336ef4a2b34229133762a20cb1cbecf021cf540aeadd164
data/Gemfile CHANGED
@@ -3,8 +3,9 @@ source 'https://rubygems.org'
3
3
  gem 'rake'
4
4
 
5
5
  group :test do
6
+ gem 'coveralls', require: false
6
7
  gem 'rspec', '>= 2.13'
7
- gem 'simplecov', :require => false
8
+ gem 'simplecov', require: false
8
9
  end
9
10
 
10
11
  gemspec
data/README.md CHANGED
@@ -3,8 +3,10 @@
3
3
  Gem which handle csstats.dat file generated by CSX module
4
4
  in AMX Mod X ([http://www.amxmodx.org/][amxx])
5
5
 
6
+ [![Gem Version](https://badge.fury.io/rb/csstats.png)][rubygems]
6
7
  [![Build Status](https://secure.travis-ci.org/jpalumickas/csstats.png?branch=master)][travis]
7
8
  [![Dependency Status](https://gemnasium.com/jpalumickas/csstats.png?travis)][gemnasium]
9
+ [![Coverage Status](https://coveralls.io/repos/jpalumickas/csstats/badge.png?branch=master)][coveralls]
8
10
 
9
11
  ## Installation
10
12
 
@@ -24,6 +26,17 @@ Or install it yourself as:
24
26
 
25
27
  ```ruby
26
28
  require 'csstats'
29
+
30
+ stats = CSstats.new(path: 'csstats.dat')
31
+ stats.player(2).nick
32
+ ```
33
+
34
+ You can set maxplayers value if you need to get specified number of players.
35
+
36
+ ```ruby
37
+ stats = CSstats.new(path: 'csstats.dat', maxplayers: 15)
38
+ stats.players_count
39
+ # => 15
27
40
  ```
28
41
 
29
42
  ## Supported Ruby Versions
@@ -42,6 +55,7 @@ See [LICENSE][] for details.
42
55
  [rubygems]: https://rubygems.org/gems/csstats
43
56
  [travis]: http://travis-ci.org/jpalumickas/csstats
44
57
  [gemnasium]: https://gemnasium.com/jpalumickas/csstats
58
+ [coveralls]: https://coveralls.io/r/jpalumickas/csstats
45
59
 
46
60
  [amxx]: http://www.amxmodx.org/
47
61
  [license]: LICENSE.md
data/lib/csstats.rb CHANGED
@@ -1,8 +1,12 @@
1
+ require 'csstats/error'
1
2
  require 'csstats/handler'
2
3
 
3
4
  module CSstats
4
5
  class << self
5
6
 
7
+ # Alias for CSstats::Handler.new
8
+ #
9
+ # Returns CSstats::Handler
6
10
  def new(options={})
7
11
  CSstats::Handler.new(options)
8
12
  end
@@ -0,0 +1,3 @@
1
+ module CSstats
2
+ class Error < StandardError; end
3
+ end
@@ -4,6 +4,13 @@ module CSstats
4
4
  class Handler
5
5
  attr_accessor :path, :players, :fileversion, :position
6
6
 
7
+ # Initialize file.
8
+ #
9
+ # Options:
10
+ # path - The String of csstats.dat file.
11
+ # maxplayers - The Integer of how many players to return.
12
+ #
13
+ # Returns nothing.
7
14
  def initialize(options={})
8
15
  @path = options[:path]
9
16
  @players = []
@@ -17,89 +24,126 @@ module CSstats
17
24
 
18
25
  @fileversion = read_short_data(handle);
19
26
 
20
- i = 1
21
- while (!handle.eof? && (maxplayers == 0 || i <= maxplayers))
27
+ i = 0
28
+ while (!handle.eof? && (maxplayers == 0 || i < maxplayers))
22
29
  player = read_player(handle)
23
30
  if player
24
- player['rank'] = i
31
+ player['rank'] = i+1
25
32
  players[i] = player
26
33
  end
27
34
  i = i + 1
28
35
  end
29
36
  end
30
37
 
38
+ # Read player information from file.
39
+ #
40
+ # handle - The File which was opened for reading data.
41
+ #
42
+ # Returns The Hash (Mash) of player information.
31
43
  def read_player(handle)
32
- p = Hashie::Mash.new
44
+ hash = Hashie::Mash.new
33
45
  l = read_short_data(handle);
34
46
 
35
47
  return nil if (l == 0)
36
48
 
37
- p['nick'] = read_string_data(handle, l)
49
+ hash['nick'] = read_string_data(handle, l)
38
50
  l = read_short_data(handle)
39
- p['uniq'] = read_string_data(handle, l)
51
+ hash['uniq'] = read_string_data(handle, l)
40
52
 
41
- p['teamkill'] = read_int_data(handle)
42
- p['damage'] = read_int_data(handle)
43
- p['deaths'] = read_int_data(handle)
44
- p['kills'] = read_int_data(handle)
45
- p['shots'] = read_int_data(handle)
46
- p['hits'] = read_int_data(handle)
47
- p['headshots'] = read_int_data(handle)
53
+ hash['teamkill'] = read_int_data(handle)
54
+ hash['damage'] = read_int_data(handle)
55
+ hash['deaths'] = read_int_data(handle)
56
+ hash['kills'] = read_int_data(handle)
57
+ hash['shots'] = read_int_data(handle)
58
+ hash['hits'] = read_int_data(handle)
59
+ hash['headshots'] = read_int_data(handle)
48
60
 
49
- p['defusions'] = read_int_data(handle)
50
- p['defused'] = read_int_data(handle)
51
- p['plants'] = read_int_data(handle)
52
- p['explosions'] = read_int_data(handle)
61
+ hash['defusions'] = read_int_data(handle)
62
+ hash['defused'] = read_int_data(handle)
63
+ hash['plants'] = read_int_data(handle)
64
+ hash['explosions'] = read_int_data(handle)
53
65
 
54
66
  read_int_data(handle) # 0x00000000
55
67
 
56
- p['head'] = read_int_data(handle)
57
- p['chest'] = read_int_data(handle)
58
- p['stomach'] = read_int_data(handle)
59
- p['leftarm'] = read_int_data(handle)
60
- p['rightarm'] = read_int_data(handle)
61
- p['leftleg'] = read_int_data(handle)
62
- p['rightleg'] = read_int_data(handle)
68
+ hash['head'] = read_int_data(handle)
69
+ hash['chest'] = read_int_data(handle)
70
+ hash['stomach'] = read_int_data(handle)
71
+ hash['leftarm'] = read_int_data(handle)
72
+ hash['rightarm'] = read_int_data(handle)
73
+ hash['leftleg'] = read_int_data(handle)
74
+ hash['rightleg'] = read_int_data(handle)
63
75
 
64
76
  read_int_data(handle) # 0x00000000
65
77
 
66
- return p
78
+ return hash
67
79
  end
68
80
 
81
+ # Get the 32bit integer from file.
82
+ #
83
+ # handle - The File which was opened for reading data.
84
+ #
85
+ # Returns the integer.
69
86
  def read_int_data(handle)
70
- #f = File.new(handle)
71
- d = handle.read(4)
72
- d = d.unpack("V")
73
- return d[0]
87
+ data = handle.read(4)
88
+ raise CSstats::Error, "Cannot read int data." unless data
89
+ data = data.unpack("V")
90
+ return data.first
74
91
  end
75
92
 
93
+ # Get the 16bit integer from file.
94
+ #
95
+ # handle - The File which was opened for reading data.
96
+ #
97
+ # Returns the integer.
76
98
  def read_short_data(handle)
77
- #f = File.new(handle)
78
- d = handle.read(2)
79
- #if ($d === false)
80
- # throw new CSstatsException("Can not read data.");
81
- d = d.unpack("v")
82
- return d[0]
99
+ data = handle.read(2)
100
+ raise CSstats::Error, "Cannot read short data." unless data
101
+ data = data.unpack("v")
102
+ return data.first
83
103
  end
84
104
 
85
- def read_string_data(handle, len)
86
- d = handle.read(len)
87
- #if ($d === false)
88
- # throw new CSstatsException("Can not read data.");
89
- d = d.strip
90
- return d
105
+ # Get the string from file.
106
+ #
107
+ # handle - The File which was opened for reading data.
108
+ # len - length of string to read.
109
+ #
110
+ # Returns the string.
111
+ def read_string_data(handle, length)
112
+ data = handle.read(length)
113
+ raise CSstats::Error, "Cannot read string data." unless data
114
+ data = data.strip
115
+ return data
91
116
  end
92
117
 
118
+ # Get the player information of specified id.
119
+ #
120
+ # id - The Integer of player id.
121
+ #
122
+ # Returns the Hash of player information.
93
123
  def player(id)
94
- unless (@players[id].nil?)
95
- @players[id]
124
+ unless (@players[id-1].nil?)
125
+ @players[id-1]
96
126
  else
97
127
  nil
98
128
  end
99
129
  end
100
130
 
131
+ # Get total players count.
132
+ #
133
+ # Returns the Integer of player count.
101
134
  def players_count
102
- @players.count - 1
135
+ @players.count
136
+ end
137
+
138
+ # Get player by specified name.
139
+ #
140
+ # name - The String of player name.
141
+ #
142
+ # Returns the Hash of player information.
143
+ def search_by_name(name)
144
+ @players.each do |player|
145
+ return player if (name == player.nick)
146
+ end
103
147
  end
104
148
  end
105
149
  end
@@ -1,3 +1,3 @@
1
1
  module CSstats
2
- VERSION = "0.2.0" unless defined?(CSstats::VERSION)
2
+ VERSION = "1.0.0" unless defined?(CSstats::VERSION)
3
3
  end
@@ -14,4 +14,9 @@ describe CSstats::Handler do
14
14
  expect(@handler.player(2).nick).to eq "CHMARSON"
15
15
  end
16
16
 
17
+ it "should return searched player" do
18
+ player = @handler.search_by_name("CHMARSON")
19
+ expect(player.nick).to eq "CHMARSON"
20
+ end
21
+
17
22
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'simplecov'
2
+ require 'coveralls'
2
3
 
3
4
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
- SimpleCov::Formatter::HTMLFormatter
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
5
7
  ]
6
8
  SimpleCov.start
7
9
 
@@ -14,14 +16,7 @@ RSpec.configure do |config|
14
16
  end
15
17
  end
16
18
 
17
- def fixture_path
18
- File.expand_path("../fixtures", __FILE__)
19
- end
20
-
21
- def fixture(file)
22
- File.new(fixture_path + '/' + file)
23
- end
24
-
25
19
  def csstats_file
26
- fixture('csstats.dat').path
20
+ fixtures = File.expand_path("../fixtures", __FILE__)
21
+ File.new(fixtures + '/csstats.dat').path
27
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justas Palumickas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-15 00:00:00.000000000 Z
11
+ date: 2013-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,6 +56,7 @@ files:
56
56
  - Rakefile
57
57
  - csstats.gemspec
58
58
  - lib/csstats.rb
59
+ - lib/csstats/error.rb
59
60
  - lib/csstats/handler.rb
60
61
  - lib/csstats/version.rb
61
62
  - spec/csstats/handler_spec.rb