Skip to content

Instantly share code, notes, and snippets.

@dyerc
Created September 7, 2014 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyerc/3595a53939d2fb9193db to your computer and use it in GitHub Desktop.
Save dyerc/3595a53939d2fb9193db to your computer and use it in GitHub Desktop.
nhlscrapr integration
require 'rsruby'
r = RSRuby.instance
r.eval_R("library('nhlscrapr')")
#r.library('nhlscrapr')
data = r.eval_R('retrieve.game (season="20132014", gcode="21197")')
times = data["playbyplay"]["seconds"]
class Event
attr_accessor :type, :time, :zone, :team, :method, :period, :aplayers, :hplayers
def initialize(params)
params.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
end
class Game
attr_accessor :teams, :plays
def parse_player(name)
return {
:number => name[/\A\d+/].to_i
}
end
def on_ice_at(side, i)
if side == :home
fields = %w{ h1 h2 h3 h4 h5 h6 }
else
fields = %w{ a1 a2 a3 a4 a5 a6 }
end
numbers = []
fields.each do |f|
p = parse_player(@rdata[f][i])
numbers << p[:number]
end
return numbers
end
def initialize(rdata)
@rdata = rdata
@plays = []
@teams = {
:home => @rdata["hometeam"][0],
:away => @rdata["awayteam"][0]
}
@play_count = @rdata["seconds"].count
@rdata["seconds"].each_with_index do |t, i|
e = Event.new(
:type => @rdata["etype"][i],
:method => @rdata["type"][i],
:period => @rdata["period"][i].to_i,
:time => t,
:zone => @rdata["homezone"][i],
:distance => @rdata["homezone"][i].to_i || 0,
:team => @rdata["ev.team"][i],
:aplayers => self.on_ice_at(:away, i),
:hplayers => self.on_ice_at(:home, i)
)
@plays << e
end
end
def events_for_side(side = :home, type = nil)
plays.select do |p|
next if p.period > 4
if type.is_a? Array
p.team == @teams[side] && type.include?(p.type)
else
p.team == @teams[side] && p.type == type
end
end
end
def events_involving_player(number, team = :home, type = nil)
plays.select do |p|
next if p.period > 4
p.aplayers.include?(number) && p.team == @teams[team] && type.include?(p.type)
end
end
def corsi(side)
self.events_for_side(side, %w{ SHOT MISS BLOCK GOAL }).count
end
def fenwick(side)
self.events_for_side(side, %w{ SHOT MISS GOAL }).count
end
def player_corsi_for(number, team)
self.events_involving_player(number, team, %{ SHOT MISS BLOCK GOAL }).count
end
def player_corsi_against(number, team)
self.events_involving_player(number, team, %{ SHOT MISS BLOCK GOAL }).count
end
end
game = Game.new(data["playbyplay"])
=begin
events = []
team_cf = 0
team_ca = 0
times.each_with_index do |t, i|
e = Event.new(
:type => data["playbyplay"]["etype"][i],
:time => t
)
events << e
#puts "#{e.time} #{e.type} H [#{data["playbyplay"]["a1"][i]}] A[]"
team = data["playbyplay"]["ev.team"][i]
if data["playbyplay"]["period"][i] < 5
if team == "N.J"
if e.type == "SHOT" || e.type == "MISS" || e.type == "BLOCK" || e.type == "GOAL"
team_cf += 1
end
end
if team == "OTT"
if e.type == "SHOT" || e.type == "MISS" || e.type == "BLOCK" || e.type == "GOAL"
team_ca += 1
end
end
end
end
team_cf = game.events_for_side(:away, %w{ SHOT MISS BLOCK GOAL }).count
team_ca = game.events_for_side(:home, %w{ SHOT MISS BLOCK GOAL }).count
cfp = team_cf.to_f / (team_cf.to_f + team_ca.to_f)
puts "Calculating CF = #{team_cf} CA = #{team_ca} CF% #{cfp.round(4) * 100}"
=end
puts game.player_corsi_for(5, :away)
puts game.player_corsi_against(5, :home)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment