Skip to content

Instantly share code, notes, and snippets.

@pdg137
Last active Sep 12, 2021
Embed
What would you like to do?
Quick script for exporting pairings from an OGS tournament round to a csv, including STARTING ranks, handicaps, and komi.
require 'open-uri'
require 'json'
def game(id)
id = id.to_i
JSON.parse(URI.open("https://online-go.com/api/v1/games/#{id}").read)
end
def rating_at_time(id, time)
id = id.to_i
time = time.to_i
history_lines = URI.open("https://online-go.com/termination-api/player/#{id}/v5-rating-history").read
first_line = true
history_lines.each_line { |line|
fields = line.split("\t")
if first_line
first_line = false
next
end
ended = fields[0].to_i
rating = fields[4].to_f
return rating if ended < time
}
# No rating found! Maybe the round was too long ago, before the
# beginning of their rating history? Let's not break everything for
# that - return an obviously wrong value instead.
1
end
def check_match(id)
game = game(id)
start_time = game['gamedata']['start_time']
black = game['players']['black']
white = game['players']['white']
handicap = game['handicap']
komi = game['komi']
# find the ranks of the players at that time
black_rating = rating_at_time(black['id'], start_time)
black_rank = Math.log(black_rating/525)*23.15
white_rating = rating_at_time(white['id'], start_time)
white_rank = Math.log(white_rating/525)*23.15
rank_diff = white_rank - black_rank
winner =
case game['gamedata']['winner']
when black['id']
'black'
when white['id']
'white'
else
'?'
end
puts "#{id},\"#{black['username']} vs #{white['username']}\",#{black_rating},#{black_rank},#{white_rating},#{white_rank},#{rank_diff},#{handicap},#{komi},#{winner}"
$stdout.flush
sleep(1) # avoid throttling
end
def check(round)
round['matches'].each { |m| check_match(m['gameid']) }
end
def tournament_rounds(id)
id = id.to_i
JSON.parse(URI.open("https://online-go.com/api/v1/tournaments/#{id}/rounds").read)
end
if ARGV.length != 2
$stderr.puts "Usage: ruby check_pairings.rb <tournament> <round>"
exit
end
puts 'id,game,black rating,black rank,white rating,white rank,rank diff,handicap,komi,winner'
rounds = tournament_rounds(ARGV[0].to_i)
check(rounds[ARGV[1].to_i-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment