hon 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ require 'rexml/document'
3
+ require 'hon/hon'
4
+ require 'hon/stats'
5
+ require 'hon/match'
6
+ require 'hon/hero'
7
+ require 'hon/player'
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module HoN
4
+ class HeroStats < Stats
5
+
6
+ def initialize nickname
7
+ @nickname = nickname
8
+ @statistics = Hash.new
9
+
10
+ retrieve_statistics
11
+ end
12
+
13
+ private
14
+ def retrieve_statistics
15
+ open "http://xml.heroesofnewerth.com/xml_requester.php?f=player_hero_stats&opt=nick&nick[]=#{@nickname}" do |response|
16
+ data = REXML::Document.new response.read
17
+ data.elements.each '/xmlRequest/stats/player_hero_stats/hero' do |hero|
18
+ buffer = Hash.new
19
+ hero.elements.each do |element|
20
+ buffer[element.attributes["name"].to_sym] = element.text
21
+ end
22
+ @statistics[hero.attributes["cli_name"]] = buffer
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ module HoN
4
+ class HeroError < StandardError; end
5
+ class MatchError < StandardError; end
6
+ class PlayerError < StandardError; end
7
+
8
+ def self.heroes nickname
9
+ hero = HeroStats.new nickname
10
+ yield hero if block_given?
11
+ hero
12
+ end
13
+
14
+ def self.player nickname, *options
15
+ player = PlayerStats.new nickname, *options
16
+ yield player if block_given?
17
+ player
18
+ end
19
+
20
+ def self.match id
21
+ match = MatchStats.new id
22
+ yield match if block_given?
23
+ match
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+
3
+ module HoN
4
+ class MatchStats < Stats
5
+
6
+ def initialize match_id
7
+ @match_id = match_id
8
+ @statistics = { server: {}, legion: { players: [] }, hellbourne: { players: [] } }
9
+
10
+ retrieve_statistics
11
+ end
12
+
13
+ def server
14
+ @statistics[:server]
15
+ end
16
+
17
+ def legion
18
+ @statistics[:legion]
19
+ end
20
+
21
+ def legion_players
22
+ legion[:players]
23
+ end
24
+
25
+ def hellbourne
26
+ @statistics[:hellbourne]
27
+ end
28
+
29
+ def hellbourne_players
30
+ hellbourne[:players]
31
+ end
32
+
33
+ private
34
+ def retrieve_statistics
35
+ open "http://xml.heroesofnewerth.com/xml_requester.php?f=match_stats&opt=mid&mid[]=#{@match_id}" do |response|
36
+ data = REXML::Document.new response.read
37
+
38
+ # Server statistics
39
+ data.elements.each '/xmlRequest/stats/match/summ/stat' do |element|
40
+ @statistics[:server][element.attributes["name"].to_sym] = element.text
41
+ end
42
+
43
+ data.elements.each '/xmlRequest/stats/match/team[@side=1]/stat' do |element|
44
+ @statistics[:legion][element.attributes["name"].to_sym] = element.text
45
+ end
46
+
47
+ data.elements.each '/xmlRequest/stats/match/team[@side=2]/stat' do |element|
48
+ @statistics[:hellbourne][element.attributes["name"].to_sym] = element.text
49
+ end
50
+
51
+ data.elements.each '/xmlRequest/stats/match/match_stats/ms' do |element|
52
+ buffer, team = Hash.new, 0
53
+ element.elements.each do |stat|
54
+ team = stat.text.to_i if stat.attributes["name"] == "team"
55
+ buffer[stat.attributes["name"]] = stat.text
56
+ end
57
+
58
+ @statistics[(team == 1) ? :legion : :hellbourne][:players].push buffer
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,138 @@
1
+ # encoding: utf-8
2
+
3
+ module HoN
4
+ class PlayerStats < Stats
5
+
6
+ def initialize nickname, *options
7
+ @nickname = nickname
8
+ @statistics = Hash.new
9
+
10
+ retrieve_statistics
11
+ retrieve_heroes if options.include? :heroes
12
+ raise PlayerError, "Player not found" if @statistics.empty?
13
+ end
14
+
15
+ def inspect
16
+ %{<#{self.class} @nickname="#{nickname}" @games=#{games} @wins=#{wins} @losses=#{losses}>}
17
+ end
18
+
19
+ def nickname
20
+ @statistics[:nickname]
21
+ end
22
+
23
+ # The total amount of games played.
24
+ def games
25
+ @statistics[:acc_games_played]
26
+ end
27
+
28
+ # The total amount of victories.
29
+ def wins
30
+ @statistics[:acc_wins]
31
+ end
32
+
33
+ # The total amount of defeats.
34
+ def losses
35
+ @statistics[:acc_losses]
36
+ end
37
+
38
+ # The total amount of games conceded.
39
+ def concedes
40
+ @statistics[:acc_concedes]
41
+ end
42
+
43
+ # The amount of times the player has tried to concede.
44
+ def concede_votes
45
+ @statistics[:acc_concedevotes]
46
+ end
47
+
48
+ # The (total? average?) amount of hero buybacks.
49
+ def buybacks
50
+ @statistics[:acc_buybacks]
51
+ end
52
+
53
+ # The amount of times the player has disconnected from a game.
54
+ def disconnects
55
+ @statistics[:acc_discos]
56
+ end
57
+
58
+ # The amount of times the player has been kicked from a game.
59
+ def kicked
60
+ @statistics[:acc_kicked]
61
+ end
62
+
63
+ # The players Public Skill Rating.
64
+ def psr
65
+ @statistics[:acc_pub_skill]
66
+ end
67
+
68
+ # The amount of public games the player has played (non-matchmaking games).
69
+ def public_games
70
+ @statistics[:acc_pub_count]
71
+ end
72
+
73
+ # The total amount of heroes the player has killed.
74
+ def kills
75
+ @statistics[:acc_herokills]
76
+ end
77
+
78
+ # The total amount of damage the player has dealt to heroes.
79
+ def total_damage
80
+ @statistics[:acc_herodmg]
81
+ end
82
+
83
+ # The total amount of experience the player has gained.
84
+ def total_exp
85
+ @statistics[:acc_heroexp]
86
+ end
87
+
88
+ # The total amount of gold consumed from killing enemy heroes.
89
+ def total_hero_gold
90
+ @statistics[:acc_herokillsgold]
91
+ end
92
+
93
+ def assists
94
+ @statistics[:acc_heroassists]
95
+ end
96
+
97
+ def deaths
98
+ @statistics[:acc_deaths]
99
+ end
100
+
101
+ def total_gold_lost
102
+ @statistics[:acc_goldlost2death]
103
+ end
104
+
105
+ def idle_time
106
+ @statistics[:acc_secs_dead]
107
+ end
108
+
109
+ def win_percentage
110
+ (wins.to_f / games.to_f * 100).round 1
111
+ end
112
+
113
+ def kdr
114
+ (kills.to_f / deaths.to_f).round 1
115
+ end
116
+
117
+ def heroes
118
+ @heroes or {}
119
+ end
120
+
121
+ private
122
+ # Send a HTTP request to the HoN XML API.
123
+ def retrieve_statistics
124
+ open "http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=#{@nickname}" do |response|
125
+ data = REXML::Document.new response.read
126
+ data.elements.each '/xmlRequest/stats/player_stats/stat' do |element|
127
+ @statistics[element.attributes["name"].to_sym] = element.text
128
+ end
129
+ end
130
+ end
131
+
132
+ def retrieve_heroes
133
+ @heroes = HoN.heroes @nickname
134
+ end
135
+
136
+ end
137
+ end
138
+
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require 'open-uri'
3
+
4
+ module HoN
5
+ class Stats
6
+
7
+ def [] name
8
+ @statistics[name]
9
+ end
10
+
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hon
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Mikkel Kroman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-25 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Heroes of Newerth API for Ruby.
23
+ email: mk@maero.dk
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/hon.rb
32
+ - lib/hon/hero.rb
33
+ - lib/hon/hon.rb
34
+ - lib/hon/match.rb
35
+ - lib/hon/player.rb
36
+ - lib/hon/stats.rb
37
+ has_rdoc: true
38
+ homepage: http://maero.dk/
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Heroes of Newerth API
71
+ test_files: []
72
+