implemented stats

This commit is contained in:
danijoo
2013-12-13 12:01:09 +01:00
parent 452aced320
commit 54eb56376d
5 changed files with 154 additions and 2 deletions

View File

@@ -4,10 +4,11 @@ require 'sightstone/modules/champion_module'
require 'sightstone/modules/summoner_module' require 'sightstone/modules/summoner_module'
require 'sightstone/modules/game_module' require 'sightstone/modules/game_module'
require 'sightstone/modules/league_module' require 'sightstone/modules/league_module'
require 'sightstone/modules/stats_module'
class Sightstone class Sightstone
attr_accessor :region, :api_key attr_accessor :region, :api_key
attr_accessor :summoner, :champion, :game, :league attr_accessor :summoner, :champion, :game, :league, :stats
def initialize(api_key, region) def initialize(api_key, region)
@api_key = api_key @api_key = api_key
@@ -17,6 +18,7 @@ class Sightstone
@champion = ChampionModule.new(self) @champion = ChampionModule.new(self)
@game = GameModule.new(self) @game = GameModule.new(self)
@league = LeagueModule.new(self) @league = LeagueModule.new(self)
@stats = StatsModule.new(self)
end end
end end

View File

@@ -0,0 +1,74 @@
require 'sightstone/summoner'
require 'sightstone/player_stats_summary'
require 'sightstone/ranked_stats'
class StatsModule
def initialize(sightstone)
@sightstone = sightstone
end
def summary(summoner, season=nil)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "https://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/stats/by-summoner/#{id}/summary"
response = if season.nil?
_get_api_response(uri)
else
_get_api_response(uri, {'season' => season})
end
_parse_response(response) { |resp|
data = JSON.parse(resp)
return PlayerStatsSummaryList.new(data)
}
end
def ranked(summoner, season=nil)
id = if summoner.is_a? Summoner
summoner.id
else
summoner
end
uri = "https://prod.api.pvp.net/api/lol/#{@sightstone.region}/v1.1/stats/by-summoner/#{id}/ranked"
response = if season.nil?
_get_api_response(uri)
else
_get_api_response(uri, {'season' => season})
end
_parse_response(response) { |resp|
data = JSON.parse(resp)
return RankedStats.new(data)
}
end
def _get_api_response(uri, header={})
params = {'api_key' => @sightstone.api_key}.merge header
RestClient.get(uri, headers={:params => params}) {|response, request, result| response }
rescue SocketError => e
nil
end
def _parse_response(response, &block)
response_code = if response.nil?
500
else
response.code
end
if response_code == 200
block.call(response.body)
elsif response_code == 404
raise Sightstone::SummonerNotFoundException
elsif response_code == 500
raise Sightstone::SightstoneConnectionException
end
end
end

View File

@@ -0,0 +1,30 @@
require 'sightstone/stat'
class PlayerStatsSummaryList
attr_accessor :summonerId, :playerStatSummaries
def initialize(data)
@summonerId = data['playerStatSummary']
@playerStatSummaries = []
data['playerStatSummaries'].each do |summary|
@playerStatSummaries << PlayerStatSummary.new(summary)
end
end
end
class PlayerStatSummary
attr_accessor :wins, :losses, :modifyDateStr, :modifyDate, :playerStatSummaryType, :aggregatedStats
def initialize(data)
@wins = data['wins']
@losses = data['losses']
@modifyDatStr = data['modifyDateStr']
@modifyDate = data['modifyDate']
@playerStatSummaryType = data['playerStatSummaryType']
@aggregatedStats = []
data['aggregatedStats'].each do |stat|
@aggregatedStats << Stat.new(data)
end
end
end

View File

@@ -0,0 +1,40 @@
require 'sightstone/stat'
class RankedStats
attr_accessor :champions, :modifyData, :modifyDateStr, :summonerId
def initialize(data)
@summonerId = data['summonerId']
@modifyDate = data['modifyDate']
@modifyDateStr = data['modifyDateStr']
@champions = []
data['champions'].each do |champ|
@champions << ChampionStats.new(champ)
end
end
end
class ChampionStats
attr_accessor :id, :name, :stats
def initialize(data)
@id = data['id']
@name = data['name']
@stats = []
data['stats'].each do |stat|
@stats << ChampionStat.new(stat)
end
end
end
class ChampionStat < Stat
attr_accessor :count
def initialize(data)
super(data)
@count = data['c']
end
end

View File

@@ -1,7 +1,13 @@
class Stat class Stat
attr_accessor :id, :name, :value
def initialize(data) def initialize(data)
@id = data['id'] @id = data['id']
@name = data['name'] @name = data['name']
@value = data['value'] @value = if data.has_key? 'value'
data['value']
else
data['count']
end
end end
end end