csstats 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +33 -0
  3. data/.travis.yml +23 -7
  4. data/Gemfile +3 -7
  5. data/LICENSE +1 -1
  6. data/README.md +12 -9
  7. data/bin/console +1 -1
  8. data/csstats.gemspec +3 -4
  9. data/lib/csstats.rb +5 -3
  10. data/lib/csstats/client.rb +23 -0
  11. data/lib/csstats/columns.rb +28 -0
  12. data/lib/csstats/parser.rb +13 -4
  13. data/lib/csstats/parser/reader.rb +11 -0
  14. data/lib/csstats/parser/reader/file_reader.rb +45 -0
  15. data/lib/csstats/parser/{file_reader/handler.rb → reader/file_streamer.rb} +8 -8
  16. data/lib/csstats/parser/reader/player.rb +41 -0
  17. data/lib/csstats/parser/reader/players.rb +41 -0
  18. data/lib/csstats/{writer.rb → parser/writer.rb} +4 -2
  19. data/lib/csstats/{writer/file_writer/handler.rb → parser/writer/file_streamer.rb} +9 -10
  20. data/lib/csstats/parser/writer/player.rb +34 -0
  21. data/lib/csstats/parser/writer/players.rb +29 -0
  22. data/lib/csstats/player.rb +34 -0
  23. data/lib/csstats/players.rb +52 -0
  24. data/lib/csstats/version.rb +1 -1
  25. data/spec/csstats/client_spec.rb +13 -0
  26. data/spec/csstats/parser/reader/player_spec.rb +17 -0
  27. data/spec/csstats/parser/writer/players_spec.rb +30 -0
  28. data/spec/csstats/player_spec.rb +109 -0
  29. data/spec/csstats/players_spec.rb +30 -0
  30. data/spec/csstats_spec.rb +2 -2
  31. data/spec/spec_helper.rb +3 -7
  32. metadata +29 -21
  33. data/lib/csstats/handler.rb +0 -50
  34. data/lib/csstats/parser/file_reader.rb +0 -39
  35. data/lib/csstats/parser/player.rb +0 -82
  36. data/lib/csstats/parser/players.rb +0 -42
  37. data/lib/csstats/writer/player.rb +0 -39
  38. data/lib/csstats/writer/players.rb +0 -24
  39. data/spec/csstats/handler_spec.rb +0 -113
  40. data/spec/csstats/parser/player_spec.rb +0 -17
  41. data/spec/csstats/writer/players_spec.rb +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b6e7d60a406dbc4c07be6f28a230f6cd8609c509
4
- data.tar.gz: f0a2a88fc14bd167cc7cc06400cbddaa316a9e56
2
+ SHA256:
3
+ metadata.gz: eea82615b3acd8120d7a37421c4a8dc927d7fb287b87dc7114398081bb37c6ed
4
+ data.tar.gz: 06f4df73a2c9be3f1f966c5b92db657c635741b2ebfd6d31940a917777e9bc99
5
5
  SHA512:
6
- metadata.gz: 49e281f6effe3c0534b25f0e3f43430b399c726512a9eeb7105fa6f73425c99c220a68ae37260bb377e918833a853269e58507475f11ac860c90137226c910dd
7
- data.tar.gz: 9453510b9dcb1b140243e4a0778b073e6339632a26fb12e7afad8d314190ccbb230b3720847e353a1ef236c6272b01428623194a9d0e763d7686bf0135396aed
6
+ metadata.gz: d6209408d58fcf60f9f4627f85f8f9a1683b059e3cdfe1501bf10ab740d6a1ff742d1a2587564314a690476719608fd9ce9bba902016d33308399abc816c5b32
7
+ data.tar.gz: 6adbfb5ebf1396f25b0e50741b4591381332701c8de674e759f738e11c6605716c4a57897b950fe1f364035a96042053a7bb00f7e2f38755098e6c3981f05d64
@@ -1,2 +1,35 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
+ Include:
4
+ - '**/Rakefile'
5
+ - '**/Gemfile'
6
+
7
+ Layout/AlignParameters:
8
+ Enabled: true
9
+ EnforcedStyle: with_fixed_indentation
10
+
11
+ Layout/MultilineOperationIndentation:
12
+ Enabled: true
13
+ EnforcedStyle: indented
14
+
15
+ Layout/MultilineMethodCallIndentation:
16
+ Enabled: true
17
+ EnforcedStyle: indented
18
+
19
+ Layout/CaseIndentation:
20
+ Enabled: true
21
+ EnforcedStyle: end
22
+
23
+ Layout/EndAlignment:
24
+ Enabled: true
25
+ EnforcedStyleAlignWith: variable
26
+
27
+ Style/FrozenStringLiteralComment:
28
+ Enabled: false
29
+
1
30
  Style/Documentation:
2
31
  Enabled: false
32
+
33
+ Metrics/BlockLength:
34
+ Exclude:
35
+ - 'spec/**/*_spec.rb'
@@ -1,20 +1,36 @@
1
1
  sudo: false
2
- language: ruby
2
+ bundler_args: --without development
3
3
 
4
- before_install:
5
- - gem install bundler
4
+ cache:
5
+ bundler: true
6
6
 
7
- bundler_args: --without development
7
+ env:
8
+ global:
9
+ - CC_TEST_REPORTER_ID=b17eed44b7f9100d09d50eb58b2b4e7272022e7245676d04dd30378f69eba6dd
8
10
 
11
+ language: ruby
9
12
  rvm:
10
- - 2.0.0
11
13
  - 2.1.0
12
14
  - 2.2.0
13
15
  - 2.3.0
16
+ - 2.4.0
17
+ - 2.5.0
14
18
  - ruby-head
15
- - jruby-head
19
+
20
+ before_install:
21
+ - gem update --system
22
+
23
+ before_script:
24
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
25
+ - chmod +x ./cc-test-reporter
26
+ - ./cc-test-reporter before-build
27
+
28
+ script:
29
+ - bundle exec rspec
30
+
31
+ after_script:
32
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
16
33
 
17
34
  matrix:
18
35
  allow_failures:
19
36
  - rvm: ruby-head
20
- - rvm: jruby-head
data/Gemfile CHANGED
@@ -1,9 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rake'
4
-
5
3
  group :development do
6
- gem 'rubocop', '~> 0.44.1'
4
+ gem 'rubocop', '~> 0.53'
7
5
  end
8
6
 
9
7
  group :development, :test do
@@ -11,10 +9,8 @@ group :development, :test do
11
9
  end
12
10
 
13
11
  group :test do
14
- gem 'codeclimate-test-reporter', '~> 0.6.0', require: false
15
- gem 'coveralls', '~> 0.8.15', require: false
16
- gem 'rspec', '~> 3.5.0'
17
- gem 'simplecov', '~> 0.12.0', require: false
12
+ gem 'rspec', '~> 3.7'
13
+ gem 'simplecov', '~> 0.15', require: false
18
14
  end
19
15
 
20
16
  gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016 Justas Palumickas
3
+ Copyright (c) 2018 Justas Palumickas
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  [![Gem Version](https://img.shields.io/gem/v/csstats.svg?style=flat-square)][rubygems]
4
4
  [![Build Status](https://img.shields.io/travis/jpalumickas/csstats.svg?style=flat-square)][travis]
5
5
  [![Dependency Status](https://img.shields.io/gemnasium/jpalumickas/csstats.svg?style=flat-square)][gemnasium]
6
- [![Coverage Status](https://img.shields.io/coveralls/jpalumickas/csstats.svg?branch=master&style=flat-square)][coveralls]
7
- [![Code Climate](https://img.shields.io/codeclimate/github/jpalumickas/csstats.svg?style=flat-square)][codeclimate]
6
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/jpalumickas/csstats.svg?style=flat-square)][codeclimate]
7
+ [![Test Coverage](https://img.shields.io/codeclimate/c/jpalumickas/csstats.svg?style=flat-square)][codeclimate_coverage]
8
8
 
9
9
  Gem which handle `csstats.dat` file generated by CSX module
10
10
  in AMX Mod X ([http://www.amxmodx.org/][amxx])
@@ -29,14 +29,14 @@ Or install it yourself as:
29
29
  require 'csstats'
30
30
 
31
31
  stats = CSstats.new(path: 'csstats.dat')
32
- stats.player(2).nick
32
+ stats.players.find(2).nick
33
33
  ```
34
34
 
35
- You can set maxplayers value if you need to get specified number of players.
35
+ You can set `max_players` option if you need to get specified number of players.
36
36
 
37
37
  ```ruby
38
- stats = CSstats.new(path: 'csstats.dat', maxplayers: 15)
39
- stats.players_count
38
+ stats = CSstats.new(path: 'csstats.dat', max_players: 15)
39
+ stats.players.count
40
40
  # => 15
41
41
  ```
42
42
 
@@ -44,9 +44,10 @@ You can get player information by specified name.
44
44
 
45
45
  ```ruby
46
46
  stats = CSstats.new(path: 'csstats.dat')
47
- player_stats = stats.search_by_name('nick')
47
+ player_stats = stats.players.find_by(nick: 'my-super-nick')
48
48
 
49
49
  puts player_stats.kills
50
+ # => 3
50
51
  ```
51
52
 
52
53
  ## Supported Ruby Versions
@@ -54,13 +55,14 @@ puts player_stats.kills
54
55
  This library aims to support and is [tested against][travis] the following Ruby
55
56
  implementations:
56
57
 
57
- * Ruby 2.0.0
58
58
  * Ruby 2.1.0
59
59
  * Ruby 2.2.0
60
60
  * Ruby 2.3.0
61
+ * Ruby 2.4.0
62
+ * Ruby 2.5.0
61
63
 
62
64
  ## Copyright
63
- Copyright (c) 2013-2016 Justas Palumickas.
65
+ Copyright (c) 2013-2018 Justas Palumickas.
64
66
  See [LICENSE][license] for details.
65
67
 
66
68
  [rubygems]: https://rubygems.org/gems/csstats
@@ -68,6 +70,7 @@ See [LICENSE][license] for details.
68
70
  [gemnasium]: https://gemnasium.com/jpalumickas/csstats
69
71
  [coveralls]: https://coveralls.io/r/jpalumickas/csstats
70
72
  [codeclimate]: https://codeclimate.com/github/jpalumickas/csstats
73
+ [codeclimate_coverage]: https://codeclimate.com/github/jpalumickas/csstats/test_coverage
71
74
 
72
75
  [amxx]: http://www.amxmodx.org/
73
76
  [license]: https://raw.githubusercontent.com/jpalumickas/csstats/master/LICENSE
@@ -7,7 +7,7 @@ require 'csstats'
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- require "pry"
10
+ require 'pry'
11
11
  Pry.start
12
12
 
13
13
  # require 'irb'
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'csstats/version'
5
4
 
@@ -14,7 +13,7 @@ Gem::Specification.new do |gem|
14
13
  ' module in AMX Mod X (http://www.amxmodx.org/)'
15
14
 
16
15
  gem.license = 'MIT'
17
- gem.required_ruby_version = '>= 1.9.2'
16
+ gem.required_ruby_version = '>= 2.1.0'
18
17
 
19
18
  gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
20
19
  gem.executables = gem.files.grep(%r{^bin\/}).map { |f| File.basename(f) }
@@ -22,5 +21,5 @@ Gem::Specification.new do |gem|
22
21
  gem.require_paths = ['lib']
23
22
 
24
23
  gem.add_dependency 'hashie', '~> 3.4'
25
- gem.add_development_dependency 'bundler', '~> 1.6'
24
+ gem.add_development_dependency 'bundler', '~> 1.16'
26
25
  end
@@ -1,13 +1,15 @@
1
+ require 'csstats/columns'
1
2
  require 'csstats/error'
3
+ require 'csstats/player'
2
4
  require 'csstats/parser'
3
- require 'csstats/writer'
4
- require 'csstats/handler'
5
+ require 'csstats/client'
6
+ require 'csstats/players'
5
7
 
6
8
  module CSstats
7
9
  # Alias for CSstats::Handler.new
8
10
  #
9
11
  # Returns CSstats::Handler
10
12
  def self.new(options = {})
11
- CSstats::Handler.new(options)
13
+ CSstats::Client.new(options)
12
14
  end
13
15
  end
@@ -0,0 +1,23 @@
1
+ module CSstats
2
+ class Client
3
+ attr_reader :file_path, :max_players
4
+
5
+ # Public: Initialize file.
6
+ #
7
+ # options - The Hash options:
8
+ # :path - The String of csstats.dat file path (required).
9
+ # :max_players - The Integer of how many players to return (optional).
10
+ #
11
+ # Returns nothing.
12
+ def initialize(options = {})
13
+ @file_path = options[:path]
14
+ @max_players = options[:max_players] || 0
15
+
16
+ raise CSstats::FileNotExist unless File.exist?(file_path.to_s)
17
+ end
18
+
19
+ def players
20
+ @players ||= CSstats::Players.new(self)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module CSstats
2
+ COLUMNS = [
3
+ { name: :nick, type: :string },
4
+ { name: :uniq, type: :string },
5
+ { name: :teamkill, type: :integer },
6
+ { name: :damage, type: :integer },
7
+ { name: :deaths, type: :integer },
8
+ { name: :kills, type: :integer },
9
+ { name: :shots, type: :integer },
10
+ { name: :hits, type: :integer },
11
+ { name: :headshots, type: :integer },
12
+ { name: :defusions, type: :integer },
13
+ { name: :defused, type: :integer },
14
+ { name: :plants, type: :integer },
15
+ { name: :explosions, type: :integer },
16
+ {}, # Empty
17
+ { name: :head, type: :integer },
18
+ { name: :chest, type: :integer },
19
+ { name: :stomach, type: :integer },
20
+ { name: :leftarm, type: :integer },
21
+ { name: :rightarm, type: :integer },
22
+ { name: :leftleg, type: :integer },
23
+ { name: :rightleg, type: :integer },
24
+ {} # Empty
25
+ ].freeze
26
+
27
+ COLUMN_KEYS = COLUMNS.reject(&:empty?).flat_map { |column| column[:name] }
28
+ end
@@ -1,9 +1,18 @@
1
- require_relative 'parser/file_reader/handler'
2
- require_relative 'parser/file_reader'
3
- require_relative 'parser/player'
4
- require_relative 'parser/players'
1
+ require_relative 'parser/reader'
2
+ require_relative 'parser/writer'
5
3
 
6
4
  module CSstats
7
5
  module Parser
6
+ class << self
7
+ def get_players(client)
8
+ CSstats::Parser::Reader::Players.new(
9
+ client.file_path, max_players: client.max_players
10
+ ).parse
11
+ end
12
+
13
+ def write_players(client, players)
14
+ CSstats::Parser::Writer::Players.new(client.file_path).write(players)
15
+ end
16
+ end
8
17
  end
9
18
  end
@@ -0,0 +1,11 @@
1
+ require_relative 'reader/file_streamer'
2
+ require_relative 'reader/file_reader'
3
+ require_relative 'reader/player'
4
+ require_relative 'reader/players'
5
+
6
+ module CSstats
7
+ module Parser
8
+ module Reader
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'file_streamer'
2
+
3
+ module CSstats
4
+ module Parser
5
+ module Reader
6
+ class FileReader
7
+ attr_reader :file_path, :limit
8
+
9
+ def initialize(file_path, options = {})
10
+ @file_path = file_path
11
+ @limit = options[:limit] || 0
12
+
13
+ raise CSstats::FileNotExist unless File.exist?(file_path.to_s)
14
+ end
15
+
16
+ def read(&block)
17
+ return unless block_given?
18
+
19
+ file = File.new(file_path, 'r')
20
+
21
+ # Need to read first data, which means file version.
22
+ _file_version = streamer(file).read_short_data
23
+
24
+ read_data(file, &block)
25
+
26
+ file.close
27
+ end
28
+
29
+ private
30
+
31
+ def streamer(file)
32
+ CSstats::Parser::Reader::FileStreamer.new(file)
33
+ end
34
+
35
+ def read_data(file, &_block)
36
+ index = 0
37
+ while !file.eof? && (limit.zero? || index < limit)
38
+ yield(streamer(file), index)
39
+ index += 1
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,18 +1,18 @@
1
1
  module CSstats
2
2
  module Parser
3
- class FileReader
4
- class Handler
5
- attr_reader :handle
3
+ module Reader
4
+ class FileStreamer
5
+ attr_reader :stream
6
6
 
7
- def initialize(handle)
8
- @handle = handle
7
+ def initialize(stream)
8
+ @stream = stream
9
9
  end
10
10
 
11
11
  # Internal: Get the 32bit integer from file.
12
12
  #
13
13
  # Returns the Integer.
14
14
  def read_int_data
15
- data = handle.read(4)
15
+ data = stream.read(4)
16
16
  raise CSstats::Error, 'Cannot read int data.' unless data
17
17
 
18
18
  data.unpack('V').first
@@ -22,7 +22,7 @@ module CSstats
22
22
  #
23
23
  # Returns the Integer.
24
24
  def read_short_data
25
- data = handle.read(2)
25
+ data = stream.read(2)
26
26
  raise CSstats::Error, 'Cannot read short data.' unless data
27
27
 
28
28
  data.unpack('v').first
@@ -34,7 +34,7 @@ module CSstats
34
34
  #
35
35
  # Returns the String.
36
36
  def read_string_data(length)
37
- data = handle.read(length)
37
+ data = stream.read(length)
38
38
  raise CSstats::Error, 'Cannot read string data.' unless data
39
39
 
40
40
  data.strip
@@ -0,0 +1,41 @@
1
+ module CSstats
2
+ module Parser
3
+ module Reader
4
+ class Player
5
+ attr_reader :stream
6
+
7
+ def initialize(stream)
8
+ @stream = stream
9
+ end
10
+
11
+ def parse
12
+ player = CSstats::Player.new
13
+ parse_columns(player)
14
+ return unless player.nick
15
+ player
16
+ end
17
+
18
+ private
19
+
20
+ def parse_columns(player)
21
+ CSstats::COLUMNS.each do |column|
22
+ next parse_integer if column.empty?
23
+
24
+ data = send("parse_#{column[:type]}") || break
25
+ player.send("#{column[:name]}=", data)
26
+ end
27
+ end
28
+
29
+ def parse_string
30
+ length = stream.read_short_data
31
+ return if length.zero?
32
+ stream.read_string_data(length)
33
+ end
34
+
35
+ def parse_integer
36
+ stream.read_int_data
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end