Last active
February 23, 2019 22:05
-
-
Save jubishop/022ffe2cfaf624b080f480cf1b001dd5 to your computer and use it in GitHub Desktop.
RL Replay Analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'PP' | |
class Player | |
attr_accessor :name, :assists, :goals, :saves, :score, :shots, :teamID | |
def initialize(stats) | |
@name = stats['Name']['value']['str'] | |
@assists = stats['Assists']['value']['int'] | |
@goals = stats['Goals']['value']['int'] | |
@saves = stats['Saves']['value']['int'] | |
@score = stats['Score']['value']['int'] | |
@shots = stats['Shots']['value']['int'] | |
@teamID = stats['Team']['value']['int'] | |
end | |
def to_h | |
[name, self] | |
end | |
end | |
class ReplayStats | |
attr_accessor :players, :ourScore, :theirScore | |
def initialize(file) | |
@data = JSON::parse(File.open(file).read)['header']['body']['properties']['value'] | |
@players = @data['PlayerStats']['value']['array'].map { |stats| | |
Player.new(stats['value']).to_h | |
}.to_h | |
team0Score = @data.has_key?('Team0Score') ? @data['Team0Score']['value']['int'] : 0 | |
team1Score = @data.has_key?('Team1Score') ? @data['Team1Score']['value']['int'] : 0 | |
@ourScore, @theirScore = *(self.jubi.teamID == 0 ? [team0Score, team1Score] : [team1Score, team0Score]) | |
end | |
def won? | |
@ourScore > @theirScore | |
end | |
def method_missing(name, *args, &block) | |
super unless @players.has_key? name.to_s | |
@players[name.to_s] | |
end | |
end | |
system("rm -rf 'jsonFiles/'") | |
system("mkdir 'jsonFiles'") | |
Dir['./ReplayFiles/*'].each { |file| | |
system("./rattletrap-6.2.2-osx -c < #{file} > jsonFiles/#{File.basename(file, '.replay')}.json") | |
} | |
gameStats = Dir['./jsonFiles/*'].map { |file| ReplayStats.new(file) } | |
wins, losses = 0, 0 | |
gameStats.each { |game| | |
game.won? ? wins += 1 : losses += 1 | |
} | |
puts "#{wins} wins / #{losses} losses" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment